// JavaScript Document
// JavaScript Document
/*
Función: Ventanas del menú
Descripción: Función que permite ver las ventanas al mouse hover en el menu central
*/
var Toggle = {

	/**
	 * Node cache.
	 */
	cache: {},

	/**
	 * Custom defined callback function.
	 */
	callback: null,

	/**
	 * Timeout to close the menu automatically.
	 */
	timeout: 500,

	/**
	 * Determines whether or not to persist menu open.
	 */
	keepOpen: false,

	/**
	 * Opens a menu / dropdown element.
	 *
	 * @param object triggerNode
	 * @param string activeClass
	 * @param string targetPath
	 * @param int delay
	 */
	open: function(triggerNode, activeClass, targetPath, delay) {
		if (delay)
			Toggle.timeout = delay;

		//keep menu open
		Toggle.keepOpen = true;

		//make sure any other menus that could be open are closed
		for (var id in Toggle.cache) {
			if (id != targetPath) {
				var currentID = Toggle.cache[id];
				$(currentID.trigger).removeClass(currentID.activeClass);
				$(id).hide();
			}
		}

		//bind events and cache
		if (!Toggle.cache[targetPath]) {
			//bind events and toggle the class
			$(triggerNode)
				.mouseleave(function() {
					Toggle.close(triggerNode, activeClass, targetPath, Toggle.timeout);
				})
				.mouseenter(function() {
					Toggle.keepOpen = true;
					window.clearTimeout(Toggle.cache[targetPath].timer);
				});

			//bind events and toggle display of the target
			$(targetPath)
				.mouseleave(function() {
					Toggle.close(triggerNode, activeClass, targetPath, Toggle.timeout);
				})
				.mouseenter(function() {
					Toggle.keepOpen = true;
					window.clearTimeout(Toggle.cache[targetPath].timer);
				});

			//cache properties
			Toggle.cache[targetPath] = {
				trigger:	 triggerNode,
				activeClass: activeClass,
				timer:	   null
			};

			//allow clicking anywhere off the dropdown to close it
			$("body").click(function() {
				if (!Toggle.keepOpen) {
					Toggle.triggerCallback();

					$(targetPath).hide();
					$(triggerNode).removeClass(activeClass);
					window.clearTimeout(Toggle.cache[targetPath].timer);
				}
			});
		}

		//toggle class/display
		$(triggerNode).toggleClass(activeClass);
		$(targetPath).toggle();

		window.clearTimeout(Toggle.cache[targetPath].timer);
	},

	/**
	 * Close the menu and clear any cached timers.
	 *
	 * @param object triggerNode
	 * @param string activeClass
	 * @param string targetPath
	 * @param int delay
	 */
	close: function(triggerNode, activeClass, targetPath, delay) {
		//allow clicking off the menu to close it
		Toggle.keepOpen = false;

		window.clearTimeout(Toggle.cache[targetPath].timer);

		Toggle.cache[targetPath].timer = setTimeout(function() {
			$(targetPath).hide();
			$(triggerNode).removeClass(activeClass);
			Toggle.triggerCallback();
		}, delay);
	},

	/*
	 * Trigger a callback if defined
	 */
	triggerCallback: function() {
		if ((Toggle.callback) && (typeof Toggle.callback === 'function'))
			Toggle.callback();
	}
};
/*
Función: Aumentar o disminuir texto
Descripción: Esta función permite aumentar, disminuir y re-establecer el tamaño del texto
*/
function zoomText(Accion,Elemento){

//inicializacion de variables y parámetros
var obj=document.getElementById(Elemento);
var max = 200 //tamaño máximo del fontSize
var min = 70 //tamaño mínimo del fontSize
if (obj.style.fontSize==""){
obj.style.fontSize="100%";
}
actual=parseInt(obj.style.fontSize); //valor actual del tamaño del texto
incremento=10;// el valor del incremento o decremento en el tamaño

//accion sobre el texto
if( Accion=="reestablecer" ){
obj.style.fontSize="100%"
}
if( Accion=="aumentar" && ((actual+incremento) <= max )){
valor=actual+incremento;
obj.style.fontSize=valor+"%"
}
if( Accion=="disminuir" && ((actual+incremento) >= min )){
valor=actual-incremento;
obj.style.fontSize=valor+"%"
}
}
/*
Función: Efectos
Descripción: Efectos para el sitio de usabilidad
*/
$(function() {
	// Scroll
	$(".scrollable").scrollable({circular: true}).autoscroll({ autoplay: true, interval: 2000 });
	// Tool Tips
	$("#nav ul").tabs("#panes > div");
	$("#img_link_clases img[title]").tooltip();
	$(".tip[title]").tooltip();
	// Despliegue de Testimonios
	$("#testimonio1").hover(
		function () {
			$("#testimonio1 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio1 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio2").hover(
		function () {
			$("#testimonio2 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio2 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio3").hover(
		function () {
			$("#testimonio3 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio3 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio4").hover(
		function () {
			$("#testimonio4 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio4 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio5").hover(
		function () {
			$("#testimonio5 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio5 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio6").hover(
		function () {
			$("#testimonio6 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio6 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio7").hover(
		function () {
			$("#testimonio7 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio7 .infoTestimonio").fadeOut();
		}
	);
	$("#testimonio8").hover(
		function () {
			$("#testimonio8 .infoTestimonio").fadeIn();
		}, 
		function () {
			$("#testimonio8 .infoTestimonio").fadeOut();
		}
	);
	// ShadowBox Normal
	$("a[rel]").overlay({
		mask: 'white',
		fixed: false,
		effect: 'apple',
		onBeforeLoad: function() {
			var wrap = this.getOverlay().find(".contentWrap");
			wrap.load(this.getTrigger().attr("href"));
		}
	});
});
/*
Función: RollOver Images
Descripción: Cambia las imágenes al rollover o mouseover
*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/* Abre Popup Promocion */
function pop_promocion()
{
	window.open('http://www.energy.cl/promociones/navidad/index.php','promo','status=no,resizable=no,toolbar=no,location=0,scrollbars=yes,menubar=0,width=800px,height=600px');
}
