// Javascript Slideshow - Jeff Davis / Gundog Central

var xmlhttp = false;
try
{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}   catch (e)
{
    try
    {
        xmlhttp = new ActivexObject("Microsoft.XMLHTTP")
    }   catch (E)
    {
        xmlhttp = false;
    }
}
			
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
    xmlhttp = new XMLHttpRequest();
}
        
function getPhoto(category, picID)
{
    page = "functions/get_pictures.php?category="+category+"&picID="+picID;
    xmlhttp.open("GET", page, true);
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseXML!=null)
        {
            var items = [];
            var rsp = xmlhttp.responseXML.getElementsByTagName( 'photo' );
            for( var i = 0; i < rsp.length; i++ )
            {
                var pic = rsp.item( i );
                var id = parseInt( pic.getAttribute( 'id' ).toString() );
                var src = pic.getAttribute( 'src' ).toString();
                var width = parseInt( pic.getAttribute( 'width' ).toString() );
                var height = parseInt( pic.getAttribute( 'height' ).toString() );
                var caption = pic.getAttribute( 'caption' ).toString();
                items.push( { id: id, src: src, width: width, height: height, caption: caption } );
            }
            init_slides ( items );
        }
    }
    xmlhttp.send("");
}

function customHomes()
{
	var items = new Array();
	items.push ( { id: "1", src: "assets/custom_building1.jpg", width: "550", height: 314, caption: ' ' } );
	items.push ( { id: "1", src: "assets/white.jpg", width: "550", height: 314, caption: " " } );
	items.push ( { id: "1", src: "assets/custom_building2.jpg", width: "550", height: 314, caption: " " } );
	items.push ( { id: "1", src: "assets/white.jpg", width: "550", height: 314, caption: " " } );
	items.push ( { id: "1", src: "assets/custom_building3.jpg", width: "550", height: 314, caption: " " } );
	items.push ( { id: "1", src: "assets/white.jpg", width: "550", height: 314, caption: " " } );
	
	init_slides ( items );

}

function init_slides(images)
{
    var current_slide = 0;
    create_new_image(images, current_slide, 0);
}

function create_new_image(images, current_slide, cache)
{
    // Check Image Container Exist, if not create.
    if(document.getElementById("gallery") == null )
    {
        var image_container = document.createElement("img");
        var caption_container = document.createElement("div");
    }
    else
    {
	var image_container = document.getElementById("gallery");
        var caption_container = document.getElementById("caption");
    }
    
    if(document.getElementById("cache")  == null )
    {
        var nImage_contatiner = document.createElement("img");
        var nCaption_contatiner = document.createElement("img");
        
    }
    else
    {
	var nImage_contatiner = document.getElementById("cache");
        var nCaption_contatiner = document.getElementById("cap_cache");
    }
    
    var container = document.getElementById("pictureContainer");
    var opacity = 0;
    var scriptState = 0;
    var next_slide = current_slide+1;
    var img = images[current_slide];
    var num_of_slides = images.length;
    container.style.height = (img.height+25)+"px";
    container.style.width = img.width+"px";
    
    ///Add Image to Canvas
    image_container.id = "gallery"
    image_container.src = img.src;
    image_container.width = img.width;
    image_container.height = img.height;
    image_container.style.opacity = opacity;
    image_container.style.visibility = "hidden";
    container.appendChild( image_container );
    
    if(img.caption=='') { img.caption = "No Picture" ; }
    var textNode=document.createTextNode("");
    caption_container.id = "caption"
    caption_container.align = "center";
    caption_container.setAttribute("class", "vs_text");
    caption_container.setAttribute("text", img.caption);
    caption_container.style.visibility = "hidden";
    caption_container.appendChild(textNode);
    container.appendChild( caption_container );
    theDiv = document.getElementById("caption");
    oldNode = theDiv.childNodes[0];
    newNode = document.createTextNode(img.caption);
    theDiv.replaceChild(newNode, oldNode);
    
    
    if ( cache == 1 && num_of_slides > 1 )
        {
        for( next_slide; next_slide < num_of_slides; next_slide++)
        {
            var next_img = images[next_slide];
            
            // Try to preload Image on hidden canvas
            nImage_contatiner.style.position = 'absolute';
            nImage_contatiner.style.left = '0px';
            nImage_contatiner.style.top = '0px';
            nImage_contatiner.id = "cache"
            nImage_contatiner.src = next_img.src;
            nImage_contatiner.style.opacity = opacity;
            nImage_contatiner.style.visibility = "hidden";
            container.appendChild( nImage_contatiner );
            
            var ntextNode=document.createTextNode(img.caption);
            nCaption_contatiner.align = "center";
            nCaption_contatiner.setAttribute("class", "content_title");
            nCaption_contatiner.style.visibility = "hidden";
            nCaption_contatiner.appendChild(textNode)
            container.appendChild( nCaption_contatiner );
        }
    }
    if(num_of_slides>1)
    {
		display_image(images, image_container, caption_container, opacity, current_slide, scriptState);
    }
    else
    {
		image_container.style.visibility = "visible";
		image_container.style.opacity = 100;
        caption_container.style.visibility = "visible";
    }
}

function display_image(images, image_container, caption_container, opacity, current_slide, scriptState)
{
    if(scriptState == 0)
    {
        opacity++;
        if(opacity<100)
        {
            setTimeout(function() { set_opacity(images, image_container, caption_container, opacity, current_slide, scriptState) }, 30);
        }
        else
        {
            scriptState = 1;
        }
    }
    if(scriptState==1)
    {
        var container = document.getElementById("pictureContainer");
        container.style.backgroundImage = "url('"+images[current_slide].src+"')";
		container.style.backgroundRepeat = 'no-repeat';
        current_slide++;
        if( current_slide > (images.length-1) )
        {
            current_slide=0;
        }
        setTimeout(function() { create_new_image(images, current_slide, 0) }, 2000);
    }
}

function set_opacity(images, image_container, caption_container, opacity, current_slide, scriptState)
{
    image_container.style.visibility = "visible";
    image_container.style.filter = 'alpha(opacity=' + opacity + ')';
    image_container.style.opacity = (opacity / 100);
    image_container.style.MozOpacity = (opacity / 100);
    image_container.style.KhtmlOpacity = (opacity / 100);
    caption_container.style.visibility = "visible";
    display_image(images, image_container, caption_container, opacity, current_slide, scriptState);
}
