﻿/*
var pageTracker1 = _gat._getTracker("UA-4946594-1");
pageTracker1._initData();
pageTracker1._setDomainName(".responsiblesports.com");
//pageTracker1._trackPageview();

var pageTracker2 = _gat._getTracker("UA-4946594-4");
pageTracker2._setDomainName(".responsiblesports.net");
//pageTracker2._trackPageview();
*/
var currentVideoTitle = '';
currentVideoLink = '';
var vids = new Object();

var FEED_BATCH_SIZE = 40;
var entries = [];

function trimTitle(title) {
    if (title.length > 21) {
        if (title.substring(0, 21) == "Responsible Sports - ") {
            title = title.substring(20);
        }
    }
    return title;
}

/*
function trackHit(url) {
    url = url.replace('$currentVideoTitle$', currentVideoTitle);
    url = url.replace('$pageUrl$', location.href.replace('http://', '').replace('https://', ''));
    pageTracker1._trackPageview(url);
    pageTracker2._trackPageview(url);
}
*/

function formatVideoTitle(title) {
    return title.replace(/[^a-zA-Z0-9]+/g, '');
}

function loadVideo(playerUrl, autoplay, title, description, commentsLink) {
    $('#videoTitle').text(title);
    $('#videoSummary').text(description);
    swfobject.embedSWF(
                playerUrl + '&rel=1&border=0&fs=1&autoplay=' +
                (autoplay ? 1 : 0), 'player', '376', '226', '9.0.0', false,
                false, { allowfullscreen: 'true',wmode: 'opaque' });
    // Populate the Link textbox
    $('#txtLink').val(baseUrl + '?vid=' + escape(playerUrl));
    currentVideoTitle = formatVideoTitle(title);
    currentVideoLink = baseUrl + '?vid=' + escape(playerUrl);
    resetForms();
    //trackHit('/Media/Template/PageUrl/$pageUrl$/Video/' + formatVideoTitle(title));
}

function loadVideoById(id) {
    var entry = vids[id];
    var title = trimTitle(entry.title.$t);
    var description = entry.media$group.media$description.$t;
    if (description.length > 27) {
        description = description.substring(26);
    } else {
        description = "";
    }
    var commentsUrl = entry.gd$comments.gd$feedLink.href;
    var playerUrl = entry.media$group.media$content[0].url.replace('?', '&');
    loadVideo(playerUrl, true, title, description, commentsUrl);
}

function showMyVideos(data) {
    var feed = data.feed;
    var newEntries = feed.entry || [];
    //alert("adding " + newEntries.length + " new entries");
    entries = entries.concat(newEntries);

    if (newEntries.length == FEED_BATCH_SIZE) {
        GetResultsBatch();
    }
    else {
        //alert("have entire list, beginning parse");


        var playerUrl = '';
        var defaultVideoIndex = 0;
        var rv1 = 1;
        var rv2 = 2;
        var rv3 = 3;
        var msg = "default: " + defaultVideo.replace("?", "&").split("&")[0] + "\n" +
                  "related1: " + relatedVideo1.replace("?", "&").split("&")[0] + "\n" +
                  "related2: " + relatedVideo2.replace("?", "&").split("&")[0] + "\n" +
                  "related3: " + relatedVideo3.replace("?", "&").split("&")[0] + "\n\n";
        for (var i = 0; i < entries.length; i++) {
            msg = msg + playerUrl + "\n";
            playerUrl = entries[i].media$group.media$content[0].url.replace('?', '&').split("&")[0];
            if (playerUrl == defaultVideo.replace("?", "&").split("&")[0]) { defaultVideoIndex = i; }

            if (playerUrl == relatedVideo1.replace("?", "&").split("&")[0]) { rv1 = i; }
            if (playerUrl == relatedVideo2.replace("?", "&").split("&")[0]) { rv2 = i; }
            if (playerUrl == relatedVideo3.replace("?", "&").split("&")[0]) { rv3 = i; }
        }
        //alert(msg);

        // Load info for related video 1
        var entry = entries[rv1];
        playerUrl = entry.media$group.media$content[0].url.replace('?', '&');
        var title = trimTitle(entry.title.$t);
        var description = entry.media$group.media$description.$t;
        if (description.length > 27) {
            description = description.substring(26);
        } else {
            description = "";
        }

        var commentsUrl = entry.gd$comments.gd$feedLink.href;
        var durationS = entry.media$group.yt$duration.seconds;
        var duration = (Math.floor(durationS / 60)) + ':' + (((durationS % 60) < 10) ? '0' : '') + (durationS % 60);
        var thumbnailUrl = entry.media$group.media$thumbnail[0].url;
        vids[formatVideoTitle(title)] = entry;

        var v = $('<div id="' + formatVideoTitle(title) + '" class="video">');
        v.append(
                    '<div class="resultWrapper">' +
                    '<div class="videoAvatar"><img src="' + thumbnailUrl + '" width="90" /></div>' +
                    '<div class="title">' + title + '</div>' +
                    '<div class="description">' + description + '</div>' +
                    '<div class="duration">' + duration + '</div>' +
                    '</div>');
        //v.click(function() { trackHit('/Media/Template/PageUrl/$pageUrl$/Video/$currentVideoTitle$/ClickClip/' + formatVideoTitle(title)); });
        v.click(function() { loadVideoById($(this).attr('id')) });
        $('#videoList').append(v);

        // Load info for related video 2
        entry = entries[rv2];
        playerUrl = entry.media$group.media$content[0].url.replace('?', '&');
        title = trimTitle(entry.title.$t);
        description = entry.media$group.media$description.$t;
        if (description.length > 27) {
            description = description.substring(26);
        } else {
            description = "";
        }
        commentsUrl = entry.gd$comments.gd$feedLink.href;
        durationS = entry.media$group.yt$duration.seconds;
        duration = (Math.floor(durationS / 60)) + ':' + (((durationS % 60) < 10) ? '0' : '') + (durationS % 60);
        thumbnailUrl = entry.media$group.media$thumbnail[0].url;
        vids[formatVideoTitle(title)] = entry;

        v = $('<div id="' + formatVideoTitle(title) + '" class="video">');
        v.append(
                    '<div class="resultWrapper">' +
                    '<div class="videoAvatar"><img src="' + thumbnailUrl + '" width="90" /></div>' +
                    '<div class="title">' + title + '</div>' +
                    '<div class="description">' + description + '</div>' +
                    '<div class="duration">' + duration + '</div>' +
                    '</div>');
        //v.click(function() { trackHit('/Media/Template/PageUrl/$pageUrl$/Video/$currentVideoTitle$/ClickClip/' + formatVideoTitle(title)); });
        v.click(function() { loadVideoById($(this).attr('id')) });
        $('#videoList').append(v);

        // Load info for related video 3
        entry = entries[rv3];
        playerUrl = entry.media$group.media$content[0].url.replace('?', '&');
        title = trimTitle(entry.title.$t);
        description = entry.media$group.media$description.$t;
        if (description.length > 27) {
            description = description.substring(26);
        } else {
            description = "";
        }
        commentsUrl = entry.gd$comments.gd$feedLink.href;
        durationS = entry.media$group.yt$duration.seconds;
        duration = (Math.floor(durationS / 60)) + ':' + (((durationS % 60) < 10) ? '0' : '') + (durationS % 60);
        thumbnailUrl = entry.media$group.media$thumbnail[0].url;
        vids[formatVideoTitle(title)] = entry;

        v = $('<div id="' + formatVideoTitle(title) + '" class="video">');
        v.append(
                    '<div class="resultWrapper">' +
                    '<div class="videoAvatar"><img src="' + thumbnailUrl + '" width="90" /></div>' +
                    '<div class="title">' + title + '</div>' +
                    '<div class="description">' + description + '</div>' +
                    '<div class="duration">' + duration + '</div>' +
                    '</div>');
        //v.click(function() { trackHit('/Media/Template/PageUrl/$pageUrl$/Video/$currentVideoTitle$/ClickClip/' + formatVideoTitle(title)); });
        v.click(function() { loadVideoById($(this).attr('id')) });
        $('#videoList').append(v);
        $('#videoList').append('<br class="clear"/>');

        var defaultDescription = entries[defaultVideoIndex].media$group.media$description.$t.replace('&#39;', "'");
        if (defaultDescription.length > 27) {
            defaultDescription = defaultDescription.substring(26);
        } else {
            defaultDescription = "";
        }

        loadVideo(
                    entries[defaultVideoIndex].media$group.media$content[0].url.replace('?', '&'),
                    false,
                    trimTitle(entries[defaultVideoIndex].title.$t.replace('&#39;', "'")),
                    defaultDescription,
                    entries[defaultVideoIndex].gd$comments.gd$feedLink.href);
    }
}

function tabClicked(tab) {
    //trackHit('/Media/Template/PageUrl/$pageUrl$/Video/$currentVideoTitle$/ClickTab/' + tab);
}

var activePanel = '';
var activeTab = '';
var animationTime = 500;
function togglePanel(tab, panel) {
    if (activePanel == '') {
        activeTab = tab;
        activePanel = panel;
        showActivePanel();
    }
    else if (panel == activePanel) {
        $('#' + activeTab).removeClass('active');
        $('#' + activePanel).slideUp(animationTime);
        activeTab = '';
        activePanel = '';
    }
    else {
        $('#' + activeTab).removeClass('active');
        $('#' + activePanel).slideUp(animationTime, showActivePanel);
        activeTab = tab;
        activePanel = panel;
    }
}
function showActivePanel() {
    tabClicked(activeTab.substring(3));
    $('#' + activeTab).addClass('active');
    $('#' + activePanel).slideDown(animationTime);
}

function shareSubmit() {
    if ($('#btnShareSubmit').val() == 'Sending') {
        return false;
    }
    $('#btnShareSubmit').val('Sending...');
    var url = "/common/ashx/video_forms_request_handler.ashx?f=1&fe=$fromEmail$&fn=$fromName$&vu=$videoUrl$&1n=$f1Name$&1e=$f1Email$&2n=$f2Name$&2e=$f2Email$&vt=$videoTitle$";
    url = url.replace('$fromEmail$', $('#txtYourEmail').val());
    url = url.replace('$fromName$', $('#txtFirstName').val());
    url = url.replace('$videoUrl$', escape(currentVideoLink));
    url = url.replace('$f1Name$', $('#txtFriendName1').val());
    url = url.replace('$f1Email$', $('#txtFriendEmail1').val());
    url = url.replace('$f2Name$', $('#txtFriendName2').val());
    url = url.replace('$f2Email$', $('#txtFriendEmail2').val());
    url = url.replace('$videoTitle$', $('#videoTitle').text());
    $.get(url,
        function(data) {
            var r = eval('(' + data + ')');
            var retVal = r.retVal;
            var msg = r.msg || "";
            if (retVal == 0 || retVal == '0') {
                $('#shareV0').hide();
                $('#shareV2').hide();
                $('#shareV1').show();
                //trackHit('/Media/Template/PageUrl/$pageUrl$/Video/$currentVideoTitle$/Action/ShareVideoSubmitted');
            }
            else {
                $('#shareError').html(msg);
                $('#btnShareSubmit').val('Send');
                $('#shareV2').show();
            }
        }
    );
    return false;
}

function resetForms() {
    $('#shareV0').show();
    $('#shareV1').hide();
    $('#shareV2').hide();
    $('#shareError').html('');
    $('#txtYourEmail').val('');
    $('#txtFirstName').val('');
    $('#txtFriendName1').val('');
    $('#txtFriendEmail1').val('');
    $('#txtFriendName2').val('');
    $('#txtFriendEmail2').val('');
    $('#btnShareSubmit').val('Send');
}

$(document).ready(function(e) {
    $('#btnShareSubmit').click(shareSubmit);
    $('a[rel="popup"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });
    togglePanel('tabRelevant', 'pnlRelated');
});

function GetResultsBatch() {
    //alert("getting "+FEED_BATCH_SIZE+" more starting at " + (entries.length+1));
    $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/users/responsiblesports/uploads?" +
                "alt=json-in-script&" +
                "max-results=" + FEED_BATCH_SIZE + "&" +
                "start-index=" + (entries.length + 1) + "&" +
                "format=5&" +
                "callback=AddToMyVideos&key=AI39si7Dn7S1F_OOqD8E0-LfhMjRENIcxD8DzcK5lXvKHe9YTpJJanXzsT1W4SLGZwqz7_SVYzH-KcKhG9-mmKpCTnbxupg1-Q",
        dataType: "script"
    });
}

function AddToMyVideos(data) {
    showMyVideos(data);
}


