/*
 * Thickbox 3 - 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 = "g/loader.gif";
/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
window.addEvent('domready',function(){
	$$("a.smoothbox", "area.smoothbox", "input.smoothbox").each(function(el){el.onclick=TB_bind});
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});

//add thickbox to href & area elements that have a class of .thickbox

function TB_bind(event) {
	var event = new Event(event);
	event.preventDefault();
	this.blur();
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(t,a,g);
	return false;
}
function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
				$E("html").setStyle("overflow", "hidden");
				$E('html').setStyles({height: "100%", width: "100%"});
				$E("body").setStyles({height: "100%", width: "100%"});
			if ($("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				TB_HideSelect = new Element('iframe', { 'id': 'TB_HideSelect'}).injectInside(document.body);
				TB_Overlay = new Element('div', {'id': 'TB_overlay'}).injectAfter(TB_HideSelect);
				TB_Window = new Element('div', {'id': 'TB_window'}).injectAfter(TB_Overlay);
				TB_Overlay.onclick=tb_remove;
			}
		}else{//all others
			if($("TB_overlay") === null){
				TB_Overlay = new Element('div', {'id': 'TB_overlay'}).injectInside(document.body);
				TB_Window = new Element('div', {'id': 'TB_window'}).injectAfter(TB_Overlay);
				TB_Overlay.onclick=tb_remove;
			}
		}
		
		if(caption===null){caption="";}
		TB_Load = new Element('div', {'id': 'TB_load'}).injectInside(document.body);
		new Element('img', {'src': imgLoader.src }).injectInside(TB_Load);
		TB_Load.setStyle('display', 'block');//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/g;
	   var urlType = baseURL.toLowerCase().match(urlString);
		if(1){//code to show html pages
			
			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = tb_parseQuery( queryString );

			TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;
			
			if(url.indexOf('TB_iframe') != -1){				
					urlNoQuery = url.split('TB_');		
					TB_Title =  new Element('div', {'id': 'TB_title'}).injectInside(TB_Window);
					TB_AjaxWindowTitle = new Element('div', {'id': 'TB_ajaxWindowTitle'}).injectInside(TB_Title);
					TB_AjaxWindowTitle.setHTML(caption);
					TB_CloseAjaxWindow = new Element('div', {'id': 'TB_closeAjaxWindow'}).injectAfter(TB_AjaxWindowTitle);
					TB_CloseWindowButton = new Element('a', {'href': '#', 'id': 'TB_closeWindowButton', 'title': 'Close'}).injectInside(TB_CloseAjaxWindow);
					TB_CloseWindowButton.setHTML('close');
					TB_IframeContent = new Element('iframe', {'frameborder': '0', 'hspace': '0', 'src': urlNoQuery[0], 'id': 'TB_iframeContent', 'name': 'TB_iframeContent', 'style': 'width: "'+(ajaxContentW+29)+'"px;height:"'+(ajaxContentH+17)+'"px;', 'events': { 'onload': tb_showIframe()}}).injectAfter(TB_Title);
				}else{
					if($("TB_window").getStyle("display") != "block"){
						if(params['modal'] != "true"){
							TB_Title =  new Element('div', {'id': 'TB_title'}).injectInside(TB_Window);
							TB_AjaxWindowTitle = new Element('div', {'id': 'TB_ajaxWindowTitle'}).injectInside(TB_Title);
							TB_AjaxWindowTitle.setHTML(caption);
							TB_CloseAjaxWindow = new Element('div', {'id': 'TB_closeAjaxWindow'}).injectAfter(TB_AjaxWindowTitle);
							TB_CloseWindowButton = new Element('a', {'href': '#', 'id': 'TB_closeWindowButton', 'title': 'Close'}).injectInside(TB_CloseAjaxWindow);
							TB_CloseWindowButton.setHTML('close');
							TB_AjaxContent = new Element('div', {'id': 'TB_ajaxContent', 'styles': {'width': ajaxContentW+'px','height': ajaxContentH+'px'}}).injectAfter(TB_Title);

						}else{
							$("#TB_overlay").unbind();
							$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
						}
					}else{
						$("TB_ajaxContent").setStyle('width', ajaxContentW +"px");
						$("TB_ajaxContent").setStyle('height', ajaxContentH +"px");
						$("TB_ajaxContent").scrollTop = 0;
						$("TB_ajaxWindowTitle").setHtml(caption);
					}
			}
					
			$("TB_closeWindowButton").onclick=tb_remove;
			
				if(url.indexOf('TB_inline') != -1){
					TB_AjaxContent.innerHTML = ($(params['inlineId']).innerHTML);
					tb_position();
					TB_Load.remove();
					TB_Window.setStyle('display',"block"); 
				}else if(url.indexOf('TB_iframe') != -1){
					tb_position();
					if(frames['TB_iframeContent'] === undefined){//be nice to safari
						TB_Load.remove();
						TB_Window.setStyle('display',"block"); 
						$(document).keyup( function(e){ var key = e.keyCode; if(key == 27){tb_remove();}});
					}
				}else{
						var errorhandlerfunc = function(){
							TB_AjaxContent.setHTML('Error in connection');
							tb_position();
							TB_Load.remove();
							TB_Window.setStyle("display", "block");
						}
						var handlerfunc = function(){
							tb_position();
							TB_Load.remove();
							TB_Window.setStyle("display", "block");
						}
						var AjaxResp = new Ajax(url += "&random=" + (new Date().getTime()), { method: 'get', update: TB_AjaxContent, evalScripts: true, onComplete: handlerfunc, onFailure: errorhandlerfunc }).request();
				}
		}

		if(!params['modal']){
			document.onkeyup = function(e){
				var event = new Event(e);
				if(event.code == 27){ // close
					tb_remove();
				}	
			};
		}
//	} catch(e) {
		//nothing here
//	}
}

//helper functions below
function tb_showIframe(){
	TB_Load.remove();
	TB_Window.setStyle('display',"block");
}

function tb_remove() {
	TB_Overlay.onclick=null;

	if ($('TB_closeWindowButton')) TB_CloseWindowButton.onclick=null;

	new Fx.Style(TB_Window, 'opacity', {duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){TB_Window.remove(); TB_Overlay.remove()}}).start(1,0);
//	new Fx.Style(TB_Overlay, 'opacity', {duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){TB_Overlay.remove()}}).start(0.75,0);
	if(typeof TB_HideSelect === "undefined"){
	}else {
		TB_HideSelect.remove();
	}
	if($('TB_load')){
		$('TB_load').remove();
	}
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
				$E("html").setStyle("overflow", "auto");
				$E('html').setStyles({height: "auto", width: "auto"});
				$E("body").setStyles({height: "auto", width: "auto"});
	}
	document.onkeydown = null;
	document.onkeyup = null;
	return false;
}

function tb_position() {
	TB_Window.setStyles({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
	} else {
		TB_Window.setStyles({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + '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_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;
}

