var img_ptr = 0;

var opacity = 0;

var fade_speed = 100;   // time in milliseconds between an opacity change
var fade_amount = 10;   // percentage of each opacity change
var toggle = 0;
var loading = false;    // this is true when stuff is loading
var browser;

function initphotos (){
    var client_name = navigator.appName;

    if(client_name.indexOf ('Microsoft') != -1){
        browser = "ie";
    } else {
        browser = "firefox";
    }

    img_ptr = 0;
}

// preload the images
function preload_images (){
    var loaded_images = new Array (images.length);

    for (i = 0; i != images.length; i++){
        loaded_images[i] = new Image ();
        loaded_images[i].src = "/property-images/?photo_id=" + images[i];
        loaded_images[i].onload = function (){
            //  loaded ();
        }
    }
}

function next (){
    if (img_ptr == images.length-1){
        img_ptr = -1;
    }
    do_fade (++img_ptr);
}

function prev (){
    if (img_ptr == 0){
        img_ptr = images.length;
    }
    do_fade (--img_ptr);
}

function do_fade (index){
    var image;      // this is the target image
    var target;     // this is the target image element name

    // skip if loading something else
    if (loading){
        return;
    }

    // make sure no other images can be loaded while this is being run
    loading = true;

    opacity = 0;

    // which of the 2 image divs is on top
    (toggle == 1) ? target = 'main_img2' : target = 'main_img1';
    image = document.getElementById (target);

    // use a callback to make sure the image has loaded
    image.onload = function (){
        if (browser == "ie"){
            image.style.MozOpacity = 0;
        } else {
            image.style.filter = "alpha(opacity=0)";
        }

        toggle_image_zindex ();
        apply_fade_in (target);

        update_reportlink ();
        update_quoteref ();
    }

    image.src = "/property-images/?photo_id=" + images[index];
    // updates the txt in link when the image has loaded

}

function toggle_image_zindex (){
    if (toggle == 0){
        document.getElementById ('image_container1').style.zIndex = 10;
        document.getElementById ('image_container1').style.visibility = 'visible';
        document.getElementById ('image_container2').style.zIndex = 0;
        document.getElementById ('image_container2').style.visibility = 'hidden';

        toggle = 1;
    } else {
        document.getElementById ('image_container1').style.zIndex = 0;
        document.getElementById ('image_container1').style.visibility = 'hidden';
        document.getElementById ('image_container2').style.zIndex = 10;
        document.getElementById ('image_container2').style.visibility = 'visible';

        toggle = 0;
    }
}

function apply_fade_in (target){
    var div = document.getElementById (target);

    // set the opacity
    var moz_opacity = (opacity / 100);

    if (browser == "firefox"){
        div.style.MozOpacity = moz_opacity;
    } else {
        div.style.filter = "alpha(opacity= " + opacity + ")";
    }

    if (opacity < 100){
        var new_opacity = opacity + fade_amount;
        new_opacity = (new_opacity / 100);
        setTimeout("apply_fade_in ('" + target + "', " + new_opacity + ");", 25);
        opacity += fade_amount;
    } else {
        loading = false;
    }
}

