/* 
	Ventana modal, Customizacion de thickbox.js
	Autor: Claudio Romano Cherñac
	http://www.siestaviolenta.com
*/

function modal(elem){
	//alert ("entro a modal()");
	var t = elem.title || elem.name || null;
	var a = elem.href || elem.alt;
	var g = elem.rel || false;
//alert(this.href);
	layer(t,a,g);
	
	

	//this.blur();
	return false;
}



function layer(caption, url, imageGroup) {
	//alert("ar url:"+url);
	try {
		
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("PU_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='PU_HideSelect' ></iframe><div id='PU_overlay'></div><div id='PU_modal'></div>");
				$("#PU_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("PU_overlay") === null){
				$("body").append("<div id='PU_overlay'></div><div id='PU_modal'></div>");
				$("#PU_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#PU_overlay").addClass("PU_background");//use png overlay so hide flash
		}else{
			$("#PU_overlay").addClass("PU_background");//use background and opacity
		}

		$("body").append("<div id='loader'><img src='/interface/base/default/imgs/layout/modal-loader.gif' /></div>");//add loader to the page
		$('#loader').show();//show loader



		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery( queryString );

		TB_WIDTH = (params['width']*1) + 30 || 710; //defaults to 630 if no paramaters were added to URL
		
		if(params['height']=='window'){
			TB_HEIGHT = $(window).height();// - 50;
		}else{
			TB_HEIGHT = (params['height']*1) + 40 || 530; //defaults to 440 if no paramaters were added to URL
		}

		ajaxContentW = TB_WIDTH - 30;
		ajaxContentH = TB_HEIGHT - 45;


			urlNoQuery = url.split('PU_');
			//alert(urlNoQuery);
			$("#PU_top").remove();

				$("#PU_modal").append("<div id='PU_top'><div id='PU_close'><a href='#' id='PU_closeButton' title='Cerrar ventana modal'>Cerrar</a> </div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='PU_iframe' name='PU_iframe"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");


		$("#PU_closeButton").click(tb_remove);

		tb_position();
		if($.browser.safari){//safari needs help because it will not fire iframe onload
			$("#loader").remove();
			$("#PU_modal").css({display:"block"});
		}

		document.onkeyup = function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				tb_remove();
			}
		}


	}
	catch(e){
		alert(e);
	}

}

function tb_showIframe(){
	$("#loader").remove();
	$("#PU_modal").css({display:"block"});
}

function tb_remove() {

	$("#PU_closeButton").unbind("click");
	$("#PU_modal").fadeOut("fast",function(){$('#PU_modal,#PU_overlay,#PU_HideSelect').trigger("unload").unbind().remove();});
	$("#loader").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

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_showIframe(){
	$("#loader").remove();
	$("#PU_modal").css({display:"block"});
}

function tb_position() {
marginLeft = parseInt((TB_WIDTH / 2),10)+3;

$("#PU_modal").css({marginLeft: '-' + marginLeft + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#PU_modal").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}


function setInicio(el){
	if(typeof document.body.style.maxHeight === "undefined") { // IE6
		el.setHomePage(location.href);
	}
	else{
		modal(el);
	}
	return false;
}





