window.onload=initAll;

function initAll(){
	createPortfolioLinks();
}

function createPortfolioLinks(){
	for (var i=0; i<document.images.length; i++) {
		if(document.images[i].parentNode.id=="portfolio"){
			var thisImage=document.images[i];
			imageSwap(thisImage);
			thisImage.onclick=openImage;
		}
	}
}

function imageSwap(thisImage){
	var offImage=thisImage.src;
	var onImage=new Image(70,70);
	onImage=offImage.replace("thumb", "on_thumb");
	thisImage.onmouseover=rollOver;
	thisImage.onmouseout=rollOut;
	function rollOver(){
		thisImage.src=onImage;
	}
	function rollOut(){
		thisImage.src=offImage;
	}
}

function openImage(){
	//loads the XML file with captions
	var xmlDoc=null;
	var thisImage=this.id;
	var currentSection=thisImage.split("/");
	var imageNumber=parseInt(thisImage.match(/\d+/),10); //add 10 as second parameter to fix javascript bug that retures 08, 09 as 0
	var theCaptionsFile="includes/"+currentSection[0]+".xml";
	//create divs
	var portfolioImage=new Image();
	var portDiv=document.createElement("div");
	var captionDiv=document.createElement("div");
	portDiv.setAttribute("id", "portDiv");
	captionDiv.setAttribute("id", "captionDiv");
	var containDiv=document.getElementById("container");
	portDiv.innerHTML="<a href='#' id='closeLink'> </a><div id='imageDiv'><img src='images/img_preloader.gif' id='loadedImage'/></div>";
	containDiv.appendChild(portDiv);
	captionDiv.style.display="none";
	portDiv.appendChild(captionDiv);
	//center loader image
	centerImage(document.getElementById('loadedImage'), captionDiv, portDiv);
	
	//XML handler
	var request = null;
	if(xmlDoc==null){
 		if (window.XMLHttpRequest) {
    		request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (request) {
			request.open("GET", theCaptionsFile);
		
			request.onreadystatechange=function() {
			if (request.readyState == 4) {
				xmlDoc=request.responseXML;
				checkImage();
			}
		}
		request.send(null);
		}
	}
	
	function checkImage(){
		matchCaption();
		//get image and center
		portfolioImage.src="images/" + thisImage;
		
		if(thisImage.indexOf(".swf")!=-1){
			var swfWidth=xmlDoc.getElementsByTagName("project")[imageNumber].getElementsByTagName("width")[0].childNodes[0].nodeValue;
			var swfHeight=xmlDoc.getElementsByTagName("project")[imageNumber].getElementsByTagName("height")[0].childNodes[0].nodeValue;
			portDiv.innerHTML+='<div id="animation_graphic"><h2>The Flash plug-in is required to view animation.<h2></div>';
		
			//this line comes from SWFObject
			var flashvars = {};var params = {};var attributes = {};attributes.id = "flash_animation"; swfobject.embedSWF(portfolioImage.src, "animation_graphic", swfWidth, swfHeight, "9.0.0", false, flashvars, params, attributes);
			//end SWFObject
			var flashDiv=document.getElementById("flash_animation");
			centerFlash(flashDiv, swfHeight, swfWidth);
		}else{
			portfolioImage.onload=function(){
			document.getElementById("loadedImage").src=portfolioImage.src;
			centerImage(portfolioImage, captionDiv, portDiv);
		}
		//window.onresize=function(){centerImage(portfolioImage, captionDiv, portDiv)};
	}
	
	//portDiv.onclick=function(){removeImage(containDiv, portDiv)};
	var closeLink=document.getElementById("closeLink");
	closeLink.onmousedown=function(){removeImage(containDiv, portDiv)};
	}
	
	function matchCaption(){
		var theCaption="<h2>"+xmlDoc.getElementsByTagName("captitle")[imageNumber].childNodes[0].nodeValue+"</h2>";
		//check to see if this is the Web Portfolio XML file which has Project Link nodes
		if(theCaptionsFile.indexOf("web.xml")!=-1){
			var projectLink=xmlDoc.getElementsByTagName("project")[imageNumber].getElementsByTagName("capurl")[0].childNodes[0].nodeValue;
				if(projectLink!="none"){
				theCaption+="<a href='"+projectLink+"' target='new_win'>Click here to launch project</a><br />";
			}
		}
		
		theCaption+=xmlDoc.getElementsByTagName("capbody")[imageNumber].childNodes[0].nodeValue;
		captionDiv.innerHTML=theCaption;
	}
}

function removeImage(containDiv, portDiv){
	containDiv.removeChild(portDiv);
}

function centerImage(portfolioImage, captionDiv, portDiv){
	var closeLink=document.getElementById("closeLink");
	var captionOffset=10;
	var imageX=parseInt(document.body.clientWidth/2-(portfolioImage.width/2));
	var imageY=parseInt(document.body.clientHeight/2-(portfolioImage.height/2));
	if(imageY<40){
		imageY=40;
	}
	if(imageX<10){
		imageX=10;
	}
	document.getElementById('imageDiv').style.left = imageX + "px";
	document.getElementById('imageDiv').style.top = imageY + "px";
	closeLink.style.left=imageX+"px";
	closeLink.style.top=(imageY-30)+"px";
	//check to see if image's height is larger than the preloader image, so it's not the preloader
	if(portfolioImage.height>65){
		if(portfolioImage.height>portfolioImage.width){
			var captionX=imageX+portfolioImage.width+captionOffset;
			captionDiv.style.left=captionX+"px";
			captionDiv.style.top=imageY+"px";
	}
		else{
			var captionY=imageY+portfolioImage.height+captionOffset;
			captionDiv.style.top=captionY+"px";
			captionDiv.style.left=imageX+"px";
		}
		captionDiv.style.display="block";
	}
}

function centerFlash(flashDiv, swfHeight, swfWidth){
	swfHeight=parseInt(swfHeight);
	swfWidth=parseInt(swfWidth);
	var captionDiv=document.getElementById("captionDiv");
	var closeLink=document.getElementById("closeLink");
	var captionOffset=10;
	var swfX=parseInt(document.body.clientWidth/2-(swfWidth/2));
	var swfY=parseInt(document.body.clientHeight/2-(swfHeight/2));
	if(swfY<40){
		swfY=40;
	}
	if(swfX<10){
		swfX=10;
	}
	flashDiv.style.left = swfX + "px";
	flashDiv.style.top = swfY + "px";
	closeLink.style.left=swfX+"px";
	closeLink.style.top=(swfY-30)+"px";
	
	if(swfHeight>swfWidth){
		var captionX=swfX+swfWidth+captionOffset;
		captionDiv.style.left=captionX+"px";
		captionDiv.style.top=swfY+"px";
	}
	else{
		var captionY=swfY+swfHeight+captionOffset;
		captionDiv.style.top=captionY+"px";
		captionDiv.style.left=swfX+"px";
	}
	captionDiv.style.display="block";
}
function redirecter(newURL){
    window.location =newURL;
}
