function showCamera(cameraObj) {
    camera_id = cameraObj.attributes.fid;
    if(camera_display_mode == "quick") {
        var cur_camera = LIVE_CAMERAS[camera_id];
        Element.show("result-div");
        $("cam-img").src = "/cameras/images/"+ camera_id +".jpg";
        $("cam-name").innerHTML = cur_camera.label.toUpperCase();
        if(cur_camera.location != 'undefined' && cur_camera.location != null) {
            $("cam-locn").innerHTML = cur_camera.location.toUpperCase(); 
        } 
        else { 
            $("cam-locn").innerHTML = ""; 
        } 
    }
    else {
        request('/camera/show?id='+camera_id);
    }
}

function camera_not_found() {
    if(camera_auto_rotate) {
        autorotate_camera();	
    }
    else {
        $('cam-img').src = "/images/no-recent-image.jpg";
    }
}

function set_camera_meta_data() {   
    current_live_cam_info = LIVE_CAMERAS[camera_id];
    if( current_live_cam_info ) {
        cam_name = current_live_cam_info.label.toUpperCase();
        /* Add the video link next to the camera name only if the video is available for this location */
        if(current_live_cam_info.video_available) {
            cam_name += "&nbsp;<img src='/images/cam.gif' width='16px' height='8px' style='border:0;'>&nbsp;<a href='javascript:void(null);' onClick='javascript:stop_camera_auto_rotate();showVideo("+current_live_cam_info.id+");' style='font-size:8px;letter-spacing:0.1cm;'>VIDEO</a>";
        }
        if($('cam-name')) { $('cam-name').innerHTML = cam_name; }
        if($('cam-locn')) { 
            $('cam-locn').innerHTML = (current_live_cam_info.location == null)? " " : current_live_cam_info.location.toUpperCase(); 
            //highlightMarker(current_live_cam_info.lon, current_live_cam_info.lat, current_live_cam_info.label, "cameras");
        }
    }
}

function autorotate_camera() {
    if(camera_auto_rotate) {
        camera_id += 1;
        Element.show('live_cam_loading');
        current_live_cam_info = LIVE_CAMERAS[camera_id];
        if(current_live_cam_info == undefined) {
            camera_auto_rotate = false;
            camera_id = 1;
            $('cam-img').src = "/images/recycle-cameras.gif";
            $('cam-name').innerHTML = "CAMERA CYCLE COMPLETE ";
            $('cam-locn').innerHTML = "<a href='javascript:void(null);' onClick='javascript:start_camera_auto_rotate();autorotate_camera();'>SEE CAMERAS AGAIN</a>";
            return;
        }
        $('cam-img').src = "/cameras/images/" + camera_id + ".jpg?" + (new Date()).getTime();        
    }
}

function camera_on_load() {
    set_camera_meta_data();
    Element.hide("live_cam_loading");
    src = $("cam-img").src.substring($("cam-img").src.lastIndexOf("/")+1, $("cam-img").src.length);
    if($("video_links") && src != "no-recent-image.jpg") {
        $("video_links").show();
    }
}

function camera_on_error() {
    camera_not_found();
    Element.hide("live_cam_loading");
}

function refreshVideo() {
    if($("live-camera")) {
        Element.show('live_cam_loading');
        $('cam-img').src = "/cameras/videos/" + video_id + ".gif?" + (new Date()).getTime();
    }
}

function showVideo(loc_id) {
    Element.show('live_cam_loading');
    stop_camera_auto_rotate();
    video_id = loc_id;
    url = "/cameras/videos/" + video_id + ".gif";
    cam_name = current_live_cam_info.label.toUpperCase();
    cam_name += "<span id='video_links' style='display:none;'>&nbsp;<img src='/images/cam.gif' width='16px' height='8px' style='border:0;'>&nbsp;<a href='javascript:void(null);' style='font-size:8px;letter-spacing:0.1cm;'>VIDEO</a>";
    cam_name += "&nbsp;<a href='javascript:void(null);' onClick='javascript:playVideo();' style='font-size:8px;letter-spacing:0.1cm;'>PLAY</a>";
    cam_name += "&nbsp;<a href='javascript:void(null);' onClick='javascript:refreshVideo();' style='font-size:8px;letter-spacing:0.1cm;'>RELOAD</a></span>";
    $("cam-img").src = url;
    $("cam-name").innerHTML = cam_name;
}

function playVideo() {
    url = "/cameras/videos/" + video_id + ".gif";
    $("cam-img").src = url;
}


function refreshCamera() {
    if($("live-camera") && camera_id != undefined) {
        Element.show('live_cam_loading');
        $('cam-img').src = "/cameras/images/" + camera_id + ".jpg?" + (new Date()).getTime();
    }
}

function stop_camera_auto_rotate() {
    camera_auto_rotate = false;
}

function start_camera_auto_rotate() {
    camera_auto_rotate = true;
}

var camera_auto_rotate = true;
var camera_id;
var video_id;
