/*
 * 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
 * Custom Thickbox
 * Edited by Mara Greco
*/
		  
var tb_pathToImage = "img/loadingAnimation.gif";

//on page load call tb_init
$j(document).ready(function(){   
	tbC_init('a.thickboxC');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});

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

function tbC_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$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='TBc_overlay'></div><div id='TBc_window'></div>");
				$j("#TBc_overlay").click(tbC_remove);
			}
		}else{//all others
			if(document.getElementById("TBc_overlay") === null){
				$j("body").append("<div id='TBc_overlay'></div><div id='TBc_window'></div>");
				$j("#TBc_overlay").click(tbC_remove);
			}
		}
		
		if(tbC_detectMacXFF()){
			$j("#TBc_overlay").addClass("TBc_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$j("#TBc_overlay").addClass("TBc_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$j("body").append("<div id='TBc_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$j('#TBc_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 queryString = url.replace(/^[^\?]+\??/,'');
		if(queryString.length>0){
			var params = tbC_parseQuery( queryString );
			var varRandom="&random="+(new Date().getTime());
		}else{
			var params={};
			var varRandom="?random="+(new Date().getTime());
		}

		TBc_WIDTH = (params['width']*1) + 30 || 700; //defaults to 700 if no paramaters were added to URL
		TBc_HEIGHT = (params['height']*1) + 40 || 500; //defaults to 550 if no paramaters were added to URL
		ajaxContentW = TBc_WIDTH - 30;
		ajaxContentH = TBc_HEIGHT - 45;
		$j("#TBc_window").append('<div id="TBc_ajaxContent"></div>');
		$j("#TBc_ajaxContent").load(url+=varRandom,function(){//to do a post change this load method
			tbC_position();
			$j("#TBc_load").remove();
			tbC_init("#TBc_ajaxContent a.thickboxC");
			$j("#TBc_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
					tbC_remove();
				}	
			};
		}
		
	} catch(e) {
		//nothing here
	}
}

function tbC_remove() {
 	$j("#TBc_imageOff").unbind("click");
	$j("#TBc_closeWindowButton").unbind("click");
	$j("#TBc_window").fadeOut("fast",function(){$j('#TBc_window,#TBc_overlay,#TBc_HideSelect').trigger("unload").unbind().remove();});
	$j("#TBc_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 = "";
	return false;
}

function tbC_position() {
$j("#TBc_window").css({marginLeft: '-' + parseInt((TBc_WIDTH / 2),10) + 'px', width: TBc_WIDTH + 'px',height:TBc_HEIGHT+'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$j("#TBc_window").css({marginTop: '-' + parseInt((TBc_HEIGHT / 2),10) + 'px'});
	}
}

function tbC_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 tbC_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

