﻿google.load("swfobject", "2.1");

var videoId = "WpxypfijhqM";
var videoItems = new Array();
var ytplayer = null;
var ready = false;

function isIDevice() {
    return (navigator.platform == "iPad" || navigator.platform == "iPhone")
}

$(function () {
    if (isIDevice()) {
        // 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);
    }
});
function onPlayerError(errorCode) {
    alert("An error occured of type:" + errorCode);
}


//javascript api flash
function onYouTubePlayerReady(playerId) {
    ytplayer = $("#ytPlayer")[0];
    ytplayer.addEventListener("onError", "onPlayerError");
    ytplayer.heigh = 700;

    if (ytplayer) {
        ready = true;
        ytplayer.loadVideoById(videoId, '0');
    }
}
//////////////////////////////////////////iPda or iPhone code///////////////////////////////
//iframe embed for iPhone and iPad
function onYouTubePlayerAPIReady() {
    ytplayer = new YT.Player('videoDiv', {
        height: '460',
        width: '670',
        videoId: videoId,
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
        }
    });
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
    event.target.playVideo();
}
// 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.

function onPlayerStateChange(event) {
}

function loadVideo(video) {
    videoId = video;
    try {
        setTimeout(function () {
            ytplayer.loadVideoById(videoId, 0, "large");
        }, 2000);
    }
    catch (error) {
    }
}

//////////////////////////////////////////iPda or iPhone code end///////////////////////////////

function setVideo(id) {
    $.each(videoItems, function (index, value) {
        if (value.VideoId == id) {
            $("#videoTitle").html(value.Title);
            $("#videoDescription").html(value.Description);
            $("#player").show();
            setVideoDetails(id);
        }
    });
}

//single = true if a single video
function setVideoDetails(id) {
    videoId = id;
    if (isIDevice()) {
        loadVideo(videoId);
    }
    else {
        ytplayer = $("#ytPlayer")[0];
        if (ytplayer && ready) {
            ytplayer.loadVideoById(id, '0');
        }
    }
}

function stop() {
    if (isIDevice()) {
    }
    else {
        ytplayer = $("#ytPlayer")[0];
    }
    try {
        if (ytplayer) {
            ytplayer.stopVideo();
        }
    }
    catch (er) {
    }
}

// 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: "ytPlayer", allowFullScreen: "true", wmode: "window" };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    var url = "http://www.youtube.com/e/" + videoId + "?showinfo=0&version=3&autohide=1&autoplay=1&rel=0" + "&enablejsapi=1&playerapiid=player1&theme=dark";
    swfobject.embedSWF(url, "videoDiv", "670", "460", "8", null, null, params, atts);
}

google.setOnLoadCallback(loadPlayer);
