var SplashScreen = new Class({
  Implements: Options,
  options: {
    'url': null,
    'delay':5000 ,
    'showOnce':true,
    'cookiePath': null,
    'dimming': 1,
    'zindex': 999,
    'closeLabel': 'close'
  },
  initialize: function(element, options){
    this.setOptions(options);
    this.splash=$(element);
    if(this.options.showOnce){
      // check cookie to see if splash has been shown in this session
      var splashCookie = Cookie.read('showSplashCookie') || 'yes';
      var cookieOptions = {};
      if(this.options.cookiePath) cookieOptions.path = this.options.cookiePath;
      if(splashCookie=='yes'){
        //create splash div if not exists
        if(!this.splash) {
            this.splash = new Element('div', {id: 'splashContent'});
            this.splash.fade('hide');
            this.splash.inject($(document.body), 'top');
        }
        if(this.options.url) {
            this.loadContent();
        }
        else {
            this.showSplash();
        }
        Cookie.write('showSplashCookie', 'no', cookieOptions);
      }
    }else{
      this.showSplash();
    }
  },
  showSplash:function(){
  //dimming

    if(this.splash.innerHTML.length<3) return false;
    this.dim = new Element('div', {id: 'dim'});
    this.dim.inject($(document.body), 'top');
    this.dim.setStyles({
      'position': 'absolute',
      'z-index': this.options.zindex,
      'width':window.getScrollWidth(),
      'height':window.getScrollHeight(),
      'display':'block',
      'opacity': this.options.dimming
    });
    this.dim.addClass('splashDim');

    this.splash.addClass('splashContent');
    this.splash.fade('in');

    var splashDim = this.splash.getSize();
    var screenDim = window.getSize();
    this.splash.setStyles({
      'display':'block',
      'position': 'absolute',
      'z-index': this.options.zindex+1,
      'left': (screenDim.x-splashDim.x)/2+'px',
      'top': (screenDim.y-splashDim.y)/2+'px'
    });
    var splashPos = this.splash.getCoordinates();

    this.btCloseSplash = new Element('a', {id: 'btCloseSplash', 'href': 'javascript:;'});
    if(this.options.closeLabel) this.btCloseSplash.appendText(this.options.closeLabel);
    this.btCloseSplash.addClass('splashClose');
    this.btCloseSplash.inject(this.splash, 'top');
    this.btCloseSplash.setStyles({
        'position': 'absolute',
        'top': 0,
        'right': 0
    })
    var oThis = this;
    this.btCloseSplash.addEvent('click',function(){
      this.closeSplash();
    }.bind(this));

    // iframe for ie6 <Bah! Humbug!>
    if(Browser.Engine.trident4){
      var iframe2 = new Element('iframe',{
        styles:{'position':'absolute','width':'120%','height':'120%','top':'-10%','left':'-10%'},
        height:'100%',
        width:'100%',
        frameborder:0
      }).setOpacity(0).inject(this.splash,'top');
    }
    if(this.options.delay) this.timer = this.closeSplash.delay(this.options.delay, this);
  },
  loadContent: function() {
      var myHTMLRequest = new Request.HTML({ 
        url: this.options.url,
        update: this.splash,
        onSuccess: function() {
            this.showSplash()
        }.bind(this)
      });
      myHTMLRequest.send();
  },
  closeSplash: function(){
    if(this.timer) clearTimeout(this.timer);
    var myFx = new Fx.Tween(this.splash, {property: 'opacity'});
    myFx.start(this.splash.getStyle('opacity'),0).chain(
        this.dim.destroy()
    ); //Will fade the Element out and in twice.

  }
});



window.addEvent('domready', function() {

  if(window.location.search.indexOf('showsplash') >= 0)  {
    Cookie.write('showSplashCookie', 'yes', { 'path': '/' });
  }
  var splash = new SplashScreen('splashContent',{
    'url': '/hu/splash/', 
    'delay': null, 
    'dimming': 0.8, 
    'closeLabel': 'X', 
    'showOnce':true, 
    'cookiePath': '/'
  });

});


