/*
 * 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 tz_pathToImage = "loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tz_init
$(document).ready(function(){   
	tz_init('a.thickbox_photo, area.thickbox_photo, input.thickbox_photo');//pass where to apply thickbox_photo
	imgLoader = new Image();// preload image
	imgLoader.src = tz_pathToImage;
});

//add thickbox_photo to href & area elements that have a class of .thickbox_photo
function tz_init(domChunk){
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tz_show(t,text_img, text_next, text_prev, text_de, text_tecla_esc, text_fechar, text_pagina,a,g);
	this.blur();
	return false;
	});
}

function tz_show(caption, text_img, text_next, text_prev, text_de, text_tecla_esc, text_fechar, text_pagina, url, imageGroup) {//function called when the user clicks on a thickbox_photo link

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TZ_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TZ_HideSelect'></iframe><div id='TZ_overlay'></div><div id='TZ_window'></div>");
				$("#TZ_overlay").click(tz_remove);
			}
		}else{//all others
			if(document.getElementById("TZ_overlay") === null){
				$("body").append("<div id='TZ_overlay'></div><div id='TZ_window'></div>");
				$("#TZ_overlay").click(tz_remove);
			}
		}
		
		if(tz_detectMacXFF()){
			$("#TZ_overlay").addClass("TZ_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TZ_overlay").addClass("TZ_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='TZ_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TZ_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);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
				
			TZ_PrevCaption = "";
			TZ_PrevURL = "";
			TZ_PrevHTML = "";
			TZ_NextCaption = "";
			TZ_NextURL = "";
			TZ_NextHTML = "";
			TZ_imageCount = "";
			TZ_FoundURL = false;
			if(imageGroup){
				TZ_TempArray = $("a[@rel="+imageGroup+"]").get();
				for (TZ_Counter = 0; ((TZ_Counter < TZ_TempArray.length) && (TZ_NextHTML === "")); TZ_Counter++) {
					var urlTypeTemp = TZ_TempArray[TZ_Counter].href.toLowerCase().match(urlString);
						//alert(TZ_TempArray[TZ_Counter].href + " : " + url);
						if (!(TZ_TempArray[TZ_Counter].href == url)) {						
							if (TZ_FoundURL) {
								TZ_NextCaption = TZ_TempArray[TZ_Counter].title;
								TZ_NextURL = TZ_TempArray[TZ_Counter].href;
								TZ_NextHTML = "<span id='TZ_next'>&nbsp;&nbsp;<a href='#'>"+text_next+" &gt;</a></span>";
							} else {
								TZ_PrevCaption = TZ_TempArray[TZ_Counter].title;
								TZ_PrevURL = TZ_TempArray[TZ_Counter].href;
								TZ_PrevHTML = "<span id='TZ_prev'>&nbsp;&nbsp;<a href='#'>&lt; "+text_prev+"</a></span>";
							}
						} else {
							TZ_FoundURL = true;
							TZ_imageCount = ""+text_img+" " + (TZ_Counter + 1) +" "+text_de+" "+ (TZ_TempArray.length);											
						}
				}
			}

			imgPreloader = new Image();
			imgPreloader.onload = function(){		
			imgPreloader.onload = null;
				
			// Resizing large images - orginal by Christian Montoya edited by me.
			var pagesize = tz_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			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;
				}
			}
			// End Resizing
			
			TZ_WIDTH = imageWidth + 30;
			TZ_HEIGHT = imageHeight + 60;
			$("#TZ_window").append("<a href='' id='TZ_ImageOff' title='"+text_fechar+"'><img id='TZ_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TZ_caption'>"+caption+"<div id='TZ_secondLine'>" + TZ_imageCount + TZ_PrevHTML + TZ_NextHTML + "</div></div><div id='TZ_closeWindow'><a href='#' id='TZ_closeWindowButton' title='Fechar'>"+text_fechar+"</a> "+text_tecla_esc+"</div>"); 		
			
			$("#TZ_closeWindowButton").click(tz_remove);
			
			if (!(TZ_PrevHTML === "")) {
				function goPrev(){
					if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
					$("#TZ_window").remove();
					$("body").append("<div id='TZ_window'></div>");
					tz_show(TZ_PrevCaption, text_img, text_next, text_prev, text_de, text_tecla_esc, text_fechar, text_pagina, TZ_PrevURL, imageGroup);
					return false;	
				}
				$("#TZ_prev").click(goPrev);
			}
			
			if (!(TZ_NextHTML === "")) {		
				function goNext(){
					$("#TZ_window").remove();
					$("body").append("<div id='TZ_window'></div>");
					tz_show(TZ_NextCaption, text_img, text_next, text_prev, text_de, text_tecla_esc, text_fechar, text_pagina, TZ_NextURL, imageGroup);				
					return false;	
				}
				$("#TZ_next").click(goNext);
				
			}

			document.onkeydown = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tz_remove();
				} else if(keycode == 190){ // display previous image
					if(!(TZ_NextHTML == "")){
						document.onkeydown = "";
						goNext();
					}
				} else if(keycode == 188){ // display next image
					if(!(TZ_PrevHTML == "")){
						document.onkeydown = "";
						goPrev();
					}
				}	
			};
			
			tz_position();
			$("#TZ_load").remove();
			$("#TZ_ImageOff").click(tz_remove);
			$("#TZ_window").css({display:"block"}); //for safari using css instead of show
			};
			
			imgPreloader.src = url;
		}else{//code to show html
			
			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = tz_parseQuery( queryString );

			TZ_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
			TZ_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
			ajaxContentW = TZ_WIDTH - 30;
			ajaxContentH = TZ_HEIGHT - 45;
			
			if(url.indexOf('TZ_iframe') != -1){// either iframe or ajax window		
					urlNoQuery = url.split('TZ_');
					$("#TZ_iframeContent").remove();
					if(params['modal'] != "true"){//iframe no modal
						$("#TZ_window").append("<div id='TZ_title'><div id='TZ_ajaxWindowTitle'>"+caption+"</div><div id='TZ_closeAjaxWindow'><a href='#' id='TZ_closeWindowButton' title='"+text_fechar+"'>"+text_fechar+"</a> "+text_tecla_esc+"</div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TZ_iframeContent' name='TZ_iframeContent"+Math.round(Math.random()*1000)+"' onload='tz_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
					}else{//iframe modal
					$("#TZ_overlay").unbind();
						$("#TZ_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TZ_iframeContent' name='TZ_iframeContent"+Math.round(Math.random()*1000)+"' onload='tz_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
					}
			}else{// not an iframe, ajax
					if($("#TZ_window").css("display") != "block"){
						if(params['modal'] != "true"){//ajax no modal
						$("#TZ_window").append("<div id='TZ_title'><div id='TZ_ajaxWindowTitle'>"+caption+"</div><div id='TZ_closeAjaxWindow'><a href='#' id='TZ_closeWindowButton'>"+text_fechar+"</a> "+text_tecla_esc+"</div></div><div id='TZ_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
						}else{//ajax modal
						$("#TZ_overlay").unbind();
						$("#TZ_window").append("<div id='TZ_ajaxContent' class='TZ_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
						$("#TZ_ajaxContent")[0].style.width = ajaxContentW +"px";
						$("#TZ_ajaxContent")[0].style.height = ajaxContentH +"px";
						$("#TZ_ajaxContent")[0].scrollTop = 0;
						$("#TZ_ajaxWindowTitle").html(caption);
					}
			}
					
			$("#TZ_closeWindowButton").click(tz_remove);
			
				if(url.indexOf('TZ_inline') != -1){	
					$("#TZ_ajaxContent").append($('#' + params['inlineId']).children());
					$("#TZ_window").unload(function () {
						$('#' + params['inlineId']).append( $("#TZ_ajaxContent").children() ); // move elements back when you're finished
					});
					tz_position();
					$("#TZ_load").remove();
					$("#TZ_window").css({display:"block"}); 
				}else if(url.indexOf('TZ_iframe') != -1){
					tz_position();
					if($.browser.safari){//safari needs help because it will not fire iframe onload
						$("#TZ_load").remove();
						$("#TZ_window").css({display:"block"});
					}
				}else{
					$("#TZ_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
						tz_position();
						$("#TZ_load").remove();
						tz_init("#TZ_ajaxContent a.thickbox_photo");
						$("#TZ_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
					tz_remove();
				}	
			};
		}
		
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tz_showIframe(){
	$("#TZ_load").remove();
	$("#TZ_window").css({display:"block"});
}

function tz_remove() {
 	$("#TZ_imageOff").unbind("click");
	$("#TZ_closeWindowButton").unbind("click");
	$("#TZ_window").fadeOut("fast",function(){$('#TZ_window,#TZ_overlay,#TZ_HideSelect').trigger("unload").unbind().remove();});
	$("#TZ_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 tz_position() {
$("#TZ_window").css({marginLeft: '-' + parseInt((TZ_WIDTH / 2),10) + 'px', width: TZ_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TZ_window").css({marginTop: '-' + parseInt((TZ_HEIGHT / 2),10) + 'px'});
	}
}

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



