// Para evitar conflictos entre JQuery y Prototype
//Es necesario que jquery este cargado antes de esta linea
var $j = jQuery.noConflict();


var fontElementId1 = "CONTENIDO-PPAL"; 

var standardFont = "100%";
//DO NOT MODIFY BELOW

function imprimir() {
	window.print();
}

function changeFont(up, id) {
	var obj = document.getElementById(id);
    if (obj == null) return;
    
	var fontsize = obj.style.fontSize;
    if(fontsize=="") {
    	fontsize=standardFont;    	   	
    }
	
    fontsize = parseInt(fontsize.replace(/%/g, ''));	
	if(up) {
		fontsize = fontsize * 1.1;
	} else {
		fontsize = fontsize * 0.9;
	}
	
	obj.style.fontSize = fontsize+"%";
	
	
	
}

function increaseFont(component,event){	
	changeFont(true, fontElementId1);
	iceSubmit($('mainContentForm'),component,event); 
}

function reduceFont(component,event){
	changeFont(false, fontElementId1);
	iceSubmit($('mainContentForm'),component,event); 
}

/* Module Set Cookie (string, string, int) -- http://www.w3schools.com/js/js_cookies.asp */
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/* Module Get Cookie (string) -- http://www.w3schools.com/js/js_cookies.asp */
function getCookie(c_name){
	if(document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return false;
}

function loadTooltip() {
	$j('.tooltip').tooltip({
		track: true
	});
}

function initTooltip() {
	if(navigator.appName == "Netscape"){
		loadTooltip();
	} else {
		if(document.readyState == 'complete') {
			loadTooltip();
		} else {
			intervalPrint = setInterval("initTooltip()", 1000);
		}
	}
}

//Script para agregar a Favoritos
function addToFavourites(description, url){
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(description, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, description); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

function sendSearchForm(obj, event) {
	var keycode = event.keyCode;

	if (keycode == 13) {
		var form = formOf(obj);
		
		form['mainContentForm:_idcl'].value='mainContentForm:searchAction';
		return iceSubmit(form,obj,event);
	}
}

function checkBrowser() {
	//Detectar IE6
	jQuery.each(jQuery.browser, function(i) {
		if($j.browser.msie && $j.browser.version=="6.0") { 
			$j('.div_ie6').removeClass('oculto');
			$j('.div_ie6').addClass('visible');
		} else {
			$j('.div_ie6').addClass('oculto');
			$j('.div_ie6').removeClass('visible');
		}
	});
}

function closeIE6Window() {
	$j('.div_ie6').addClass('oculto');
	$j('.div_ie6').removeClass('visible');
	$j(".ul_menu_corporativo li").get()[0].style.position="relative";
}

function getTwitterUrl(text){
	var buena;
	var pos = text.indexOf("/");
	while(pos > -1) {
	    pos = text.indexOf("/", pos+1);
		if(pos!=-1)
			buena=pos;
	}
	return text.substring(0,buena+1);
}

//Galeria de imagenes de la home, llamar con $("#id_de_la_capa").loopedSlider();
(function($j) {
	$j.fn.loopedSlider = function(options) {
		
	var defaults = {			
		container: '.container',
		slides: '.slides',
		pagination: '.pagination',
		containerClick: true, // Click container for next slide
		autoStart: 1, // Set to positive number for auto interval and interval time
		restart: 1, //Set to positive number for true. Sets time until autoStart is restarted.
		layer: ''		// Nombre de la capa		
	};
		
	this.each(function() {		
		var obj = $j(this);
		var o = $j.extend(defaults, options);
		var m = 0;
		var t = 1;
		var total = $j(o.slides,obj).children().size();
		var w = $j(o.slides,obj).children().outerWidth();
		var p = 0;
		var u = false;
		var n = 0;
		var items = $j(o.slides,obj).children();
		var primero;		
		var ultimo;	
		var active = false;
		var interval = 0;
		var restart = 0;

		items.each(function() {
			if(primero==null) {
				primero=$j(this);
			}
			ultimo=$j(this);
		});
		
		
		$j(o.slides,obj).css({width:(total*w)});
		
		var selected;
		var posicion = 1;
		
		items.each(function() {
			if (selected == null) {
				selected = $j(this); 
				primero = selected;
			}
			$j(this).hide();
			p=p+w;
		});
		
		selected.show();
		
		$j('.a_anterior_' + o.layer,obj).click(function(){				
			if(active==false) {	
				animate("prev",true);
				if(o.autoStart){
					autoStart();
				}
			} return false;

		});
		
		$j('.a_siguiente_' + o.layer,obj).click(function(){
			if(active==false) {
				animate("next",true);
				if(o.autoStart){
					autoStart();
				}
			} return false;

		});
		
		if (o.autoStart) {
			sliderIntervalID = setInterval(function(){
				if(active==false) {animate("next",true);}
			},o.autoStart);
			function autoStart() {
				if (o.restart) {
					clearInterval(sliderIntervalID);
					clearInterval(interval);
					clearTimeout(restart);
					restart = setTimeout(function() {
						interval = setInterval(	function(){
							animate("next",true);
						},o.autoStart);
					},o.restart);
				} else {
					sliderIntervalID = setInterval(function(){
						if(active==false) {animate("next",true);}
					},o.autoStart);
				}
			};
		}

		$j('.sp_activo_' + o.layer).text(posicion+" de "+total);
		
		function animate(dir) {
			switch(dir){
				case 'next':
					posicion++;
					selected.fadeOut(500);
					selected = selected.next();
					selected.fadeIn(500);
					
					if (posicion > total) {
						selected = primero;
						selected.fadeIn(500);
						posicion = 1;
					}
					$j('.sp_activo_' + o.layer).text(posicion+" de "+total);
					
					break;
				case 'prev':					
					posicion--;
					selected.fadeOut(500);
					if (posicion <= 0) {
						selected = ultimo;
						posicion = total;
					} else {
						selected = selected.prev();
					}
					
					selected.fadeIn(500);				
					$j('.sp_activo_' + o.layer).text(posicion+" de "+total);
					
					break;
				default:
					break;
				}	
			};
		});
	};
})(jQuery);


//Carrousel de noticias por sector
jQuery(document).ready(function() {
	try{
	jQuery('#carousel-noticias-sector').jcarousel({
       animation: "slow"
    });
	}catch(excp){}
});	

//Galeria de nuevos productos
jQuery(document).ready(function() {
	jQuery('#div_nuevos_productos').loopedSlider({
		containerClick: false,
		autoHeight: true,
		autoStart: 10000,
		layer: "nuevos_productos"
	});
});

//Galeria de aperturas
jQuery(document).ready(function() {
	jQuery('#div_aperturas').loopedSlider({
		containerClick: false,
		autoHeight: true,
		autoStart: 10000,
		layer: "aperturas"
	});
});

//Galeria de entrevistas
jQuery(document).ready(function() {
	jQuery('#div_entrevistas').loopedSlider({
		containerClick: false,
		autoHeight: true,
		autoStart: 10000,
		layer: "entrevistas"
	});
});
