var req;
var reqS;
var imgs;
var slide;
var nr;
var Album = new Array();


//**************************************************************************
//* AJAX based function to retrive list of images from server using callback
//**************************************************************************
function GetSlides()
{
req=false;

//if (window.XMLHttpRequest) //native 
//{
	try
	{
		
		req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e1)
		{
		req = false;
		}
	}
	//if (!req && typeof XMLHttpRequest!='undefined') {
	//req = new XMLHttpRequest();
	//alert("Firefox");
	//}
	if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
			alert("nothing");
        }
        }

//}
	if(req)
	{
	var path=''+document.location;
	//alert('document.location: ' +document.location)
	var xpath=path.substring(0,(path.lastIndexOf('/')+1));
	path=path.substring((path.indexOf('?'))+16);
	if (path=="")
	path="Cars"; //first folder available, please correct for your case.
	req.onreadystatechange = ProcessResponse;
	req.open("GET", xpath+"ThumbGen.ashx?CurrentGallery="+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;
		
		if (p==null)
		{
		alert('IsNull');
		return;
		}
		
		//alert('req.responseXML: ' +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('photos');
	
	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(0);
	SlideMenu();

}
}
//********************************************************************************
//*Search items through media library
//*make a call to search files on a drive through Handler
//********************************************************************************
function PreviewImage(adress)
{
//window.open(adress,'','width=400,height=400,left=0,top=0,status=no, scrollbars=no,toolbar=no,menubar=no, resizable=yes');
document.all.imgLarge.src=adress;
}

function trimAll(sString) 
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}


function SearchItems()
{

reqS=false;
try
	{
		
		reqS = new ActiveXObject("Microsoft.XMLHTTP");
		
				
	}
	catch(e)
	{
		try
		{
			reqS = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e1)
		{
		reqS = false;
		}
	}
	//if (!reqS && typeof XMLHttpRequest!='undefined') {
	//reqS = new XMLHttpRequest();
	//alert("Firefox");
	//}
	
	if(window.XMLHttpRequest) {
    	try {
			reqS = new XMLHttpRequest();
        } catch(e) {
			reqS = false;
			alert("nothing");
        }
        }
	
	if(reqS)
	{
	var path=''+document.location;
	var xpath=path.substring(0,(path.lastIndexOf('/')+1));
	var searchcall=document.all.txtbSearch.value;
	reqS.onreadystatechange = SearchResponse;
	reqS.open("GET", xpath+"ThumbGen.ashx?Search="+searchcall, true);
	reqS.send("");         
	
	}
}

function SearchResponse()
{

if(reqS.readyState == 4)
	{
	if(reqS.status == 200)  //analog "OK", 
		{
		//here we are processing result
		
		var p=reqS.responseXML.documentElement;
		

		if (p==null)
		{
			divSearchResult.innerHTML='';
		return;
		}
		
		//if some data present we will build a table and fill DIV by data
		var items = p.getElementsByTagName('photos');
		
		//*******************************************************************************
		//* Change made by: PDietz
		//* Change Date: 8/06/2007
		//* Change: Changed table border style
		//*******************************************************************************
		var text="<table cellspacing = '1' padding='1' rules ='rows' bordercolor='#FFC080' border-collapse:collapse border='1' class='lbl8' style='z-index: 2;border:2px solid;position:relative;left:-160px;top:-630px; width:350px; background-color: white;'>"; 
		
		if (items.length>0)
		{
		
		//show div and dynamically build a table. Other option I gues have atable ready and assign table.datasrc to response
		    document.all.divSearchResult.style.display="";
//		var rtop=(document.all.txtbSearch.style.pixelTop+16).toString()+'px';
//		document.all.divSearchResult.style.top=rtop;
            document.all.divSearchResult.style.top="16px";
//		document.all.divSearchResult.style.left=document.all.txtbSearch.style.left;
			text=text+"<tr><td width='350' style='z-index: 2;position:relative;width: 350px; height: 21px; background-color: white;'><b>File Name</td><td width='150'><b>Gallery</b></td></tr>"
			for (var count = 0; count < items.length; count++)
			{
			var value=(items[count].textContent || 
			items[count].innerText || items[count].text);
			//remove whitespace
			//value=trimAll(value);
			var regEx = new RegExp (" ", "i") ;
			value=value.replace(regEx,"%20");
			//value=value.replace(/^\s+|\s+$/, '');
			text=text+"<tr><td width='350'  align='left' style='z-index: 2;  width: 350px; height: 21px; background-color: white;'>";
			text=text+"<a href=javascript:PreviewImage('"+value+"')>";
			//text=text+value;
			//text=text+"','','width=400, left=0, resizable=no, height=400, scrollbars=no,toolbar=no')'>";
			
			text=text+value+"</a></td>";
			//assign link to gallery folder. In your case change page name/fotofolder etc
			text=text+"<td width='100' style='z-index: 2;top:0px; width: 100px; height: 21px; background-color: white;'><a href='PhotoGallery.aspx?CurrentGallery="+value.substring(6,(value.lastIndexOf('/')))+"'>";
			text=text+value.substring(6,(value.lastIndexOf('/')))+"</a></td></tr>";
			}
			text=text+"</table>";
			divSearchResult.innerHTML=text;
		}
		}
		else
		{
		//divSearchResult.innerHTML="";
		}
	}
}
//********************************************************
//* SLIDESHOW SCRIPT
//* OPTION1
//********************************************************
var i=1;
var oInterval;
function StartSlideShow()
{
SlideMenu();
var tm=document.getElementById("slidetime").value*1000
oInterval=window.setInterval("SlideShow()",tm);

//if needed hide slideshow menu after click
//document.getElementById('idSlide').style.display='none';
}

//*******************************************************
//start slideshow, using list of images from list box
//*******************************************************
function SlideShow()
{

document.imgLarge.filters[0].Duration=2.5;
document.imgLarge.filters[0].Apply();
//alert(document.getElementById("listImg").options[i].text);
document.getElementById('hdr').innerText=document.getElementById("listImg").options[i].text;
document.imgLarge.src=document.getElementById("listImg").options[i].text;
document.imgLarge.filters[0].Play();

if (i>=((document.getElementById("listImg").length)-1))
	{i=0;}
else
	{
	i=i+1;
	}
}

//*******************************************************
//* Stop slide show.
//*******************************************************
function StopSlide()
{
//* AjaxSlideShow();
if (oInterval==null)
return;

if(oInterval!="")
	{
		
		window.clearInterval(oInterval);
		//window.clearInterval("AjaxSlideShow()");
		oInterval="";
		document.getElementById('idSlide').style.display="none";
		return;
	}
	else
	{
	return;
	}
	
	//**************************************************************
	//* if we want we can hide slide control
    //* document.getElementById('idSlide').style.display='none';
    //**************************************************************

}

//*******************************************************
//* pause slide show.
//*******************************************************
function PauseSlide()
{
//* AjaxSlideShow();
if (oInterval==null)
return;

if(oInterval!="")
	{
		
		window.clearInterval(oInterval);
		//window.clearInterval("AjaxSlideShow()");
		oInterval="";
		document.getElementById('pause').src="images/button_play.gif";
		
	}
	else
	{
	document.getElementById('pause').src="images/button_pause.gif";
	AjaxSlideShow();
	return;
	}
	
	//**************************************************************
	//* if we want we can hide slide control
    //* document.getElementById('idSlide').style.display='none';
    //**************************************************************

}

//*****************************************************************
//* Show/hide slide  
//* options on top of image.
//*****************************************************************
function SlideMenu()
{
document.getElementById('idSlide').style.top=document.getElementById('slideBox').style.pixelTop+1;//'195px';
document.getElementById('idSlide').style.left=document.getElementById('slideBox').style.pixelLeft+1;//'235px';
document.getElementById('idSlide').style.display='';
}

//******************************************************************
//* FullSizeGallery
//******************************************************************
function FullSizeGallery()
{ 
if(document.getElementById('divCat').style.display=="none")
{
document.getElementById('imgLarge').style.pixelWidth = 553;
          document.getElementById('imgLarge').style.pixelHeight = 420;
}

if(document.getElementById('divCat').style.display!="none")
   {
   
    window.moveTo(0,0);
    window.resizeTo(screen.availWidth,screen.availHeight);
    
    if (document.getElementById('DivThumb').style.display!= "none" )
        {
         document.getElementById('DivThumb').style.width = screen.availWidth*.9;
         
        }
        
//     if (document.getElementById('ct108_ucDivThumb').style.display!= "none" )
//        {
//        document.getElementById('ct108_usDivThumb').style.width = screen.availWidth-.9;
//  
//        }
     
      
    if (document.getElementById('slideBox').style.display!= "none" )
        {
         document.getElementById('slideBox').align='center'; 
          
         document.getElementById('slideBox').style.width = screen.availWidth*.90;
         document.getElementById('slideBox').style.height=screen.availHeight * .90;
         document.getElementById('slideBox').style.border='none';
         

 //        document.getElementById('imgLarge').style.Height=screen.availHeight * .62;
       
       
          
          var OrgWidth = document.getElementById('imgLarge').style.pixelWidth*1;
        
          var OrgHeight = document.getElementById('imgLarge').style.pixelHeight*1;
         
          var ratio =  OrgHeight/ OrgWidth;
         
          document.getElementById('imgLarge').style.pixelWidth = (screen.availWidth*.60);
          document.getElementById('imgLarge').style.pixelHeight = (screen.availWidth*.60)*ratio;
          
       
          
//         document.getElementById('imgLarge').style.width=screen.availWidth * .90;
        }
        
    document.getElementById('divCat').style.display='none';
    document.getElementById('idControlBar').style.width = screen.availWidth*.9;
    document.getElementById('divPgNavBar').style.width  = screen.availWidth*.9;
    
  
//    document.getElementById('imgLarge').style.margin="10px 10px 10px 100px";
//    document.getElementById('imgLarge').style.padding="10px 10px 10px 10px"


     document.getElementById('imgLarge').style.align='center'
}
else
    {
 
   // document.getElementById('imgLarge').style.pixelHeight=document.getElementById('imgLarge').style.pixelHeight/1;

    document.getElementById('divThumb').style.width = '822';
    document.getElementById('divThumb').style.hieght = '140';
    document.getElementById('idControlBar').style.width = '';
    document.getElementById('divPgNavBar').style.width  = '';
    document.getElementById('slideBox').style.width = '';
    document.getElementById('divCat').style.display='';
    document.getElementById('slideBox').style.height='450'; //screen.availHeight * .75;
    document.getElementById('slideBox').style.borderWidth='2';
    document.getElementById('slideBox').style.borderColor='#FFC080';
    document.getElementById('slideBox').style.borderStyle='solid';
    document.getElementById('imgLarge').style.display=''; 
    
    }
}

//******************************************************************
//* 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('idCatalog').style.display=="none")
return;

//if everyhting is OK, then proceed
window.moveTo(0,0);
window.resizeTo(screen.availWidth,screen.availHeight);
//document.getElementById('idCatalog').style.display="none";
//document.getElementById('idCatalog').style.display=document.getElementById('idCatalog').style.display?'none','';
document.getElementById('divThumb').style.display="none";
document.getElementById('divInfo').style.display='none';
//document.getElementById('divExif').style.display='none';

document.getElementById('idControlBar').style.top="0"; //navigation bar ontop-left
document.getElementById('idControlBar').style.left="2";
document.getElementById('idControlBar').style.width=screen.availWidth-100;
document.getElementById('hdr').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.imgLarge.style.pixelWidth=document.imgLarge.style.pixelWidth*1.4;
document.getElementById('slideBox').align='center'; //image on center
document.getElementById('imgLarge').style.pixelHeight=document.getElementById('imgLarge').style.pixelHeight*1.4;

var tempwidth=document.getElementById('imgLarge').style.pixelWidth*1.4;
var temp2=document.all.slideBox.style.pixelWidth;
document.getElementById('imgLarge').style.width='';
document.getElementById('slideBox').style.height=screen.availHeight - 250;
document.getElementById('slideBox').style.border="none"
//document.getElementById('divCat').style.top="170";
//document.getElementById('Maintable').style.width=screen.availWidth-100;
//document.getElementById('divCat').style.width=screen.availWidth-100;
document.getElementById('divCat').style.display="none"

document.getElementById('slideBox').style.left="5px";//((screen.availWidth-tempwidth)/2);
document.getElementById('slideBox').style.width=screen.availWidth-100;
//document.imgLarge.style.Width='';
document.getElementById('idSlide').style.left=document.getElementById('slideBox').style.left;//'235px';
document.getElementById('idSlide').style.top=document.getElementById('slideBox').style.top;//'195px';
document.all.slideBox.style.align= 'center';
document.all.slideBox.style.textAlign='center';

//change background for page, for better view
document.bgColor="Black";


}

function PrevSlide()
{
//change one slide back
if (i==0)
{i=((document.getElementById("listImg").length)-1);}
else
{
i=i-1;
}
document.getElementById('imgLarge').src=document.getElementById("listImg").options[i].text;
}

function NextSlide()
{
//change one slide back
if (i>=((document.getElementById("listImg").length)-1))
{i=0;}
else
{
i=i+1;
}
document.getElementById('imgLarge').src=document.getElementById("listImg").options[i].text;
}
//for AjaxSlide
if (slide>=Album.length)
{
slide=0;
}
else
{
slide=slide+1;
//document.imgLarge.src=Album[slide].src;
}


//**********************************************************
function AjaxSlideShow()
{
//StopSlide();

if (slide>=Album.length)
{
slide=0;
}

document.getElementById('idSlide').style.display="";
		
var ipath=document.location.href;
ipath=ipath.substring(0,ipath.lastIndexOf('/'));
document.all.imgLarge.style.width='';
var rv=getInternetExplorerVersion();

imgpath=Album[slide].src;
imgpath=imgpath.substring(ipath.length+1);
hdr.innerHTML=imgpath;//Album[slide].src;
//document.all.iFrameInfo.src='ThumbGen.ashx?Exif='+imgpath;

if (rv>0)
{
document.all.imgLarge.filters[0].Duration=2.5;
document.all.imgLarge.filters[0].Apply();
document.all.imgLarge.src=Album[slide].src;
document.all.imgLarge.filters[0].Play();
}
else
{
document.all.imgLarge.src=Album[slide].src;

}
var tm=document.getElementById("slidetime").value*1000; //this way we can control time during playback
oInterval=window.setTimeout("AjaxSlideShow()",tm);
//nr=nr+1;
slide++;

}



//////style
function ChangeSkin(skin)
{
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
switch (skin)
{
case 0:
document.bgColor='#30445d'
SetCookie('bgcolor',document.bgColor,exp);
break;
case 1:
document.bgcolor='Black'
SetCookie('bgColor',document.bgColor,exp);
break;
case 2:
document.bgColor='#404040'
SetCookie('bgcolor',document.bgColor,exp);
break;
}
}

function ShowInfo()
{
document.all.divExif.style.display=!document.all.divExif.style.display? 'none':'';
document.all.slideBox.style.align=!document.all.slideBox.align? 'left': 'center';
document.all.slideBox.style.textAlign=!document.all.slideBox.textAlign? 'center': 'left';

}

function fitPic() 
{
    if (window.innerWidth){
        iWidth = window.innerWidth;
        iHeight = window.innerHeight;
    }
    else
    {
        iWidth = document.body.clientWidth;
        iHeight =document.body.clientHeight;
    }
    iWidth = document.imageLarge.width - iWidth;
    iHeight = document.imageLarge.height - iHeight;
    window.resizeBy(iWidth, iHeight);
}


//**************************************************************
//*  this script resppnsible to remember background
//**************************************************************
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function color(){
var favColor = GetCookie('bgcolor');
if (favColor == null) {
//favColor = prompt("What is your favorite background color?");
favColor=document.bgColor;
SetCookie('bgcolor', favColor, exp);
}
document.bgColor=favColor;
return favColor;
}
function set(){
favColor = prompt("What is your favorite background color?");
SetCookie ('bgcolor', favColor, exp);
}
function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}
function SetCookie (name, value, expires) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
//var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires) ? "; expires=" + expires.toGMTString() : "") + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


//*  Info about Explorer

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 6.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  //alert( msg );
}




