/*
 * 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
*/

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

//on page load call tb_init
$(document).ready(function(){   
	tb_init('a.thickbox');//pass where to apply thickbox
});

//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	
    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(/^[^\?]+\??/,'');
    var params = tb_parseQuery( queryString );

    TB_WIDTH = (params['width']*1) || 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;
    ajaxContentH = TB_HEIGHT - 45;
    idPrefix = params['id_prefix']

    $("#" + idPrefix + "_TB_overlay").unbind();
    $("#" + idPrefix + "_TB_window").css({width: ajaxContentW + 'px', height: ajaxContentH + 'px'});
    $("#" + idPrefix + "_TB_ajaxContent").css({width: ajaxContentW + 'px', height: ajaxContentH + 'px'});

    $("#" + idPrefix + "_TB_closeWindowButton").click(function(){
        tb_remove(idPrefix);
        return false;
    });

    tb_position(idPrefix);
    $("#" + idPrefix + "_TB_overlay").css({display:"block"});
    $("#" + idPrefix + "_TB_window").css({display:"block"});
}

function tb_remove(idPrefix) {
	$("#" + idPrefix + "_TB_overlay").unbind("click");
	$("#" + idPrefix + "_TB_closeWindowButton").unbind("click");
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
	    $("#" + idPrefix + "_TB_overlay").css({display:"none"});
	    $("#" + idPrefix + "_TB_window").css({display:"none"});
	}
	else {
	    $("#" + idPrefix + "_TB_window").fadeOut("fast",function(){
	        $("#" + idPrefix + "_TB_window").css({display:"none"});
	        $("#" + idPrefix + "_TB_overlay").css({display:"none"});
	    });
    }
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	return false;
}

function tb_position(idPrefix) {
    $("#" + idPrefix + "_TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function')) { // take away IE6
		$("#" + idPrefix + "_TB_window").css({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;
}
