
// initalize the assets
jQuery(document).ready(function() {
    // initBioHeight();
    initPlayer();
    initComments();
    initCommentForm();
    initFlagging();
    initTooltip();
});

/**
 * Define setlist properties
**/
var setlist = new Array();

// Live from Bonnaroo 2009 Trailer *
setlist[0] = { embedCode: '55dWt3Ol9PBOFUwNx0xzhSkpiv9s34bQ', songStart: '-1', songEnd: '-1', songLength: '-1' };

// Live from Bonnaroo 2009 Trailer *
setlist[1] = { embedCode: '55dWt3Ol9PBOFUwNx0xzhSkpiv9s34bQ', songStart: '-1', songEnd: '-1', songLength: '-1' };

// Cage the Elephant: Ain't No Rest for the Wicked *
setlist[2] = { embedCode: 'p0a2d4Ot6G6RE2XytxbbOMvFT41WeU_t', songStart: '61', songEnd: '444', songLength: '384' };

// Zac Brown Band: Who Knows *
setlist[3] = { embedCode: 'hkcWJ4OpCbbRWc4uXP9Gam7bh2tbP40x', songStart: '445', songEnd: '1189', songLength: '746' };
// The Decemberists - The Wanting Comes In Waves/Repaid *
setlist[4] = { embedCode: 'Zxa2d4Om1W1UjnQA8xlfnjj5x4PLyRIA', songStart: '1190', songEnd: '1733', songLength: '544' };

// Passion Pit - Little Secrets *
setlist[5] = { embedCode: 'pya2d4OkOIMquZZkrJtZR_x_MjJuwgmS', songStart: '1734', songEnd: '2111', songLength: '378' };

// Del McCoury Band - Moneyland *
setlist[6] = { embedCode: 'p1a2d4OvIRjUVumBprjEuqrgkQM9qR-D', songStart: '2112', songEnd: '2993', songLength: '883' };

//  Ben Harper and Relentless7- Fly One Time *
setlist[7] = { embedCode: 'p2a2d4OtVQv6RpPr5PIUDazVDz539oCy', songStart: '2994', songEnd: '3609', songLength: '615' };

// Amadou & Mariam - Masiteladi *
setlist[8] = { embedCode: 'pza2d4OtsyNrwIcRDbGRA4-L9eWml9QV', songStart: '3610', songEnd: '3886', songLength: '282' };

// Raphael Saadiq - Keep Marchin'
setlist[9] = { embedCode: 'Zwa2d4OgSWqb9yFJQ7b1hSF9XDtY531A', songStart: '3610', songEnd: '3886', songLength: '282' };

// preload preloader.
var loader = jQuery('<li style="text-align: center; padding-top: 30px;">Loading comments...<br /><img src="../../images/365/DVD2009/loading.gif" alt="loading" /></li>');
             
var channel = ""//"9vZ2NpOsIAPU4dUulJePZenEFF-_-gTZ";
var channel_item_embed_code = null;
var player = null;
var playerSongStartTime = null;
var currentSongIndex = null;
var currentEmbedCode = null;
var loadComplete = false;

/**
 * Advance to the next video once previous stops playing.
**/
function receiveOoyalaEvent(playerId, eventName, data) {
    player = document.getElementById(playerId);

            
    switch (eventName) {
        case 'playheadTimeChanged':
            onPlayheadTimeChanged(data);
            break;
        case 'embedCodeChanged':
            if (loadComplete) {
                //if (currentEmbedCode != player.getCurrentItemEmbedCode()) {
                    player.playMovie();
                //}
            }
            break;
        case 'loadComplete':
            loadComplete = true;
            currentEmbedCode = player.getCurrentItemEmbedCode();
            break;
        case 'stateChanged':
            break;
        case 'playComplete':
            if ( ( currentSongIndex + 1 ) == setlist.length ) {
              currentSongIndex = 0;
            } else {
              currentSongIndex = currentSongIndex + 1;
            }
            //alert(jQuery('.enabled #song-' + currentSongIndex)[0]);
            if (jQuery('.enabled #song-' + currentSongIndex)[0] != undefined) {
 			highlightSong(currentSongIndex);

            var embed_code = setlist[currentSongIndex].embedCode;
			player.setEmbedCode(embed_code);
            player.playMovie();
            }

            break;
        case 'totalTimeChanged':
        	break;
    }
}

/**
*  g39 Modification to highlight the currently playing song
**/
function onPlayheadTimeChanged(data) {
    time = parseFloat(data.playheadTime);

    if (playerSongStartTime == null) {

        for (var i = 0; i < setlist.length; i++) {
            var song = setlist[i];

            if (song.songStart <= time && time <= song.songEnd) {
                if (currentSongIndex == i) { break; }
                //currentSongIndex = i;

                // highlight the currently playing song
                highlightSong(i);

                break;
            }
        }
    }
}
function clearPlayingSong() {
    jQuery('.song-title').each(function() {
        this.style.fontWeight = 'normal';
    });
}
function highlightSong(idx) {
    //  Clear the currently playing item
    clearPlayingSong();

//  Set the current song
    var item = jQuery('#song-' + idx)[0];
    item.style.fontWeight = 'bold';
    
}
/**
*  g39 Modification to see if we need to split media files
**/
function initPlayer() {
    jQuery(".disabled .song-title").click(function() {
        return false;
    });
    jQuery(".enabled .song-title").click(function() {
        channel_item_embed_code = null;
        var song_index = parseInt(jQuery(this).attr("id").replace("song-", ""));
        var embed_code = setlist[song_index].embedCode;
        
        if (player) {
            if ( currentSongIndex == null || currentSongIndex != song_index )
            {
 				currentSongIndex = song_index;
 				highlightSong(currentSongIndex);
				player.setEmbedCode(embed_code);
                player.playMovie();
			}
        


                


        }
    });
}

function togglePager() {
	// hide the prev. link and divider
	if (currentPage == 1) {
		jQuery("#comment-pager li#prev").css("visibility", "hidden");
	}
	else {
		jQuery("#comment-pager li#prev").css("visibility", "visible");
	}
	
	// hide next link and divider
	if (currentPage == totalPages) {
		jQuery("#comment-pager li#next").css("visibility", "hidden");
	}
	else {
		jQuery("#comment-pager li#next").css("visibility", "visible");
	}
	
	if (currentPage < totalPages && currentPage > 1) {
		jQuery("#comment-pager li#divider").css("visibility", "visible");
	}
	else {
		jQuery("#comment-pager li#divider").css("visibility", "hidden");
	}
}

function initComments() {
	jQuery("#comment-pager li#prev a").click(function(){
		jQuery("#comment-pager li#prev").css("visibility", "hidden");
		jQuery("#comment-pager li#next").css("visibility", "hidden");
		jQuery("#comment-pager li#divider").css("visibility", "hidden");
		jQuery("#comment-list").empty().append(loader).load("Default.aspx?ajax=true&page=" + (currentPage - 1), {}, function() {
			currentPage = currentPage - 1;
			togglePager();
			// initBioHeight();
			initFlagging();
		});
	});
	
	jQuery("#comment-pager li#next a").click(function(){
		jQuery("#comment-pager li#prev").css("visibility", "hidden");
		jQuery("#comment-pager li#next").css("visibility", "hidden");
		jQuery("#comment-pager li#divider").css("visibility", "hidden");
		jQuery("#comment-list").empty().append(loader).load("Default.aspx?ajax=true&page="+(currentPage+1), {}, function(){
			currentPage = currentPage + 1;
			togglePager();
			// initBioHeight();
			initFlagging();
		});
	});
	togglePager();
}

// Resize the bio block
function initBioHeight() {
	/*
	var commentsHeight = jQuery(".comment-fields").innerHeight() - 48;
	jQuery("#bio-inner").animate({height: commentsHeight + "px"}, 200);
	// jQuery("#bio-inner").css({height: commentsHeight + "px"});
	*/
}

function initFlagging() {
	jQuery("a.flag-comment").unbind("click").click(function(){
		var clicked = this;
		href = jQuery(this).attr("href");
		jQuery.get(href, {}, function(data){
			// alert(jQuery(clicked).attr("class"));
			jQuery(clicked).css({display: "none"}).after('<strong class="flag-comment">Flagged</strong>');
			
			// alert(data);
			// jQuery("(flagged)").insertAfter(jQuery(this)).fadeOut("slow");
		}, "html") 
		return false;
	});
}

function initTooltip() {
	xOffset = 10;
	yOffset = 20;		
	jQuery("a.tooltip").hover(function(e){
		this.t = this.title;
		this.title = "";
		jQuery("body").append("<p id='tooltip'>"+ this.t +"</p>");
		jQuery("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
	},
	function(){
		this.title = this.t;
		jQuery("#tooltip").remove();
	});
	jQuery("a.tooltip").mousemove(function(e){
		jQuery("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
}

function initCommentForm() {
	// init comment form
	jQuery("#comment-form input").click(function(){
		if (jQuery("#comment-form textarea").val() == "") {
			return false;
		}
		jQuery(this).css({visibility: "hidden"});
		jQuery.post("Default.aspx", { ajax: "true", comment: jQuery("#comment-form textarea").val() },
		function(data){
			if (data == 'max') {
				jQuery("#max-chars").addClass("max-chars-error");
			}
			else {
				jQuery("#comment-form textarea").val("");
				jQuery("#max-chars").removeClass("max-chars-error");
				jQuery("#comment-list").html(data);
				jQuery(".comment-count span").text(parseInt(jQuery(".comment-count span").text()) + 1);
				// initBioHeight();
			}
		}, "html");
		jQuery(this).css({visibility: "visible"});
		return false;
	});
}

// Bogus accordion function calls.
function $$() {
	var bogus = {};
	bogus.each = function(){};
	return bogus;
}
function verticalAccordions() {}