function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
}

Array.prototype.inArray = function(value) {
  var i;
  for (i = 0; i < this.length; i++) {
    if (this[i] === value) {
      return true;
    }
  }
  return false;
};

var BayleyTvFlash = {

  flashvars: {
    playlistId: '6620651',
    isCarouselEnabled: '0',
    lang: 'es-mx',
    intl: 'e1',
    autoPlay: '1'
  },
  
  flashParams: {
    allowFullScreen: 'true',
    bgcolor: '#000000',
    wmode: 'opaque'
  },
  
  display: function() {
    swfobject.embedSWF("http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.46", "baileys-tv", "300", "270", "9.0.0","expressInstall.swf", this.flashvars, this.flashParams);
  }
  
}

var FlickrFlash = {

  flashvars: {
    offsite: 'true',
    page_show_url: '%2Fphotos%2Fcompartirentreamigas%2Fshow%2F',
    page_show_back_url: '%2Fphotos%2Fcompartirentreamigas%2F',
    user_id: '42833229@N06',
    jump_to: ''
  },
  
  flashParams: {
    allowFullScreen: 'true',
    wmode: 'opaque'
  },
  
  display: function() {
    swfobject.embedSWF("http://www.flickr.com/apps/slideshow/show.swf?v=71649", "flickr-flash-container", "650", "487", "9.0.0","expressInstall.swf", this.flashvars, this.flashParams);
  }
  
}


var AgeLimit = {

  LATIN_AMERICA_DRINKING_AGE: 18, 
  WORLD_DRINKING_AGE: 23,
  YEAR_IN_MILISECONDS: (1000*60*60*24*365),  
  latinAmericaCountries: ['Argentina', 'Spain', 'Mexico', 'Colombia', 'Peru', 'Chile', 'Venezuela', 'Puerto Rico', 'Costa Rica', 'Dominican Republic'],
  
  verify: function() {
    return (this.getAge() >= this.getAllowedAge()) ? true : false;
  },
  
  getCurrentCountry: function() {
    document.getElementById('current-country').value;
  },
  
  getCountry: function() {
    var countryElement = document.getElementById('country');
    return countryElement.options[countryElement.selectedIndex].text;
  },
  
  getAllowedAge: function() {
    return this.latinAmericaCountries.inArray(this.getCountry()) ? this.LATIN_AMERICA_DRINKING_AGE : this.WORLD_DRINKING_AGE;
  },
  
  getBirthday: function() {
    var day = document.getElementById('dd').value;
    var month = document.getElementById('mm').value;
    var year = document.getElementById('yy').value;
    var date = new Date(parseInt(year), (parseInt(month) - 1), (parseInt(day) - 1));
    return date;
  },
  
  getAge: function() {
    var allowedAge = this.getAllowedAge();
    var birthday = this.getBirthday();
    var today = new Date();
    return Math.floor((today.getTime() - birthday.getTime()) / this.YEAR_IN_MILISECONDS);
  }

}

var LightBox = {

  overlay: document.getElementById('overlay'),
  popup: document.getElementById('popup'),
  closeButton: document.getElementById('close-popup'),
  form: document.getElementById('age-verification'),
  forbiddenMessage: document.getElementById('access-denied'),
  
  init: function() {
    var self = this;
    this.overlay.onclick = this.closeButton.onclick = function() {
      self.hide();
      return false;
    }
  },
  
  hide: function() {
    this.overlay.style.display = 'none';
    this.popup.style.display = 'none';
  },

  show: function(callback) {
    var self = this;
    this.popup.style.display = 'block';
    this.overlay.style.height = getPageSize()[1];
    this.overlay.style.display = 'block';
    this.forbiddenMessage.style.display = 'none';
    this.popup.style.marginTop = this.getPageScroll()[1] + 'px';
    this.form.style.display = 'block';
    
    this.form.onsubmit = function() {
      if(AgeLimit.verify()) {
        callback();
        self.hide();
      } else {
        self.setForbiddenMessage();
      }
      return false;
    }
  },
  
  setForbiddenMessage: function() {
    this.form.style.display = 'none';
    this.forbiddenMessage.style.display = 'block';
  },
  
  getPageScroll: function () {
    var xScroll = 0, yScroll = 0;
    
    if (typeof(window.pageYOffset) == 'number') {
      xScroll = window.pageYOffset;
      yScroll = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
      xScroll = document.body.scrollTop;
      yScroll = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
      xScroll = document.documentElement.scrollTop;
      yScroll = document.documentElement.scrollLeft;
    }

    return [yScroll, xScroll];
  }
  
}

function getPageSize () {
        
  var xPage, yPage;
  
  if (window.innerHeight && window.scrollMaxY) {
    xPage = window.innerWidth + window.scrollMaxX;
    yPage = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight) {
    xPage = document.body.scrollWidth;
    yPage = document.body.scrollHeight;
  } else { 
    xPage = document.body.offsetWidth;
    yPage = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;

  if (self.innerHeight) {
    if (document.documentElement.clientWidth) windowWidth = document.documentElement.clientWidth; 
    else windowWidth = window.innerWidth;
    if (document.documentElement.clientHeight == (window.innerHeight - 16)) windowHeight = document.documentElement.clientHeight;
    else windowHeight = window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) {
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }       
  
  pageWidth = windowWidth;

  if (windowHeight < yPage) pageHeight = yPage;
  else pageHeight = windowHeight;

  var returnArray = Array(pageWidth, pageHeight, windowWidth, windowHeight);
  return returnArray;
}

LightBox.init();

addLoadEvent(function() {
  var links = document.getElementById('tips').getElementsByTagName('a');
  for(var i = 0; i < links.length; i++) {
    links[i].onclick = function() {
      var self = this;
      LightBox.show((function() {
        window.location = self.href;
      }));
      return false;
    }
  }
    
  document.getElementById('baileys-tv-flash-trigger').onclick = function() {
    LightBox.show((function() { 
      BayleyTvFlash.display();
    }));
    return false;
  }

  document.getElementById('flickr-trigger').onclick = function() {
    LightBox.show((function() {
      /*
      var FLICKR_GROUP_URL = 'http://www.flickr.com/photos/compartirentreamigas/show/';
      //window.location = FLICKR_GROUP_URL;
      window.open(FLICKR_GROUP_URL, "blank");*/
      window.location = 'http://www.yahoo-entreamigas.com/flickr';
    }));
    return false;
  }
  
  if(document.getElementById('secondary-box-1')) {
    var box1 = document.getElementById('secondary-box-1');
    var box2 = document.getElementById('secondary-box-2');
    var height = box1.offsetHeight;
    if(box1.offsetHeight < box2.offsetHeight) height = box2.offsetHeight;
    box1.style.height = box2.style.height = String(height - 26) + 'px';
  }
  if(document.getElementById('flickr-flash-container')) {
    FlickrFlash.display();
  }
});
