  var landscape = true;
  var data = null;
  var fromCookie = new Cookie("fromCurrency");
  var toCookie = new Cookie("toCurrency");
  var amountCookie = new Cookie("fromAmount");
	    
  function setup() {
    landscape = window.innerWidth < window.innerHeight;    
    setTimeout(function() {window.scrollTo(0,1)}, 1);
    fillSelect("FromSelect");
    fillSelect("ToSelect");
    var from = fromCookie.get();
    if (from) {
      $("FromSelect").value = from;
    }
    var to = toCookie.get();
    if (to) {
      try {
        var values = eval(to);
        var options = $("ToSelect").options;
        $("ToSelect").selectedIndex = -1;
        for (var i = 0; i < values.length; i++) {
      	  for (var j = 0; j < options.length; j++) {
      	  	if (options[j].value == values[i]) {
      	  	  options[j].selected = true;
      	  	  $("ListForm").style.display = "none";
      	  	  break;
      	  	}
      	  }
        }
      }
      catch (e) {
        console.log(e);
      }  
    }
    var amount = amountCookie.get();
    if (amount) {
      $("Amount").value = amount;
    }
    reload();
  }
  
  function initialize() {
    var value = $("FromSelect").value;
    if (value.length == 0) {
      return;
    }
    fromCookie.store(value);
    $("Loading").style.visibility = "visible";
    $("List").innerHTML = "";
    data = new Object();
    data.from = value;
    $("ListFrom").innerHTML = value + " <span class='listFromSmall'>(" + symbols[value] + ")</span>";
    var url = "http://currencysource.com/RSS/" + value;
    url += ".xml";
    var feed = new google.feeds.Feed(url);
    feed.setNumEntries(50);
    feed.load(function(result) {
      if (!result.error) {
        data.to = new Object();
        for (var i = 0; i < result.feed.entries.length; i++) {
          var entry = result.feed.entries[i];
          var line = parseDataLine(entry.title);
          data.to[line.symbol + ""] = line;
        }
        document.getElementById("Loading").style.visibility = "hidden";
        recreate();
      }
      else {
        console.log(result.error.message);
        document.getElementById("Loading").style.visibility = "hidden";
      }
    });
  }

  function reload() {
    initialize();
  }
  
  function recreate() {
    var container = $("List");
    container.innerHTML = "";
    var amount = parseFloat($("Amount").value);
    if (isNaN(amount) || amount <= 0) {
      amount = 1;
    }
    amountCookie.store(amount);
    var cookieValue = "[";
    var options = $("ToSelect").options;
    for (var i = 0; i < options.length; i++) {
    	if (options[i].selected) {
    	  if (cookieValue.length > 1) {
    	    cookieValue += ",";
    	  }
    	  cookieValue += "'" + options[i].value + "'";
    	  var value = data.to[options[i].value + ""];
    	  var li = document.createElement("li");
    	  container.appendChild(li);
    	  
    	  var toggle = document.createElement("img");
    	  toggle.className = "listToggle";
    	  toggle.src = "toggle.png";
    	  toggle.onclick = function() {
    	    toggleCalculator(this);
    	  }
    	  li.appendChild(toggle);
     	  
    	  var title = document.createElement("div");
    	  title.className = "listTitle";
    	  title.innerHTML = "<div class='listTitleName'>" + symbols[value.symbol] + "</div>"
    	                    + (amount * value.rate).toFixed(2) 
    	                    +  "&nbsp;" + value.symbol;
    	  title.onclick = function() {
    	    toggleCalculator(this);
    	  }
    	  li.appendChild(title);
     	  
    	  var subtitle = document.createElement("div");
    	  subtitle.className = "listSubtitle";
    	  subtitle.innerHTML = amount.toFixed(2) + "&nbsp;" + value.symbol
    	                    + "&nbsp;=&nbsp;" + (amount / value.rate).toFixed(2) 
    	                    + "&nbsp;" + data.from;
    	  li.appendChild(subtitle);
    	  
    	  var calculator = document.createElement("div");
    	  calculator.className = "listCalculator";
    	  var value1 = document.createElement("input");
    	  value1.name = "zip";
    	  value1.className = "listCalcInput";
    	  value1.value = amount.toFixed(2);
    	  value1.rate = value.rate;
    	  value1.onchange = function() {
    	    var amount = parseFloat(this.value);
    	    if (isNaN(amount)) {
    	      this.result.value = "0";
    	      return;
    	    }
    	    this.result.value = (amount * this.rate).toFixed(2);
    	  }
    	  calculator.appendChild(value1);
    	  calculator.appendChild(document.createTextNode(data.from + " = "));
    	  var value2 = document.createElement("input");
    	  value2.name = "zip";
    	  value2.className = "listCalcInput";
    	  value2.value = (amount * value.rate).toFixed(2);
    	  value2.rate = 1/value.rate;
    	  value2.onchange = function() {
    	    var amount = parseFloat(this.value);
    	    if (isNaN(amount)) {
    	      this.result.value = "0";
    	      return;
    	    }
    	    this.result.value = (amount * this.rate).toFixed(2);
    	  }
    	  calculator.appendChild(value2);
    	  calculator.appendChild(document.createTextNode(value.symbol));
    	  value1.result = value2;
    	  value2.result = value1;
    	  toggle.calculator = calculator;
    	  title.calculator = calculator;
    	  li.appendChild(calculator);
    	}
    }
    cookieValue += "]";
    toCookie.store(cookieValue);
  }
  
  function toggleCalculator(element) {
    if (element.calculator.style.display == "block") {
      element.calculator.style.display = "none";
    }
    else {
      element.calculator.style.display = "block";    	      
    }    
  }
  
  function parseDataLine(line) {
    var result = new Object();
    result.line = line;
    var index = line.indexOf("=");
    if (index > 0) {
      var symbol = line.substring(index + 2, index + 5);
      result.symbol = symbol;
    }
    index = line.indexOf("(");
    if (index > 0) {
      var end = line.indexOf(")", index);
      if (end > 0) {
        result.rate = parseFloat(line.substring(index + 1, end));
      }
    }
    return result;
  }
  
  function fillSelect(id) {
    for (var i in symbols) {
    	var option = document.createElement("option");
    	option.value = i;
    	option.innerHTML = symbols[i];
    	$(id).appendChild(option);
    }
  }
  
  function tellFriend() {
    var body = "Hi,<br><br>I just stumbled upon this iPhone Currencies Exchange application:" +
        "<br><br>http://rates.speedymarks.com<br><br>" +
        "Monitors the exchange rates for all major currencies." +
        "<br><br>Best regards";
    window.open("mailto:?subject=Currencies on the iPhone&body=" + body, "_self");  
  }

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

  var symbols = {
    "USD": "U.S. Dollar $",
    "EUR": "Euro €",
    "JPY": "Japanese Yen &yen;",
    "GBP": "U.K. Pound &pound;",
    "ARS": "Argentine Peso",
    "AUD": "Australian Dollar",
    "BHD": "Bahrain Dinar",
    "BRL": "Brazilian Real",
    "BND": "Brunei Dollar",
    "CAD": "Canadian Dollar",
    "CLP": "Chilean Peso",
    "CNY": "Chinese Yuan",
    "COP": "Colombian Peso",
    "DKK": "Danish Krone",
    "HUF": "Hungarian Forint",
    "ISK": "Icelandic Krona",
    "INR": "Indian Rupee",
    "IDR": "Indonesian Rupiah",
    "ILS": "Israeli New Shekel",
    "KRW": "Korean Won",
    "KWD": "Kuwaiti Dinar",
    "MXN": "Mexican Peso",
    "NZD": "New Zealand Dollar",
    "NOK": "Norwegian Krone",
    "PKR": "Pakistan Rupee",
    "SAR": "Saudi Arabian Riyal",
    "SGD": "Singapur Dollar",
    "ZAR": "South African Rand",
    "SEK": "Swedish Krona",
    "SHF": "Swiss Frank",
    "THB": "Thai Baht",
    
    "000": null
  }  