var splashpage={
   
  // background overlay opacity
  opacity: 90,
  
  enablefrequency: true,

  // display freqency: "sessiononly" or "x days" (string value).
  displayfrequency: "sessiononly",

  // cookie setting: ["cookie_name", "cookie_path"]
  cookiename: ["splashpagecookie", "path=/"],

  // auto hide after x seconds (Integer value, 0=no)
  autohidetimer: 0,
   
  launch:false,
  
  setup:function(){
    jQuery('#splashcontainer').show();
	  
    this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
    if (!/safari/i.test(navigator.userAgent)) { //if not Safari, disable document scrollbars
    	this.standardbody.style.overflow="hidden";
    }
    this.moveuptimer=setInterval("window.scrollTo(0,0)", 50);
    
    var height = jQuery('#splashtag').height();
	if (height == 0 || height == null) {
		splashpage.closeit();
	} else if (parseInt(this.autohidetimer)>0) {
    	setTimeout(function(){splashpage.closeit();}, parseInt(this.autohidetimer)*1000);
    }
  },

  closeit:function(){	
	clearInterval(this.moveuptimer);
	jQuery('#splashpage,#splashshade').hide();
  
	// Dirty hack
	var innerHeight = jQuery('#leaderboard .inner').height();
	if (innerHeight < 35) {
		jQuery('body').addClass('no-leaderboard');
	} else {
		jQuery('body').removeClass('no-leaderboard');
	}
  
	this.standardbody.style.overflow="auto";
  },

  init:function(){
    
    // init
    if (this.enablefrequency){ //if frequency control turned on
      if (/sessiononly/i.test(this.displayfrequency)){ //if session only control
        if (this.getCookie(this.cookiename[0]+"_s")==null){ //if session cookie is empty
          this.setCookie(this.cookiename[0]+"_s", "loaded")
          this.launch=true
        }
      }
      else if (/day/i.test(this.displayfrequency)){ //if persistence control in days
        if (this.getCookie(this.cookiename[0])==null || parseInt(this.getCookie(this.cookiename[0]))!=parseInt(this.displayfrequency)){ //if persistent cookie is empty or admin has changed number of days to persist from that of the stored value (meaning, reset it)
          this.setCookie(this.cookiename[0], parseInt(this.displayfrequency), parseInt(this.displayfrequency))
          this.launch=true
        } 
      }
    } else { // if enablefrequency is off
      this.launch=true;
    }
    
    if (this.launch) { 
    	this.setup();
    }
    
  },
  
  getCookie:function(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return null
  },

  setCookie:function(name, value, days){
    var expireDate = new Date()
    //set "expstring" to either an explicit date (past or future)
    if (typeof days!="undefined"){ //if set persistent cookie
      var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
      document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; "+splashpage.cookiename[1] //last portion sets cookie path
    }
    else //else if this is a session only cookie setting
    document.cookie = name+"="+value+"; "+splashpage.cookiename[1] //last portion sets cookie path
  }

};

(function($){
	$('#splashshade,#splashpage-close,#splashpage-continue').bind('click',function(){
		splashpage.closeit();
		return false;
	});
	
	/* We need to check the cookie with javascript and not in php because the pages are cached 
 	   in oracle web cache. The cookie check is done in the init() function. */
	splashpage.init();
}(jQuery));
