/*
 * $Id: gallery.jq.js,v 1.3 2008/03/13 15:59:15 truncatei Exp $
 * jQuery version
 */
gaWidth = 460;
gaHeight = 320;
function showGallery(url) {
	var objOverlay = $('#gaOverly');
	if (!objOverlay.id) {
		var objBody = $('body');
		//alert(objBody);
		objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','gaOverly');
		objOverlay.style.display = 'none';
		objOverlay.position = 'absolute';
		objOverlay.style.backgroundColor = 'black';
		objOverlay.onclick = function() { hideGallery(); }
		//objBody.appendChild(objOverlay);
		$(objOverlay).appendTo('body');
	}
	var objContainer = $('#gaContainer');
	if (!objContainer.id) {
		var objBody = $('body');
		
		objContainer = document.createElement("div");
		objContainer.setAttribute('id','gaContainer');
		objContainer.style.display = 'none';
		objContainer.position = 'absolute';
		objContainer.style.backgroundColor = 'black';
		objContainer.onclick = function() { hideGallery(); }
		//objBody.appendChild(objContainer);
		$(objContainer).appendTo('body');
	}
	
	var arrayPageSize = getPageSize();
	//Position.absolutize(objOverlay);
	objOverlay = $(objOverlay);
	objOverlay.css('position', 'absolute');
	objOverlay.css('left', '0px');
	objOverlay.css('top', '0px');
	objOverlay.css('width', arrayPageSize[0]+'px');
	objOverlay.css('height', arrayPageSize[1]+'px');
	//new Effect.Appear('gaOverly', { duration: 0.7, from: 0.0, to: 0.8 });
	objOverlay.css('display', '');
	objOverlay.fadeTo('slow', 0.8);

	//new Effect.Appear('gaContainer', { duration: 0.7, from: 0.0, to: 1 });
	//Position.absolutize(objContainer);
	objContainer = $(objContainer);
	objContainer.css('position', 'absolute');
	objContainer.css('left', ((arrayPageSize[2] - gaWidth) / 2) + 'px');
	objContainer.css('top', ((arrayPageSize[3] - gaHeight) / 2) + 'px');
	objContainer.css('width', gaWidth+'px');
	objContainer.css('height', gaHeight+'px');
	objContainer.css('display', '');
 	//objContainer.fadeTo('slow', 1);
 	objContainer.html('<iframe id="gaIframe" width="'+gaWidth+'" height="'+gaHeight+'" frameborder="0" src="'+url+'" scrolling="no"></iframe>');
	// IE only
	document.getElementById('gaIframe').src = url;
}

function hideGallery() {
	/*var objContainer = $('#gaContainer');
	var objOverlay = $('#gaOverly');*/
	// FIX Errors occur in IE, cause gallery can't close when hit close.
	var objContainer = document.getElementById('gaContainer');
	var objOverlay = document.getElementById('gaOverly');
	if (objContainer) {
		objContainer.style.display='none';
		objContainer.innerHTML = '';
	}
	if (objOverlay) objOverlay.style.display='none';
}


// Function from lightbox, Thanks for the author :)
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}