//Intersticial BOxX jportiyo/jordi(javascriptMan) 1-Abril-2011
//basado en Script anterior

//ojito a las configuraciones de cookies!
var interstitialBox={
//1) LISTA saca aleatoriamente paginas (separar comas y comillas simples cada una)
displayfiles: ['../promos/depilacion_laser.html'],

//2) Frecuencia en pantalla: ["tipo de frecuencia", "valor"] Suerte (chance) 
//cookie sesion ["cookie", "session"] (no vuelve a salir hasta que cierra navegador y vuelve)
//cookie por dia en este caso ["cookie", "5"] seria 1 vez cada 5 dias.

//displayfrequency: ["chance", "0"],
displayfrequency: ["cookie", "session"],

//3) HTML para la parte de barra de encabezado de la caja intersticial
defineheader: '<div class="headerbar"><a href="#" onClick="javascript:interstitialBox.closeit(); return false"><img id="cierre" src="../promos/imgs/closeit.png" title="Cerrar"/></a></div>',

//4) configuracion de la cookie cookie ["cookie_name", "cookie_path"] (nombre y ruta)
cookiesetting: ["stitialvars", "path=/"],

//5) almacenar el cache de la pagina con Ajax? ---potencia!!
ajaxbustcache: true,

//6) Desactivar las barras de desplazamiento del navegador mientras se muestra intersticial (solo para IE7/Firefox/Opera8 +. IE6 sólo será desplazamiento automático página para arriba)?
disablescrollbars: true,

//7)Autocultar intersticial (0 es NO) contador en segundos.
autohidetimer: 10,

////y aqui no se toca!!//////////////////////////////////

ie7: window.XMLHttpRequest && document.all && !window.opera,
ie7offline: this.ie7 && window.location.href.indexOf("http")==-1, 
launch:false,
scrollbarwidth: 10,

ajaxconnect:function(url, thediv){
var page_request = false
var bustcacheparameter=""
if (window.XMLHttpRequest && !this.ie7offline) // SI ES mOZILA SAFARI ETC...!!
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ 
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
interstitialBox.loadpage(page_request, thediv)
}
if (this.ajaxbustcache)
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
},

loadpage:function(page_request, thediv){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
document.getElementById("interContent").innerHTML=page_request.responseText
}
},

createcontainer:function(){
//empiezo el horror, aui va el intersticial BOX
document.write('<div id="interContainer">'+this.defineheader+'<div id="interContent"></div></div><div id="interVeil"></div>')
this.interContainer=document.getElementById("interContainer")
this.interVeil=document.getElementById("interVeil")
this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body 
},


showcontainer:function(){
if (this.interContainer.style.display=="none") return //si la caja intersticial ya ha cerrado, hay que salir (window.onresize evento desencadena la función)
var ie=document.all && !window.opera
var dom=document.getElementById
var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
var scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
var docwidth=(ie)? this.standardbody.clientWidth : window.innerWidth-this.scrollbarwidth
var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight
var objwidth=this.interContainer.offsetWidth
var objheight=this.interContainer.offsetHeight+90;//añado 90 pixel para descentrar Box y subir eliminar "+90" para centrar.
this.interVeil.style.width=docwidth+"px"
this.interVeil.style.height=docheightcomplete+"px" 
this.interVeil.style.left=0 
this.interVeil.style.top=0 
this.interVeil.style.visibility="visible" //Show veil over page
this.interContainer.style.left=docwidth/2-objwidth/2+"px" //Posicion intersticial
var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+5+"px" //Posicion intersticial
this.interContainer.style.top=Math.floor(parseInt(topposition))+"px"
this.interContainer.style.visibility="visible" //Show interstitial box
if (this.autohidetimer && parseInt(this.autohidetimer)>0 && typeof this.timervar=="undefined")
this.timervar=setTimeout("interstitialBox.closeit()", this.autohidetimer*1000)
},


closeit:function(){
this.interVeil.style.display="none"
this.interContainer.style.display="none"
if (this.disablescrollbars && window.XMLHttpRequest) //si las barras de desplazamiento estan desactivadasen navegador navegadores modelllnos IE7, Firefox, Safari, Opera 8 +
this.standardbody.style.overflow="auto"
if (typeof this.timervar!="undefined") clearTimeout(this.timervar)
},

getscrollbarwidth:function(){
var scrollbarwidth=window.innerWidth-(this.interVeil.offsetLeft+this.interVeil.offsetWidth)
this.scrollbarwidth=(typeof scrollbarwidth=="number")? scrollbarwidth : this.scrollbarwidth
},

hidescrollbar:function(){
if (this.disablescrollbars){
if (window.XMLHttpRequest)
this.standardbody.style.overflow="hidden"
else 
window.scrollTo(0,0)
}
},

dotask:function(target, functionref, tasktype){ 
var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
if (target.addEventListener)
target.addEventListener(tasktype, functionref, false)
else if (target.attachEvent)
target.attachEvent(tasktype, functionref)
},

initialize:function(){
this.createcontainer() 
this.ajaxconnect(this.displayfiles[Math.floor(Math.random()*this.displayfiles.length)], this.interContainer) //load page into content via ajax
this.dotask(window, function(){interstitialBox.hidescrollbar(); interstitialBox.getscrollbarwidth(); setTimeout("interstitialBox.showcontainer()", 100)}, "load")
this.dotask(window, function(){interstitialBox.showcontainer()}, "resize")
}
}

/////////////final declaracion box intersticial ////////////////////////////////

function getCookie(Name){
var re=new RegExp(Name+"=[^;]+", "i");
if (document.cookie.match(re)) 
return document.cookie.match(re)[0].split("=")[1] 
return null
}

function setCookie(name, value, days){
//lineaOriginal
var expireDate = new Date()
//modificacion para Firefox, cookie anterior a la fecha actual.
//var expireDate = "Thu, 01-Jan-1970 00:00:01 GMT"

if (typeof days!="undefined"){
var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; "+interstitialBox.cookiesetting[1];
}
else
document.cookie = name+"="+value+"; "+interstitialBox.cookiesetting[1]
}


var stitialvars=new Object()
stitialvars.freqtype=interstitialBox.displayfrequency[0]
stitialvars.cookieduration=interstitialBox.displayfrequency[1] 
stitialvars.cookiename=interstitialBox.cookiesetting[0]

if (stitialvars.freqtype=="chance"){
if (Math.floor(Math.random()*interstitialBox.displayfrequency[1])==0)
interstitialBox.launch=true
}
else if (stitialvars.freqtype=="cookie" && stitialvars.cookieduration=="session"){ 
if (getCookie(stitialvars.cookiename+"_s")==null){
setCookie(stitialvars.cookiename+"_s", "loaded")
interstitialBox.launch=true
}
}
else if (stitialvars.freqtype=="cookie" && typeof parseInt(stitialvars.cookieduration)=="number"){ 
if (getCookie(stitialvars.cookiename)==null || parseInt(getCookie(stitialvars.cookiename))!=parseInt(stitialvars.cookieduration)){  
setCookie(stitialvars.cookiename, stitialvars.cookieduration, stitialvars.cookieduration)
interstitialBox.launch=true
} 
}

if (interstitialBox.launch)
interstitialBox.initialize()
