var req;
var imgs;
var slide;
var nr;
var Album = new Array();
var path = "";

//AJAX based function to retrive list of images from server using callback
function GetSlides()
{
    alert("In GetSlides.  Please call your system administrator.  Thanks.");
    
    req=false;

    //if (window.XMLHttpRequest)
    //{
	    try
	    {
		    req = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch(e)
	    {
		    try
		    {
			    req = new ActiveXObject("Msxml2.XMLHTTP");
		    }
		    catch(e1)
		    {
		    req = false;
		    }
	    }
    //}
	    if(req)
	    {
	    path=''+document.location;
    	
	    path=path.substring((path.indexOf('?'))+3);
	    if (path=="")
	    path=document.getElementById(FirstGalleryClientId).value; //first folder available, please correct for your case.
	    //alert(path);
	    req.onreadystatechange = ProcessResponse;
	    var linktoimage;
	    linktoimage = "http://"+self.location.host+"/CatalogView/ThumbGen.ashx?Dir="+path;
	    //alert(linktoimage);
	    req.open("GET", "http://localhost/CatalogView/ThumbGen.ashx?Dir="+path, true);
        //document.getElementById("DropDownList1").value,  true);
	    req.send("");         
	    }
}

/////////////////////////////////////////////////////////////////////////
function ProcessResponse()
{
	    if(req.readyState == 4)
	    {
	    if(req.status == 200)  //analog "OK", 
		    {
		    //here we are processing result,////////////////////////
		    //
		    var p=req.responseXML.documentElement;
    		
		    ///alert(req.responseXML);  -testing purpose
		    var ddlTest = document.getElementById("test");
		    //var limg=document.getElementById("listImg");
    		
		    for (var count = ddlTest.options.length-1;count >-1; count--)
		    {
			    ddlTest.options[count] = null;
		    }
	    var photos = p.getElementsByTagName('photo');
	    var text; 
	    //here we are trying to create array in memory
    	
	    //this example just to show how we can fill selection box from callback result.
	    //After callback we got XMl document-table and we can use it to fill table/select/list etc.
	    var listItem;
	    var l=photos.length;
	    for (var count = 0; count < photos.length; count++)
		    {
	    text = (photos[count].textContent || 
	    photos[count].innerText || photos[count].text);
	    listItem = new Option(text, text,  false, false);
	    ddlTest.options[ddlTest.length] = listItem;
		    }
		    }
	    else
		    {
		    alert("Error retrieving data!" );
		    }
    	
	    ////// this script responsible to fill Album array by data returned from client in our case 
	    // it is a images pathes. 
    	
	    for (i = 0; i<l; i++)
	    {
	    text2 = (photos[i].textContent || 
	    photos[i].innerText || photos[i].text);
	    Album[i]= new Image();
	    Album[i].src = text2;
	    }
	    //now we have array with images. call slide show
	    slide=0;
	    //call slide show and show menu on top of image
	    AjaxSlideShow();
	    SlideMenu(); // Depreciated
    }		
}

/////////////////////////////////////////////////////////////////////////
///////////////////SLIDESHOW SCRIPT////////////////////
////////////////// OPTION1  ///////////////////////////
var i=1;
var oInterval;
var boolAspectChanged = false;

function StartSlideShow()
{
    SlideMenu(); // Depreciated
    //fixed interval, but possible set through list box.
    var tm=document.getElementById(slidetimeClientId).value*1000
    oInterval=window.setInterval("SlideShow()",tm);
}

/////////////////////////////////////////////////////////////////////////
//Start slideshow, using list of images from list box

function SlideShow()
{
    //document.getElementById(imgLargeClientId).filters[0].Duration=2.5; // Fade
    //document.getElementById(imgLargeClientId).filters[0].Apply();  // Fade
    //alert(document.getElementById(listImgClientId).options[i].text); // Debug
    //document.getElementById(hdrClientId).innerText=document.getElementById(listImgClientId).options[i].text; // See DisplaySlide
    //document.getElementById(imgLargeClientId).src=document.getElementById(listImgClientId).options[i].text; // See DisplaySlide
    DisplaySlide();
    //document.getElementById(imgLargeClientId).filters[0].Play(); // Fade
    // Slide Index Management
    if (i>=((document.getElementById(listImgClientId).length)-1))
	    {i=0;} // Wrap-around
    else
	    {
	    i=i+1;
	    }
}

/////////////////////////////////////////////////////////////////////////
//Stop/pause slide show

function StopSlide()
{
    if (oInterval==null)
        return;
    if(oInterval!="")
	    {
		    window.clearInterval(oInterval);
		    oInterval="";
	    }
	    else
	    {
	    return;
	    }

    //if we want we can hide slide control
    window.clearInterval("AjaxSlideShow()");

}

/////////////////////////////////////////////////////////////////////////
//Just to show/hide slide options on top of image. (Depreciated)
function SlideMenu()
{
}

//////To SHOW A FULL SCREEN/SIZE SLIDE SHOW  ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function FullSize()
{
    //check if  already fullsize, move slide frame up, depending from screen size do calculation
    //for slide size: use 
    //hide everything
    //move explorer window to the corner and mximize for slide show
    //
    if (document.getElementById('idControlBar').style.top=="0px")
    return;

    //if everyhting is OK, then proceed
    window.moveTo(0,0);
    window.resizeTo(screen.availWidth,screen.availHeight-5);
    document.getElementById('idCatalog').style.display="none";
    document.getElementById('divThumb').style.display="none";

    document.getElementById('idControlBar').style.top="0"; //navigation bar ontop-left
    document.getElementById('idControlBar').style.left="2";
    document.getElementById('idControlBar').style.width="100%";
    document.getElementById(hdrClientId).style.width="100%";

    //reposition slide
    document.getElementById('slideBox').style.top="58"; //fixed leave space for navbar

    //change size for slide and put image at center
    document.getElementById(imgLargeClientId).style.pixelWidth=document.getElementById(imgLargeClientId).style.pixelWidth*1.4;
    document.getElementById(imgLargeClientId).style.pixelHeight=document.getElementById(imgLargeClientId).style.pixelHeight*1.4;
    document.getElementById('slideBox').style.left=((screen.availWidth-document.getElementById(imgLargeClientId).style.pixelWidth)/2);

    //change background for page, for better view
    document.bgColor="Black";

}

/////////////////////////////////////////////////////////////////////////
function PrevSlide()
{
    //change one slide back
    if (i==0)
    {i=((document.getElementById(listImgClientId).length)-1);}
    else
    {
    i=i-1;
    }
    //document.getElementById(imgLargeClientId).src=document.getElementById(listImgClientId).options[i].text;
    DisplaySlide();
}

/////////////////////////////////////////////////////////////////////////
function NextSlide()
{
    //change one slide back
    if (i>=((document.getElementById(listImgClientId).length)-1))
    {i=0;}
    else
    {
    i=i+1;
    }
    //document.getElementById(imgLargeClientId).src=document.getElementById(listImgClientId).options[i].text;
    DisplaySlide();
}

/////////////////////////////////////////////////////////////////////////
var imgWidthLast = 0;
var imgHeightLast = 0;

function DisplaySlide()
{
    // Display Header
    document.getElementById(hdrClientId).innerText=document.getElementById(listImgClientId).options[i].text;
    
    // Parse Image Name, Width, and Height Parameters
    var wrkStr = document.getElementById(listImgSlideClientId).options[i].text;
    var aryParm = wrkStr.split("|");
    var imgName = aryParm[0];
    var imgWidth = aryParm[1];
    var imgHeight = aryParm[2];
    
    // Test for Size Change
    
    if (imgWidthLast == 0 && imgHeightLast == 0) {
        imgWidthLast = aryParm[1];
        imgHeightLast = aryParm[2];
    }
    
    // Clear if a change (break) in image aspect ratio
    
    if (imgWidth != imgWidthLast || imgHeight != imgHeightLast) {
        //document.getElementById(imgLargeClientId).src="./images/white.gif"; // (Clear Image)
        //window.setTimeout('DoNada()', 1000);
        boolAspectChanged = true;
        //document.getElementById(imgLargeClientId).style.width = 1;
        //document.getElementById(imgLargeClientId).style.height = 1;
        //document.getElementById(imgLargeClientId).src="./images/white.gif";
        //window.setTimeout('DoNada()', 1000);
    } else {
        boolAspectChanged = false;
    }
    
    // Display Image
    
    if (boolAspectChanged) {
        //document.getElementById(imgLargeClientId).filters[0].Duration=1.0;
        //document.getElementById(imgLargeClientId).filters[0].Apply();
    }
    document.getElementById(imgLargeClientId).src="images/white.gif";
    //window.setTimeout('DoNada()', 1000);
    document.getElementById(imgLargeClientId).src=aryParm[0];
    document.getElementById(imgLargeClientId).style.width = aryParm[1];
    document.getElementById(imgLargeClientId).style.height = aryParm[2];
    if (boolAspectChanged) {
        //document.getElementById(imgLargeClientId).filters[0].Play();
    }
    
    // Reset Break Width and Height Fields
    
    imgWidthLast = imgWidth;
    imgHeightLast = imgHeight;
}

function DoNada() {} // Dummy catch for timer.

/////////////////////////////////////////////////////////////////////////
function AjaxSlideShow()
{
    StopSlide();
    if (slide>=Album.length)
    {
    slide=0;
    }
    //document.getElementById(imgLargeClientId).filters[0].Duration=2.5;
    //document.getElementById(imgLargeClientId).filters[0].Apply();
    document.getElementById(imgLargeClientId).src=Album[slide].src;
    //document.getElementById(imgLargeClientId).filters[0].Play();
    var tm=document.getElementById(slidetimeClientId).value*1000
    oInterval=window.setTimeout("AjaxSlideShow()",tm);
    //nr=nr+1;
    slide++;
}
/////////////////////////////////////////////////////////////////////////

//AJAX based function to test if we have a large image existing on www.newarkhousing.net
function HaveImage(imagePath)
{
    // Debug
    //alert("In HaveImage");   
    //alert("Image Path: "+imagePath);
    
    req=false;

    //if (window.XMLHttpRequest)
    //{
	    try
	    {
	        req = new XMLHttpRequest(); // Updated 13 JAN 2010
		}
	    catch(e)
	    {
		    try
		    {
		        req = new ActiveXObject("Microsoft.XMLHTTP"); // Updated 13 JAN 2010
			}
		    catch(e1)
		    {
		        req = false;
		        alert("ActiveXObject Failure 1");
		    }
	    }
    //}
	    if(req)
	    {
	        //alert("ActiveXObject Created");
	        
            // Set Callback
	        req.onreadystatechange = ProcessHaveImageResponse;
	        
	        // Process Path
	        var pathparts = imagePath.split('/');
	        var wrkpathparts = pathparts[2];
	        var P1 = wrkpathparts.indexOf("_med");
	        wrkpathparts = wrkpathparts.substring(0,P1) + wrkpathparts.substring(P1+4);
	        path=pathparts[0] + "/" + pathparts[1] + "/" + wrkpathparts;    	
	        //alert("Path: " + path);

            // Perform AJAX Call	        
	        var linktoimage;
	        linktoimage = "http://"+self.location.host+"/CatalogView/ThumbGen.ashx?Name="+path;
	        //alert("Link: " + linktoimage);
	        req.open("GET", linktoimage, true);
    	    req.send("");
	    } else {
		    alert("ActiveXObject Failure 2");
	    }
	    
} // End of HaveImage

/////////////////////////////////////////////////////////////////////////
function ProcessHaveImageResponse()
{
    //alert("State: " + req.readyState);
    
    if(req.readyState == 4)
	{
	    //alert(" Status: " + req.status + " " + req.statusText);
	    if(req.status == 200)  //analog "OK", 
		{
		    // Initialize
		    var r = "";
		    
            // Process Result
		    var p=req.responseText;    		
		    //alert(p);
		    
		    // Process Result
		    if (p.substr(0,4) == "NOPE") { // Could be NOPE1-4 or YUP 1 & 2
		        r = "(Large image not available)";
		    } else { // YUP's
		        P1 = p.indexOf('|');
		        p = p.substr(P1+1);
		        path = p.substr(14); // Hardcoded for ./CatalogView/
		        r = '<input type="button" value="Large Image" style="color:Blue;font-size:10pt;font-weight:bold;font-style:normal;height:24px;width:100px;z-index: 101;" onclick="javascript: popWindow(path); return false;">';
		    }
		    
		    // Set Link2Large
		    if (document.getElementById("link2large")) {
		        document.getElementById("link2large").innerHTML = r;
		    } else {
		        document.getElementById(link2largeClientId).innerHTML = r;
		    }
        }		
    }
    
} // End of ProcessHaveImageResponse

/////////////////////////////////////////////////////////////////////////

var whnd;

function popWindow(dpath)
{
    // Initialize URL & Name
    var dlocation = "http://www.newarkhousing.net/CatalogView/"+dpath;
    var dname = "LImage";
    
    // Pop Window with Large Image
    whnd = window.open(dlocation, dname, 'resizable=1');
    
} // End of popWindow

