// JavaScript Document

$(function(){
	$('.zoom').live('click',function(){
			lightBoxZoom(this);
			return false;
		});
		
	//add lightbox functionality to any links to images. 
	$(".entry a").each(function(){
			linkPath = this.href;
			linkArray = linkPath.split('.');
			ext = linkArray[linkArray.length-1];
			ext = ext.toLowerCase();
			//alert(ext);
			if(ext == 'jpg' || ext == 'png' || ext == 'gif' || ext == 'jpeg'){
				altText = $(this).children('img').attr('alt');
				$(this).addClass('zoom');
				$(this).attr('title',altText);
			}
		});
		
		$('.loadwpcontent').bind('click',function(){
			lbLoadWPContent(this.href);
			return false;
		});
	
});


//-----------------------------------------
//
//	ZR JQuery Lightbox zoom script 2.5	 
//	basic image zoom and gallery functionality
//
//------------------------------------------

//var lbStage = 'ready';
var lbArray = new Array();
var lbTitleArray = new Array();
var lbGallery = false;

//Set up all the HTML for the lightbox
var lbLoadHTML = '<img src="/images/lightbox/lightbox-load.gif" alt="loading" title="loading" id="lightboxload" />';
var lbMaskHTML = '<div id="mask" onclick="closeLightBox();" title="Click to Close"></div>' + lbLoadHTML;
var lbHTML = '<div id="lightbox"><div id="lightboxutil"><a href="#" id="lbclose" title="Close Window" onclick="closeLightBox(); return false;"></a></div></div>';
var lbPrevLink = '';
var lbNextLink = '';

//core function that sets up the mask and the light box
var lightBoxZoom = function(el){
	lbMaskLoad();
	//Set up arrays for Lightbox Galleries Mode.
	$(".zoom").each(function(i) {
		lbArray[i] = this.href;
		lbSetImgCaption(this);
		lbTitleArray[i] = imgCaption;
	});
	if(lbArray.length > 1) { 
		lbGallery = true;
	}
	lbLoadContent(el);
};

//function to set up the background and preloader
var lbMaskLoad = function() { 
	lbLoadTopPos = (((Math.round( $(window).height())/2) + $(window).scrollTop() )-16);
	lbLoadLeftPos = ((Math.round($(window).width())/2)-16);
	lbStage = 'loading';
	$("body").prepend(lbMaskHTML);
	$("#mask").css("height",$(document).height());
	$("#lightboxload").css("top",lbLoadTopPos);
	$("#lightboxload").css("left",lbLoadLeftPos);
	// alert(
	//	'Document Height is: ' + $(document).height() + 
	//	'\nWindow Height is: ' + $(window).height() + 
	//	'\nWindow Width is: ' + $(window).width() + 
	//	'\nScrollTop is: ' + $(window).scrollTop()
	// );*/
};

var lbSetTopPos = function() { 
	var lbTopPos = Math.round((($(window).height() - $('#lightbox').height())/2)) + $(window).scrollTop() - 10;
	$("#lightbox").css("top",lbTopPos);
};

var lbSetImgCaption = function(el){
	imgCaption = el.title;
	if(imgCaption == '') { 
		imgCaption = $(el).children('img').attr('title');
	} 
};

//Core function that loads the Image to the lightbox
var lbLoadContent = function(el){
	//Set up the Image properties
	var imgPath = el.href;
	
	//pick the title of the image if it exists then the a tag
	

	lbSetImgCaption(el);
	var lbImg = new Image();
	
	$(lbImg).load(function() { 
		//This all happens after the image loads
		var lbImgHeight = this.height;
		var lbImgWidth = this.width;
		var targetImgHeight = parseInt($(window).height() * .76);
		var targetImgWidth = parseInt($(window).width() * .8);
		var lbHeightDiff = lbImgHeight - targetImgHeight;
		var lbWidthDiff = lbImgWidth - targetImgWidth;
		/* alert(
			'window height is ' + $(window).height() + 
			'\nwindow width is ' + $(window).width() + 
			'\nImage height is ' + lbImgHeight + 
			'\nImage width is ' + lbImgWidth +
			'\nTarget height is ' + targetImgHeight + 
			'\nTarget width is ' + targetImgWidth +
			'\nHeight Difference is ' + lbHeightDiff + 
			'\nWidth Difference  is ' + lbWidthDiff 
		 );*/
		$("#lightboxload").replaceWith(lbHTML);
		$(this).attr('id','lightboximage');
		$("#lightbox").append(this);
		
		//Resizing the image if it is too large for the browser window
		if (lbHeightDiff > 0 || lbWidthDiff > 0) { 
			if (lbHeightDiff > lbWidthDiff) { 
				this.height = targetImgHeight;
				if($.browser.msie == true) {  
					//set the other dim for IE
					var imgDimRatio = targetImgHeight / lbImgHeight;
					this.width = parseInt(lbImgWidth * imgDimRatio);
				}
			} else
			if (lbWidthDiff > lbHeightDiff) { 
				this.width = targetImgWidth;
				if($.browser.msie == true) {  
					//set the other dim for IE
					var imgDimRatio = targetImgWidth / lbImgWidth;
					this.height = parseInt(lbImgHeight * imgDimRatio);
				}
			}
		}

		//Set up Previous and next content for Lightbox Galleries
		var lbCaption = '';
		if(lbGallery == true){
			lbx=0;
			lastImg = lbArray.length - 1;
			for (lbx=0; lbx<lbArray.length; lbx++){
				if(imgPath == lbArray[lbx]){
					lbCaption = lbTitleArray[lbx];
					switch (lbx){
						case 0:
							prevImg = lbArray[lastImg];
							nextImg = lbArray[lbx+1];
							prevCaption = lbTitleArray[lastImg];
							nextCaption = lbTitleArray[lbx+1];
						break;
						case lastImg:
							prevImg = lbArray[lbx-1];
							nextImg = lbArray[0];
							prevCaption = lbTitleArray[lbx-1];
							nextCaption = lbTitleArray[0];
						break;
						default:
							prevImg = lbArray[lbx-1];
							nextImg = lbArray[lbx+1];
							prevCaption = lbTitleArray[lbx-1];
							nextCaption = lbTitleArray[lbx+1];
					}
				}
			}
			var lbPrevLink = '<a href="'+prevImg+'" onclick="prevNextBox(this); return false;" title="'+prevCaption+'" class="lightboxprev"></a>';
			var lbNextLink = '<a href="'+nextImg+'" onclick="prevNextBox(this); return false;" title="'+nextCaption+'" class="lightboxnext"></a>';
			$("#lightboxutil").prepend(lbPrevLink+lbNextLink);
			$("#lightboximage").wrap('<a href=' + nextImg + ' onclick="prevNextBox(this); return false;" />');
		}
		
		//Add in the caption if it exists
		if(lbCaption != '' && lbCaption != undefined) { 
			$("#lightbox").append('<div id="lightboxcaption"></div>');
			$("#lightboxcaption").width($('#lightbox').width()-12);
			$("#lightboxcaption").html(lbCaption);
		} 
		var lbwidth = $("#lightbox").width();
		var lbLeftPos = (Math.round($(window).width()/2))-(Math.round(lbwidth/2));
		$("#lightbox").css("left",lbLeftPos);
		//Must have PNG fix enabled to fix the close button PNG on the fly
		$("#lightboxutil img").css("behavior","url(/iepngfix.htc)");
		$("#lightbox").fadeIn('normal',function() {
		//set the docHeight again because the lightbox can push it down.
		docHeight = $(document).height();
		$("#mask").css("height",docHeight);
			lbStage = 'loaded';
		});
		
		
		//Set up the top position of the LB this should happen right after we have content 
		lbSetTopPos();
	}).attr('src',imgPath).error(function () {
			 alert('there was an error loading the image:\n' + imgPath);
			 $("#lightboxload").remove();
				$("#mask").remove();
				lbImg = '';
				lbStage = 'ready';
	});
};

var prevNextBox = function(el){
	$("#lightbox").fadeOut('fast', function(){
		$("#lightbox").remove();
		$("#mask").after(lbLoadHTML);
		$("#lightboxload").css("top",lbLoadTopPos);
		$("#lightboxload").css("left",lbLoadLeftPos);
		lbStage = 'loading';
		lbLoadContent(el);
	});
}


//-----------------------------------------
//
//	ZR Close Lightbox
//
//------------------------------------------
var closeLightBox = function(callback){
	if (lbStage == 'loading') { 
		if ($.browser.msie == false) {
			//do nothing if it is IE until the window is done loading.
			stop();
			$("#lightboxload").remove();
			$("#lightbox").remove();
			$("#mask").remove();
			lbStage = 'ready';
		}
	} else {
		$("#lightbox").fadeOut('fast', function(){
			$("#lightbox").remove();
			$("#mask").remove();
			lbStage = 'ready';
			if(callback) { 
				eval(callback + '()');
			}
		});
	}
}




//-----------------------------------------
//
//	ZR JQuery Ajax Lightbox  script 1.1	
//	This takes the string between the last slash slash's
//	in the URL and loads that refid into a lightbox
//
//------------------------------------------*/

var ajaxBoxHTML = '<div id="lightbox" class="ajaxbox"><div id="lightboxutil"><a href="#" id="lbclose" title="Close Window" onclick="closeLightBox(); return false;"></a></div><div id="ajaxboxcontent"></div></div>';

var lbContentSetUp = function(){
	lbMaskLoad();
	$("#lightboxload").after(ajaxBoxHTML);
	var lbLeftPos = (Math.round($(window).width()/2))-260;
	$("#lightbox").css("left",lbLeftPos);
	$("#lightboxclose img").css("behavior","url(/iepngfix.htc)");
};

var lbContentMaxY = function(){
	var lbHeadHeight = $('#lightbox').height() - $('#ajaxboxcopy').height();
	var optimumLBHeight = $(window).height() * .8;
	var optimumLBCopy = Math.round(optimumLBHeight - lbHeadHeight);	
	if($('#lbheaderimg img').length > 0) { 
		//Load up the header image first so that we can set the height properly.
		$('#lbheaderimg img').load().height()
	}
	
	//Minimum height should be 350, if user has a tiny monitor they can scroll
	if(optimumLBCopy < 350) { 
		optimumLBCopy = 350;
	}
	if($('#ajaxboxcopy').height() > optimumLBCopy) { 
		$('#ajaxboxcopy').css('height',optimumLBCopy+'px');
		$('#ajaxboxcopy').css('overflow','auto');
		$('#ajaxboxcopy').css('width','489px');
		$('#ajaxboxcopy').css('padding-right','10px');
	}
	lbSetTopPos();
};

var ajaxLightBox = function(el) { 
	var ajaxLink = el.href;
	lbparm = ajaxLink.split("/");
	lbparm = lbparm.reverse();

	lbContentSetUp();
	$("#ajaxboxcontent").load("/includes/ajax-box/", {refid:lbparm[1]}, function(){
		$("#lightboxload").remove();
		//set the mask height again just to make sure we don't have the white space in the footer
		$("#mask").css("height",$(document).height());
		$("#lightbox").fadeIn('normal', function(){
			lbStage = 'loaded';
		});
		//set the max height;
		lbContentMaxY();
	});
};

//lbPage goes after a URL and loads the whole thing into the lightbox

var lbPageUrl = function(url) { 
	var ajaxLink = url;
	lbContentSetUp();
	$("#ajaxboxcontent").load(ajaxLink, function(){
		$("#lightboxload").remove();
		//set the mask height again just to make sure we don't have the white space in the footer
		$("#mask").css("height",$(document).height());
		//show the content
		$("#lightbox").fadeIn('normal', function(){
			lbStage = 'loaded';
		});
		//set the max height;
		lbContentMaxY();
	});
};


//lbPage goes after a URL and loads the whole thing into the lightbox

var lbPage = function(el) { 
	//alert('got here');
  var ajaxLink = el.href;
	ajaxLink = ajaxLink.split('=');
	//alert(ajaxLink);
  lbContentSetUp();
  $("#ajaxboxcontent").load(ajaxLink[0] + escape(ajaxLink[1]), function(){
    $("#lightboxload").remove();
    //set the mask height again just to make sure we don't have the white space in the footer
    $("#mask").css("height",$(document).height());
    //show the content
    $("#lightbox").fadeIn('normal', function(){
      lbStage = 'loaded';
    });
    //set the max height;
    lbContentMaxY();
  });
}

//Loads an actual URL as the parameter, used onclick, sets the focus on the first input
var lbLoadPage = function(url,callback) { 
  lbContentSetUp();
  $("#ajaxboxcontent").load(url, function(){
    $("#lightboxload").remove();
    $("#mask").css("height",$(document).height());
    $("#lightbox").fadeIn('normal', function(){
      lbStage = 'loaded';
			if(callback) { 
				eval(callback + "();");
			}
    });
    lbContentMaxY();
    $(':input:visible:enabled:first').focus();
  });
}

var skiBoxHTML = '<div id="lightbox"><div id="lightboxutil"><a href="#" id="lbclose" title="Close Window" onclick="closeLightBox(); return false;"></a></div><div id="skiajaxboxcontent"></div></div>';

var lbLoadAllSkis = function(url) { 
  lbMaskLoad();
	$("#lightboxload").after(skiBoxHTML);
	var lbLeftPos = (Math.round($(window).width()/2))-440;
	$("#lightbox").css("left",lbLeftPos);
	$("#lightboxclose img").css("behavior","url(/iepngfix.htc)");
	
  $("#skiajaxboxcontent").load(url, function(){
    $("#lightboxload").remove();
    $("#mask").css("height",$(document).height());
    $("#lightbox").fadeIn('normal', function(){
      lbStage = 'loaded';
    });
    lbContentMaxY();
    $(':input:visible:enabled:first').focus();
  });
}


var lbLoadWPContent = function(url) { 
  lbContentSetUp();
  $("#ajaxboxcontent").load(url + ' #pagecontent', function(){
    $("#lightboxload").remove();
    $("#mask").css("height",$(document).height());
    $("#lightbox").fadeIn('normal', function(){
      lbStage = 'loaded';
    });
    lbContentMaxY();
    $(':input:visible:enabled:first').focus();
  });
}


	
	
