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) {
    updateHTML("playerState", newState);
}

// Display information about the current state of the player
function updatePlayerInfo() {
// Also check that at least one function exists since when IE unloads the
// page, it will destroy the SWF before clearing the interval.
if(ytplayer && ytplayer.getDuration) {
  updateHTML("videoDuration", ytplayer.getDuration());
  updateHTML("videoCurrentTime", ytplayer.getCurrentTime());
  updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
  updateHTML("startBytes", ytplayer.getVideoStartBytes());
  updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
  updateHTML("volume", ytplayer.getVolume());
}
}

// Allow the user to set the volume from 0-100
function setVideoVolume() {
    var volume = parseInt(document.getElementById("volumeSetting").value);
    if(isNaN(volume) || volume < 0 || volume > 100) {
      alert("Please enter a valid volume between 0 and 100.");
    }
    else if(ytplayer){
      ytplayer.setVolume(volume);
    }
}

function playVideo() {
if (ytplayer) {
  ytplayer.playVideo();
}
}

function pauseVideo() {
if (ytplayer) {
  ytplayer.pauseVideo();
}
}

function muteVideo() {
if(ytplayer) {
  ytplayer.mute();
}
}

function unMuteVideo() {
    if(ytplayer) {
      ytplayer.unMute();
    }
}
function timeCheck(){
    if(ytplayer && ytplayer.getDuration) {
        if(ytplayer.getCurrentTime() == ytplayer.getDuration()){
            //backHtml = $(".mainvp1a").html();
            $(".mainvp1a").empty();
            $(".mainvp1").removeClass("mainvp1_open").addClass("mainvp1_close");
            $("#mainvideopopup_wrapper").removeClass("vpopup_on").addClass("vpopup_off");
            $("#vpop_bt").removeClass("close").addClass("open");
            draw_map();
            //switcher = 1;
            
        }
    }
}
function onYouTubePlayerReady(playerId) {
//var duration;
//var currentTime;
videoID = $(".mainvp1a").attr('rel');
//alert(videoID);
//ytplayer.cueVideoById(videoID);
ytplayer = document.getElementById("ytPlayer");
setInterval(timeCheck,250);
//setInterval(updatePlayerInfo, 250);
//updatePlayerInfo();
ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
ytplayer.addEventListener("onError", "onPlayerError");


}

function loadPlayer() {
    var videoID = $(".mainvp1a").attr('rel');
    var params = { allowScriptAccess: "always" };
    var atts = { id: "ytPlayer" };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
                       "?enablejsapi=1&playerapiid=player1&autoplay=1&hd=1",
                       "ytVideo", "480", "295", "8", null, null, params, atts);
}
function _run() {
    loadPlayer();
}
google.setOnLoadCallback(_run);
