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;
}

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;
	
	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;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}  
function doChangeImage (imageName){

	imgPreloader = new Image();

	// once image is preloaded, resize image container
	imgPreloader.onload=function(){
		var newWidth = imgPreloader.width;
		var newHeight = imgPreloader.height;
		
		//fit to screen
		var arrayPageSize = getPageSize();
		var initialPageWidth = arrayPageSize[2];
		var initialPageHeight = arrayPageSize[3] - 100;

		if (imgPreloader.height > initialPageHeight)
		{
			newWidth = parseInt((initialPageHeight/imgPreloader.height) * imgPreloader.width);
			newHeight = initialPageHeight;
		}
		else if (imgPreloader.width > initialPageWidth)
		{
			newHeight = parseInt((initialPageWidth/imgPreloader.width) * imgPreloader.height);
			newWidth = initialPageWidth;
		}

		$('#facebox-image').attr('src', imageName).width(newWidth).height(newHeight);
		$('#facebox-image-wrapper').removeClass("facebox-spinner");
		
		// resize wrappers 
//		$("#facebox-inner").css({marginTop: ((arrayPageSize[3]-(newHeight+20))/2)+'px'});
		$("#facebox-inner").animate({marginTop: ((arrayPageSize[3]-(newHeight+42))/2)+'px'},250,'linear');
		$('#facebox-inner').animate({width: (newWidth)+'px'},500,'linear',function(){
			$('#facebox-inner').animate({height: (newHeight)+'px'},500,'linear',function(){
				$("#facebox-image-wrapper").css({height:newHeight+'px'});
				$("#facebox-image").fadeIn("medium");
				$("#close-facebox").css({width: (newWidth)+'px'}).slideDown("medium");
			});
		});
		
	}

	imgPreloader.src = imageName;
}

function faceBox () {
	$('a[rel*=facebox]').click(function () {
		var imageName = $(this).attr("href");
		var imageCaption = $(this).attr("title");
		$("body").find("div#facebox").remove();
		$("body").append('<div id="facebox"><div id="facebox-inner"><div id="facebox-image-wrapper"><img id="facebox-image" alt="Press esc or Click to close"/></div></div><div id="close-facebox"><p id = "image-caption">'+imageCaption+'</p><p><a href="javascript:void()"><img src="js/closelabel.gif" alt="Close" /></a></p></div></div>');
		var arrayPageScroll = getPageScroll();
		var arrayPageSize = getPageSize();		
		//var faceBoxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var faceBoxLeft = arrayPageScroll[0];
		var faceBoxTop = arrayPageScroll[1];
		var faceBoxInnerTop = (arrayPageSize[3]-170)/2;
		$("#facebox").css({top: faceBoxTop+'px', left: faceBoxLeft+'px'});
		$("#close-facebox").css({top: faceBoxTop+'px', left: faceBoxLeft+'px'});
		$("#facebox-inner").css({marginTop: faceBoxInnerTop+'px'});
		$('#facebox-image-wrapper').addClass("facebox-spinner");
		$('#facebox-image').hide();

		//change image
		doChangeImage(imageName);
		
		$("#facebox-image,#close-facebox a").bind("click", function(){
			$("#facebox").hide('slow', function () {
				$("#facebox").remove();
			});
			return false;
		});
		
		///closing on escape key
		document.onkeydown = function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				$("#facebox").hide('slow', function () {
					$("#facebox").remove();														   
				});
			}
		};
		return false;
	}); // end on click
}
$(document).ready(function() {
	faceBox();
});