/*
 * 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 = "/common/images/renew2009/loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){   
	tb_init("a.thickbox, area.thickbox, input.thickbox"); //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 tb_init(domChunk){
	$(domChunk).click(function(){
		var a = this.href || this.alt;
		tb_show(a);
		this.blur();
		return false;
	});
}

function tb_show(url){ //function called when the user clicks on a thickbox link
	try{
		if(document.getElementById("TB_overlay") === null){
			$(".mapAjax").append("<div id=\"TB_overlay\"></div><div id=\"TB_window\"></div>");
			$("#TB_overlay").click(tb_remove);
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack"); //use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG"); //use background and opacity
		}
		
		$(".mapAjax").append("<div id=\"TB_load\"><img src=\"" + imgLoader.src + "\" /></div>"); //add loader to the page
		$("#TB_load").show(); //show loader
		
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery(queryString);
			
		TB_WIDTH = 390;
		TB_HEIGHT = 242;
		ajaxContentW = TB_WIDTH;
		ajaxContentH = TB_HEIGHT;

		if($("#TB_window").css("display") != "block"){
			var thisSource = "";
			thisSource += "<div id=\"TB_ajaxContent\" style=\"width:" + ajaxContentW + "px; height:" + ajaxContentH + "px\"></div>";
			$("#TB_window").append(thisSource);
		}
		
		$("#srch_mansion .nav li.srch_area").click(tb_remove);
			
		$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){ //to do a post change this load method
			tb_position();
			$("#TB_load").remove();
			tb_init("#TB_ajaxContent a.thickbox");
			$("#TB_window").css({ display: "block" });
			
			$("#TB_ajaxContent div.thickbox > div > p:first").css({
				backgroundImage: "url(/common/images/renew2009/map/line_solid.gif)",
				backgroundPosition: "right bottom",
				backgroundRepeat: "repeat-x"
			});
			
		});

		document.onkeyup = function(e){ 	
			if(e == null){ // ie
				keycode = event.keyCode;
			}else{ // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				tb_remove();
			}
		};
		
	} catch(e) {
		
		//nothing here
	}
}

function tb_remove(){
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast", function(){ $("#TB_window,#TB_overlay").trigger("unload").unbind().remove(); });
	$("#TB_load").remove();
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position(){
	$("#TB_window").css({ width: TB_WIDTH + "px" });
}

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_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}



