/*
 * 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 ap_pathToImage = "js/loadingAnimation.gif";
var ap_txt_close = "<strong>X</strong>";
var ap_txt_esc = "";

$(document).ready(function(){   
	ap_init('a.ajaxpanel, area.ajaxpanel, input.ajaxpanel');
	imgLoader = new Image();
	imgLoader.src = ap_pathToImage;
});

function ap_init(domChunk){
	$(domChunk).click(function(){
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		ap_show(t,a,g);
		this.blur();
		return false;
	});
}

function ap_show(caption, url, imageGroup) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("AP_HideSelect") === null) {
				$("body").append("<iframe id='AP_HideSelect'></iframe><div id='AP_overlay'></div><div id='AP_window'></div>");
				//$("#AP_overlay").click(ap_remove);
			}
		}else{
			if(document.getElementById("AP_overlay") === null){
				$("body").append("<div id='AP_overlay'></div><div id='AP_window'></div>");
				//$("#AP_overlay").click(ap_remove);
			}
		}
		
		if(ap_detectMacXFF()){
			$("#AP_overlay").addClass("AP_overlayMacFFBGHack");
		}else{
			$("#AP_overlay").addClass("AP_overlayBG");
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='AP_load'><img src='"+imgLoader.src+"' /></div>");
		$('#AP_load').show();

			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = ap_parseQuery( queryString );
			
			AP_WIDTH = (params['width']*1) + 30 || 630;
			AP_HEIGHT = (params['height']*1) + 40 || 240;
			var pagesize = ap_getPageSize();
			var x = pagesize[0] - 50;
			var y = pagesize[1] - 50;
			var AP_WIDTH = AP_WIDTH;
			if(AP_WIDTH > x){
				AP_WIDTH = AP_WIDTH * (x / AP_WIDTH);
			}
			if(AP_HEIGHT > y){
				AP_HEIGHT = AP_HEIGHT * (y / AP_HEIGHT);
			}
			ajaxContentW = AP_WIDTH - 30;
			ajaxContentH = AP_HEIGHT - 45;
			
					if($("#AP_window").css("display") != "block"){
						if(params['modal'] != "true"){
						$("#AP_window").append("<div id='AP_title'><div id='AP_ajaxWindowTitle'>"+caption+"</div><div id='AP_closeAjaxWindow'><a href='#' id='AP_closeWindowButton' title='"+ap_txt_esc+"'>"+ap_txt_close+"</a></div></div><div id='AP_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
						}else{//ajax modal
						$("#AP_overlay").unbind();
						$("#AP_window").append("<div id='AP_ajaxContent' class='AP_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
						}
					}else{
						$("#AP_ajaxContent")[0].style.width = ajaxContentW +"px";
						$("#AP_ajaxContent")[0].style.height = ajaxContentH +"px";
						$("#AP_ajaxContent")[0].scrollTop = 0;
						$("#AP_ajaxWindowTitle").html(caption);
					}
					
					$("#AP_closeWindowButton").click(ap_remove);
			
					$("#AP_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){
						$("#AP_window").css({marginLeft: '-' + parseInt((AP_WIDTH / 2),10) + 'px', width: AP_WIDTH + 'px'});
						$("#AP_load").remove();
						ap_init("#AP_ajaxContent a.ajaxpanel");
						$("#AP_window").css({display: 'block'});
					});
			
		if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { 
					keycode = event.keyCode;
				} else { 
					keycode = e.which;
				}
				if(keycode == 27){ 
					ap_remove();
				}	
			};
		}
		
	} catch(e) {
		//nothing here
	}
}

function ap_remove() {
	$("#AP_closeWindowButton").unbind("click");
	$("#AP_window").css({display: 'none'});
	$('#AP_window,#AP_overlay,#AP_HideSelect').trigger("unload").unbind().remove();
	//$("#AP_window").slideUp("slow",function(){$('#AP_window,#AP_overlay,#AP_HideSelect').trigger("unload").unbind().remove();});

	$("#AP_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function ap_parseQuery ( query ) {
	var Params = {};
	if ( ! query ) {return Params;}
	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 ap_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 ap_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
