/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
		  
var tb_pathToImage = "/i/loading.gif";

$(document).ready(function(){   
	tb_init('a.thickbox, area.thickbox, input.thickbox');
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage;
});

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

function tb_show(url, imageGroup) {

	if(document.getElementById("TB_overlay") === null){
		$("body").append("<div id='TB_overlay' class='TB_overlayBG'></div><div id='TB_window'></div>");
		$("#TB_overlay")
			.css({filter: "alpha(opacity=0)"})
			.animate({
				opacity: 0.75
			}, "medium", showLoader )
			.click(tb_remove);
	}

	if( tb_detectMacXFF() ){
		$("#TB_overlay")
			.removeClass( "TB_overlayBG" )
			.addClass( "TB_overlayMacFFBGHack" );
	}

	function showLoader(){
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");
		$('#TB_load')
			.fadeIn("medium", showImage)
			.click(tb_remove);
	}

	function showImage(){
		imgPreloader = new Image();
		imgPreloader.onload = function(){
			imgPreloader.onload = null;

			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 60;
			var y = pagesize[1] - 60;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			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;
				}
			}
			
			TB_WIDTH = imageWidth;
			TB_HEIGHT = imageHeight;
			$("#TB_window").append("<a href='' id='TB_ImageOff'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' /></a>");

			$('#TB_load').fadeOut("medium", function(){
				$("#TB_Image").fadeIn("medium", function(){
					$("#TB_load").remove();
				});
			});

			document.onkeydown = function(e){
				keycode = (e == null) ? event.keyCode : e.which;
				if(keycode == 27) tb_remove();
			};

			tb_position();
			$("#TB_ImageOff").click(tb_remove);
			$("#TB_window").css({display:"block"}); //for safari using css instead of show
		};
		
		imgPreloader.src = url;
	}
}

function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");

	$("#TB_window").fadeOut("medium",function(){
		$("#TB_overlay").fadeOut("medium", function(){
			$('#TB_window, #TB_overlay, #TB_HideSelect').trigger("unload").unbind().remove()
		})
	});

	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}



function tb_position() {
	$("#TB_window").css({
		marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px',
		width: TB_WIDTH + 'px'
	});
//	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
	if ( !jQuery.browser.msie ) { // take away IE6
		$("#TB_window").css({
			marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'
		});
	}
}

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;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
