/********************************************************
FUNCIONES DEL TAPEBOX
*********************************************************/
var indicator_img_path = "img/precarga.gif";
var indicator_img_html = "<img name=\"ibox_indicator\" src=\""+indicator_img_path+"\" />"; 

var opacity_level = 6; // how transparent our overlay bg is
var ibAttr = "rel"; 	// our attribute identifier for our iBox elements
	
var imgPreloader = new Image(); // create an preloader object

function init_ibox() {
	var elem_wrapper = "ibox";
	
	createIbox(document.getElementsByTagName("body")[0]); //create our ibox

	//	elements here start the look up from the start non <a> tags
	//var docRoot = (document.all) ? document.all : document.getElementsByTagName("*");
	
	// Or make sure we only check <a> tags
	var docRoot = document.getElementsByTagName("a");

	var e;
	for (var i = 0; i < docRoot.length - 1; i++) {
			e = docRoot[i];
			if(e.getAttribute(ibAttr)) {
				var t = e.getAttribute(ibAttr);
				if ((t.indexOf("ibox") != -1)  ||  t.toLowerCase() == "ibox") { // check if this element is an iBox element
						e.onclick = function() { // rather assign an onclick event
							var t = this.getAttribute(ibAttr);
							var params = parseQuery(t.substr(5,999));
							var url = this.href;
							if(this.target != "") {url = this.target} 
	
							var title = this.title;

							if(showIbox(url,title,params)) {
								showBG();
								window.onscroll = maintPos;
								window.onresize = maintPos;
							}
							return false;
						}; 
						
				}
			}
     }
}

//ADD BY RUNEBRAND, MUESTRA LAS POP MEDIANTE JAVASCRIPT

function showForce (url,title,params){
	//alert('LLama a la pop desde un javascript');
	params=parseQuery(params)
	if(showIbox(url,title,params)) {
		showBG();
		window.onscroll = maintPos;
		window.onresize = maintPos;
	}
}

//ADD BY BREALCO, 


//oculta la capa de carga
function oculta_carga() {
	//alert('Oculta la carga desde el tapebox');
	//document.getElementById('cargando_lar').style.display = 'none';
	//document.getElementById('cargando_txt').style.display = 'none';
	
		var tipo_nav = detecta_navegador();
	if (tipo_nav=="safari"){
	document.getElementById('bodegashtm').style.overflow = 'scroll';
	}
	else{
	document.getElementById('bodegashtm').style.overflow = 'auto';
	}
	//muestra_flashes('visible');
	visibleforms('visible');	
	
}

function muestra_carga() {
visibleforms('hidden');
	//document.getElementById('cargando_lar').style.display = 'block';
	//document.getElementById('cargando_txt').style.display = 'block';
document.getElementById('bodegashtm').style.overflow = 'hidden';
}

//oculta o muestra los select para evitar que se superpongan con la capa de carga
visibleforms = function(estado) {
	//alert('Cambia el estado de los select a '+estado);
var selforms = document.getElementsByTagName("select");

	var ef;
	for (var i = 0; i < selforms.length ; i++) {
			ef = selforms[i];
			cadena=ef.id;
			if (cadena!='sel_busc'){
			ef.style.visibility = estado;
			}
			else{
				pliega_busc();
			}
	}
}



showBG = function() {
	
	visibleforms('hidden');
	muestra_flashes('hidden');
	//alert('Se evita el forzado de la recarga');
	
	var box_w = getElem('ibox_w');
	

	box_w.style.opacity = 0;
	box_w.style.filter = 'alpha(opacity=0)';
	setBGOpacity = setOpacity;
	for (var i=0;i<=opacity_level;i++) {setTimeout("setIboxOpacity('ibox_w',"+i+")",70*i);} // from quirksmode.org
	
		
	box_w.style.display = "";
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;
	
	if(ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+'px';} 
	/*else {box_w.style.width = pagesize.width-20+'px';}*/ // scrollbars removed! Hurray!
	box_w.style.height = pagesize.height+scrollPos.scrollY+'px';

}


hideBG = function() {
	//alert('Cerrando');
	var box_w = getElem('ibox_w');
	box_w.style.display = "none";
	
	visibleforms('visible');
	muestra_flashes('visible');
//document.getElementById('ibox_content').src = '../templates/blank.htm';
	
	if (document.form_pop.modpop.value=="si"){
		//alert('lo intentamos')
	visibleforms('hidden');
	//document.getElementById('cargando_lar').style.display = 'block';
	//document.getElementById('cargando_txt').style.display = 'block';
document.getElementById('bodegashtm').style.overflow = 'hidden';
	
		url_act=document.location.href;
		url_actb=url_act.replace('#','');
		url_act2=url_actb.replace('marca_sup','');
		
		if (url_act2.indexOf("?") != -1 && url_act2.indexOf("?refresh") == -1){
		caracter="&";		
		}
		else{
		caracter="?";	
		}
		
		if (url_act2.indexOf(caracter+"refresh=true2") != -1){
			url_act3=url_act2.replace(caracter+'refresh=true2','');
			url_act4=url_act3+caracter+"refresh=true";	
		}
		else{	
				
				if (url_act2.indexOf(caracter+"refresh=true") != -1){
					url_act3=url_act2.replace(caracter+'refresh=true','');
					url_act4=url_act3+caracter+"refresh=true2";	
				}
				else{
					url_act4=url_act2+caracter+"refresh=true";	
				}
		}
		//alert(url_act4);*/
		document.location.href=url_act4;
		//document.location.href='prueba';
	}
	
}

var loadCancelled = false;
showIndicator = function() {
	var ibox_p = getElem('ibox_progress');
	ibox_p.style.display = "";
	posToCenter(ibox_p);
	ibox_p.onclick = function() {hideIbox();hideIndicator();loadCancelled = true;visibleforms('visible');}
	
}


hideIndicator = function() {
	var ibox_p = getElem('ibox_progress');
	ibox_p.style.display = "none";
	ibox_p.onclick = null;
}

createIbox = function(elem) {
	
	// a trick on just creating an ibox wrapper then doing an innerHTML on our root ibox element
	var strHTML = "<div id=\"ibox_w\" style=\"display:none;\"></div>";
	strHTML +=	"<div id=\"ibox_progress\" style=\"display:none;\">";
	strHTML +=  indicator_img_html;
	strHTML +=  "</div>";
	strHTML +=	"<div id=\"ibox_wrapper\" style=\"display:none\">";
	strHTML +=	"<div id=\"ibox_lft\" >&nbsp;</div>";  //add by brealco
	strHTML +=	"<div id=\"ibox_rgt\" >&nbsp;</div>"; //add by brealco
	strHTML +=	"<div id=\"ibox_esq1\" >&nbsp;</div>";  //add by brealco
	strHTML +=	"<div id=\"ibox_esq2\" >&nbsp;</div>"; //add by brealco
	strHTML +=	"<div id=\"ibox_esq3\" >&nbsp;</div>";  //add by brealco
	strHTML +=	"<div id=\"ibox_esq4\" >&nbsp;</div>"; //add by brealco
	strHTML +=	"<div id=\"ibox_barra_sup\" >&nbsp;</div>"; //add by brealco
	strHTML +=	"<div id=\"ibox_barra_inf\" >&nbsp;</div>"; //add by brealco
	strHTML +=	"<div id=\"ibox_titulo\" >&nbsp;</div>"; //add by brealco
	strHTML +=	"<iframe id=\"ibox_content\" frameborder=\"no\" border=\"0\" framespacing=\"0\"></iframe>";
	strHTML +=	"<div id=\"ibox_close\" >";
	strHTML +=	"<a id=\"ibox_close_a\" href=\"#\" >cerrar</a> <a id=\"ibox_close_b\" href=\"#\" ><img src=\"../img/pops/boton_cerrar.gif\" alt=\"Cerrar\" title=\"Cerrar\" /></a></div>";
	strHTML +=  "</div></div>";

	var docBody = document.getElementsByTagName("body")[0];
	var ibox = document.createElement("div");
	ibox.setAttribute("id","ibox");
	ibox.style.display = '';
	ibox.innerHTML = strHTML;
	elem.appendChild(ibox);
}

var ibox_w_height = 0;
showIbox = function(url,title,params) {
	
	var ibox = getElem('ibox_wrapper');
	var ibox_cnt = getElem('ibox_content');
	var iboxlft = getElem('ibox_lft');//add by brealco
	var iboxrgt = getElem('ibox_rgt');//add by brealco
	var iboxbarrasup = getElem('ibox_barra_sup');//add by brealco
	var iboxbarrainf = getElem('ibox_barra_inf');//add by brealco
	var iboxesqu1 = getElem('ibox_esq1');//add by brealco
	var iboxesqu2 = getElem('ibox_esq2');//add by brealco
	var iboxesqu3 = getElem('ibox_esq3');//add by brealco
	var iboxesqu4 = getElem('ibox_esq4');//add by brealco
	var ibox_type = 0;
	
	/*ADD by brealco, cambia el fondo a oscuro en las páginas que queramos*/
	
	var nombre_nav = navigator.appName
		if (nombre_nav == "Microsoft Internet Explorer"){
			carpeta_img="explorer"
			extension_img="gif"
		}
			else{
			carpeta_img="firefox"
			extension_img="png"
		}
	
	
	
	color_pop=title.split("-");
	subrutaimg=color_pop[1];
	
	if(params['color']) {
				subrutaimg=params['color'];
				} 
		
		iboxlft.style.backgroundImage="url(../img/pops/"+carpeta_img+"/lat_1."+extension_img+")";
		iboxrgt.style.backgroundImage="url(../img/pops/"+carpeta_img+"/lat_2."+extension_img+")";
		iboxbarrasup.style.backgroundImage="url(../img/pops/"+subrutaimg+"/"+carpeta_img+"/sup."+extension_img+")";
		iboxbarrainf.style.backgroundImage="url(../img/pops/"+carpeta_img+"/inf."+extension_img+")";
		iboxesqu1.style.backgroundImage="url(../img/pops/"+subrutaimg+"/"+carpeta_img+"/esq_1."+extension_img+")";
		iboxesqu2.style.backgroundImage="url(../img/pops/"+subrutaimg+"/"+carpeta_img+"/esq_2."+extension_img+")";
		iboxesqu3.style.backgroundImage="url(../img/pops/"+carpeta_img+"/esq_3."+extension_img+")";
		iboxesqu4.style.backgroundImage="url(../img/pops/"+carpeta_img+"/esq_4."+extension_img+")";
	
	/*fin del cambio de imágenes de fondo*/
												
	// set title here
	 var ibox_titulo = getElem('ibox_titulo'); 
    if(title != "") {ibox_titulo.innerHTML = title;} else {ibox_titulo.innerHTML = "&nbsp;";} 
	
	// file checking code borrowed from thickbox
	//var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.jsp|\.jst|\.rb|\.rhtml|\.txt/g;
	var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.jsp|\.jst|\.rb|\.rhtml|\.txt|\http/i; //add by runebrand
	var urlType = url.match(urlString);
	
	if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif'){
		ibox_type = 1;
	} else if(url.indexOf("#") != -1) {
		ibox_type = 2;
	} else if(urlType=='.htm'||urlType=='.html'||urlType=='.php'||
			 urlType=='.asp'||urlType=='.aspx'||urlType=='.jsp'||
			 urlType=='.jst'||urlType=='.rb'||urlType=='.txt'||urlType=='.rhtml'||
			 urlType=='.cfm'||urlType=='http') {
		ibox_type = 3;
	} else {
		// override our ibox type if forced param exist
		if(params['type']) {ibox_type = parseInt(params['type']);}
		else{hideIbox();return false;}
	}
	
	ibox_type = parseInt(ibox_type);

	switch(ibox_type) {
		
		case 1:

			showIndicator();
			
			imgPreloader = new Image();
			
			imgPreloader.onload = function(){
	
				imgPreloader = resizeImageToScreen(imgPreloader);
				hideIndicator();
	
				var strHTML = "<img name=\"ibox_img\" src=\""+url+"\" style=\"width:"+imgPreloader.width+"px;height:"+imgPreloader.height+"px;border:0;cursor:hand;margin:0;padding:0;position:absolute;\"/>";
	
				if(loadCancelled == false) {
					
					// set width and height
					ibox.style.height = imgPreloader.height+'px';
					ibox.style.width = imgPreloader.width+'px';
				
					ibox.style.display = "";
					ibox.style.visibility = "hidden";
					posToCenter(ibox); 	
					ibox.style.visibility = "visible";

					setIBoxContent(strHTML);
				}
					
			}
			
			loadCancelled = false;
			imgPreloader.src = url;
			
			break;

		case 2:
			
			var strHTML = "";

			
			if(params['height']) {
				ibox.style.height = params['height']+'px';
				iboxlft.style.height = params['height']+'px'; //add by brealco
				iboxrgt.style.height = params['height']+'px';
				ibox_cnt.style.height = params['height']+'px';//add by brealco
				} 
			else {ibox.style.height = '280px';}
			
			if(params['width']) {
				ibox.style.width = params['width']+'px';
				iboxbarrasup.style.width = params['width']+'px'; //add by brealco
				iboxbarrainf.style.width = params['width']+'px'; //add by brealco
				} 
			else {ibox.style.width = '450px';}

		
			ibox.style.display = "";
			ibox.style.visibility = "hidden";
			posToCenter(ibox); 	
			ibox.style.visibility = "visible";
			
			getElem('ibox_content').style.overflow = "auto";
			
			/*
			var elemSrcId = url.substr(url.indexOf("#") + 1,1000);
			
			var elemSrc = getElem(elemSrcId);
			
			if(elemSrc) {strHTML = elemSrc.innerHTML;}
		
			setIBoxContent(strHTML);
			*/
			
			setIBoxURL(url); //add by runebrand
			break;
			
		case 3:
			showIndicator();
			hideIndicator();
					
			if(params['height']) {
				ibox.style.height = params['height']+'px';
				iboxlft.style.height = params['height']+'px'; //add by brealco
				iboxrgt.style.height = params['height']+'px'; //add by brealco
				ibox_cnt.style.height = params['height']+'px';//add by brealco
				} 
			else {ibox.style.height = '280px';}
					
			if(params['width']) {
				ibox.style.width = params['width']+'px';
				iboxbarrasup.style.width = params['width']+'px';//add by brealco
				iboxbarrainf.style.width = params['width']+'px';//add by brealco
				} 
			else {ibox.style.width = '450px';}
		
			ibox.style.display = "";
			ibox.style.visibility = "hidden";
			posToCenter(ibox); 	
			ibox.style.visibility = "visible";
			getElem('ibox_content').style.overflow = "auto";
			/*
			http.open('get',url,true);

			http.onreadystatechange = function() {
				if(http.readyState == 4){
					hideIndicator();
					
					if(params['height']) {ibox.style.height = params['height']+'px';} 
					else {ibox.style.height = '280px';}
					
					if(params['width']) {ibox.style.width = params['width']+'px';} 
					else {ibox.style.width = '450px';}
		
					ibox.style.display = "";
					ibox.style.visibility = "hidden";
					posToCenter(ibox); 	
					ibox.style.visibility = "visible";
					getElem('ibox_content').style.overflow = "auto";
					
					var response = http.responseText;
					setIBoxContent(response);
					
				}
			}
			
			http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			http.send(null);*/
			setIBoxURL(url); //add by runebrand
			break;
		
		default:
			
	 } 
	 
	
	ibox.style.opacity = 0;
	ibox.style.filter = 'alpha(opacity=0)';	
	var ibox_op_level = 10;
	
	setIboxOpacity = setOpacity;
	for (var i=0;i<=ibox_op_level;i++) {setTimeout("setIboxOpacity('ibox_wrapper',"+i+")",30*i);}

	if(ibox_type == 2 || ibox_type == 3) {
		ibox.onclick = null;
		getElem("ibox_close_a").onclick = function() {hideIbox();};
		getElem("ibox_close_b").onclick = function() {hideIbox();};
		getElem("ibox_w").onclick = function() {hideIbox();};
	} 
	
	else {ibox.onclick = hideIbox;getElem("ibox_close_b").onclick = null;getElem("ibox_close_a").onclick = null;}

	return true;
}

setOpacity = function (elemid,value)	{
		var e = getElem(elemid);
		e.style.opacity = value/10;
		e.style.filter = 'alpha(opacity=' + value*10 + ')';
}

resizeImageToScreen = function(objImg) {
	
	var pagesize = new getPageSize();
	
	var x = pagesize.width - 100;
	var y = pagesize.height - 100;

	if(objImg.width > x) { 
		objImg.height = objImg.height * (x/objImg.width); 
		objImg.width = x; 
		if(objImg.height > y) { 
			objImg.width = objImg.width * (y/objImg.height); 
			objImg.height = y; 
		}
	} 

	else if(objImg.height > y) { 
		objImg.width = objImg.width * (y/objImg.height); 
		objImg.height = y; 
		if(objImg.width > x) { 
			objImg.height = objImg.height * (x/objImg.width); 
			objImg.width = x;
		}
	}

	return objImg;
}

maintPos = function() {
	
	var ibox = getElem('ibox_wrapper');
	var box_w = getElem('ibox_w');
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;

	if(ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+'px';} 
	/*else {box_w.style.width = pagesize.width-20+'px';}*/

	if(ua.indexOf("Opera/9") != -1) {box_w.style.height = document.body.scrollHeight+'px';}
	else {box_w.style.height = pagesize.height+scrollPos.scrollY+'px';}
	
	// alternative 1
	//box_w.style.height = document.body.scrollHeight+50+'px';	
	
	posToCenterHor(ibox);
	
}

hideIbox = function() {
	clearIboxContent();
	var ibox = getElem('ibox_wrapper');
	ibox.style.display = "none";
	hideBG();
	window.onscroll = null;
	
}

function subir_a_marca(){
	url_act=document.location.href;
	url_act2=url_act.replace('#marca_sup','');
	url_act3=url_act2+'#marca_sup';
	document.location.href=url_act3;
}

posToCenter = function(elem) {
	var scrollPos = new getScrollPos();
	var pageSize = new getPageSize();
	var emSize = new getElementSize(elem);
	var x = Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSize.height /2) + scrollPos.scrollY;	
	elem.style.left = x+'px';
	if (pageSize.height > emSize.height) {
	elem.style.top = y+'px';
	}
	else {
	elem.style.top = '100px';
	subir_a_marca();
	}
}

posToCenterHor = function(elem) {
	var scrollPos = new getScrollPos();
	var pageSize = new getPageSize();
	var emSize = new getElementSize(elem);
	var x = Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	elem.style.left = x+'px';
}



getScrollPos = function() {
	var docElem = document.documentElement;
	this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
}

getPageSize = function() {
	var docElem = document.documentElement
	this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
	this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}

getElementSize = function(elem) {
	this.width = elem.offsetWidth ||  elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
}

setIBoxContent = function(str) {
	clearIboxContent();
	var e = getElem('ibox_content');
	e.style.overflow = "auto";
	e.innerHTML = str;
	
}

//add by runebrand
setIBoxURL = function(url) {
	blankIboxContent();
	var e = getElem('ibox_content');
	e.style.overflow = "auto";
	e.src = url;
	
}

clearIboxContent = function() {
	var e = getElem('ibox_content');
	e.src = "../templates/blank.htm";

}

blankIboxContent = function() {
	var e = getElem('ibox_content');
	e.src = "../templates/blank.htm";
}

getElem = function(elemId) {
	return document.getElementById(elemId);	
}

// parseQuery code borrowed from thickbox, Thanks Cody!
parseQuery = function(query) {
   var Params = new Object ();
   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;
}

/********************************************************
 Make this IE7 Compatible ;)
 http://ajaxian.com/archives/ajax-on-ie-7-check-native-first
*********************************************************/
createRequestObject = function() {
	var xmlhttp;
		/*@cc_on
	@if (@_jscript_version>= 5)
			try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
					try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
					catch (E) {xmlhttp = false;}
			}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
			try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
	}
	return xmlhttp;
}

var http = createRequestObject();

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

muestra_flashes = function(estado) {
	//busca los flashes y los muestra depués del fin de la precarga
var flh_items = document.getElementsByTagName("div");

	var ef2;
	for (var i = 0; i < flh_items.length ; i++) {
			ef2 = flh_items[i].id;
			 if(ef2.indexOf("flash") != -1){
				 flh_items[i].style.visibility=estado
 			 }
	}
}
