// JavaScript Document
function showmenu(elmnt){document.getElementById(elmnt).style.display="block";};
function hidemenu(elmnt){document.getElementById(elmnt).style.display="none";};
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
    	window.onload = func;
	} else {
		window.onload = function() {
			func();
			if (oldonload) {
				oldonload();
			}
		}
	}
}

function fileDownloading(serverData) {
	var thisURL=window.location.href;
	var thisPath=thisURL.split('/');
	var postWWWpath=thisPath.splice(3,thisPath.length-4);
	//gets the files path.
	var filePath=serverData.href.split('/');
	filePath=filePath.splice(3,filePath.length-3);
	filePath=filePath.join('/');
	var add2DB = $.ajax({
		type: "POST",
		url: "http://"+location.host+"/scripts/AJAX.add2Database.php",
		data: 'filePath='+postWWWpath+'/'+filePath + "&upDown=" + "down",
		async: false
	}).responseText;	
}
function deleteFile(obj,fileName) {
	var deleteFile= confirm("Do you really want to delete this file?");
	if (deleteFile==true){//path minus www.gauger-associates.com
		var thisURL=window.location.href;
		var thisPath=thisURL.substring(thisURL.indexOf("\/",8),thisURL.lastIndexOf("\/"));
		var html = $.ajax({//deletes the file
			type: "POST",
			url: "http://"+location.host+"/scripts/AJAX.DeleteClient.php",
			data: "uploadName=" + fileName + "&baseDir=" + thisPath,
			async: false
		}).responseText;
		if(html==1) {//if file deleted
			obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
			//updates the file list for things uploaded in this session only.
			var uploadImg = document.getElementById("uploadImg");
			if(uploadImg) {
				var uploadImages=uploadImg.value.split(",");
				var newImages="";
				for (i in uploadImages) {
					justDeletedFileName=uploadImages[i].split("/");
					justDeletedFileName=justDeletedFileName[justDeletedFileName.length-1];
					justFileName=fileName.split("/");
					justFileName=justFileName[justFileName.length-1];
					if(justDeletedFileName==justFileName) {
						continue;	
					}
					newImages+=uploadImages[i];
				}
				uploadImg.value=newImages;
			}
		} else {//if there was an error deleting
			var newP = document.createElement('p');
			newP.appendChild(document.createTextNode('Error deleting: '+html));
			obj.parentNode.appendChild(newP);
		}
	}
}

//blackbox
//determines the scoll position... used for placing popup images
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}
//shows the still popup images
function showFull(args,imgLink) {
	var bodyH=document.body.offsetHeight;
	var container=document.getElementById("container");
	var contH=container.offsetHeight;
	var blackBack=document.getElementById("blackBack");
	//creates/scales the blackBackground/container to fill the entire page.
	if(!blackBack) {
		document.body.innerHTML+='<div id="blackBack" class="fullScreen"></div><div id="popContainer" class="fullScreen" onclick="hideFull();"><div id="popCenter"></div></div>';
	}
	var blackBack=document.getElementById("blackBack");
	var popContainer=document.getElementById("popContainer")
	blackBack.style.display="block";
	popContainer.style.display="block";
	//middle of the screen, unless image > screen height
	var popCenter=document.getElementById("popCenter")
	//current screen position
	var pageScroll=getPageScroll();
	if(bodyH>popCenter.offsetHeight+20) {
		var centerNothing=(bodyH-popCenter.offsetHeight)/2;
		popCenter.style.top=pageScroll[1]+centerNothing+"px"; 
		var pagePostion=pageScroll[1]+centerNothing;
	} else {
		popCenter.style.top=pageScroll[1]+"px"; 
		var pagePostion=pageScroll[1];
	}
	//Finds the height of the content container
	if(bodyH>contH+36) {
		popContainer.style.height=bodyH+"px";
		blackBack.style.height=bodyH+"px";
		var backHeight=bodyH;
	} else {
		popContainer.style.height=contH+36+"px";
		blackBack.style.height=contH+36+"px";
		var backHeight=contH+36;
	}
	//if arguments are supplied by the function, add images, add loading gif
	if (args) {//finds just the name of the image thumbnail that was clicked
		/*if(!imgLink) {
			var thisPath=args.src.substring(0,args.src.lastIndexOf("\/")-4);
			var thisImg=args.src.substring(args.src.lastIndexOf("\/")+1);
		} else {
			var thisPath=args.substring(0,args.lastIndexOf("\/")-4);
			var thisImg=args.substring(args.lastIndexOf("\/")+1);
		}*/
		//var thisPath=args.substring(0,args.lastIndexOf("\/")-4);
		var thisImg=args;
		
		//the element that is centered... Holds the popup image
		//var popCenter=document.getElementById("popCenter");
		//new image element
		var imgPop = new Image();
		var imgX = new Image();
		var imgBlocker = new Image();
		//when image is loaded
		imgPop.onload = function() {
			
			//writes the image once it's loaded
			popCenter.innerHTML='<img src="'+imgPop.src+'" id="popImg" name="popImg" />';
			var xtraBorder=0;
			//finds the demensions
			popCenter.style.width=imgPop.width+xtraBorder+"px";
			popCenter.style.height=imgPop.height+xtraBorder+"px";
			//middle of the screen, unless image > screen height
			if(bodyH>imgPop.height+20) {
				var centerPic=(bodyH-imgPop.height)/2;
				popCenter.style.top=pageScroll[1]+centerPic+"px"; 
			} else {
				popCenter.style.top=pageScroll[1]+"px"; 
			}
			//makes sure image doesn't push past the backer
			
			var newBackHeight=Number(imgPop.height+20)+Number(pagePostion);
			if (newBackHeight>backHeight) {
				popContainer.style.height=newBackHeight+"px";
				blackBack.style.height=newBackHeight+"px";
			}
			//adds the closeX
			if(!imgLink) {
				imgX.onload = function() {
					popCenter.innerHTML+='<img src="'+imgX.src+'" id="closeX" name="closeX" />';
				}
				imgX.src = "http://"+location.host+"/media/closebox.png";
			}
		};
		//writes the loading gif
		popCenter.innerHTML='<img src="http://'+location.host+'/media/loading.gif" id="loading" name="loading" />';
		var popLoading=document.getElementById("loading");
		popLoading.style.marginTop=popCenter.offsetHeight/2+"px";
		//imgPop.src = thisPath+"/lrg/"+thisImg;
		imgPop.src=thisImg;
	} 
}
//hides the popup image element
function hideFull() {
	var blackBack=document.getElementById("blackBack");
	var popContainer=document.getElementById("popContainer");
	var popCenter=document.getElementById("popCenter");
	blackBack.style.display="none";
	popContainer.style.display="none";
	var video=document.getElementById('Vid');
	var imageContainer=document.getElementById('imageContainer');
	if(imageContainer) {popCenter.innerHTML='';}
	if(video) {popCenter.innerHTML='<div id="videoContainer"></div>';}
}
