/*
 *	F: Takes an element by its id (id_element (string)), and puts a dark layer on it, disabling the element
 *		Optional parameters: set_close_button (bool): Puts a close link button to hide the element
 *			auto_close (bool): Closes the element after 2 seconds
 */
var floating_elements = new Array();
keepCentered = function()
{
	floating_elements.each(function(id_element, key){
		//alert(id_element);
		if(typeof(id_element) != 'string')
		{
			floating_elements =  floating_elements.without(id_element);
			return;
		}
		if($(id_element))
		{
			centerLayer(id_element);
		}
		else
		{
			//Removes this element of the list
			floating_elements = floating_elements.without(id_element);
		}
	});
};

F = function(id_element, set_close_button, auto_close)
{
	if(!$(id_element)) return;
	
	if(set_close_button)
	{
		var cont_close = new L(T.close);
		cont_close.onclick = function(){$(id_element).hide();};
		//var aclose = new Element('a',{href:'#', onclick:'$(\''+id_element+'\').hide()'}).update('Cierra');
		var br = new Element('br');
		//cont_close.insert(aclose);
		$(id_element).insert(br);
		$(id_element).insert(cont_close);
	}
	var w=$(id_element).getWidth();
	var h=$(id_element).getHeight();
	var W = document.viewport.getWidth();
	var H = document.viewport.getHeight();
	var newT = Math.round(H/2-h/2);
	var newL = Math.round(W/2-w/2);
	newT = newT<0?0:newT;
	newL = newL<0?0:newL;
	$(id_element).style.top=newT+"px";
	$(id_element).style.left=newL+"px";
	$(id_element).style.padding="5px";
	$(id_element).style.width=w+'px';
	$(id_element).style.height=h+'px';
	$(id_element).style.zIndex = 100;
	$(id_element).absolutize();
	if(auto_close)
	{
		setTimeout(function(){
			Effect.Fade(id_element);
		}, 2000);
	}
	

};
L = function(text)
{
	var cent = new Element('p',{align:'center',style:'padding-top:10px'});
	var container = new Element('span', {'class':'btn_link_out',onmouseover:"this.className='btn_link_in'", onmouseout:"this.className='btn_link_out'", onclick:"this.className='btn_link_click'"}).update(text);
	if(is_ie8) container.className='btn_link_out';
	cent.update(container);
	return cent;
	
};
D = function(id_element, show_loading)
{
	if(!$(id_element)) return;
	
	if(is_ie6)
	{
		//IE6 bug: Hide textarea and select elements
		var descendants = $(id_element).descendants();
		descendants.each(function(descendant, key){
			if(typeof(descendant)!='object') return;
			if(descendant.nodeType!=1) return;
			
			if(descendant.tagName.toLowerCase()=="select")
			{
				descendant.style.visibility='hidden';
				descendant.writeAttribute('iehide', 'yes');
				//alert(descendant.inspect());
			}
			else if(descendant.tagName.toLowerCase()=="textarea")
			{
				descendant.style.visitiblity='hidden';
				descendant.writeAttribute('iehide', 'yes');
			}
		});
	}
	var new_id_layer = id_element+'_hiddenCMReg';
	/*if(is_ie8 && id_element=='body')
	{
		//Correct the body sizes because the real width was generated after load the page
		if(document.body.scrollWidth)
		{
			var w=document.body.scrollWidth;
			var h=document.body.scrollHeight;
			
			$('body').style.width=w+'px';
			$('body').style.height=h+'px';
		}
	}*/

	if($(new_id_layer))
	{
		$(new_id_layer).show();
		return;
	}
	var hidden_layer = new Element('div', {id:new_id_layer});
	if(typeof(w) == 'undefined' && typeof(h) == 'undefined')
	{
		var w=$(id_element).getWidth();
		var h=$(id_element).getHeight();
	}
	
	var zi=$(id_element).style.zIndex;
	
	hidden_layer.style.zIndex=100;
	hidden_layer.style.backgroundImage="url(/img/overlay.png)";
	hidden_layer.style.position="absolute";
	
	//Only IE alert($(id_element).outerHTML);
	var scrolling = $(id_element).cumulativeScrollOffset();
	if(!is_ie)
	{
		var scr_scrolling = document.viewport.getScrollOffsets();
		var l = Math.abs(scrolling.left-scr_scrolling.left);
		var t = Math.abs(scrolling.top-scr_scrolling.top);
		hidden_layer.clonePosition(id_element,{offsetLeft:l, offsetTop:t});
	}
	else
	{
		hidden_layer.style.posLeft = $(id_element).viewportOffset().left;// + 'px';
		hidden_layer.style.posTop = $(id_element).viewportOffset().top;// + 'px';
		hidden_layer.style.posWidth = $(id_element).getWidth();
		hidden_layer.style.posHeight = $(id_element).getHeight();
	}
	
	
	if(is_ie)
	{
		
		if(scrolling.left>0)
		{
			hidden_layer.style.posLeft += scrolling.left;
			//hidden_layer.style.left = hidden_layer.cumulativeOffset().left + scrolling.left + 'px';
		}
		if(scrolling.top>0)
		{
			hidden_layer.style.posTop += scrolling.top;
			//hidden_layer.style.top = hidden_layer.cumulativeOffset().top + scrolling.top + 'px';
	
		}
	}
	
	//alert(scrolling.inspect());
	if(show_loading)
	{
		var layer_loading = new Element('div', {id:'loading_sublayer_hiddenCMReg'});
		layer_loading.style.backgroundColor = "#fff";
		layer_loading.addClassName('normal');
		layer_loading.insert(new Element('img',{src:'/img/ajax-loader.gif'}),'top');
		layer_loading.innerHTML += T.tag_loading;
		
		if(h>25)
		{
			var h2 = layer_loading.getDimensions().height;
		//var w2 = $('loading_sublayer_hiddenCMReg').getDimensions().width;
			h2 = 20;
		//var l = Math.round((w/2) - (w2/2));
			var t = Math.round((h/2)-(h2/2));
		//alert("Parent height: "+h+"Child height: "+h2+" Middle: "+t);
		//alert("Parent width: "+w+"Child widht: "+w2+" Middle: "+l)
			layer_loading.style.marginTop = t+'px';
		//layer_loading.style.marginLeft = l+'px';
		}
		hidden_layer.style.textAlign='center';
		
		hidden_layer.insert(layer_loading);
		
	}

	if(is_ie6) document.body.appendChild(hidden_layer);
	else document.body.insert(hidden_layer, 'top');
};
P = function(show_loading)
{
	var new_id_layer = 'body_hiddenCMReg';
	if($(new_id_layer))
	{
		$(new_id_layer).show();
		return;
	}
	var hidden_layer = new Element('div', {id:new_id_layer});
	var w=document.viewport.getWidth();
	var h=document.viewport.getHeight();
	
	hidden_layer.style.zIndex=101;
	hidden_layer.style.backgroundImage="url(/img/overlay.png)";
	hidden_layer.style.position="absolute";
	//hidden_layer.clonePosition(document/*.body*/);
	hidden_layer.style.width=w+'px';
	hidden_layer.style.height=h+'px';
	if(show_loading)
	{
		var layer_loading = new Element('div', {id:'loading_sublayer_hiddenCMReg'});
		layer_loading.style.backgroundColor = "#fff";
		layer_loading.addClassName('normal');
		layer_loading.insert(new Element('img',{src:'/img/ajax-loader.gif'}),'top');
		layer_loading.innerHTML += T.tag_loading;
		
		if(h>25)
		{
			var h2 = layer_loading.getDimensions().height;
		//var w2 = $('loading_sublayer_hiddenCMReg').getDimensions().width;
			h2 = 20;
		//var l = Math.round((w/2) - (w2/2));
			var t = Math.round((h/2)-(h2/2));
		//alert("Parent height: "+h+"Child height: "+h2+" Middle: "+t);
		//alert("Parent width: "+w+"Child widht: "+w2+" Middle: "+l)
			layer_loading.style.marginTop = t+'px';
		//layer_loading.style.marginLeft = l+'px';
		}
		hidden_layer.style.textAlign='center';
		hidden_layer.insert(layer_loading);
		
	}
	loadBody();
	function waitBody()
	{
		var load_layer = new Element('div', {id:'body_hiddenCMReg', style:'left:0px;top:0px;width:100%;height:100%;z-index:100;background-image:url(/img/overlay.png);position:absolute'});

		if($('body'))
		{
			$('body').insert(load_layer, 'top');
			return;
		}
		setTimeout("waitBody()",50);
	}
};

var load_layer = new Element('div', {id:'body_hiddenCMReg', style:'left:0px;top:0px;width:100%;height:100%;z-index:100;background-image:url(/img/overlay.png);position:absolute'});

/*
 * PD: PageDark: Makes all body inactive and with a semi-transparent layer
 */
PD = function(transparent)
{
	//setTimeout(loadBody(load_layer),50);	
	
	loadBody();
	//$('body_hiddenCMReg').style.opacity = '0.4';
	if(typeof transparent != 'undefined')
		//$('body_hiddenCMReg').style.backgroundImage = '';
		$('body_hiddenCMReg').style.backgroundColor = 'transparent';
		/*$('body_hiddenCMReg').style.opacity = 0.4;
		$('body_hiddenCMReg').style.filter = 'alpha(opacity=40)';*/
};

function loadBody()
{
	if(!$('body_hiddenCMReg'))
	{
		var load_layer = new Element('div', {id:'body_hiddenCMReg'});
		load_layer.setStyle({
			left:'0px',
			top:'0px',
			width:'100%',
			height:'100%',
			position:'fixed',
			zIndex:10,
			backgroundColor : 'rgb(59, 89, 152)',
			filter : 'alpha(opacity=40)',
			opacity : 0.4
		});
		if($('body'))
		{
			$('body').insert(load_layer, 'top');
			//return;
		}
		else
		{
			//setTimeout("loadBody()",50);
		}
		
	}
	return;
	if(!is_ie6 && !is_ie7)	$('body').style.overflow='hidden';
	//if(!is_ie7) 
	
  	var myWidth = 0, myHeight = 0;
  	/*
  	if( typeof( window.innerWidth ) == 'number' ) {
    	//Non-IE
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
   		//IE 4 compatible
    	myWidth = document.body.clientWidth;
   		myHeight = document.body.clientHeight;
  	}
  	
	
  	//window.alert( 'Width = ' + myWidth );
  	//window.alert( 'Height = ' + myHeight );
  	*/
  	myWidth = document.viewport.getDimensions().width + document.viewport.getScrollOffsets().left;
  	myHeight = document.viewport.getDimensions().height + document.viewport.getScrollOffsets().top;
  	
  	
  	myWidth = myWidth<document.width	?	document.width	:	myWidth;
  	myHeight = myHeight<document.height	?	document.height	:	myHeight;
	//view_width = document.documentEle
	//$('body_hiddenCMReg').style.width = myWidth+"px";
	//$('body_hiddenCMReg').style.height = myHeight+"px";
  	$('body_hiddenCMReg').style.width = document.viewport.getWidth()+'px';
  	$('body_hiddenCMReg').style.height = document.viewport.getHeight()+'px';
}
/*
 * PC: PageClearance: Makes appear all the body and makes it active
 */
PC = function()
{
	if($('body_hiddenCMReg')) $('body_hiddenCMReg').remove();	
	if(!is_ie6 && !is_ie7) $('body').style.overflow='auto';
};
C = function(id_element)
{
	//alert(1);
	if(!$(id_element)) return;
	//alert(2);
	if(is_ie6)
	{
		//IE6 bug: Show textarea and select elements
		var descendants = $(id_element).descendants();
		descendants.each(function(descendant, key){
			if(typeof(descendant)!='object') return;
			if(descendant.nodeType!=1) return;
			
			if(descendant.tagName.toLowerCase()=="select")
			{
				//alert(descendant.readAttribute('iehide'));
				if(descendant.style.visibility=='hidden' && descendant.readAttribute('iehide')=='yes')
				{
					descendant.removeAttribute('iehide');
					descendant.style.visibility='visible';
				}
			}
			else if(descendant.tagName.toLowerCase()=="textarea")
			{
				if(descendant.style.visibility=='hidden' && descendant.readAttribute('iehide')=='yes')
				{
					descendant.removeAttribute('iehide');
					descendant.style.visibility='visible';
				}
			}
		});
	}

	var new_id_layer = id_element+'_hiddenCMReg';
	if($(new_id_layer))
	{ //$(new_id_layer).hide();
		$(new_id_layer).remove();
	}
};

RC = function()
{
	//TODO: Finish
	var rounded = new Element('div', {'class' : 'rounded'});
	var top = new Element('div', {'class' : 'top'});
	var right = new Element('div', {'class':'right'});
	var middle = new Element('div', {'class':'middle'});
	var right2 = new Element('div', {'class':'right'});
	var content = new Element('div', {'class':'content'}).update('text');
	var bottom = new Element('div', {'class':'bottom'});
	var right3 = new Element('div', {'class':'right'});
	top.insert(right);
	right2.insert(content);
	middle.insert(right2);
	bottom.insert(right3);
	
	rounded.insert(top);
	rounded.insert(middle);
	rounded.insert(bottom);
	
	
	
	
	return rounded;
	
};

centerLayer = function(layer, keep_centered)
{
	if(typeof(layer)=='string') layer=$(layer);
	if(!layer) return;
	
	var w = layer.getWidth();
	var h = layer.getHeight();
	var W = document.viewport.getWidth();
	var H = document.viewport.getHeight();
	if(w>W || h>H)
	{
		document.getElementsByTagName('body')[0].style.overflow='auto';
	}
	
	var newT = Math.round(H/2-h/2);
	var newL = Math.round(W/2-w/2);
	newT = newT<0?0:newT;
	newL = newL<0?0:newL;
	layer.style.top = newT + 'px';
	layer.style.left = newL + 'px';
	if(typeof(keep_centered)!='undefined' && keep_centered)
	{
		layer.style.position='fixed';
	}
	else
		layer.absolutize();
	
	layer.scrollTo();
};
centerLayerOld = function(layer, keep_centered)
{
	
	keep_centered = (keep_centered)	?	true	:	false;
	
	if(layer)
	return;
	var scrOfX = 0, scrOfY = 0;

  	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
  	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
  	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
  	}
	//alert('Scroll X: '+scrOfX+', scroll Y: '+scrOfY);
	//scrOfY = document.viewport.getScrollOffset().top;
	//scrOfX = document.viewport.getScrollOffset().left;
	
 	var winWidth = 0, winHeight = 0;
  	if( typeof( window.innerWidth ) == 'number' )
	{
    	//Non-IE
    	winWidth = window.innerWidth;
    	winHeight = window.innerHeight;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
    	//IE 6+ in 'standards compliant mode'
    	winWidth = document.documentElement.clientWidth;
    	winHeight = document.documentElement.clientHeight;
  	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
    	//IE 4 compatible
    	winWidth = document.body.clientWidth;
   		winHeight = document.body.clientHeight;
  	}
  	
  	winWidth = document.viewport.getWidth()+document.viewport.getScrollOffsets().left;
  	winHeight = document.viewport.getHeight()+document.viewport.getScrollOffsets().top;
	//alert("Win view width :" + winWidth+", window view height :"+winHeight);
	//winWidth = document.viewport.getWidth();
	//winHeight = document.viewport.getHeight();
  	if(typeof(layer)=='object') var obj = layer;
  	else var obj = $(layer);
	if(obj)
	{
		
		obj.position='abolute';
		obj.style.marginLeft='0px';
		obj.style.marginTop='0px';
		obj.absolutize();
		var topRel = Math.round(obj.getHeight()/2);
		
		var top = scrOfY+30;
		//top = Math.round(winHeight/2)+scrOfY;
		var leftRel = Math.round(obj.getWidth()/2);
		var left=Math.round(winWidth/2)-leftRel;
		if(left<0) left=0;
		left += scrOfX;
		//alert(left+'x'+top);
		obj.style.left=left+'px';
		obj.style.top=top+'px';
		obj.makePositioned();
		if(keep_centered)
		{
			floating_elements.push(layer);
			floating_elements=floating_elements.uniq();
		}
	}
	
	
};
VI = function(idmi)
{
	if(!idmi) return false;
	new Ajax.Request('/cp/ctrl_panel.php',{
		method:'get',
		parameters:{
			action:'ajax_callback',
			subaction:'getVersionInfo',
			'idmi':idmi
		},
		onCreate:function()
		{
			if($('help_container_out')) $('help_container_out').remove();
			var help_container=new Element('div', {id:'help_container'});
			var cTop=document.viewport.getScrollOffsets()[1]+100;
			help_container.setStyle({
				position:'absolute',
				border:'10px solid rgba(60, 60, 60, 0.5)',
				top:cTop+'px',
				overflow:'auto',
				width:'75%',
				left:'12%',
				MozBorderRadius:'5px',
				WebkitBorderRadius:'5px',
				padding:'10px',
				backgroundColor:'rgb(255, 255, 255)',
				backgroundPosition:'10px 10px'
			});
			var help_container2 = new Element('div', {id:'help_container_in', style:'margin:10px;background-color:rgb(255, 255, 255);width:100%;height:100%'});
			
			help_container2.update(T.tag_loading);
			help_container.insert(help_container2);
			document.body.insert(help_container, {position:'top'});
			var close_butt = L(T.close);
			close_butt.onclick=function(){
				$('help_container').remove();	
			};
			help_container.insert(close_butt);
			
		},
		onSuccess:function(t)
		{
			$('help_container_in').update(t.responseText);
		}
	});
};
wopen = function(page, title)
{
	window.open(page, title, "widht=800,height=600,location=no,scrollbars=yes,resizable=yes", true);
};

HL = function(section)
{
	if(!$('help_container'))
	{	
		if(is_ie) var bodyEl=$('body');
		else var bodyEl=document.body;
		bodyEl.insert(new Element('div', {id:'help_container', style:'display:none; position:absolute; z-index:10;height:70%;overflow:auto', 'class':'popup_container'}), {position:'top'});
		$('help_container').insert(new Element('div', {id:'help_container_in', style:'padding:10px;background-color:#eee;'}));
	}
	new Ajax.Request('/ajax/helper.php', {
		method : 'get',
		parameters : {
			action : section	
		},
		onCreate : function(){
			$('help_container_in').update(T.tag_loading);
			$('help_container').appear();
			//Center layer in page
			centerLayer('help_container');
		},
		onSuccess : function(t){
			//$('help_container').hide();
			$('help_container_in').innerHTML = "<div align=\"right\" onclick=\"$('help_container').remove()\"><img style=\"cursor:pointer\" src=\"/img/icons/close.gif\" title=\""+T.close+"\" alt=\""+T.close+"\" /></div>";
			$('help_container_in').innerHTML += t.responseText;
			//$('help_container').show();
			centerLayer('help_container');
		}
					 
	});
};

