var summerFestivalVars = {
    xmlEntries: null,
    videos: null,
    newCommentsHeader: null,
    maxPostCount: 50,
 //   blogFriendId: 419618055,
 //   videoFriendId: 393660983
 
 //allVideoId: 393660983,
 NzVideoId: 393660983,
 UsVideoId: 393660983,
 GbVideoId: 486115141
 


}

function loadVideoSwitcher() {
	
  //var feedUrl = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.channel&ChannelID=' + summerFestivalVars.videoFriendId + '&RSSAction=VideoChannel'
	if (MySpaceClientContext.PreferredCulture == 'en-US') {
		var feedUrl = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.channel&ChannelID=' + summerFestivalVars.UsVideoId + '&RSSAction=VideoChannel'
		runvideolist();
		//var feedUrl = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.channel&ChannelID=' + summerFestivalVars.allVideoId + '&RSSAction=VideoChannel'
		//runvideolist();
		//alert ('us')
		}
		else if (MySpaceClientContext.PreferredCulture == 'en-GB') {
		var feedUrl = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.channel&ChannelID=' + summerFestivalVars.UsVideoId + '&RSSAction=VideoChannel'	
		runvideolist();
		var feedUrl = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.channel&ChannelID=' + summerFestivalVars.GbVideoId + '&RSSAction=VideoChannel'	
		runvideolist();
		//alert ('GB')
			}
		else {
		var feedUrl = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.channel&ChannelID=' + summerFestivalVars.NzVideoId + '&RSSAction=VideoChannel'
		runvideolist();
		//alert ('all')
		}	
function runvideolist() {

 var feed = new google.feeds.Feed(feedUrl);
    feed.setNumEntries(summerFestivalVars.maxPostCount);
    feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
    var subscribeLink = $('<a href="' + feedUrl + '">Subscribe To Video</a>');

    $('#slider-left').click(function(event) { slideLeft(); });
    $('#slider-right').click(function(event) { slideRight(); });
    $('#slider-left').css({'cursor':'pointer'});
    $('#slider-right').css({'cursor':'pointer'});
    $('#video-container').scrollLeft(0);


    feed.load(function(result) {
        if (!result.error) {
            var feedItems = result.feed.entries;
            summerFestivalVars.videos = feedItems;
            $.each(feedItems, function(i) {
                var videoXml = this.xmlNode;
                var contentElement = getElementByTagName(videoXml, 'media:content');
                var contentUrl = $(contentElement).attr('url');
                var thumbnailElement = getElementByTagName(videoXml, 'media:thumbnail');
                var thumbnailUrl = $(thumbnailElement).attr('url');

                var thumbnailDiv = $('<div class="window"></div>');

                var thumbnailLink = $('<a title="' + this.title + '"></a>');
                thumbnailLink.click(function(event) { displayVideo(i); });
                //thumbnailLink.bind('mouseover', this.contentSnippet, showSnippet);
                //thumbnailLink.bind('mouseout', hideSnippet);

                var thumbnailImage = $('<img src="' + thumbnailUrl + '" />');
                /*if (i == summerFestivalVars.videos.length - 1) {
                    thumbnailImage.addClass('lastImage');
                }*/

                thumbnailLink.appendTo(thumbnailDiv[0]);
                thumbnailImage.appendTo(thumbnailLink[0]);
                thumbnailImage.css({'cursor':'pointer', 'width':'103px', 'height':'65px','margin-top':'5px','margin-left':'2px'});
                thumbnailDiv.appendTo($('#video-list')[0]);

                $('#video-list').width($('#video-list').width() + 113);
            });

            if (feedItems.length > 0) {
               // displayVideo(0);
			   if (MySpaceClientContext.PreferredCulture == 'en-US'){displayVideo(3);}
			   else if (MySpaceClientContext.PreferredCulture == 'en-GB'){displayVideo(0);}
            }
        }
    });

}   
}





function slideLeft() {
    $('#video-container').animate({scrollLeft:$('#video-container').scrollLeft() - 113}, 'fast');
}

function slideRight() {
    $('#video-container').animate({scrollLeft:$('#video-container').scrollLeft() + 113}, 'fast');
}

function showSnippet(event) {
    $('#thumbnailDescription').html(event.data);
}

function hideSnippet(event) {
    $('#thumbnailDescription').html('');
}

function getElementByTagName(parentNode, childNode) {
    var targetNode = null;

    $.each(parentNode.childNodes, function() {
        if (this.tagName == childNode) {
            targetNode = this;
            return false;
        }
    });

    return targetNode;
}

function displayVideo(index) {
    var video = summerFestivalVars.videos[index];
    var videoXml = video.xmlNode;
    var contentUrl = $(getElementByTagName(videoXml, 'media:content')).attr('url');
    var videoUrl = contentUrl.replace(/ap=0/, 'ap=0,primarycolor=0x000000,secondarycolor=0xbb161f');

    swfobject.embedSWF(videoUrl, 'video-feature', '270', '210', '9.0.0');
    //$('#video-title').html(video.title);
    //$('#videoDescription').html(video.content);
}

function getPopups() {
    $('A[rel="popup"]').click(function() {
        var dimensions = $(this).attr('class');
        dimensions = dimensions.split('x');
        width = dimensions[0];
        height = dimensions[1];
        var winvars = 'width=' + width + ', height=' + height + ', menubar=1, resizable=1, location=1, status=1, scrollbars=1';

        window.open($(this).attr('href'), 'MySpace', winvars);

        return false;
    });
}

$(document).ready(function() {
    loadVideoSwitcher();
    getPopups();
});