//=============================================================================
// Set properties
//============================== GIJS - OCCHIO ================================

var sBorderWidth		= 10; // Width in pixels
var sBorderHeight		= 22; // Height in pixels
var sCloseBtnLocation 	= "/images/layout/close.gif";
var sCloseBtnText 		= "Close";

lightboxInit();
//=============================================================================
//	Function lightboxInit:
//	1. Collect all anchors with a lightbox relation and assign onclick events
//	2. Prepare the lightbox if necessary on this page
//============================== GIJS - OCCHIO ================================

function lightboxInit() {
	
	// Prepare the lightbox background
	var divLightboxBackground = document.createElement("div");
	divLightboxBackground.setAttribute("id" , "lightboxBackground");
	divLightboxBackground.setAttribute("class" , "lightboxBackground");
	divLightboxBackground.setAttribute("className" , "lightboxBackground");
	divLightboxBackground.style.display = "none";
	document.body.appendChild(divLightboxBackground);

	// Preparte the lightbox
	var divLightbox = document.createElement("div");
	divLightbox.setAttribute("id" , "lightbox");		
	divLightbox.setAttribute("class" , "lightbox");
	divLightbox.setAttribute("className" , "lightbox");
	divLightbox.style.display = "none";
	document.body.appendChild(divLightbox);
	
	// Collect <a> tags
	var aAnchors = document.getElementsByTagName("a"); 

	// Assign onclick events to all <a> tags with a lightbox relation
	for (var i = 0; i < aAnchors.length; i++) { 
		var sAnchor = aAnchors[i];
		if(sAnchor.getAttribute('rel') == "lightbox") {
			
			// Get properties
			aProperties = getProperties(sAnchor.getAttribute('href')).split("||");
			sAnchor.sFile = aProperties[0];
			sAnchor.sExtension = aProperties[1];
			sAnchor.sWidth = aProperties[2];
			sAnchor.sHeight = aProperties[3];

			// Assign onclick event
			sAnchor.onclick = function() {
				showLightbox(this.sFile, this.sExtension, this.sWidth, this.sHeight);
				return false;
			}
			
		} 
	}	
}

//=============================================================================
// Function getProperties
//============================== GIJS - OCCHIO ================================

function getProperties(sUrl) {
	// Set width and height if given
	var sWidthPos = sUrl.indexOf("width");
	var sHeightPos = sUrl.indexOf("height");
	
	if(sWidthPos < sHeightPos) {
		sWidthQuery = sUrl.substring(sWidthPos , sHeightPos - 1);

		sAdditionalVariablePos = sUrl.indexOf("&" , sHeightPos);
		if(sAdditionalVariablePos > -1) {
			var sHeightQuery = sUrl.substring(sHeightPos, sAdditionalVariablePos);
		} else {
			var sHeightQuery = sUrl.substring(sHeightPos);
		}
		var sWidth = sWidthQuery.split("=")[1];
		var sHeight = sHeightQuery.split("=")[1];
		
	} else if (sHeightPos < sWidthPos) {
		sHeightQuery = sUrl.substring(sHeightPos , sWidthPos - 1);
		var sHeight = sHeightQuery.split("=")[1];
		sAdditionalVariablePos = sUr.indexOf("&" , sWidthPos);
	}
	
	// Set filename
	if(sUrl.indexOf("?") != -1) { 
		var sFile = sUrl.substring(0, sUrl.indexOf("?"));
	} else {
		var sFile = sUrl;
	}	
	
	// Set extension
	var sExtension = sFile.substring(sFile.lastIndexOf(".") + 1);
	
	if((sExtension == "jpg") || (sExtension == "jpeg") || (sExtension == "gif")|| (sExtension == "png")) {
		sWidth = 0;
		sHeight = 0;
		showLightbox(sFile, sExtension, sWidth, sHeight);
		hideLightbox();
	}
	
	return(sFile + "||" + sExtension + "||" + sWidth + "||" + sHeight + "||");
}


//=============================================================================
// Function showLightBox
//============================== GIJS - OCCHIO ================================

function showLightbox(sFile, sExtension, sWidth, sHeight) {

	// Create variables to refer to lightbox
	var divLightbox = document.getElementById("lightbox");
	var divLightboxBackground = document.getElementById("lightboxBackground");
	
	// Show lightbox for flash
	if(sExtension == "swf") {
		//divLightbox.innerHTML = '<object data="' + sFile + '" width="' + sWidth + '" height="' + sHeight + '" type="application/x-shockwave-flash"><param name="movie" value="' + sFile + '" /><param name="quality" value="high" /><param name="loop" value="true" /><param name="wmode" value="transparent" /></object>';
		divLightbox.innerHTML = '<embed type="application/x-shockwave-flash" src="' + sFile + '" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="sameDomain" wmode="transparent" quality="high" height="' + sHeight + '" width="' + sWidth + '"></div>';
	}

	// Show lightbox for images
	else if((sExtension == "jpg") || (sExtension == "jpeg") || (sExtension == "gif")|| (sExtension == "png")) {
	
		if(sWidth == 0) {
			// Duplicate image to retreive width and height
			var imageCopy = new Image;
			imageCopy.src = sFile;
			
			sWidth = imageCopy.width;
			sHeight = imageCopy.height;
			
		}
		
		// Build HTML
		divLightbox.innerHTML = '<img src="' + sFile + '" alt="" onclick="hideLightbox()" id="lightboxImage"/>';
		
	}
	
	// Show lightbox for movies
	else if((sExtension == "mpeg") || (sExtension == "mov") || (sExtension == "mpg") || (sExtension == "avi") || (sExtension == "wmv")) {
		divLightbox.innerHTML = '<embed src="' + sFile + '" href="' + sFile + '" width="' + sWidth + '" height="' + sHeight + '" autostart="true" controls="smallconsole"></embed>';
	} 
	
//=============================================================================
// Function showLightBox - Output
//============================== GIJS - OCCHIO ================================

	// Get window properties
	if(document.documentElement.clientHeight) {
		sWindowHeight = document.documentElement.clientHeight;
	} else {
		sWindowHeight = document.body.clientHeight;
	}
		
	sWindowPos = document.documentElement.scrollTop;
	sLightboxPos = sWindowPos + (sWindowHeight / 2);

	// Show close button
	divLightbox.innerHTML += '<img src="' + sCloseBtnLocation + '" alt="' + sCloseBtnText + '" class="closebutton" style="cursor: pointer;" onclick="hideLightbox()" /><\/div>';

	// Show lightbox
	divLightbox.style.marginTop = ("-" + (parseInt(sHeight / 2) + parseInt(sBorderHeight)) + "px");
	divLightbox.style.marginLeft = ("-" + (parseInt(sWidth / 2) + parseInt(sBorderWidth)) + "px");
	divLightbox.style.top = sLightboxPos + "px";
	divLightbox.style.display = "block";
	divLightboxBackground.style.height = document.body.clientHeight + "px";
	divLightboxBackground.style.display = "block";
	
	// Assign onclick event to background
	divLightboxBackground.onclick = function() {
		hideLightbox();
	}
}

//=============================================================================
// Hide Lightbox
//============================== GIJS - OCCHIO ================================
	
function hideLightbox() {
	var divLightbox = document.getElementById("lightbox");
	var divLightboxBackground = document.getElementById("lightboxBackground");
	
	divLightbox.innerHTML = "";
	divLightbox.style.display = "none"
	divLightboxBackground.style.display = "none";
}