﻿$(document).ready(function () {
    /*===================================================================================
    Get Video Id Passed In From URL
    ===================================================================================*/
    var VideoId = getVideoIdVar()["VideoId"];

    function getVideoIdVar() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
            vars[key] = value;
        });
        return vars;
    }

    if (VideoId) {

        var CleanVideoId = VideoId.replace("#", "");
        // alert(CleanVideoId);

        var vidId = $(this).attr("id");
        //console.log(vidId);
        $(function () {
            // read data from youtube
            $.getJSON('http://gdata.youtube.com/feeds/api/users/neverhidefilms/uploads/?q=' + CleanVideoId + '&alt=json-in-script&callback=?&max-results=1', function (data) {
                $.each(data.feed.entry, function (i, item) {
                    var updated = item.updated;
                    var title = item['title']['$t'];
                    var desc = item['media$group']['media$description']['$t'];
                    var url = item['media$group']['media$content'][0]['url'];
                    var thumb = item['media$group']['media$thumbnail'][0]['url'];
                    var numViews = item['yt$statistics']['viewCount'];
                    var rating = 2.5; //item['gd$rating']['average'];

                    //   swfobject.embedSWF(url + '&rel=1&border=0&fs=1&autoplay=0', 'heroHolder', '926', '470', '9.0.0', false, false, { allowfullscreen: 'true', wmode: 'transparent' });

                    $('#heroHolder').html(' <iframe title="YouTube video player" width="920" height="548" src="http://www.youtube.com/embed/' + CleanVideoId + '?wmode=transparent" frameborder="0" allowfullscreen="true"></iframe>');

                    //$('#videoTitle').html('<a href="' + url + '" target="_blank">' + TruncateText(title, 30) + '</a>');
                    //$('#title-bar').html(title);
                    Cufon.replace('#videoTitle', { hover: true });

                    var ra = (80 * rating) / 5;
                    var raWidth = Math.round(ra);
                    //$('#videoRealRating').css("width", raWidth);
                    //$('#videoInfo').html(numViews);
                    //$('#videoDesc').html(TruncateText(desc, 200));
                });
            });

            // read data from local xml
            $.ajax({
                type: "GET",
                url: "/_repository/_resources/_cutwater/_nhfilms/xml/nhfilms.xml",
                dataType: "xml",
                success: function (xml) {
                    var xml = $(xml);
                    var obj = xml.find("film[ id = '" + CleanVideoId + "' ]");

                    var child = obj.children();
                    for (var i = 0; i < child.length; i++) {
                        switch (child[i].nodeName) {
                            case 'title':
                                var title = child[i].childNodes[0].nodeValue;
                                $('#videoTitle').html('<a href="http://www.youtube.com/v/' + CleanVideoId + '" target="_blank">' + TruncateText(title, 30) + '</a>');
                                $('#title-bar').html(title);
                                break;
                            case 'desc':
                                var desc = child[i].childNodes[0].nodeValue;
                                $('#videoDesc').html(TruncateText(desc, 200));
                                break;
                        }
                    }
                }
            });
        });

        if (CleanVideoId == "-prfAENSh2k" || "hUt0RSU9qok") {
            $('#heroHolder').html(' <iframe title="YouTube video player" width="920" height="548" src="http://www.youtube.com/embed/' + CleanVideoId + '?wmode=transparent" frameborder="0" allowfullscreen="true"></iframe>');

            // $('#hero').html('<div id="heroHolder"><a target="_blank" href="http://www.youtube.com/watch?v=' + CleanVideoId + ?wmode=transparent'"><img src="/_repository/_resources/_cutwater/_nhfilms/images/error.jpg" alt="Play On YouTube" /></a></div>');
        }

        var nowPlaying = $('#ytList').find('.playing').parent().attr("id");
        $('#ytList').find('.playing').removeClass('playing').attr("src", '/_repository/_resources/_cutwater/_nhfilms/images/' + nowPlaying + '.jpg');
        $('#ytList a').each(function () {
            if ($(this).attr('id').match(CleanVideoId)) {
                $(this).children("img").attr("src", '/_repository/_resources/_cutwater/_nhfilms/images/' + CleanVideoId + '-playing.jpg').addClass("playing");
            }
        });



    } else {
        //EMBED CODE FROM GOOGLE IFRAME
         $('#heroHolder').html(' <iframe title="YouTube video player" width="920" height="548" src="http://www.youtube.com/embed/SIYbyb8xXTQ?wmode=transparent" frameborder="0" allowfullscreen="true"></iframe>');
        $(function () {

            $.getJSON('http://gdata.youtube.com/feeds/api/users/neverhidefilms/uploads/?q=SIYbyb8xXTQ&alt=json-in-script&callback=?&max-results=1', function (data) {
                $.each(data.feed.entry, function (i, item) {
                    var updated = item.updated;
                    var title = item['title']['$t'];
                    var desc = item['media$group']['media$description']['$t'];
                    var url = item['media$group']['media$content'][0]['url'];
                    var thumb = item['media$group']['media$thumbnail'][0]['url'];
                    var numViews = item['yt$statistics']['viewCount'];
                    var rating = 2.5; //item['gd$rating']['average'];

                    
                    //$('#videoTitle').html('<a href="' + url + '" target="_blank">' + title + '</a>');
                    //Cufon.replace('#videoTitle', { hover: true });
                    //$('#title-bar').html(title);
                    var ra = (80 * rating) / 5;
                    var raWidth = Math.round(ra);
                    //$('#videoRealRating').css("width", raWidth);
                    //$('#videoInfo').html(numViews);
                    //$('#videoDesc').html(TruncateText(desc, 200));
                });
               
                   
            });

            // read data from local xml
            $.ajax({
                type: "GET",
                url: "/_repository/_resources/_cutwater/_nhfilms/xml/nhfilms.xml",
                dataType: "xml",
                success: function (xml) {
                    var xml = $(xml);
                    var obj = xml.find("film[ id = 'SIYbyb8xXTQ' ]");

                    var child = obj.children();
                    for (var i = 0; i < child.length; i++) {
                        switch (child[i].nodeName) {
                            case 'title':
                                var title = child[i].childNodes[0].nodeValue;
                                $('#videoTitle').html('<a href="http://www.youtube.com/v/SIYbyb8xXTQ" target="_blank">' + TruncateText(title, 30) + '</a>');
                                $('#title-bar').html(title);
                                break;
                            case 'desc':
                                var desc = child[i].childNodes[0].nodeValue;
                                $('#videoDesc').html(TruncateText(desc, 200));
                                break;
                        }
                    }
                }
            });
        });
    }
    /*===================================================================================
    YouTube API Application
    ===================================================================================*/
    $('.thumbContainer a img').hover(function () {
        var parentId = $(this).parent().attr("id");
        $(this).attr("src", '/_repository/_resources/_cutwater/_nhfilms/images/' + parentId + '-playing.jpg');

    }, function () {
        var parentId = $(this).parent().attr("id");
        if (!($(this).hasClass("playing"))) {
            $(this).attr("src", '/_repository/_resources/_cutwater/_nhfilms/images/' + parentId + '.jpg');
        };
    });

    function TruncateText(text, len) {

        //default value for length to truncate
        if (!len) { len = 50; }

        if (text) {
            var trunc = text;
            if (trunc.length > len) {
                trunc = trunc.substring(0, len);
                trunc = trunc.replace(/\w+$/, '');
                trunc += '...';
            }
        }

        return trunc;
    }
    $('.thumbContainer a').click(function (event) {
        $('.ytThumbs').data('jsp').scrollElementToCenter(this);

        var vidId = $(this).attr("id");
        var urlCut = String(window.location).split("?VideoId=")[0];
        location.href = urlCut + '?VideoId=' + vidId;
    });

    $('.ytThumbs').jScrollPane({
        showArrows: false,
        horizontalDragMinWidth: 54,
        horizontalDragMaxWidth: 54,
        animateScroll: true
    });

    // scroll to selected item
    $('.ytThumbs').data('jsp').scrollElementToCenter($('#ytList').find('.playing').parent());

    /*===================================================================================
    Hot Spot Div
    ===================================================================================*/
    $("div.clickDiv").click(function () {
        window.location = $(this).attr("url");
        return false;
    });

    // poster slideshow
    $('.poster-slideshow').cycle({
        fx: 'fade',
        speed: 2500
    });
});
