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

/*
 * Modificado por Manuel García Urreta - SisDev Software (c) 2007 - mgu@sisdevsoft.com
 */

var tb_pathToImage = "images/ajax-loader.gif";

//on page load call tb_init
$(document).ready(
	function(){   
				imgLoader = new Image();// preload image
				imgLoader.src = tb_pathToImage;
			});

function tb_show_inline(caption, url){
	tb_show(caption, '#TB_inline?' + url);
}

function tb_show(caption, url) {//function called when the user clicks on a thickbox link

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_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 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($("#TB_window").css("display") != "block"){
			if(params['modal'] == "true")
				$("#TB_overlay").unbind();
			
			$("#TB_window").append("<div id='TB_title'><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Cerrar'>cerrar</a></div></div>" +
								   "<div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
		}else{
			$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
			$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
			$("#TB_ajaxContent")[0].scrollTop = 0;
			$("#TB_ajaxWindowTitle").html(caption);
		}
				
		if(url.indexOf('TB_inline') != -1){
			var htmlAux = $('#' + params['inlineId']).html();

			$("#TB_ajaxContent").html(htmlAux);
			tb_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 
		}else{
			$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
				tb_position();
				$("#TB_load").remove();
				$("#TB_window").css({display:"block"});
			});
		}

        //cerramos el overlay
        $("#TB_closeWindowButton").click(tb_remove);
        
		//dependemos de jqDnR
	    $('#TB_window').jqDrag('#TB_title'); // Queremos mover todo el TB_window utilizando como handler al div TB_title
		
		if(!params['modal']){
			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
		}
}

//helper functions below
function tb_remove() {
	$("#TB_overlay").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
	$("#TB_load").remove();
	document.onkeydown = "";
	return false;
}

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

