var preloadFlag = false;
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var DOM2=document.getElementById;

//Countdown Settings
var fcontent = new Array();
var crosscount='';
var occasion="Next Game: ";
var message_on_occasion="Game in Progress";
var message_after_occasion="Last Game Has Ended";

//Fader Settings
var hex=0;
var endcolor="rgb(255,255,255)";
var startcolor="rgb(0,0,0)";
var faderdelay=0;
var index=parseInt(getCookie('fadeIndex'));
var frame=20;
var linksobj;
var timer;
if (DOM2)
  faderdelay=2000;

/*----------------------------------------------------------------------------*/
function checkLength(field, length){
	if (field.value.length > length){
		alert("That's right Funny Man....I am now forwarding your unnecessarily long response to your email address every minute for a week");
		field.value = field.value.substring(0, length);
	}
}
/*----------------------------------------------------------------------------*/
function load(){
    window.name = 'main';
    preloadImages();
    start_countdown();
    if (fcontent.length)
      changeFadeItem();
    if (document.login)
      	document.login.uid.focus();
   
}

/*----------------------------------------------------------------------------*/
function updatePlayerInfo(){
    var winAttrs = "height=300,width=300"
    var newWindow = window.open("update.html", "Update", winAttrs)
    newWindow.focus()
}

/*----------------------------------------------------------------------------*/
function viewPlayerInfo(pid) {
    var winAttrs = "height=400,width=400,scrollbars"
    var newWindow = window.open("player.html?player="+pid, "Info", winAttrs)
    newWindow.focus()
}

/*----------------------------------------------------------------------------*/
function viewGameInfo(gid) {
    var winAttrs = "height=400,width=400,scrollbars"
    var newWindow = window.open("game.html?game="+gid, "Info", winAttrs)
    newWindow.focus()
}

/*----------------------------------------------------------------------------*/
function setcountdown(theyear,themonth,theday,thehour,themin,thesec){
  yr=theyear;mo=themonth;da=theday;hr=thehour;min=themin;sec=thesec
}

/*----------------------------------------------------------------------------*/
function start_countdown(){
  if (DOM2)
    crosscount = document.getElementById("countdownie");

  countdown()
}

/*----------------------------------------------------------------------------*/
function countdown(){

  if(!(mo || da || yr || hr || min || sec))
    return;

  var today=new Date()
  var todayy=today.getYear()
  if (todayy < 1000)
    todayy+=1900
  var todaym=today.getMonth()
  var todayd=today.getDate()
  var todayh=today.getHours()
  var todaymin=today.getMinutes()
  var todaysec=today.getSeconds()
  var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
  futurestring=montharray[mo-1]+" "+da+", "+yr+" "+hr+":"+min+":"+sec
  dd=Date.parse(futurestring)-Date.parse(todaystring)
  dday=Math.floor(dd/(60*60*1000*24)*1)
  dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
  dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
  dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

  if (dday  < 10 && dday  >= 0) { dday =  "0" + dday  }
  if (dhour < 10 && dhour >= 0) { dhour = "0" + dhour }
  if (dmin  < 10 && dmin  >= 0) { dmin =  "0" + dmin  }
  if (dsec  < 10 && dsec  >= 0) { dsec =  "0" + dsec  }

  //if on day of occasion
  if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1&&todayd==da){
    if (DOM2)
      crosscount.innerHTML=message_on_occasion
    return
  }
  //if passed day of occasion
  else if (dhour<=-6){
    if (DOM2)
      crosscount.innerHTML=message_after_occasion
    return
  }
  //else, if not yet
  else{
    if (DOM2)
      crosscount.innerHTML=occasion+dday+" day(s) "+dhour+":"+dmin+":"+dsec
  }
  setTimeout("countdown()",1000)
}

/*----------------------------------------------------------------------------*/
function changeFadeItem()
{
  if (!index)
    index=0;
    
  if (index>=fcontent.length)
    index=0;

  setCookie('fadeIndex', index, 5);
  if (DOM2){
    document.getElementById("fscroller").style.color=startcolor;
    document.getElementById("fscroller").innerHTML=begintag+fcontent[index]+closetag;
    linksobj=document.getElementById("fscroller").getElementsByTagName("A");
    linkcolorchange(linksobj);
    colorfade();
  }

  timer = setTimeout("changeFadeItem()",delay+faderdelay);
  index++;
}

/*----------------------------------------------------------------------------*/
function prevFadeItem()
{
  clearTimeout(timer);
  index-=2;
  if (index < 0)
    index = fcontent.length - 1;

  changeFadeItem();
}

/*----------------------------------------------------------------------------*/
function nextFadeItem()
{
  clearTimeout(timer);
  changeFadeItem();
}

/*----------------------------------------------------------------------------*/
function colorfade() {

  if(frame>0) {	
    hex = hex+12;
    document.getElementById("fscroller").style.color="rgb("+hex+","+hex+","+hex+")";
    linkcolorchange(linksobj);
    frame--;
    setTimeout("colorfade()",50);
  }
  else{
    document.getElementById("fscroller").style.color=endcolor;
    frame=20;
    hex=0;
  }   
}

/*----------------------------------------------------------------------------*/
function linkcolorchange(obj){
  if (obj.length>0) {
    for (i=0;i<obj.length;i++)
      obj[i].style.color="rgb("+hex+","+hex+","+hex+")";
  }
}

/*----------------------------------------------------------------------------*/
function newImage(arg) {
  if (document.images) {
    rslt = new Image();
    rslt.src = arg;
    return rslt;
  }
}

/*----------------------------------------------------------------------------*/
function changeImages() {
  if (document.images && (preloadFlag == true)) {
    for (var i=0; i<changeImages.arguments.length; i+=2) {
      document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
    }
  }
}

/*----------------------------------------------------------------------------*/
function preloadImages() {
  if (document.images) {
    play_button_over = newImage("images/button_play-over.png");
    main_button_over = newImage("images/button_main-over.png");
    admin_button_over = newImage("images/button_admin-over.png");
    logout_button_over = newImage("images/button_logout-over.png");
    preloadFlag = true;
  }
}

/*----------------------------------------------------------------------------*/
function getCookie(nameOfCookie) {
  if (document.cookie.length <=0) {
    return null;
  }

  begin = document.cookie.indexOf(nameOfCookie+"=");

  if (begin == -1) {
    return null;
  }

  begin += nameOfCookie.length+1;
  end = document.cookie.indexOf(";", begin);

  if (end == -1) {
    end = document.cookie.length;
  }

  return unescape(document.cookie.substring(begin, end));
}

/*----------------------------------------------------------------------------*/
function setCookie(nameOfCookie, value, expireDays)
{
  var expireDate = new Date();
  expireDate.setTime(expireDate.getTime() + (expireDays * 24 * 3600 * 1000));
  document.cookie = 
    nameOfCookie + 
    "=" + 
    escape(value) + 
    ((expireDays == null) ? "" : "; expires="+ expireDate.toGMTString());
}

/*----------------------------------------------------------------------------*/
function delCookie(nameOfCookie) {
  if (getCookie(nameOfCookie)) {
    document.cookie = nameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
/*----------------------------------------------------------------------------*/
