var RUTA = "";
var logueado = false;
var InitFuncArray = new Array();
var browser = navigator.appName;

// Funciones que se llaman al terminar de cargar la pagina
function onLoadInit(){
	for (var i = 0; i < InitFuncArray.length; i++)
	{
		eval(InitFuncArray[i]).call();
	}
	checkScrollTo();
}

function addInitFunc(func){
	InitFuncArray.push(func);
}


var common_update_banner_clicks_sending = false;
function common_update_banner_clicks(id_banner, url_redirect)
{

	if (!common_update_banner_clicks_sending) {
		common_update_banner_clicks_sending = true;
		var AJAX = new sack(RUTA+"ajax/banner_update_clicks.php");
		AJAX.setVar("id_banner", id_banner);
		AJAX.onCompletion = function() {
			common_update_banner_clicks_sending = false;
			if (url_redirect && url_redirect != '') {
				document.location.href = url_redirect;
				//window.open(url_redirect, '');
			}
		}
		AJAX.runAJAX();
	}
}


function reducir_alto(){
	$('cabezal_flash').style.height = '50px';
}

function aumentar_alto(){
	$('cabezal_flash').style.height = '370px';
}

function ajustar_altos(){

	var fbHeight = 290;
	//$('#facebook').find('iframe').ready();
	
	$('relleno_col').style.height = 0+'px';
	
	/*if($('aviso_legal')){
		$('main_content').style.height = 1850+'px';
	}
	*/
	// Barra de filtro de noticias para la home
	if($('filtrar_noticias')){

		var diferencia = $('filtrar_noticias').offsetHeight;

	}else if($('main_headers')){

		var diferencia = $('main_headers').offsetHeight;

	}else{

		var diferencia = 0;
	}

	var medio = $('col_medio').offsetHeight;
	var left  = $('col_izq').offsetHeight;
	var right = $('col_der').offsetHeight + fbHeight;

	if(( left > right) && (left > medio)){

			$('col_der').style.height = left + 'px' ;
			$('col_medio').style.height = left + 'px';

	}else if((medio > left) && (medio > right)){

			console.log(medio + " : medio, " + left + " : left, " + right + " : right");

			// esperemos que esto no pase
			$('col_der').style.height = medio + 'px' ;
			$('col_izq').style.height = medio + 'px';
			//jQuery('#col_left').height(medio)  ;

	}else if((right > left) && (right > medio)){
			
			$('col_izq').style.height = right + fbHeight + 'px';


			$('col_medio').style.height = right + fbHeight + 'px';
			//jQuery('#col_der').height(left )  ;
			//jQuery('#col_medio').height(left )  ;
	}
	
		
			
		//	jQuery('#col_der').height(medio)  ;
		//	jQuery('#col_left').height(medio)  ;
		
					
			//jQuery('#col_izq').height(right+ fbHeight) ;
			//jQuery('#col_medio').height(right+ fbHeight ) ;

		//	$('col_izq').style.height= right+ fbHeight + 'px';
		//	$('col_medio').style.height = right+ fbHeight+ 'px';
	
		// verificar que pasa cuando son iguales !!!
		//jQuery('#col_der').height(left +fbHeight);
	}

	

	/*
	var height_der = $('col_der').offsetHeight ;
	
	if($('col_izq')){
		var height_izq = $('col_izq').offsetHeight;
		
		if($('col_medio')){
			var col_medio = $('col_medio').offsetHeight;
			if(col_medio>height_izq){
				height_izq = col_medio;
			}
		}
	}	
	else{
		var height_izq = $('main_content').offsetHeight;
	}
	
	//alert(height_der+'--'+height_izq);
	height_izq = height_izq + diferencia ;

	if(height_der > height_izq){
		var new_height_izq = height_der - diferencia  //Alto del  "Filtrar Noticias";
		if($('col_izq')){
			$('col_izq').style.height = 300 + new_height_izq+'px';
			if($('col_medio')){
				$('col_medio').style.height = new_height_izq+'px';
			}
		}
		else{
			//alert(new_height_izq);
			$('main_content').style.height = new_height_izq+'px';
			if($('col_medio')){
				$('col_medio').style.height = new_height_izq+'px';
			}
		}
	}
	else{
		var new_height_der = height_izq - height_der;
		$('relleno_col').style.height = new_height_der+'px';
		if($('col_medio')){
			$('col_medio').style.height = (height_izq-diferencia)+'px';
		}
	}
	*/


// Anchor dinámico
function checkScrollTo(){
	if (location.hash) {
		var str_anchor = location.hash.split("#")[1];
		if ($("anchor_"+str_anchor)) {
			Effect.ScrollTo("anchor_"+str_anchor);
		}
	}
}

function urlencode(str) {
    var result = "";
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) == " ") result += "+";
        else result += str.charAt(i);
    }
    return escape(result);
}

// Borrar contenido de inputs
function clear_input(mi_input,valor){
	if(mi_input.value == valor){
		mi_input.value = "";
	}
}

function complete_input(mi_input,valor){
	if(mi_input.value == ""){
		mi_input.value = valor;
	}
}

String.prototype.trim = function() {
  return this.replace(/^\s+/, '').replace(/\s+$/, '');
};

function only_numbers(evt){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, '.'=46, ','  = 44
	var key = IE ? evt.keyCode : evt.which;
	return (key <= 13 || (key >= 48 && key <= 57));
}

function buscar_blog(url){
	if($F('texto_busqueda') != '' && $F('texto_busqueda') != 'Buscar en el Blog' ){
		//alert(RUTA+url+'/buscar/'+urlencode($F('texto_busqueda'))+'/');
		window.location.href = RUTA+url+'/buscar/'+urlencode($F('texto_busqueda'))+'/';
	}
}

function urlencode(str) {
    var result = "";
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) == " ") result += "+";
        else result += str.charAt(i);
    }
    return escape(result);
}


// abrir una div 
function open_div(nombre,mod,idRef){
	var url = 'estructura/div_'+nombre+'.php?modulo='+mod+'&id='+idRef;
	
	if(nombre == 'blog_categorias'){
		div = 'div_message2';
	}
	else{
		div = 'div_message';
	}
	reload_div(div,url,true);
}

function open_video(url){
	var url = RUTA+'estructura/div_video.php?url='+url;
	new Ajax.Updater('div_message', url);
	$('div_message').style.display ='';
	$('div_message').style.width = '600px';
	$('div_message').style.height = '460px';
	showDiv('div_message');
}

function reload_div(div,page,onComp){
	var url = RUTA+page;
	new Ajax.Updater(div,url, {
		evalScripts:true,
		onComplete:function(){
			if(onComp){
				showDiv(div);
			}
		}
	});
}

function open_photo_gallery(modulo, id_ref, index) {
	var url = RUTA+'estructura/div_galeria.php?modulo='+modulo+'&id_ref='+id_ref+'&index='+index;
	new Ajax.Updater('div_message', url);
	$('div_message').style.display ='';
	$('div_message').style.width = '600px';
	$('div_message').style.height = '600px';
	showDiv('div_message');
}
function close_photo_gallery(){
	hideDiv('div_message');
}

function ir_a_mi_perfil(){
	window.location.href = RUTA+'mi_perfil/';
}

/* Funciones para centrar las divs que se muestran como pop-ups */
function showDiv(div){

	if($('contenedor')){
		var content_height = $('contenedor').offsetHeight;
	}
	else{
		var content_height = $('container').offsetHeight;
	}
	
	
	$('overlay').style.height =  content_height+"px";
	$('overlay').show();
	
	var scroll_size = getScrollXY();
	var page_size = getPageSize();
	
	var div_element = $(div);
	div_element.style.position = 'absolute';
	div_element.style.zIndex = 10000;
	div_element.style.display = 'block';
	//div_element.style.overflow = 'hidden';
	
	var my_height = div_element.offsetHeight;
	var my_width = div_element.offsetWidth;

	if($('nuevo_post') && (my_height+100)<page_size[1]){
		my_height = my_height+100;
	}

	
	var setX = ( page_size[0] - my_width  ) / 2 + scroll_size[0];
    var setY = ( page_size[1] - my_height ) / 2 + scroll_size[1];
	
	div_element.style.left = setX + "px";
    div_element.style.top  = setY + "px";
	
	//alert();
	
	//center(div,width,height);
}

function hide_div(div){
	div = parent.$(div);
	div.hide();
	$('overlay').hide();
}

function hideDiv(div){
	div = parent.$(div);
	div.hide();  
	$('overlay').hide();
}


function center(element,width,height){
 
 	element = $(element);
	
	var my_width  = getWindowWidth();
    var my_height = getWindowHeight();

	element.style.position = 'absolute';
	element.style.zIndex   = 999999999;

	var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - width  ) / 2;
    var setY = ( my_height - height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}


var IE = navigator.appName.toLowerCase().indexOf("microsoft") > -1;

// Alto de la Pagina
function getWindowHeight() {

	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

// Ancho de la Pagina
function getWindowWidth() {

	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}


function getPageSize() {
	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;
	}
	return [ myWidth, myHeight ];
}

function getScrollXY() {
  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;
  }
  return [ scrOfX, scrOfY ];
}
