﻿
var params = { allowScriptAccess: "always", wmode: "transparent" };
var atts = { id: "myytplayer", wmode: "transparent" };

swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=myytplayer",
                     "ytapiplayer", "225", "135", "8", null, null, params, atts);

function onYouTubePlayerReady(playerId) {
    if (playerId == "myytplayer") {
        ytplayer = document.getElementById(playerId);
        ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
        load('zP5RB9HcsYw');
    }
}

function onytplayerStateChange(newState) {

    if (ytplayer.getPlayerState() == 1) {
        document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/pause.png";
    }
    else {
        document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/play.png";
    }
}

function pbover() {

    if (ytplayer.getPlayerState() == 1) {
        document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/pausemo.png";
    }
    else {
        document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/playmo.png";
    }
}

function pbout() {

    if (ytplayer.getPlayerState() == 1) {
        document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/pause.png";
    }
    else {
        document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/play.png";
    }
}

function load(id) {

    if (!isiPhone()) {

        if (typeof (ytplayer) != "undefined") {
            ytplayer.cueVideoById(id, 0);
            play();
        }
    }
    else {

        player = new YT.Player('ytapiplayer', {
            height: '135',
            width: '225',
            videoId: id,
            playerVars: { 'autoplay': 1, 'controls': 1 },
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            }
        });

    }

    var hf = document.getElementById('ctl00_ctl00_MainContentPlaceHolder1_BlockContentPlaceHolder1_hfpv');

    if (hf.value != null) {
        var pv = document.getElementById('video' + hf.value);

        if (pv != null) {
            pv.style.background = 'white';
        }
    }

    if(document.getElementById('video' + id) != null)
        document.getElementById('video' + id).style.background = '#cce0d3';

    document.getElementById('ctl00_ctl00_MainContentPlaceHolder1_BlockContentPlaceHolder1_hfpv').value = id;

    $(document).ready(function () {
        $('#ytseeker').slider('option', 'value', 0);
    });

    //document.imgplay.src = "http://www.monstervox.com/MasterPages/js/images/play.png";
}



function play() {

    if (ytplayer) {
        if (ytplayer.getPlayerState() == 1) {
            ytplayer.pauseVideo();
        }
        else {
            ytplayer.playVideo();
        }
    }
}

function stop() {
    if (ytplayer)
        ytplayer.stopVideo();

}

function seek(sec) {
    if (ytplayer)
        ytplayer.seekTo(sec, true);
}


function isiPhone() {

    return (
    //Detect iPhone
        (navigator.platform.indexOf("iPhone") != -1) ||
    //Detect iPod
        (navigator.platform.indexOf("iPod") != -1) ||
    //Detect iPad
        (navigator.userAgent.match(/iPad/i) != null)
    );
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {

}

// 5. The API calls this function when the player's state changes.
//    The function indicates that when playing a video (state=1),
//    the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
    
}


function createAPIPlayer() {

    // 2. This code loads the IFrame Player API code asynchronously.
    var tag = document.createElement('script');
    tag.src = "http://www.youtube.com/player_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    // 3. This function creates an <iframe> (and YouTube player)
    //    after the API code downloads.

}

var player;
function onYouTubePlayerAPIReady() {

    load('zP5RB9HcsYw');
}

$(document).ready(function () {

    if (isiPhone()) {
        //$("#player-container-large").css("background-image", "url('http://www.monstervox.com/MasterPages/js/images/player_bg_676_nocontrols.png')");
        //$("#player-controls").addClass("hideme");
        createAPIPlayer();
    }

    var isSliding = false;

    $("#ytseeker").slider({ min: 0, max: 1000, orientation: 'horizontal', animate: true, value: 0 });
    $("#ytseeker").bind('slide', function (event, ui) {


        seek(ui.value / 1000 * ytplayer.getDuration());
        isSliding = true;


    });

    $('#ytseeker').bind('slidechange', function (event, ui) {
        isSliding = false;
    });

    window.setInterval(function () {


        if (typeof (ytplayer) != "undefined") {
            if (!isSliding) {
                if (ytplayer.getDuration() > 0) {
                    $('#ytseeker').slider('option', 'value', 1000 * (ytplayer.getCurrentTime() / ytplayer.getDuration()));
                }
            }
        }

    }, 250);


});




