// Abrir Popups
function newWindow(mypage,myname,w,h,features) { var winl = (screen.width-w)/2; var wint = (screen.height-h)/2; if (winl < 0) winl = 0; if (wint < 0) wint = 0; var settings = 'height=' + h + ','; settings += 'width=' + w + ','; settings += 'top=' + wint + ','; settings += 'left=' + winl + ','; settings += features; win = window.open(mypage,myname,settings); win.window.focus(); }

// Invocar Timeouts en Javascript
function sessionTimeout(url_site){
	alert('Ha pasado mucho tiempo de inactividad. Por favor, inicie sesion automaticamente.');
	window.location = site + 'login/index/' + url_site.split('/').join('-');
};

// PNG
function PNG_loader(){
  for(var i=0; i<document.images.length; i++) {
    var img = document.images[i];
    var imgName = img.src.toUpperCase();
    if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
      var imgID = (img.id) ? "id='" + img.id + "' " : "";
      var imgClass = (img.className) ? "class='" + img.className + "' " : "";
      var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
      var imgStyle = "display:inline-block;" + img.style.cssText;
      if (img.align == "left") imgStyle += "float:left;";
      if (img.align == "right") imgStyle += "float:right;";
      if (img.parentElement.href) imgStyle += "cursor:hand;";
      var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; clear:right; height:" + (img.height) + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>";
      img.outerHTML = strNewHTML;
      i--;
    }
  }
}


// banners
function get_banners(page_name, ignore_banners){
	var url_site = site + 'administration/banners/get/';
	banners = $$('.banner');
	if(banners.length > 0) {
		__callBanner( banners[0], page_name, url_site, ignore_banners);
	}
}


function __callBanner(banner, page_name, url_site, ignore_banners){
			banner.removeClassName('banner');
			banner_type = '';
			
			if(banner.hasClassName('banner_flash')){
				banner.removeClassName('banner_flash');
				banner_type = 'flash';
			}
			
			if(banner.hasClassName('banner_image')){
				banner.removeClassName('banner_image');
				banner_type = 'image';
			}
			zone_nbr = banner.className.replace(/zone_/, "");
			if(page_name == ''){ page_name = ''; }
			
			if(ignore_banners == ''){
				ignore_banners = '';
			}
			
			
			ajaxCall = new Ajax.Request( url_site, {
				method: 'POST',
				parameters: 'page=' + page_name + '&zone=' + zone_nbr + '&type=' + banner_type + '&ignore_banners=' + ignore_banners,
				onSuccess: function(t){
					response = t.responseText.split('||');
					banner.update(response[1]);
					if(ignore_banners == ''){
						ignore_banners = response[0];
					} else {
						ignore_banners += ',' + response[0];
					}
					get_banners(page_name, ignore_banners);
				},
				onFailure: function(t) { Element.remove(banner); }
			});
		}


// Obtener valor Cookie
function getCookie(c_name){
	if(document.cookie.length>0){
		var 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 "";
}


// setear Cookie
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() );
}


// Comparar valor de Cookie
function checkCookie( cookie, val){
	var ckie = getCookie( cookie );
	if ( ckie == val ){
		return true;
	}
	return false;
}




/*
  Simple slideshow using prototype and scriptaculous.
  
  Usage:
  
    <script src="prototype.js"></script>
    <script src="effects.js"></script>
    <script src="slideshow.js"></script>
    <style type="text/css">
      div.slideshow { position: relative; width: 100px; height: 100px; }
      div.slideshow div { position: absolute; left: 0; top: 0; }
    </style>
    <div class="slideshow" id="slideshow">
      <div class="slide"><img src="slide1.jpg"></div>
      <div class="slide"><img src="slide2.jpg"></div>
      <div class="slide"><img src="slide3.jpg"></div>
    </div>
    <script type="text/javascript">new Slideshow('slideshow', 3000);</script>
*/

function Slideshow(slideshow, timeout) {
  this.slides = [];
  var nl = $(slideshow).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {
      this.slides.push(nl[i]);
    }
  }
  this.timeout = timeout;
  this.current = 0;

  for (var i = 0; i < this.slides.length; i++) {
    this.slides[i].style.zIndex = this.slides.length - i;
  }

  Element.show(slideshow);
  setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
}
Slideshow.prototype = {
  next: function() {
    for (var i = 0; i < this.slides.length; i++) {
      var slide = this.slides[(this.current + i) % this.slides.length];
      slide.style.zIndex = this.slides.length - i;
    }

    Effect.Fade(this.slides[this.current], {
      afterFinish: function(effect) {
        effect.element.style.zIndex = 0;
        Element.show(effect.element);
        Element.setOpacity(effect.element, 1);
      }
    });
    
    this.current = (this.current + 1) % this.slides.length;
    setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
  }
}





// Catfish Banner






// Deploy the Catfish

// The Catfish should be located in an element of id 'catfish' and should be hidden
// out of view

var catfish;

var path = "/upload/image/banners/pace/"; //ruta de combos
var	imgsName = "combo"; //nombre generico para las imagenes
var	imgsToShow = 3; //Cantidad de imagenes que hay en la carpeta "/upload/image/banners/pace/"

function displayRandImg(){
	var banner_img = document.getElementById('bottom_banner_img');
	randNum =(Math.floor(Math.random()*imgsToShow)+1); //numero random mayor que 0;
	ranImg = imgsName +  randNum//generando un nombre de imagen  ej "combo2"
	banner_img.src=path+ranImg+".png";
	banner_img.onclick = function(){openPopup(path+"/popup/"+ranImg+".jpg")};//openPopup(path+ranImg)
	document.getElementById('randNumSaved').value = randNum;
}
function displayNextImg(){
	var banner_img = document.getElementById('bottom_banner_img');
	nextNum = document.getElementById('randNumSaved').value;
	nextNum = parseInt(nextNum) + 1;
	if(nextNum > imgsToShow) nextNum =1;
	nextImg = imgsName +  nextNum;
	banner_img.src=path+nextImg+".png";
	document.getElementById('randNumSaved').value = nextNum;
}
function openPopup(imgPath){
	var url="/popups/catfish.php?imageName="+imgPath;
ventanita = window.open(url, 'catfishPopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=460,height=660');
	ventanita.moveTo(300,200);

}

function destroycatfish(){
	$('bottom_banner_cnt').remove();
	document.getElementsByTagName('html')[0].style.padding= '0'; /* reset the padding at the bottom */
	return false;
}

function deploycatfish(){
	catfish = document.getElementById('bottom_banner_cnt');
	catfishheight = 95; // total height of catfish in pixels
	catfishoverlap = 46; // height of the 'overlap' portion only (semi-transparent)
	catfishtimeout = setTimeout(startcatfish, 3000);
	
	displayRandImg();
}


function startcatfish(){
	catfishposition = 0;
	catfishtimeout = setInterval(positioncatfish, 25);
}

function positioncatfish(){
	catfishposition += 10;
	catfish.style.top = (((100 - catfishposition) / 100) * catfishheight) + 'px';
	if (catfishposition >= 100){
		clearTimeout(catfishtimeout);
		catfishtimeout = setTimeout(finishcatfish, 1);
	}
}

function finishcatfish(){
	catfish.style.marginBottom = '0';	
	document.body.parentNode.style.paddingBottom = (catfishheight - catfishoverlap) +'px';
}
