﻿
var popupStatus = 0; //variable global
function loadPopup(){
	//lo carga si está deshabilitado
	if(popupStatus===0){
		$("#backgroundPopup").css({
			"opacity": "0.2"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	//deshabilita si está habilitado
	if(popupStatus===1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}
// Funciones nuevas que reciben como parámetros los nombre de los divs y un número correlativo
var popupStatusNueva = new Array();
for(i=0;i<10;i++){popupStatusNueva[i]=0;}
function loadPopupNueva(divBackGround, divPopUp, j){
	//lo carga si está deshabilitado
	if(popupStatusNueva[j]===0){
		$(divBackGround).css({"opacity": "0.2"});
		$(divBackGround).fadeIn("slow");
		$(divPopUp).fadeIn("slow");
		popupStatusNueva[j] = 1;
	}
}

function disablePopupNueva(divBackGround, divPopUp, j){
	//deshabilita si está habilitado
	if(popupStatusNueva[j]===1){
		$(divBackGround).fadeOut("slow");
		$(divPopUp).fadeOut("slow");
		popupStatusNueva[j] = 0;
	}
}
//centrar popup
function centerPopup(){
	//datos para centrado
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	var delta=windowWidth-990;
	
	//
	if (delta<0) {delta=0}
	//centrar
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": delta/2 + 20 
	});
	//IE	
	$("#backgroundPopup").css({
		"height": windowHeight
	});	
}
//
function centerResultado(divBackGround, divPopUp){
	
	var windowHeight = document.documentElement.clientHeight;
	var windowPosition = $(".div_encuesta").offset().top;
	//alert(windowPosition);
	//1400-windowPosition era para TOP
	$(divPopUp).css({
		"position": "absolute",
		"top": 10, 
		"left": -300 
	});
	//IE, arriba era 250 en lugar de 600		
	$(divBackGround).css({
		"height": windowHeight
	});	
}
// para central cuando hay Scroll
function centerPopupScroll(){
	//datos para centrado
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.scrollTop;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	var delta=windowWidth-990;
	//
	if (delta<0) {delta=0}
	//centrar
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight,
		"left": delta/2 + 100 
	});
	//IE	
	$("#backgroundPopup").css({
		"height": windowHeight
	});	
}
// mostrar popup, valor es el contenido de la cookie: publi/zzz.ext
function mostrar(valor)
{
    centerPopup();
    loadPopup();
    if (valor!==''){
     createCookie('popup',valor,4);}  // 4 horas
}
// Inicializar, acá comienza el PopUp  ------->
$(document).ready(function(){
// Carga con retardo
// verifica si es tapa
if ($("#ctl00_logos").height() !== null)
{
if ($("#popupContact").height() !== null) // si es nulo, no hay publicidad en ese horario
{
var contenido=$("#popupContact").html();
var valor='ie6';  //(contenido.substring(contenido.indexOf('publi')+6,contenido.indexOf('.',contenido.indexOf('publi')+6))) // valor de la cookie
    if ((readCookie('popup') === null) || (readCookie('popup') !== valor))
    {
    setTimeout(function() {mostrar(valor)},2000,valor);
    setTimeout("disablePopup()",17000);
    }
}
else
{
    eraseCookie('popup');
}
}   
// Photomatic
if ($('#thumbnails img').length >0){
$('#thumbnails img').photomatic({
          photoElement: '#photo',
          previousControl: '#previousButton',
          nextControl: '#nextButton',
          firstControl: '#firstButton',
          lastControl: '#lastButton'
        }); 
} 
if ($('#thumbnails_avisos img').length >0){
$('#thumbnails_avisos img').photomatic({
          photoElement: '#photo',
          previousControl: '#previousButton',
          nextControl: '#nextButton',
          firstControl: '#firstButton',
          lastControl: '#lastButton'
        }); }  	   	
 	
//Cargar con botón, para pruebas, le puese el evento al Logo de El Día
	$("#button").click(function(){
		//centrar
		centerPopup();
		//cargar
		loadPopup();
	});
				
	//cerrar POPUP
	//Click  evento x!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#popupDenunciaClose").click(function(){
		disablePopupNueva('#backgroundDenuncia','#popupDenuncia',0);
	});
	//
	$("#popupResultadoClose").click(function(){
		disablePopupNueva('#backgroundResultado','#popupResultado',1);
	});
	//Click evento out!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press evento Escape!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
// funciones para crear, leer y borrar cookies
function createCookie(name,value,horas) {
	if (horas) {
		var date = new Date();
		// para días: dias*24*60*60*1000
		date.setTime(date.getTime()+(horas*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	//	document.cookie = name+"="+value+expires+"; path=/;domain=eldia.com.ar";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)===' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
