var current = 'new';
var localbaseurl = '';
var mobilebaseurl = 'http://pitcamproxy.appspot.com'
var base = '';

function setimage() {
  var file = base+'/'+current+'.jpeg';
  var tmpdate = new Date();
  tmp = '?nocache=' + tmpdate.getTime();
  $('#webcamimage').attr('src', file+tmp);
}
function periodicupdate() {
  update();
  setTimeout("periodicupdate()", 15000);
}
function update() {
  $('select#webcam_selector').val(current);
  window.location.hash = "#"+current;
  setimage();
}
function oldCam()
{
current = 'old';
update();
}
function newCam()
{
current = 'new';
update();
}

function usemobilesize()
{
  base = mobilebaseurl;
  $('#webcamimage').attr('height', 240);
  $('#webcamimage').attr('width', 320);
  update();
}
function usefullsize()
{
  base = localbaseurl;
  $('#webcamimage').attr('height', 480);
  $('#webcamimage').attr('width', 640);
  update();
}

function camfromurl() {
  hash = window.location.hash;
  if (hash == "#wentworth") {
    return "wentworth";
  }
  else if (hash == "#old") {
    return "old";
  }
  else if (hash == "#new") {
    return "new";
  }
  else {
    return "";
  }
}

$(document).ready(function(){
  cfu = camfromurl();
  if (cfu.length > 0) {
    current = cfu;
  }
  else {
    current = $('select#webcam_selector').val();
  }
	$('select#webcam_selector').change(function() {
	  current = $(this).val();
    update();	
	});
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf('iphone') != -1) {
	  $("#mobilecheckbox").attr("checked", true);
  }
  else if (agent.indexOf('android') != -1) {
	  $("#mobilecheckbox").attr("checked", true);
  }
  else if (agent.indexOf('ipod') != -1) {
	  $("#mobilecheckbox").attr("checked", true);
  }
  else if (agent.indexOf('pre/1.0') != -1) {
	  $("#mobilecheckbox").attr("checked", true);
  }
	if ($('#mobilecheckbox').is(':checked')) {
    usemobilesize();
  }
  else
  {
    usefullsize();
  }
	$('#mobilecheckbox').click(function() {
	  if ($(this).is(':checked'))
    {
      usemobilesize();
    }
    else
    {
      usefullsize();
    }});
	periodicupdate();
});

