/*
/*
 * NEW VERSIONING: 
 * 
 * Version 3.3.1 - Mikael Ramirez
 *
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http: // www.codylindley.com)
 * Modified by Mikael Ramirez 
 *
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http: // www.opensource.org/licenses/mit-license.php
*/

var tb_pathToLoading = "images/thick-box-loading.gif";
var tb_pathToBlank = "images/blank.gif";
var tb_autoSizeValue = false; // this will shrink the picture to fit in your browser 
var tb_padding = 15;
var tb_border = 0; // be sure to set this if your using a border around your image
var tb_captionTop = true;
var tb_showDownloadButton = true;
var tb_resizePadding = 150; // used by tb_autoSizeValue

(function() {

var closeWindowHTML = "<a href='javascript:void(0);' id='TB_closeWindowButton' title='Close'>close <strong>x</strong></a>";
var isImage = true;
var $j = jQuery;

 // on page load call tb_init
jQuery(document).ready(function() { 
	tb_init('a.thickbox, area.thickbox, input.thickbox'); // pass where to apply thickbox
	imgLoader = new Image(); //  preload image
	imgLoader.src = tb_pathToLoading;
	
	window.tb_showIframe = function () {
		$j("#TB_load").remove();
		$j("#TB_window").css({display:"block"});
	}
	window.tb_forceLoad = tb_init;
	window.tb_showPopup = function(url, closeWindowCallback) {
		tb_show('', url, null, closeWindowCallback);
	}
	window.tb_remove = tb_remove;
});

 // add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk) {
	$j(domChunk).click(function() {
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		tb_show(t,a,g);
		this.blur();
		return false;
	});
}

// function called when the user clicks on a thickbox link
function tb_show(caption, url, imageGroup, closeWindowCallback) {
	try {
		if (closeWindowCallback) window.TB_closeWindowCallback = closeWindowCallback;

		if (typeof document.body.style.maxHeight === "undefined") {

			// ie6 doesn't support position: fixed. quick hack to do the trick
			$j("body","html").css({height: "100%", width: "100%"});
			$j("html").css("overflow","hidden");
		
			if (document.getElementById("TB_HideSelect") === null) { // iframe to hide select elements in ie6
				$j("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$j("#TB_overlay").click(tb_remove);
			}
			$j("#TB_overlay").css({height: $j(document).height(), width: $j(document).width()});

		}
		else { // all others
			if (document.getElementById("TB_overlay") === null) {
				$j("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$j("#TB_overlay").click(tb_remove);
			}
		}
		
		if (tb_detectMacXFF())
			$j("#TB_overlay").addClass("TB_overlayMacFFBGHack"); // use png overlay so hide flash
		else
			$j("#TB_overlay").addClass("TB_overlayBG"); // use background and opacity
		
		if (caption===null)
			caption="";
		$j("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>"); // add loader to the page
		$j('#TB_load').show(); // show loader
		
		var baseURL;
		if (url.indexOf("?")!==-1) // ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
		else
			baseURL = url;
		 
	 	var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	 	var urlType = baseURL.toLowerCase().match(urlString);
	
	 	// code to show images	
		if (urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp') {
			isImage = true;
			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevEnabled = false;
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextEnabled = false;
			TB_imageCount = "";
			TB_FoundURL = false;

			if (imageGroup) {
				TB_TempArray = $j("a[rel="+imageGroup+"]").get();
				for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && TB_NextEnabled == false); TB_Counter++) {
					var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
					if (!(TB_TempArray[TB_Counter].href == url)) {						
						if (TB_FoundURL) {
							TB_NextCaption = TB_TempArray[TB_Counter].title;
							TB_NextURL = TB_TempArray[TB_Counter].href;
							TB_NextEnabled = true;
						}
						else {
							TB_PrevCaption = TB_TempArray[TB_Counter].title;
							TB_PrevURL = TB_TempArray[TB_Counter].href;
							TB_PrevEnabled = true;
						}
					}
					else {
						TB_FoundURL = true;
						TB_imageCount = "Image " + (TB_Counter + 1) +" of "+ (TB_TempArray.length);											
					}
				}
			}
			imgPreloader = new Image();
			imgPreloader.onload = function() {
				imgPreloader.onload = null;
				 //  Resizing large images - orginal by Christian Montoya edited by me.
				var pagesize = tb_getPageSize();
				var x = pagesize.width - tb_resizePadding;
				var y = pagesize.height - tb_resizePadding;
				var imageWidth = imgPreloader.width;
				var imageHeight = imgPreloader.height;
			
				// by default we will not resize images to the fit inside the browser with a margin
				// but if you use set tb_autoSize(true) it will resize
				if (tb_autoSize()) {
					if (imageWidth > x) {
						imageHeight = imageHeight * (x / imageWidth); 
						imageWidth = x; 
						if (imageHeight > y) { 
							imageWidth = imageWidth * (y / imageHeight); 
							imageHeight = y; 
						}
					}
					else if (imageHeight > y) { 
						imageWidth = imageWidth * (y / imageHeight); 
						imageHeight = y; 
						if (imageWidth > x) { 
							imageHeight = imageHeight * (x / imageWidth); 
							imageWidth = x;
						}
					}
				}
				//  End Resizing
			
				TB_WIDTH = imageWidth + (tb_padding * 2);
				TB_HEIGHT = imageHeight + 60;

				var TB_imageContainerHTML = '';
				var TB_imageHTML = "<img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/>";
				var containerHTML = function(html) {
					return "<div id='TB_imageContainer' style='position: relative; margin-left:"+(tb_padding-tb_border)+"px; margin-"+(tb_captionTop?'bottom':'top') +":"+tb_padding+"px;'>"+html+"</div>";
				}
				// if we have a prev or next control create the controls and container
				if (TB_PrevEnabled || TB_NextEnabled) {
					var bh = imageHeight + (tb_border * 2);
					var leftWidth = Math.floor(imageWidth/2);
					var rightWidth = imageWidth-leftWidth;
					var nextHTML = (!TB_NextEnabled ? '' :
						"<a id='TB_next' href='javascript:void(0)' style='display: block; position: absolute; top: 0; left: "+(imageWidth-rightWidth+tb_border)+"px;'>" +
							"<img src='"+tb_pathToBlank+"' width='"+ rightWidth+"' height='"+ bh +"' />" +
						"</a>");
					var prevHTML = (!TB_PrevEnabled ? '' :
						"<a id='TB_prev' href='javascript:void(0)' style='display: block; position: absolute; top: 0; left: "+tb_border+"px;'>" +
							"<img src='"+tb_pathToBlank+"' width='"+leftWidth+"' height='"+ bh +"' />" +
						"</a>");

					TB_imageContainerHTML = containerHTML(prevHTML+nextHTML+TB_imageHTML);
				}
				else // otherwise just create an anchor tag around the image that closes the box
					TB_imageContainerHTML =  containerHTML("<a href='' id='TB_ImageOff' title='Close'>"+TB_imageHTML+"</a>");
				
				var TB_captionHTML =
					"<div id='TB_captionBar'>" + 
						"<div id='TB_caption'>" + caption + 
							"<div id='TB_secondLine'>" + TB_imageCount + (tb_showDownloadButton ? "&nbsp;&nbsp;<a class='TB_downloadLink' href='" + url + "' target='_blank'>(download)</a>" : "") + "</div>" + 
						"</div>" +
						"<div id='TB_closeWindow'>" + closeWindowHTML + "</div>" +
					"</div>" + 
					"<div style='clear:both'></div>";
					
				if (tb_captionTop)
					$j("#TB_window").append(TB_captionHTML + TB_imageContainerHTML);
				else
					$j("#TB_window").append(TB_imageContainerHTML + TB_captionHTML);

				$j("#TB_closeWindowButton").click(tb_remove);
				if (TB_PrevEnabled) {
					function goPrev() {
						if ($j(document).unbind("click", goPrev))
							$j(document).unbind("click", goPrev);
						$j("#TB_window").remove();
						$j("body").append("<div id='TB_window'></div>");
						tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
						return false;	
					}
					$j("#TB_prev").click(goPrev);
				}
			
				if (TB_NextEnabled) {
					function goNext() {
						$j("#TB_window").remove();
						$j("body").append("<div id='TB_window'></div>");
						tb_show(TB_NextCaption, TB_NextURL, imageGroup);				
						return false;	
					}
					$j("#TB_next").click(goNext);
				
				}
				document.onkeydown = function(e) {	 
					if (e == null) //  ie
						keycode = event.keyCode;
					else  //  mozilla
						keycode = e.which;
					
					if (keycode == 27) //  close
						tb_remove();
					else if (keycode == 39) {  //  display next image = right arrow
						if (TB_NextEnabled == true) {
							document.onkeydown = "";
							goNext();
						}
					}
					else if (keycode == 37) {  //  display prev image = left arrow
						if (TB_PrevEnabled == true) {
							document.onkeydown = "";
							goPrev();
						}
					}	
				};
			
				tb_position();
				$j("#TB_load").remove();
				$j("#TB_ImageOff").click(tb_remove);
				$j("#TB_window").css({display:"block"});  // for safari using css instead of show
			};
			
			imgPreloader.src = url;
		}
		else { // code to show iframe/ajax
			isImage = false;
			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = tb_parseQuery( queryString );
			var tbwPadding = 0;
			var tbhPadding = 0;
			TB_WIDTH = (params['width']*1) + tbwPadding || 630;  // defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = (params['height']*1) + tbhPadding || 440;  // defaults to 440 if no paramaters were added to URL
			ajaxContentW = TB_WIDTH - tbwPadding;
			ajaxContentH = TB_HEIGHT - tbhPadding;

			if (url.indexOf('TB_iframe') != -1) { //  either iframe or ajax window
				urlNoQuery = url.split('TB_');
				$j("#TB_iframeContent").remove();
				if (params['modal'] != "true") { // iframe no modal
				 	$j("#TB_window").append(
						"<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div>" +
						"<div id='TB_closeAjaxWindow'>"+closeWindowHTML+"</div></div>" +
						"<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;' > </iframe>"
					); 
				}
				else { // iframe modal
					$j("#TB_overlay").unbind();
					$j("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
				}
			}
			else { //  not an iframe, ajax
				if ($j("#TB_window").css("display") != "block") {
					if (params['modal'] != "true") { // ajax no modal
						$j("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'>"+closeWindowHTML+"</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
				}
				else { // ajax modal
					$j("#TB_overlay").unbind();
					$j("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
				}
			}
			else { // this means the window is already up, we are just loading new content via ajax
				$j("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
				$j("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
				$j("#TB_ajaxContent")[0].scrollTop = 0;
				$j("#TB_ajaxWindowTitle").html(caption);
			}
		}
					
		$j("#TB_closeWindowButton").click(tb_remove);
		
		if (url.indexOf('TB_inline') != -1) {	
			$j("#TB_ajaxContent").append($j('#' + params['inlineId']).children());

			$j("#TB_window").unload(function () {
				$j('#' + params['inlineId']).append( $j("#TB_ajaxContent").children() );  //  move elements back when you're finished
			});
			tb_position();
			$j("#TB_load").remove();
			$j("#TB_window").css({display:"block"}); 
		}
		else if (url.indexOf('TB_iframe') != -1) {
			tb_position();
			if ($.browser.safari) { // safari needs help because it will not fire iframe onload
				$j("#TB_load").remove();
				$j("#TB_window").css({display:"block"});
			}
		}
		else {
			$j("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function() { // to do a post change this load method
				tb_position();
				$j("#TB_load").remove();
				tb_init("#TB_ajaxContent a.thickbox");
				$j("#TB_window").css({display:"block"});
			});
		}
	}
	if (!params['modal']) {
		document.onkeyup = function(e) {
			if (e == null) //  ie
				keycode = event.keyCode;
			else //  mozilla
				keycode = e.which;
			if (keycode == 27) //  close
				tb_remove();
		};
	}
	}
	catch(e) {}
}

function tb_remove(closeWindowCallback) {
	// this was an attempt to fix a bug in ie6 which didn't allow the iframe to be closed in ie6
	//$j("#TB_window iframe").contents().find('object').remove();
	
	$j("#TB_imageOff").unbind("click");
	$j("#TB_closeWindowButton").unbind("click");
	$j("#TB_window").stop();
	$j("#TB_window").empty();
	$j('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
	$j("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") { // if IE 6
		$j("body","html").css({height: "auto", width: "auto"});
		$j("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";

	if (window.TB_closeWindowCallback) {
		window.TB_closeWindowCallback.call(this);
		window.TB_closeWindowCallback = null;
	}
	return false;
}
function tb_position() {
	var view = tb_getPageSize();
	var overSized = (TB_WIDTH >= view.width || TB_HEIGHT > view.height) ? true : false;

	if (overSized) {
		$j("#TB_window").css({position: 'absolute'});
		// we're in ie6 so undo html{overflow:hidden} and TB_overlay{position: fixed}
		if (typeof document.body.style.maxHeight === "undefined") {
			$j("html").css('overflow', 'auto');
			$j("html", "body").css({height: 'auto', width: 'auto'});
			$j("#TB_window").css({position: 'absolute', marginTop: 0});
		}
	}
	
	$j("#TB_window").css({
		marginLeft: (TB_WIDTH <= view.width ? '-' + parseInt((TB_WIDTH / 2),10) + 'px' : '-'+(view.width/2)+'px'),
		width: TB_WIDTH + 'px'
	});

	if (overSized)
		$j("#TB_window").css('top', tb_scrollTop());
	else if (!(jQuery.browser.msie && jQuery.browser.version < 7))
		$j("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});


	$j("#TB_window").stop();
	$j("#TB_window").fadeIn(186);

	// if we're in ie 6 fix a couple bugs
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		// fixing a bug here in ie6. something keeps adding a negative margin-top value here but 
		// only if the scrollTop position is set to 0.
		if (tb_scrollTop() == 0 && overSized)
			$j("#TB_window").css('margin-top', '0px');
			
		// ie6 doesn't properly calculate the dimensions from the css. we're resetting it here
		// after the image is up to get the proper width/height
		$j("#TB_overlay").css({height: $j(document).height(), width: $j(document).width()});
	}
}
function tb_parseQuery(query)  {
	 var Params = {};
	 if (!query)
		return Params; //  return empty object
	 var Pairs = query.split(/[;&]/);
	 for (var i = 0; i < Pairs.length; i++) {
		var KeyVal = Pairs[i].split('=');
		if (!KeyVal || KeyVal.length != 2)
			continue;
		var key = unescape(KeyVal[0]);
		var val = unescape(KeyVal[1]);
		val = val.replace(/\+/g, ' ');
		Params[key] = val;
	 }
	 return Params;
}
function tb_getPageSize() {
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	return {width: w, height: h}
}
function tb_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)
		return true;
	return false;
}
function tb_autoSize(val) {
	if (typeof(val) == 'undefined')
		return tb_autoSizeValue;
	tb_autoSizeValue = val;
}
// get the vertical scroll position of the browser
function tb_scrollTop() {
	if (typeof window.pageYOffset != 'undefined')
		return window.pageYOffset;
	else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
		return document.documentElement.scrollTop;
	else if (typeof document.body.scrollTop != 'undefined')
		return document.body.scrollTop
	return 0;3
}
// get the full width and height of the page
function tb_getDocSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	return { 
		width: (xScroll < windowWidth) ? windowWidth : xScroll,
		height: (xScroll < windowHeight) ? windowHeight : yScroll
	}
}
})();