// custom gallery script by bob rios
var bobgal = []; var linknow = 0;
var linknext = 0; var linkback = 0; var arrayPageSize = [];

// when the DOM is ready
$(function () {
	$("#thumbs").find("a[rel=gallery]").each(function(i) {
     bobgal[i] = [];
		 bobgal[i]['href'] = $(this).attr('href');
		 bobgal[i]['title'] = $(this).attr('title');
		 $(this).click(function (event) {
			itclicker(i, $(this).attr('href'), $(this).attr('title'));
			event.preventDefault();
		 });
   });
	if (bobgal.length > 0) {
		$('#plinkbox a[class="go_next"]').bind('click', donext);
		$('#plinkbox a[class="go_back"]').bind('click', doback);
		$('#plinkbox a[class="go_close"]').bind('click', closepop);
		$('#pimagebox').bind('click', closepop);
		$('#plinkbox a[class="go_start"]').click(function (event) {
			itclicker(0, bobgal[0]['href'], bobgal[0]['title']);
		}); 
		$('#plinkbox a[class="go_last"]').click(function (event) {
			itclicker(bobgal.length - 1, bobgal[bobgal.length - 1]['href'], bobgal[bobgal.length - 1]['title']);
		});
	}
});

function closepop() {
	$('#lightboxpop').hide();
	$('#overlaypop').hide();
	$('#plinkbox').hide();
}
function doback () {
	itclicker(linkback, bobgal[linkback]['href'], bobgal[linkback]['title']);
}
function donext() {
	itclicker(linknext, bobgal[linknext]['href'], bobgal[linknext]['title']);
}
function itclicker(num, src, title) {
	var img = new Image();
  
	$('#dvloader').show();
	$('#lightboxpop').hide();
  $('#ptextbox').html(title);
	
	if (bobgal.length > 0) {
		linknow = num;	
		linknext = num+1;
		linkback = num-1;
		
		if (linknext >= bobgal.length) {
			linknext = 0;
		}
		if (linkback < 0) {
			linkback = bobgal.length - 1;
		}	
	}
	$(img).load(function () {    
		$('#pimagebox').html(this);
		$('#lightboxpop').fadeIn();
		$('#dvloader').hide();
		arrayPageSize = getPageSize();
		$('#overlaypop').css("width",arrayPageSize[0] +"px");
		$('#overlaypop').css("height",arrayPageSize[1] +"px");
		$('#overlaypop').show();
		$('#plinkbox').show();
		//console.log(arrayPageSize);
		//console.log($(window).width(), ", ", $(window).height(), ", ", $(document).scrollLeft(), ", ", $(document).scrollTop());
	})
	.error(function () {
		// notify the user that the image could not be loaded
	})
	.attr('src', src);
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
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;
}

// -----------------------------------------------------------------------------------

//
// 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;

	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;
}

// -----------------------------------------------------------------------------------
