  var landscape = true;
  var timer;
  var settingsCookie;
  var id = 1;
	    
  function setup() {
    landscape = window.innerWidth < window.innerHeight;    
    setTimeout(function() {window.scrollTo(0,1)}, 1);
    $("Loading").style.visibility = "hidden";
    $("Countdown").style.display = "block";
    $("EntriesCount").value = 6;
    settingsCookie = new Cookie("BBallSettings");
    if (settingsCookie.get()) {
      try {
        var settings = eval("new Object({" + settingsCookie.get() + "})");
        $("EntriesCount").value = settings.entriesCount;
      }
      catch (e) {
        alert(e);
      }  
    }
    if (!window.predefinedCalendar) {
      $("Countdowns").innerHTML = "Loading schedule failed"
        +"<p><small>(Please make sure you have a connection to the internet)</small></p>";
    }
    else {
      loadCalendar("Golf", predefinedCalendar.feed);
    }  
  }
  
  function loadCalendar(title, url) {
    $("Countdowns").innerHTML = "";
    
    var count = parseInt($("EntriesCount").value);
    var time = new GoogleCalendar(url, true, "CalScript");
    for (var i = 0; i < count; i++) {
      var counter = document.createElement("div");
      counter.id = "Counter" + id;
      id++;
      counter.className = "countdown";
      var template = document.getElementById("CountdownTemplate").innerHTML;
      counter.innerHTML = template.replace(/\$ID\$/g, id);
      if (i < count - 1) {
        counter.innerHTML += "<hr/>"
      }
      $("Countdowns").appendChild(counter);
      counter.counter = new Countdown(time, counter, id, i);
      
    }
    time.requestCalendar();
    start();    	
  }  
  
  function retry() {
    location.reload(true);
  }  
  
  function reload() {
    $("Countdowns").innerHTML = "";
    loadCalendar("Football", predefinedCalendar.feed);    
  }  
  
  function updateAll() {
    var now = new Date();
    //console.log(now);
    var counters = $("Countdowns").childNodes;
    for (var i = 0; i < counters.length; i++) {
    	counters[i].counter.update(now);
    }    
  }
  
  function start() {
    pause();
    timer = window.setInterval(function() {
      updateAll();
    }, 1000);
    updateAll();
  }
  
  function pause() {
    if (timer) {
      window.clearInterval(timer);
    }
    timer = null;
  }
  
  function openLink() {
    window.open(this.link, "_blank");
  }
  
  function saveSettings() {
    var text = "entriesCount:" + $("EntriesCount").value;
    settingsCookie.store(text);
  }

  function tellFriend() {
    var body = "Hi,<br><br>I just stumbled upon this iPhone Golf Countdown application:" +
        "<br><br>http://golf.speedymarks.com<br><br>" +
        "countdown to the upcoming PGA Tour matches." +
        "<br><br>Best regards";
    window.open("mailto:?subject=Golf on the iPhone&body=" + body, "_self");  
  }

	function orientationChanged() {
    landscape = window.innerWidth < window.innerHeight;
    window.scrollTo(0,1);
  }
	
  function debug(msg) {
    var e = document.getElementById("Debug");
    e.innerHTML += msg + "<br>";
  }
  
  function $(id) {
    return document.getElementById(id);
  }