/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var mute_on = false;
var current_video = 0;

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
    //alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
    if (newState == 0) {
        nextVideo();
    }
}

function changeVideo(video_id) {
    if (video_list[video_id]) {

        $("#video_image_" + current_video).addClass('video_notselected');
        $("#video_image_" + current_video).removeClass('video_selected');
        current_video = video_id;
        $("#video_image_" + current_video).addClass('video_selected');
        $("#video_image_" + current_video).removeClass('video_notselected');

        $("#video_title").html(video_list[video_id]['name']);
        mimiPlayer.loadVideoById(video_list[video_id]['code']);
    }
}

function nextVideo() {
    var found_current = false;
    for (var id in video_list) {
        if (found_current == true) {
            changeVideo(id);
            return;
        }
        if (id == current_video) {
            found_current = true;
        }
    }

    //nothing was found - maybe we are at the end - run first video
    for (id in video_list) {
        changeVideo(id);
        return;
    }
}

function prevVideo() {
    var prev_id = 0;
    var id = 0;
    for (id in video_list) {
        if (id == current_video && prev_id != 0) {
            changeVideo(prev_id);
            return;
        }
        prev_id = id;
    }

    //nothing was found - maybe we are at the beginning - run last video
    for (id in video_list) {
        // just get to the last one
    }
    changeVideo(id);
    return;
}

// Display information about the current state of the player
function updatePlayerInfo() {
    if(mimiPlayer && mimiPlayer.getDuration) {
        var progress = Math.ceil(mimiPlayer.getCurrentTime() * 100 / mimiPlayer.getDuration());
        $("#video_progress").progressbar('option', 'value', progress);
    }
}

// Allow the user to set the volume from 0-100
function setVideoVolume(volume) {
    if(mimiPlayer){
        mimiPlayer.setVolume(volume);
    }
}

function playVideo() {
    if (mimiPlayer) {
        mimiPlayer.playVideo();
    }
}

function pauseVideo() {
    if (mimiPlayer) {
        mimiPlayer.pauseVideo();
    }
}

function toggleMute() {
    if (mute_on == true) {
        unMuteVideo();
    } else {
        muteVideo();
    }
}

function muteVideo() {
    if(mimiPlayer) {
        mimiPlayer.mute();
        mute_on = true;
        $("#image_volume").attr('src', '/images/player_volume_off.gif');
    }
}

function unMuteVideo() {
    if(mimiPlayer) {
        mimiPlayer.unMute();
        mute_on = false;
        $("#image_volume").attr('src', '/images/player_volume.gif');
    }
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
  // Lets Flash from another domain call JavaScript
  var params = { allowScriptAccess: "always" };
  // The element id of the Flash embed
  var atts = { id: "mimiPlayer" };
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1&rel=0&autoplay=1&iv_load_policy=3",
                     "videoDiv", "640", "505", "8", null, null, params, atts);

}

function _run() {
    loadPlayer();
}

