$(document).ready(function() {

    // ************************* //
    // select links navigation
    $('#quicklinks').change(function() {
        if ($(this).val() != "") {
            var s=s_gi('rsid');
            s.prop23 = $(this).val();
            s.eVar32 = s.prop23
            s.linkTrackVars="events,prop23,eVar32";
            s.linkTrackEvents='events15';
            s.events='event15';
            s.tl(this, 'o','Link Name');
            window.location = site_url + $(this).val();
        }
    })

    $('#catChannels').change(function() {
        if ($(this).val() != "") {
            var s = s_gi('rsid');
            s.prop23 = $(this).val();
            s.eVar32 = s.prop23
            s.linkTrackVars = "events,prop23,eVar31";
            s.linkTrackEvents = 'events15';
            s.events = 'event15';
            s.tl(this, 'o', 'Link Name');
            window.location = site_url + $(this).val();
        }
    })

    // ************************ //
    // write flash objects * 
    // required because wigs out inline
    if (isset('so_leaderboard')) {
        so_leaderboard.write("banner_leaderboard");
    }
    if (isset('so_island')) {
        so_island.write("banner_island");
    }
    if (isset('so_custom1')) {
        so_custom1.write("banner_custom1");
    }

    // ************************* //
    if ($("#flashdata").html() != null) {
        setTimeout("$(\"#flashdata\").fadeOut();", 4000);
    }

    // *********************** //
    // Ajax code to update video list
    // Determine initial load
    var currSelected = $('#video_multi_container .selected');


    // ********************** //
    // ** TAB HANDLER CODE ** //
    $('#recent').click(function() {
        if ($(this).parent().attr("class") != "selected") {
            $(currSelected).removeClass("selected");
            $(this).parent().addClass("selected");
            currSelected = $(this).parent();
            if (typeof channelId != 'undefined') {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/recentByType/" + channelId + "/channel/1";
            } else if (typeof catId != 'undefined') {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/recentByType/" + catId + "/category/1";
            } else {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/recent/null/null/1";
            }
            updateVideos(pUrl);
        }
        return false;
    })


    $('#popular').click(function() {
        if ($(this).parent().attr("class") != "selected") {
            $(currSelected).removeClass("selected");
            $(this).parent().addClass("selected");
            currSelected = $(this).parent();
            if (typeof channelId != 'undefined') {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/popularByType/" + channelId + "/channel/1";
            } else if (typeof catId != 'undefined') {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/popularByType/" + catId + "/category/1";
            } else {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/popular/null/null/1";
            }
            updateVideos(pUrl);
        }
        return false;
    })

    $('#top_rated').click(function() {
        if ($(this).parent().attr("class") != "selected") {
            $(currSelected).removeClass("selected");
            $(this).parent().addClass("selected");
            currSelected = $(this).parent();
            if (typeof channelId != 'undefined') {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/topratedByType/" + channelId + "/channel/1";
            } else if (typeof catId != 'undefined') {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/topratedByType/" + catId + "/category/1";
            } else {
                pUrl = site_url + "ajax/updateContent/" + videoListType + "/toprated/null/null/1";
            }
            updateVideos(pUrl);
        }
        return false;
    })

    $('#all').click(function() {
        if ($(this).parent().attr("class") != "selected") {
            $(currSelected).removeClass("selected");
            $(this).parent().addClass("selected");
            currSelected = $(this).parent();
            pUrl = site_url + "ajax/updateContent/" + videoListType + "/allByChannel/" + channelId + "/null/1";
            updateVideos(pUrl);
        }
        return false;
    })

    $('#related').click(function() {
        if ($(this).parent().attr("class") != "selected") {
            $(currSelected).removeClass("selected");
            $(this).parent().addClass("selected");
            currSelected = $(this).parent();
            pUrl = site_url + "ajax/updateContent/" + videoListType + "/related/" + video_id + "/null/1";
            //pUrl = site_url+"video/relatedAjax/"+video_id;
            updateVideos(pUrl);
        }
        return false;
    })

    // load initial tab once only
    if (isset('firstRun') == false) {
        if (isset('pageType')) {
            if (pageType == "video") {
                $('#related').click();
            } else {
                $('#recent').click();
            }
        } else {
            $('#recent').click();
        }
        firstRun = true;
    }

    // remove current list of videos and replace with new ones
    function updateVideos(pUrl) {
        $('#videolist_loading').fadeIn();
        $('div.pagination').remove();

        $.ajax({
            type: "POST",
            url: pUrl,
            async: true,
            data: "url=" + pUrl,
            success: function(msg) {
                $('#videoGridInner').empty();
                $('#videoGridInner').append(msg);
                updateRatings();

                // swap the ajax function in the link
                var pagUrl = pUrl.replace("updateContent", "updatePagination");
                updatePagination(pagUrl);
            }
        });
    }

    // function to remove current pagination and update with new values
    function updatePagination(pUrl) {
        $("#gridViewBtn a").unbind();
        $("#listViewBtn a").unbind();
        $('div.pagination').remove();
        $.ajax({
            type: "POST",
            url: pUrl,
            data: "url=" + pUrl,
            success: function(msg) {
                $('#videolist_loading').fadeOut();
                $('#video_multi_container').append(msg);
                // redeclare pagination links
                $('.pagination a').click(function() {
                    var pUrl = $(this).attr('href');
                    updateVideos(pUrl);
                    return false;
                });
            }
        });
    }

    // ************************* //
    // ** Accordion code ******* //

    var currOpen;
    $('#attractions dd').css("display", "none");
    $('#attractions dd:first').css("display", "block");
    currOpen = $('#attractions dd:first');
    $('#attractions dt').hover(function() {
        if ($(this).attr('class') != 'active') {
            animating = true
            $(currOpen).prev('dt').removeClass("active");
            $(currOpen).css("display", "none");
            $(this).addClass("active");
            $(this).next('dd').fadeIn('slow');
            currOpen = $(this).next('dd');

            if ($(this).attr('id') == undefined) {
                $('#attractions dt:first').addClass('nobg');
            } else {
                $('#attractions dt:first').removeClass('nobg');
            }
        }
    }, function() {

    })

    // ************************* //
    // ** PROMO tabs *********** //
	
    $("#promo_menu li a").click(function() {
        var next = "#" + $(this).parent().attr("id");
        var current = "#" + $(this).parent().attr("id") + "panel";
        displayPromo(next, current);
        // stop auto scrolling of promos when one is clicked
        promoAutoScroll = null;
        return false;
    });

    // Only run once. If promoIndex already exists, skip initialisation
    if (!isset('promoIndex')) {
        promoIndex = 1;
        promoAutoScroll = false;
        // start auto scrolling of promos
        waitForNextPromo();
		$("ul#promo_menu li.selected div.promo_menu_details").fadeIn('slow');
		$("li.selected a.promo_menu_btns").css({'background-image':'url(../images/promo-menu-arrow-down.png)','background-repeat':'no-repeat','background-position':'right'});
		$("#promo1panel").show();
    }

    $('#embed_code').click(function() {
        $(this).focus();
        $(this).select();
    })

    updateRatings();




});

function waitForNextPromo() {
	if ($('#promo'+(promoIndex+1)).attr("id") == undefined && promoIndex == 1) {
		// do nothing
		promoAutoScroll = false;
	} else if ($('#promo'+(promoIndex+1)).attr("id") == undefined) {
		promoIndex = 1;
	} else {
		promoIndex++;
	}
	promosScrolling = true;
	/*setTimeout(function() {
	  if(promoAutoScroll) {
		  var next = "#"+$('#promo'+promoIndex).attr("id");
		  var current = "#"+$('#promo'+promoIndex).attr("id") + "panel";
		  promosScrolling = null;
		  displayPromo(next, current);
	  }
	}, 6000);*/
}

function displayPromo(next, current) {

	next = next;
	current = current;
	
	$(".promo_menu_details").hide();
	$("ul#promo_menu li.selected a.promo_menu_btns").css({'background-image':'url(../images/promo-menu-arrow-right.png)','background-repeat':'no-repeat','background-position':'right'});
	$("#promo_menu .selected").removeClass("selected");
	$("#promo_panel .activePanel").fadeOut('fast');
	$("#promo_panel .activePanel").removeClass("activePanel");
	
	$(next).addClass("selected");
	$(current).addClass("activePanel");
	$("ul#promo_menu li.selected div.promo_menu_details").fadeIn(1750);
	$("ul#promo_menu li.selected a.promo_menu_btns").css({'background-image':'url(../images/promo-menu-arrow-down.png)','background-repeat':'no-repeat','background-position':'right'});
	$("#promo_panel .activePanel").fadeIn(1750);
}
	
function updateRatings() {
	$('.rating').each(function(i) {
		$(this).empty();
		var tempId = $(this).attr("id");
		var videoId = tempId.substr(1);
		ratingUrl = site_url+'video/rateVideo/'+videoId;
		var currVal = $(this).attr("title");
		$(this).rating(ratingUrl, {maxvalue:5, curvalue:currVal})
	})
	
	// ** Striped videolist *** //
	$('#videoList dl:odd').addClass('alt');
}

function isset(varname){
	return(typeof(window[varname])!='undefined');
}