var cVideo = 0;


function searchVideos()
{
    if($("#search_term4").val() == "")
    {
        alert("Please enter a search term");
    }
    else
    {
        window.location = ("/video/search."+escape($("#search_term4").val())+"");
    }
}

function moveNext()
{
    x = 0;
    for(z in oVideos)
    {
        x++;
    }

    cVideo = cVideo ? cVideo : cVideo+1;
    if(cVideo == x)
    {
        cVideo = 0;
    }

    var nextPic = oVideos[cVideo].tmb300;

    $('#sendLike')[0].onclick = function(){
        sendLike(oVideos[cVideo].video_id);
    };
    $('#sendDislike')[0].onclick = function(){
        sendDislike(oVideos[cVideo].video_id);
    };

    $.get("/scripts/ajax.php",( {
        getMethod: 'checkVideoStatus',
        videoID: oVideos[cVideo].video_id
    }  ),
    function(databack)
    {
        if(databack == "out")
        {
            $("#save_for_later").empty().html("<a href='javascript:void(0);' onclick='saveVideo("+oVideos[cVideo].video_id+");'>Save for Later</a>");
        }
        else
        {
            $("#save_for_later").empty().html("<a href='javascript:void(0);' onclick='removeVideo("+oVideos[cVideo].video_id+");'>Remove from Saved</a>");
        }

        $("#videoPreview").fadeOut("slow",function()
        {
            $(this).empty().html("<a href='"+oVideos[cVideo].tmb485+"' title='"+oVideos[cVideo].caption+"' class='thickbox'><img src='"+nextPic+"' border='0' /></a>");
            tb_init('a.thickbox, area.thickbox, input.thickbox');
            $("#hidden_video_id").empty().html(oVideos[cVideo].video_id);
            $("#big_caption").empty().html(oVideos[cVideo].caption);
            $("#video_credit").empty().html(oVideos[cVideo].credit);
            $("#video_tags").empty().html(oVideos[cVideo].tags);
            cVideo++;
        }).fadeIn("slow");

    });
}


function saveVideo(nVideoID)
{
    $.get("/scripts/ajax.php", ({
        getMethod: 'saveVideo',
        nVideo: nVideoID
    }),
    function(databack)
    {
        $("#save_for_later").empty().html("<a href='javascript:void(0);' onclick='removeVideo("+nVideoID+");'>Remove from Saved</a>");
    });
}

function removeVideo(nVideoID)
{
    $.get("/scripts/ajax.php", ({
        getMethod: 'removeVideo',
        nVideo: nVideoID
    }),
    function(databack)
    {
        $("#save_for_later").empty().html("<a href='javascript:void(0);' onclick='saveVideo("+nVideoID+");'>Save for Later</a>");
    });
}





var flashVideoPlayer;
var statusCount = 1;

function getPlayer() {
    var ie = navigator.appName.indexOf("Microsoft") != -1;
    flashVideoPlayer = (ie) ? window['myContent'] : document['myContent'];
}

//----------------------------- called by flash object

function updateState(message) {
    //var controls = document.getElementById('controls').rows[0].cells;
    //controls[1].innerHTML=message;
}

function updateSource(message) {
    //var controls = document.getElementById('controls').rows[4].cells;
    //controls//[1].innerHTML=message;
}

function updateTimer(message) {
    //var controls = document.getElementById('controls').rows[1].cells;
    //controls[1].innerHTML=message;
}

function updateDimensions(message) {
    // gotcha: getting metadata only works after the metadata is loaded. here flash checks it, but js can just as easily ask flash to check it
    //var controls = document.getElementById('controls').rows[2].cells;
    //controls[1].innerHTML=message;
}

//------------------------------- called by js

function callFlashPlayPauseVideo() {
    flashVideoPlayer.pauseResume();
}

function playAtTime() {
    time = document.getElementById('time').value;
    flashVideoPlayer.playAtTime(time);
    return false;
}

function playVideo(nVideoID)
{
	$('#noFlash').empty().html('<img src="'+oVideos[nVideoID].tmb300+'" width="312" height="230" />');
    $('#big_caption').html(oVideos[nVideoID]['title']);
    $('#photo_credit').html(oVideos[nVideoID]['credit']+"<br /><br />"+oVideos[nVideoID]['tags']+"<br /><br />"+oVideos[nVideoID]['listings']);
    $('#photoTagList').magic_drop({readyName: 'Related Topics', linkPathA:'/', width: '200px'});
    $('#photoListingsList').magic_drop({readyName: 'While you are here, visit...', linkPathA:'/listings/view.profile/id.', width: '200px'});
    $('#rating_element2').html(oVideos[nVideoID]['rating']);
    $('.star').removeClass('star_on');
    getPlayer();
    flashVideoPlayer.playVideo(oVideos[nVideoID]['video_file']);   
    $('#hidden_video_id').html(oVideos[nVideoID]['video_id']);
    $('#fb_like').empty().html('<iframe scrolling="no" frameborder="0" allowtransparency="true" style="border:medium none; overflow:hidden; width:320px; height:26px;" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.travelok.com%2Fvideo%2Fvideo_id.'+oVideos[nVideoID]['video_id']+'&amp;layout=standard&amp;show_faces=false&amp;width=442&amp;height=26&amp;action=like&amp;colorscheme=dark"></iframe>');
    
    $.get("/scripts/ajax.php",( {
        getMethod: 'checkVideoStatus',
        videoID: oVideos[nVideoID]['video_id']
    }),

    function(databack)
    {
        if(databack == "out")
        {
            $("#save_for_later").empty().html("<a href='javascript:void(0);' onclick='saveVideo("+nVideoID+");'>Save for Later</a>");
        }
        else
        {
            $("#save_for_later").empty().html("<a href='javascript:void(0);' onclick='removeVideo("+nVideoID+");'>Remove from Saved</a>");
        }
    });
}
