/* 
 This file was generated by Dashcode.  
 You may edit this file to customize your widget or web page 
 according to the license.txt file included in the project.
 */

//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//

function load() {
    dashcode.setupParts();
    hideCam()
    stackLayout = document.getElementById('stackLayout').object; // Replace with id of StackLayout
    camURL = "http://sccplugins.sheffield.gov.uk/utc/Images/";
    checkCamNo();
    camtoDisplay = camURL + camNo;
    document.cam.src=camtoDisplay;
// Values you provide
    var popupToChange = document.getElementById("CamPopup");	// replace with ID of popup
    var newPopupValue = camNo;								// string with option name to select
// Popup code
    popupToChange.value = newPopupValue;
    timetoRefresh = 30 // interval in seconds
    StartRefresh();
    showCam();
}

//Check for CamNo
function checkCamNo() {
    camNo = get_cookie("SheffCam");
    if (null == camNo)
        defaultCam();
    else
        camNo = get_cookie("SheffCam");
}
//End

//Set Default Cam if no cookie
function defaultCam() {
    camNo = "Cam01.jpg"
    set_cookie ( "SheffCam", camNo, 2027, 06, 27  );
}
//End

//Change cam via popup menu
function changeCam() {
// Values you provide
    var popupValue = document.getElementById("CamPopup");	// replace with ID of popup
    hideCam();
// Popup code
    popupValue = popupValue.value;
    popupName = popupValue;
    camtoDisplay = camURL + popupValue;
    document.cam.src=camtoDisplay;
    set_cookie ( "SheffCam", popupValue, 2027, 06, 27  );
    showCam();
    StartRefresh()
}
//End

//Web links
function goToSheffWeb(event) {
    document.location = "http://www.sheffield.gov.uk/roads-and-transport/travel-information/traffic-cameras/camera-list"
}

function goToApt(event) {
    document.location = "http://www.aptportal.co.uk"
}

function donate(event) {
    document.location = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6187552"
}
//End

// Get/Set Cookie function
function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ){
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}
//End

//Switch Views
function view1() {
    stackLayout.setCurrentView('view1'); // Replace with id of new view (or the view element itself)
}

function view2() {
    stackLayout.setCurrentView('view2'); // Replace with id of new view (or the view element itself)
}
//End

// Show/Hide Activity Indicator
function showActivity(event) {
    document.getElementById('activityIndicator').style.display = 'block';
}

function hideActivity(event) {
    document.getElementById('activityIndicator').style.display = 'none';
}
// End

//Show/Hide Cam
function showCam(event) {
    document.getElementById('cam').style.display = 'block';
}

function hideCam(event) {
    document.getElementById('cam').style.display = 'none';
}
// End

//Refresh Cam function
function StartRefresh() {
    tmp = new Date();
    tmp = "?"+tmp.getTime()
    document.images["refresh"].src = camtoDisplay+tmp
    setTimeout("StartRefresh()", timetoRefresh*1000)
}
//End