// (C)2008 ealandmania.net
// webmaster@ealandmania.net
// Not to be used without permission

var jobTimer;
var jobCookie;
var newJob;

function jobCountdown() {
  jobCookie = readCookie("jobCookie");
  if (jobCookie != null) {
    jobTime(jobCookie);
  } else {
    jobCookie = "Factory";
    createCookie("jobCookie",jobCookie,365);
    jobTime(jobCookie);
  }
}

function changeJob() {
  curJob = readCookie("jobCookie");
  if (curJob == "Factory") {
    newJob = "Diner";
  } else if (curJob == "Diner") {
    newJob = "Nightclub";
  } else if (curJob == "Nightclub") {
    newJob = "Factory";
  }
  createCookie("jobCookie",newJob,365);
  clearTimeout(jobTimer);
  jobTime(newJob);
}

function simTime() {
  var currenttime = new Date() ;
  var hours = currenttime.getUTCHours();
  var minutes = currenttime.getUTCMinutes();
  var seconds = currenttime.getUTCSeconds();
  var timesuffix = "AM";
  var cycle = 0;
  if (hours %2 ==1) {
    cycle = 3600;
    timesuffix = "PM";
  }
  cycle = cycle + minutes * 60 + seconds;
  var tsohours =  Math.floor(cycle /300);
  if (tsohours > 12) {
    tsohours = tsohours - 12;
  }
  if (tsohours ==0) {
    tsohours = 12;
  }
  var tsomins = Math.floor((cycle % 300) / 5);
  if (tsomins < 10) {
    tsomins = "0" + tsomins;
  }
  document.getElementById('simtime').innerHTML= tsohours + ":" + tsomins + " " + timesuffix ;

  setTimeout("simTime()",1000);
}

function jobTime(job) {
  var currenttime = new Date() ;
  var hours = currenttime.getUTCHours();
  var minutes = currenttime.getUTCMinutes();
  var seconds = currenttime.getUTCSeconds();
  var cycle = 0;
  var startTime = 0;
  var startTime2 = 0;
  var endTime = 0;
  var endTime2 = 0;
  if (hours %2 ==1) {
    cycle = 3600;
  }
  cycle = cycle + minutes * 60 + seconds;
  if (job == "Factory") {
    startTime = 2700; // 9am
  } else if (job == "Diner") {
    startTime = 3300; // 11am
  } else if (job == "Nightclub") {
    startTime = 6000; // 8pm
  }
  if (cycle > startTime) {
    startTime2 = startTime + 7200;
  } else {
    startTime2 = startTime;
  }
  var jobSecs = startTime2 - cycle;
  var jobMins = Math.floor(jobSecs / 60);
  var jobHours = Math.floor(jobMins / 60);
  jobMins = Math.floor(jobMins % 60);
  jobSecs = jobSecs % 60;
  if (jobSecs < 10) {
    jobSecs = "0" + jobSecs;
  }
  if (jobMins < 10) {
    jobMins = "0" + jobMins;
  }
  document.getElementById('jobposition').innerHTML = job + ' Job';
  document.getElementById('jobcountdown').innerHTML = "0" + jobHours + ":" + jobMins + ":" + jobSecs;

  // Job Countdown Clock Popup Window
  if (document.getElementById('countdowndetails')) {
    var message = "";
    var tip = "";
    greenTime = startTime - 600;
    carTime =  startTime - 300;
    endTime = startTime + 2400;
    if (endTime > 6900) {
      endTime2 = endTime - 7200;
    }
    if (cycle > greenTime && cycle < carTime) {
      message = "Prepare for Work";
      document.getElementById('jobmessage').style.color = "#F67F00";
    } else if (cycle >= carTime && cycle < startTime) {
      message = "Take Carpool / Call Taxi";
      document.getElementById('jobmessage').style.color = "#F67F00";
    } else if (cycle >= startTime && cycle < endTime) {
      message = "Open";
      document.getElementById('jobcountdown').innerHTML = "00:00:00";
      document.getElementById('jobmessage').style.color = "#19953F";
    } else if (startTime > endTime2) {
      if (cycle <= endTime2) {
        message = "Open";
        document.getElementById('jobcountdown').innerHTML = "00:00:00";
        document.getElementById('jobmessage').style.color = "#19953F";
      } else {
        message = "Closed";
        document.getElementById('jobmessage').style.color = "#951922";
      }
    } else {
      message = "Closed";
      document.getElementById('jobmessage').style.color = "#951922";
    }
    if (job == "Factory") {
      document.getElementById('jobimage').src = "/assets/images/gameguides/jobs_intro_factory.jpg";
      if (cycle < 900) {
        tip = "Remember, RIGHT hand RIGHT tool. A fish will always be held in the left hand.";
      } else if (cycle < 2100) {
         tip = "It only takes one robot per shift for the round to count towards your total shift count for promotion.";
      } else if (cycle < 3300) {
        tip = "When working alone activate machines in reverse order so there is always an empty machine for your robot to go.";
      } else if (cycle < 4500) {
        tip = "Don't activate a machine too soon or it will deactivate itself causing you to make an extra trip.";
      } else if (cycle < 5700) {
        tip = "In higher levels machines will fib, it may say it needs one tool but you will quickly find it needs two others. ";
      } else if (cycle < 6900) {
        tip = "The ignore button is there for a reason, if a player is causing problems use it.";
      } else {
        tip = "If you miss the carpool use a phone to call a Taxi.";
      }
    } else if (job == "Diner") {
      document.getElementById('jobimage').src = "/assets/images/gameguides/jobs_intro_rest.jpg";
      if (cycle < 900) {
        tip = "If you are working alone don't worry about giving customers social, unless you need it.";
      } else if (cycle < 2100) {
         tip = "Even if you are working alone yell out your orders so you can look back at them later.";
      } else if (cycle < 3300) {
        tip = "When working with a group it helps to use a numbering system for tables. Ex. C1..C4 for counter seats.";
      } else if (cycle < 4500) {
        tip = "Customers eat what is put in front of them even if they didn't order it, leaving you with a small tip.";
      } else if (cycle < 5700) {
        tip = "Busboys and chefs are prone to goofing off so keep them in line with a good scolding when the need arises.";
      } else if (cycle < 6900) {
        tip = "The ignore button is there for a reason, if a player is causing problems use it.";
      } else {
        tip = "If you miss the carpool use a phone to call a Taxi.";
      }
    } else if (job == "Nightclub") {
      document.getElementById('jobimage').src = "/assets/images/gameguides/jobs_intro_dj.jpg";
      if (cycle < 900) {
        tip = "Even if you don't have a big crowd, you are still gaining points for the time you have them grooving to your moves.";
      } else if (cycle < 2100) {
         tip = "Team up with a good DJ, and you will be raking in the dough without breaking a sweat. Real Sims are far more efficient than NPC's.";
      } else if (cycle < 3300) {
        tip = "Once you find a good beat stick with it, the NPCs will drift back and forth to you the entire round. ";
      } else if (cycle < 4500) {
        tip = "Pair up with a good dancer. The better you work together and most importantly please the crowd the better your payout will be.";
      } else if (cycle < 5700) {
        tip = "The floor, pay attention to the floor! The more of your color that is in the dance floor, the better you are doing.";
      } else if (cycle < 6900) {
        tip = "The ignore button is there for a reason, if a player is causing problems use it.";
      } else {
        tip = "If you miss the carpool use a phone to call a Taxi.";
      }
    }
    document.getElementById('jobtip').innerHTML = tip;
    document.getElementById('jobmessage').innerHTML = message;
    //document.getElementById('debug').innerHTML = "Cycle: " + cycle + " Start Time: " + startTime + " End Time: " + endTime + " Green Time: " + greenTime + " Car Time: " + carTime;
  }
  // End Popup Window

  eval(jobTimer = setTimeout("jobTime('" + job + "')",1000));
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function popWindow(url,name,width,height) {
  eval('window.open("' + url + '", "' + name + '", "width=' + width + ',height=' + height + ',location=0,resizable=0,scrollbars=0,status=0,toolbar=0")');
}

var xmlHttp;

function getData(page,query,ctl,indicator) {
  if (indicator != 'false'){
    loading('true')
  }
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    return;
  }
  var url = page + query;
  xmlHttp.onreadystatechange=function() { stateChanged(ctl); }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function stateChanged(ctl) {
  if (xmlHttp.readyState==4) {
    document.getElementById(ctl).innerHTML=xmlHttp.responseText;
    loading('false');
  }
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
    xmlHttp=new XMLHttpRequest();
  } catch (e) {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function loading(state) {
  if (state == 'true') {
    document.getElementById('loadingicon').style.visibility = "visible";
    document.getElementById('loadingicon').style.display = "inline";
  } else {
    document.getElementById('loadingicon').style.visibility = "hidden";
    document.getElementById('loadingicon').style.display = "none";
  }
}

var curfont = 11;
function increaseFont() {
  if (curfont <= 16) {
    curfont = curfont + 1;
    document.getElementById('article').style.fontSize = curfont + "px";
  }
}
function decreaseFont() {
  if (curfont >= 12) {
    curfont = curfont - 1;
    document.getElementById('article').style.fontSize = curfont + "px";
  }
}