/////////////////////////////////////////////////////
// Settings
/////////////////////////////////////////////////////

var container_width = 240;
var container_height = 160;
var thumb_width = 240;
var thumb_height = 160;
var phpthumb_path = "/thumb/phpThumb.php";
var image_path = "/assets/galleries/";

/////////////////////////////////////////////////////
// Do not edit below
/////////////////////////////////////////////////////

var initial = 1; // this may be dynamic in the future

$(function () {
	
	$('.imagewrap').addClass('loading');

    // // enlarge the gallery image
    // $('.enlarge').click(function(){
    //  
    //  // $('#flashPromo').hide();
    //  // setup some variables
    //  var rel = $(this).attr('rel');
    //  var id = $(this).attr('id').substring(8);
    //  
    //  $('#gallery_box_img').addClass('loading');
    //  
    //  // convert string to array object it references
    //  object = eval(rel);
    //  
    //  // intialize the large image viewer
    //  _intializeViewer(object,rel,id);
    //  
    //  return false;
    // });
	
    // // enlarge the gallery image
    // $('#image_expander .expand').click(function(){
    //  
    //  // setup some variables
    //  var rel = $(this).attr('rel');
    //  //  var id = $(this).attr('id').substring(8);
    //  
    //  $('#gallery_box_img').addClass('loading');
    //  
    //  // convert string to array object it references
    //  // object = eval(rel);
    //  
    //  // intialize the large image viewer
    //  _intializeViewerOne(rel);
    //  
    //  return false;
    // });
	
	// PAGINATION LINKS
	
	$('.gcontrols a.prev_link').click(function(){
		
		var rel = $(this).attr('rel');
		
		
		
		prev = parseInt($('#'+rel+' a.prev_link').attr('id').substring(5));
		next = parseInt($('#'+rel+' a.next_link').attr('id').substring(5));
		
		// convert string to array object it references
		object = eval(rel);
		
		// load the gallery image
		imageLoader(object,rel,prev,thumb_width,thumb_height);
		
		// handle the previous link		
		if(prev <= 1) {
			prev = object.length;
			current = 1;
		}else{
			prev =  prev - 1;
			current = prev + 1;
		}
			
		// handle the next link
		if(next <= 1) {
			var next = object.length;
		}else{
			var next =  next - 1;
		}
		
		// update the pagination links
		updatePagination(rel,prev,next,current);
		
		return false;
	});
	
	$('.gcontrols a.next_link').click(function(){
		
		var rel = $(this).attr('rel');
		
    // alert(rel);
		
		prev = parseInt($('#'+rel+' a.prev_link').attr('id').substring(5));
		next = parseInt($('#'+rel+' a.next_link').attr('id').substring(5));

		// convert string to array object it references
		object = eval(rel);

		// load the gallery image
		imageLoader(object,rel,next,thumb_width,thumb_height);
		
		// handle the previous link		
		if(prev >= object.length) {
			var prev = 1;
		}else{
			var prev =  prev + 1;
		}

		// handle the next link
		if(next >= object.length) {
			var next = 1;
			var current = object.length;
		}else{
			var next =  next + 1;
			var current = prev + 1;

		}
		
		// update the pagination links
		updatePagination(rel,prev,next,current);

		return false;
	});
	
});

// GALLERY FUNCTIONS

function initializeMiniGallery(object,str) {	
	prev = object.length;
	next =  2;
	updatePagination(str,prev,next, 1);
	imageLoader(object,str,1,thumb_width,thumb_height);
}

function updatePagination(str,prev,next,current) {
	$('#'+str+' .prev_link').attr('id', 'prev_'+prev);
	$('#'+str+' .next_link').attr('id', 'next_'+next);	
	// $('#'+str+' .enlarge').attr('id', 'enlarge_'+current);	
}

function updateCaption(object,str,id) {
	$('#'+str+' .imagecaption').css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
	$('#'+str+' .imagecaption').html('').append(object[id][1]);
	$('#'+str+' .imagecaption').show();
}

function imageLoader(object,str,id,width,height){
	
	var img = new Image(); // create a blank image
	
	var url_id = id;
	id = id-1; // adjust the id for the array
	
	var fullpath = phpthumb_path+"?src="+image_path+object[id][0];
	
	// alert(fullpath);
	
	// alert(fullpath);
	var link_id = $('div#'+str+' .imagepreview a').attr('id').substring(4);
	$('div#'+str+' .imagepreview a').attr("href","/embeds/_view_full_gallery/"+link_id+"/"+url_id);
	
	$(img).load(function() {
		
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		
		$('div#'+str+' .imagepreview a').html('').append(this);
		
		$(img).fadeIn();
		
		$('div#'+str+' .imagecaption a').fadeIn('slow');
		
		updateCaption(object,str,id);
		
		
	}).attr('src', fullpath+"&h="+height+"&w="+width+"&zc=1&q=100");
}

// GALLERY VIEWER FUNCTIONS

function _intializeViewer(object,str,id){
	
	$('body').append('<div id="overlay"></div><div id="gallery_control_wrap"><div id="gallery_controls"><div id="gcinner"><p><a href="javascript:void(0)" id="viewerClose">(x) Close</a> <a href="javascript:void(0)" rel="'+str+'" class="prev_link">Prev</a>&nbsp;&nbsp;/ <a href="javascript:void(0)" rel="'+str+'" class="next_link">Next</a>  <span id="viewerCaption"></span></p></div></div><div id="gallery_box" class="loading"><div id="gallery_box_img"><img id="viewer_image" /></div></div></div>');
	
	var arrPageSizes = ___getPageSize();
	
	$('#overlay').css({
		backgroundColor:	"#000",
		opacity:		0.9,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn('slow');
	
	// handle the previous link
	if(id <= 1) {
		var prev = object.length;
	}else{
		var prev =  parseInt(id) - 1;
	}
	
	// handle the next link
	if(id >= object.length) {
		var next = 1;
	}else{
		var next = parseInt(id) + 1;
	}
	
	updateViewerPagination(prev,next);
	imageViewerLoader(object,str,id,800,600);

	
	$('#gallery_controls .prev_link').click(function(){

		$('#viewerCaption').css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$('#gallery_box').addClass('loading');
		
		var rel = $(this).attr('rel');

		prev = parseInt($('#gallery_controls a.prev_link').attr('id').substring(5));
		next = parseInt($('#gallery_controls a.next_link').attr('id').substring(5));

		object = eval(rel);

		imageViewerLoader(object,rel,prev,800,600);

		// handle the previous link		
		if(prev <= 1) {
			prev = object.length;
			current = 1;
		}else{
			prev =  prev - 1;
			current = prev + 1;
		}
			
		// handle the next link
		if(next <= 1) {
			var next = object.length;
		}else{
			var next =  next - 1;
		}
		
		updateViewerPagination(prev,next);

		return false;
	});



	$('#gallery_controls a.next_link').click(function(){

		$('#viewerCaption').css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$('#gallery_box').addClass('loading');
		
		var rel = $(this).attr('rel');

		prev = parseInt($('#gallery_controls a.prev_link').attr('id').substring(5));
		next = parseInt($('#gallery_controls a.next_link').attr('id').substring(5));

		object = eval(rel);

		imageViewerLoader(object,rel,next,800,600);

		// handle the previous link		
		if(prev >= object.length) {
			var prev = 1;
		}else{
			var prev =  prev + 1;
		}

		// handle the next link
		if(next >= object.length) {
			var next = 1;
			var current = object.length;
		}else{
			var next =  next + 1;
			var current = prev + 1;

		}
		
		updateViewerPagination(prev,next);

		return false;
	});
	
	$('#viewerClose').click(function(){
		_finish();
		return false;
	});
	
	return false;
}


function _intializeViewerOne(rel){
	
	$('body').append('<div id="overlay"></div><div id="gallery_control_wrap"><div id="gallery_controls"><p><a href="javascript:void(0)" id="viewerClose">(x) Close</a> <span id="viewerCaption"></span></p></div><div id="gallery_box" class="loading"><div id="gallery_box_img"><img id="viewer_image" /></div></div></div>');
	
	var arrPageSizes = ___getPageSize();
	
	$('#overlay').css({
		backgroundColor:	"#000",
		opacity:		0.8,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn('slow');
	
	
	
	// imageViewerLoader(object,str,id,800,600);
	
	$('#viewer_image').hide();
	var fullpath = phpthumb_path+"?src=/"+rel;
	
	$('#viewer_image').load(function() {
		// $(this).hide();
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		
		$('#gallery_box_img').html('').append(this);
		$('#viewer_image').show();
		
		// updateViewerCaption(object,id);
		// $('#gallery_box_img').removeClass('loading');
		
		moveby = (600 - $('#viewer_image').height()) / 2;
		
		$('#gallery_box_img').css('top',moveby);
	}).attr('src', fullpath+"&hp=600&wl=800&q=100");
	

	$('#viewerClose').click(function(){
		_finish();
		return false;
	});
	
	return false;
}

function imageViewerLoader(object,str,id,width,height){
	// $('.gallery_item').hide();
	$('#viewer_image').hide();
	
	id = id-1;

	var fullpath = phpthumb_path+"?src="+image_path+object[id][0];
	
	$('#viewer_image').load(function() {
		// $(this).hide();
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		
		$('#gallery_box_img').html('').append(this);
		$('#viewer_image').show();
		
		updateViewerCaption(object,id);
		// $('#gallery_box_img').removeClass('loading');
		
		moveby = (600 - $('#viewer_image').height()) / 2;
		$('#gallery_box_img').css('top',moveby);
	}).attr('src', fullpath+"&hp="+height+"&wl="+width+"&q=100");
	
	return false;
	
}

function _finish() {
	$('.gallery_item').show();
	$('#gallery_control_wrap').remove();
	$('#overlay').fadeOut(function() {$('#overlay').remove();});
}

function updateViewerPagination(prev, next) {
	$('#gallery_controls .prev_link').attr('id', 'prev_'+prev);
	$('#gallery_controls .next_link').attr('id', 'next_'+next);		
	return false;
}

function updateViewerCaption(object,id) {
	$('#viewerCaption').html('').append(object[id][1]);
	$('#viewerCaption').fadeIn('slow');
}

// THIRD PARTY FUNCTIONS

/**
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
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;
};

/**
* getPageScroll() by quirksmode.com
*
* @return Array Return an array with x,y page scroll values.
*/
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;
};