//utility.js 
//this file must go after the preview.js

/*
	Light box controls
	-- ajax add to light box
*/

function init_lightbox(){
	images = window.document.getElementsByTagName("img");
	for(i=0;i<=images.length-1;i++){
		//adds register the on mousedown function
		if(images[i].className == "lb"){
			img = images[i];
			if (browser.IE) {
				img.attachEvent("onclick", showlb);
			}
			if (browser.NS){
				img.addEventListener("click", showlb,false);
			}
		}
	}
}
function init_lightbox_link(){
	links = window.document.getElementsByTagName("a");
	for(i=0;i<=links.length-1;i++){
		//adds register the on mousedown function
		if(links[i].className == "lb"){
			img = links[i];
			if (browser.IE) {
				img.attachEvent("onclick", showlb);
			}
			if (browser.NS){
				img.addEventListener("click", showlb,false);
			}
		}
	}
}
function showlb(event){
	if(browser.IE){
		src_obj = window.event.srcElement;	
	}
	if(browser.NS){
		src_obj = event.target;
	}

	var tbody = (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
	var docwidth=document.all? tbody.scrollLeft+tbody.clientWidth : pageXOffset+window.innerWidth;
	var docheight=document.all? Math.min(tbody.scrollHeight, tbody.clientHeight) : Math.min(window.innerHeight);
	xpos = tbody.scrollLeft+event.clientX
	ypos = tbody.scrollTop + event.clientY;	
	

	var id = src_obj.id;
	var imageid = window.document.getElementById(id).getAttribute("im");

	var lightbox = window.document.getElementById("lightbox");
	window.document.getElementById("addbtn").onclick = function(){
		addtolb(imageid);
	}
	
	lightbox.style.left = (xpos + 5)+"px";
	lightbox.style.top = (ypos+5)+"px";
}

function addtolb(imageid){

	var lightboxinform = window.document.getElementById("lightboxinform");
	lightboxinform.innerHTML = "<span class=\"p\">Adding image "+imageid+ " Please wait</span>";
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		window.document.alert ("Browser does not support HTTP Request")
		return
	} 
	var url="http://www.shutterfarm.com/include/addtolightbox.php?imid="+imageid;
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			success = xmlHttp.responseText;
			if(success == "go"){
				window.document.getElementById("addform").style.visibility = "hidden";
				window.document.getElementById("lightboxinform").innerHTML= "Added Successfully";
				lbclose = setInterval("closelbt()",2000);
			}else if(success == "error2"){
				window.location = "http://www.shutterfarm.com/include/secure.php";
			}
   		}
	}

xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function closelb(){
	lbreset();
	var lightbox = window.document.getElementById("lightbox");
	lightbox.style.left = (-500)+"px";
	lightbox.style.top = (-500)+"px";
}
function closelbt(){
	lbreset();
	clearInterval(lbclose);
	var lightbox = window.document.getElementById("lightbox");
	lightbox.style.left = (-500)+"px";
	lightbox.style.top = (-500)+"px";
}
function lbreset(){
	window.document.getElementById("lightboxinform").innerHTML = "";
	window.document.getElementById("addform").style.visibility = "visible";
}
/* end light box*/


/*
	help pop up utility 
	will desplay help information to the user via ajax
*/




/* end popup utility*/


/* init Ajax Controls*/
//register the http object
function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	
	if (window.XMLHttpRequest){
	  objXMLHttp=new XMLHttpRequest()
	}else if(window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

/* For the Download Manager */

function opendl(){
	var dlnum = window.document.getElementById("dnum").value;
	var params = "dnum="+dlnum+"&dl=d";
	var dctr = 0;
	for(i=1;i<=dlnum;i++){
		if(window.document.getElementById("log_"+i).checked == true){	
			dctr++;
			var lid = window.document.getElementById("lid_"+i).value;
			params += "-"+lid;
		}
	}
	if(dctr != 0){	
		windl = window.open("downloadmanager.php?"+params+"","DownloadManager","toolbar=no&scollbar=no&resizable=no&status=no");
		swidth = screen.width;
		sheight = screen.height;
		winWidth = 462;
		winHeight = 180;
		windl.resizeTo(winWidth,winHeight);
		
		winPosX = swidth /2 - (winWidth /2);
		winPosY = sheight /2 - (winHeight /2);
		windl.moveTo(winPosX,winPosY);
	}else{
		//no download was selected
		window.alert("Please Select The Image(s) you would like to download first");
	}
}
function dl(){
	var manager = window.document.getElementById("dlmanager");
	clsdl = setInterval("closedl()",2000);
}
function closedl(){
	clearInterval(clsdl);
	window.close();
}
