// for external presetting
var iTempoMin = 40;
var iTempoMax = 180;
var iTimeMin = 0;
var iTimeMax = 600;

// proceed
$(document).ready(function () {
    $(".ct").corner("top 5px");
    $(".cb").corner("bottom 5px");
    $(".ca").corner("5px");
    $(".featured-item").corner("10px");
    $(".select-list li a").corner("10px");
    $(".snapshot").corner("10px");
    $(".composed-by").corner("10px");
    $("#login-nav").corner("10px");
    $("#territory-nav").corner("10px");
    $("#account-info div").corner("5px");
    $(".cusage").corner("10px");
    $("#territory").corner("10px");
    $("#usage").corner("10px");

    $(".row-wrap:odd").addClass("grey");
    $("#checkout-table tbody tr:odd").addClass("grey");
    $(".row-wrap .composer-wrap:even").addClass("first");
    $(".row-wrap .composer-wrap:odd").addClass("second");
    $(".styles, .moods, .tempo, .time").prev(".filter-item").addClass("open");
    $(".styles, .moods, .tempo, .time").show();
    $(".blog article:first").addClass("first");

    if ($("body").hasClass("no-player")) {
        $("footer").css("bottom", "-88px");
        $(".lower-footer").css("bottom", "88px");
        $("#show-player").show();
        $("#shrink-player").hide();
        $("#page-footer").css('padding-bottom', "32px");
    }

    //stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
    if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
        $("#fixed-footer").css("position", "static");
        $("#page-footer").css("paddingBottom", "32px");
        $("#shrink-player").hide();
        $("#show-player").hide();
        $("#player-holder").addClass("apple");
    };

    //stick the footer at the bottom of the page if we're on an android device
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("android") != -1) {
        $("#fixed-footer").css("position", "static");
        $("#page-footer").css("paddingBottom", "32px");
        $("#shrink-player").hide();
        $("#show-player").hide();
        $("#player-holder").addClass("android");
    }

    $('.slideshow, .cycle').cycle({
        fx: 'fade',
        timeout: 8000,
        speed: 2000
    });

    $(".gsClose").click(function () {
        $(this).parents(".st").fadeOut('slow');
        cSet($(this).attr('id'));
    });

    $("#search").autocomplete('/handlers/search/prompter.aspx');

    $("#search-btn").click(function () {
        $('#search-form').submit();
    });

    $("#s-s").click(function (e) {
        e.stopPropagation();
        var w = $("#s-s").width();
        var p = $("#s-s").position();
        $(".extra").width(w - 2);
        $("#types").css({ 'left': p.left + 2 });
        $("#types").toggle();
        $("#search-wrapper").toggleClass("search-open");
        $("#your-account").removeClass("clicked");
        $("#set-territory").removeClass("clicked");
        $("#login-nav").fadeOut();
        $("#territory-nav").fadeOut();

    });

    $("#types ul li a").click(function () {
        if ($("#types ul li a").hasClass("selected")) {
            $("#types ul li a").removeClass("selected");
        };
        $(this).addClass("selected");
        $("#search-select").replaceWith("<a id='search-select' class='search'>" + $("#types ul li a.selected").text() + "</a>");
        $("#stype-hdn").val($("#types ul li a.selected").text());
        $("#types").hide();
        $("#search-wrapper").removeClass("search-open");
    });

    $("li.hover").mouseleave(function () {
        $(this).fadeOut();
        $("#header-bottom-navigation ul li").removeClass("hover");
    });

    $("#header-bottom-navigation ul li div").parent().mouseenter(function () {
        $(this).addClass("hover");
        $(this).children(".dd").show();
    }).mouseleave(function () {
        $(this).removeClass("hover");
        $(this).children(".dd").hide();
    });

    $(".featured-item:even").addClass("first");
    $(".featured-aside ul li:odd").addClass("grey");
    $(".quick-links .recent li:first").addClass("first");
    $(".quick-links .recent li:last").addClass("last");

    $(".featured-aside ul li .play").click(function () {
        if ($(".featured-aside ul li").hasClass("on")) {
            $(".featured-aside ul li").removeClass("on");
        }
        $(this).parent().addClass("on");
    });

    $(".play").live("click", function () {
        if ($(".play").hasClass("on")) {
            if ($(this).hasClass("on")) {
                $(this).removeClass("on");
                $("#jquery_jplayer_1").jPlayer("pause");
                return false;
            }
            $(".play").removeClass("on");
        }
        if (!$(this).hasClass("on")) {
            $(this).addClass("on");
        }
    });

    $(".jp-pause").click(function () {
        if ($(".play").hasClass("on")) {
            $(".play").removeClass("on");
        }
    });

    $("#big-play").click(function () {
        if ($(this).hasClass("on")) {
            $(this).removeClass("on");
            $("#jquery_jplayer_1").jPlayer("pause");
            return false;
        }
        $(this).addClass("on");
    });

    $(".recent li a").click(function () {
        var id = $(this).attr("id");
        var toShow = "#" + id + " .featured-aside";
        if ($("#recent-composer .featured-aside").hasClass("shown")) {
            $("#recent-composer .featured-aside").addClass("hidden")
            $("#recent-composer .featured-aside").removeClass("shown")
        }
        $(toShow).removeClass("hidden");
        $(toShow).addClass("shown");
    });

    $("#your-account").click(function (e) {
        e.stopPropagation();
        $(this).toggleClass("clicked");
        $("#login-nav").toggle();
        $("#search-wrapper").removeClass("search-open");
        $("#types").fadeOut();
    });

    $("#set-territory").click(function (e) {
        e.stopPropagation()
        $(this).toggleClass("clicked");
        $("#territory-nav").toggle();
        $("#search-wrapper").removeClass("search-open");
        $("#types").fadeOut();
    });

    $(document).click(function () {
        if ($("#your-account").hasClass("clicked")) {
            $("#your-account").removeClass("clicked");
            $("#login-nav").fadeOut();
        }
        if ($("#search-wrapper").hasClass("search-open")) {
            $("#search-wrapper").removeClass("search-open");
            $("#types").fadeOut();
        }
        if ($("#set-territory").hasClass("clicked")) {
            $("#set-territory").removeClass("clicked");
            $("#territory-nav").fadeOut();
        }
        if ($(".selector").hasClass("on")) {
            $(".selector").removeClass("on");
            $(".selector").fadeOut();
        }
        if ($(".edits ul.cusage").hasClass("open")) {
            $(".edits ul.cusage").removeClass("open");
            $(".edits ul.cusage").fadeOut();
        }
    });

    $("#shrink-player").click(function () {
        $("footer").animate({ "bottom": "-=88px" }, 'slow', function () {
            $("#show-player").show();
            $("#shrink-player").hide();
        });
        $(".lower-footer").animate({ "bottom": "+=88px" }, "slow");
        $("#page-footer").animate({ 'paddingBottom': 32 }, "slow");
    });

    $("#show-player").click(function () {
        $("#shrink-player").show();
        $("#show-player").hide();
        $("footer").animate({ "bottom": "0px" }, 'slow');
        $(".lower-footer").animate({ "bottom": "0px" }, 'slow');
        $("#page-footer").animate({ 'paddingBottom': 120 }, "slow");
    });

    $('.filter-item').click(function () {
        $(this).toggleClass("open").next().slideToggle(300);
        return false;
    });

    // menu items 
    $('.hidden ul li a').click(menuItem_Click);

    // clear all filters
    $('#f-clear').click(clearAll_Click);

    // clear single filter group
    $("a.f-clear-btn").click(clearFilters_Click);

    $("#f-time-clear").click(function () {
        resetSlider($("#slider-time-range"), 0, 600);
        return false;
    });

    $("#f-tempo-clear").click(function () {
        resetSlider($("#slider-tempo-range"), 40, 180);
        return false;
    });

    $("#slider-tempo-range").slider({
        range: true,
        min: 40,
        max: 180,
        values: [iTempoMin, iTempoMax],
        slide: tempoSlider_SlideChange,
        change: tempoSlider_SlideChange,
        create: tempoSlider_OnCreate
    });

    $("#slider-time-range").slider({
        range: true,
        min: 0,
        max: 600,
        values: [iTimeMin, iTimeMax],
        slide: timeSlider_SlideChange,
        change: timeSlider_SlideChange,
        create: timeSlider_OnCreate
    });

    $('#ex-all').click(function () {
        if ($(this).hasClass("expanded")) {
            $(".row-wrap").removeClass("opened").children(".row-hidden").slideUp(300);
            $(this).removeClass("expanded");
            $(this).text('Expand All');
        }
        else {
            $(".row-wrap").addClass("opened").children(".row-hidden").slideDown(300);
            $(this).addClass("expanded");
            $(this).text('Close All');
        }
        return false;
    }).children(".row-hidden").hide();

    $('#remove-all').click(function () {
        $(".main-rows").hide();
        $(".table-head .selector-row").replaceWith("<div class='selector-row'><span>Your Shopping basket is currently empty</span></div>");
        $("#options").hide();
        $(".selector-row:eq(1)").hide();
    });


    $('.row-wrap .plus').click(function () {
        var nRows = ('.row-wrap').length;
        var oRows = ('.opened').length;
        $(this).parents(".row-wrap").toggleClass("opened").children(".row-hidden").slideToggle(300);
        if (oRows == nRows - 1) {
            $("#ex-all").addClass("expanded");
        }
        else {
            $("#ex-all").removeClass("expanded");
        }
        return false;
    }).children(".row-hidden").hide();

    $("#radio li > a").live('click', function (event) {
        event.preventDefault();
        $('#radio .active').removeClass("active");
        var id = this.id;
        var newselect = id.replace('link', 'option');
        $('#' + newselect).attr('checked', true);
        $(this).addClass("active").parent().addClass("active");
        return false;
    });

    $("span.u").click(function (e) {
        e.stopPropagation()
        $("ul.usage").toggle();
        $("ul.usage").toggleClass("on");
    });

    // Events for basket selector functionality
    $("a.cusage").click(function (e) {
        e.stopPropagation()
        // alert($(this).attr('id').replace("_cusage", "_usage"));
        $(this).siblings(".cusage").addClass("open");
        $("#" + $(this).attr('id').replace("_cusage", "_usage")).toggle();
    });

    $(".edits ul.cusage li").click(function () {
        var trackRow = $(this).parent().attr('id').replace("_usage", "");
        lSwitch("/handlers/basket/track_updateusage.aspx", "id=" + $(this).attr('id'), "#" + trackRow);
    });

    $(".selector-row span.t").click(function (e) {
        e.stopPropagation()
        $("ul.territory").toggle();
        $("ul.territory").toggleClass("on");
    });

    $("ul.selector").mouseleave(function () {
        $(this).fadeOut();
    });

    $(".selector-row ul.usage li").click(function () {
        if ($('ul.usage li').hasClass("selected")) {
            $('ul.usage li').removeClass("selected");
        };
        $(this).addClass("selected");
        $("#usage").replaceWith("<a id='usage'>" + $(".selector-row ul.usage li.selected").text() + "</a>");
        $("#usage").corner("10px");
        checkUT();
        uSet($(this).attr('id'));
        $(this).parent().hide();
    });

    $(".modal-wrap ul.usage li").click(function () {
        if ($('ul.usage li').hasClass("selected")) {
            $('ul.usage li').removeClass("selected");
        };
        $(this).addClass("selected");
        $("#u-selection").replaceWith("<span id='u-selection'>" + $(".modal-wrap ul.usage li.selected").text() + "</span>");
        $(this).parent().hide();
    });

    $(".selector-row ul.territory li").click(function () {
        if ($('ul.territory li').hasClass("selected")) {
            $('ul.territory li').removeClass("selected");
        };
        $(this).addClass("selected");
        $("#territory").replaceWith("<a id='territory'>" + $(".selector-row ul.territory li.selected").text() + "</a>");
        if ($("#top_territory") != null) {
            $("#top_territory").replaceWith("<span id='top_territory'>" + $(".selector-row ul.territory li.selected").text() + "</span>");
        }
        $("#territory").corner("10px");
        checkUT();
        tSet($(this).attr('id'));
        $(this).parent().hide();
    });

    $("ul#territory-nav li a").click(function () {
        $("#top_territory").replaceWith("<span id='top_territory'>" + $(this).text() + "</span>");
        if ($("#territory") != null) {
            $("#territory").replaceWith("<a id='territory'>" + $(this).text() + "</a>");
            $("#territory").corner("10px");
        }
        checkUT();
        tSet($(this).attr('id'));
    });

    $("#featured-composers .row-wrap").each(function () {
        var h = $(this).height();
        $(".composer-wrap", this).css("height", h - 25);
    });

    $(".delete").click(function () {
        $(".range_item").html((1 * $(".range_item").html()) - 1);
        if ($(this).hasClass('chkout')) {
            $(this).parents("li").hide(100);
            bRem($(this).attr('id'));
        }
        else if ($(this).hasClass('trybuy')) {
            $(this).parents(".row-wrap").hide(100);
            tbRem($(this).attr('id'));
        }
        else {
            $(this).parents(".row-wrap").hide(100);
            fRem($(this).attr('id'));
        }
    });

    $('a.scroll[href*=#]').click(function () {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
		&& location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');

            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({ scrollTop: targetOffset }, 1000);
                return false;
            }
        }
    });

    $("a.download").click(function () {
        if ($(this).hasClass('available')) {
            if (!$(this).hasClass('on')) {
                $(this).addClass('on');
            }
            if (!$(this).hasClass('big')) {
                tbSet($(this).attr('id'));
                openDownloadModal('/modals/download.aspx?id=' + $(this).attr('id'))
            }
            else {
                ptbSet();
            }
        }
        if ($(this).hasClass('blanket')) {
            openDownloadModal('/modals/blanket-download.aspx?id=' + $(this).attr('id'))
        }
        if ($(this).hasClass('historical')) {
            openDownloadModal('/modals/history-download.aspx?id=' + $(this).attr('id'))
        }
        $("#download").dialog({
            modal: true,
            posistion: 'center',
            resizable: false,
            width: 635,
            height: 345,
            closeText: 'hide'
        });
    });

    $("a.big-d").click(function () {
        openDownloadModal('/modals/history-download.aspx?id=' + $(this).attr('id'))
        $("#download").dialog({
            modal: true,
            posistion: 'center',
            resizable: false,
            width: 635,
            height: 345,
            closeText: 'hide'
        });
    });

    $("a.tmodal").click(function () {
        $("#download").dialog({
            modal: true,
            posistion: 'center',
            resizable: false,
            width: 635,
            height: 345,
            closeText: 'hide'
        });
    });

    $(".acc-edit").click(function (e) {
        e.preventDefault();
        $(this).parent().hide();
        $("#selection").show();
    });

    // Ajax button calls
    $('.like').live("click", function () {
        var html = $(".lower-footer .wrap-left").html();
        if (!$(this).hasClass('on')) {
            $(this).addClass('on');
            $(".lower-footer").animate({ "backgroundColor": "#ed3b76" }, 1800, function () {
                $(".lower-footer").animate({ "backgroundColor": "#E0E0E0" }, 800);
                if ($("#show-player").is(':visible')) {
                    $(".lower-footer #like-show-player").fadeOut(800);
                    $(".lower-footer #show-player").fadeIn(800);
                }

                $(".lower-footer .wrap-left p").replaceWith(html);
            });
            if ($("#show-player").is(':visible')) {
                $(".lower-footer #show-player").fadeIn(1800);
                $(".lower-footer #like-show-player").fadeIn(1800);
            }
            $(".lower-footer .wrap-left p").replaceWith("<p style='color: #fff'>This track has been added to your favourites</p>");
        }
        if (!$(this).hasClass('big')) {
            fSet($(this).attr('id'));
        }
        else {
            pfSet();
        }
    });

    $('#like').live("click", function () {
        var html = $(".lower-footer .wrap-left").html();
        if (!$(this).hasClass('on')) {
            $(this).addClass('on');
            $(".lower-footer").animate({ "backgroundColor": "#ed3b76" }, 1800, function () {
                $(".lower-footer").animate({ "backgroundColor": "#E0E0E0" }, 800);
                if ($("#show-player").is(':visible')) {
                    $(".lower-footer #like-show-player").fadeOut(800);
                    $(".lower-footer #show-player").fadeIn(800);
                }
                $(".lower-footer .wrap-left p").replaceWith(html);
            });
            if ($("#show-player").is(':visible')) {
                $(".lower-footer #show-player").fadeIn(1800);
                $(".lower-footer #like-show-player").fadeIn(1800);
            }
            $(".lower-footer .wrap-left p").replaceWith("<p style='color: #fff'>This track has been added to your favourites</p>");
        }
        if (!$(this).hasClass('big')) {
            fSet($("#curr_track").html());
        }
        else {
            pfSet();
        }
    });

    $('.basket').live("click", function () {
        if ($(this).hasClass('inc')) {
            openUsageModal('/modals/usage-types.aspx');
            return false;
        }
        var html = $(".lower-footer .wrap-left").html();
        if (!$(this).hasClass('on')) {
            $(this).addClass('on');
        }
        $(".lower-footer").animate({ "backgroundColor": "#be58e9" }, 1800, function () {
            $(".lower-footer").animate({ "backgroundColor": "#E0E0E0" }, 800);
            if ($("#show-player").is(':visible')) {
                $(".lower-footer #basket-show-player").fadeOut(800);
                $(".lower-footer #show-player").fadeIn(800);
            }
            $(".lower-footer .wrap-left p").replaceWith(html);
        });
        if ($("#show-player").is(':visible')) {
            $(".lower-footer #show-player").fadeIn(1800);
            $(".lower-footer #basket-show-player").fadeIn(1800);
        }
        $(".lower-footer .wrap-left p").replaceWith("<p style='color: #fff'>This track has been added to your basket</p>");
        if (!$(this).hasClass('big')) {
            abSet($(this).attr('id'));
        }
        else {
            pabSet();
        }
        $("#n_basket").show();
    });

    $(".page_switch").click(function () {
        pGo($(this).attr('id'));
        return (false);
    });

    $(".composer_switch").click(function () {
        cGo($(this).attr('id'));
        return (false);
    });

    $("a.sorter").click(function () {
        sSet($(this).attr('id'));
        // return (false);
    });
});

var modalWindow = {  
     parent:"body",  
     windowId:null,  
     content:null,  
     width:null,  
     height:null,  
     close:function()  
     {  
         $(".modal-window").remove();  
         $(".modal-overlay").remove();  
     },  
     open:function()  
     {  
         var modal = "";  
         modal += "<div class=\"modal-overlay\"></div>";  
         modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";  
         modal += this.content;  
       	 modal += "</div>";     
   
         $(this.parent).append(modal);  
  
         $(".modal-window").append("<a class=\"close-window\"></a>");  
         $(".close-window").click(function(){modalWindow.close();});  
         $(".modal-overlay").click(function(){modalWindow.close();});  
    }  
};  

var openLoginModal = function(source)  
{  
     modalWindow.close();
     modalWindow.windowId = "myModal";  
     modalWindow.width = 630;  
     modalWindow.height = 250;  
     modalWindow.content = "<iframe width='651' height='250' frameborder='0' scrolling='no' allowtransparency='false' src='" + source + "'></iframe>";  
     modalWindow.open();  
};

var openDownloadModal = function (source) 
{
     modalWindow.close();
     modalWindow.windowId = "myModal";
     modalWindow.width = 635;
     modalWindow.height = 345;
     modalWindow.content = "<iframe width='656' height='355' frameborder='0' scrolling='no' allowtransparency='false' src='" + source + "'></iframe>";
     modalWindow.open();
};

var openTryBeforeModal = function(source)  
{
     modalWindow.close();
     modalWindow.windowId = "myModal";  
     modalWindow.width = 635;  
     modalWindow.height = 293;  
     modalWindow.content = "<iframe width='656' height='293' frameborder='0' scrolling='no' allowtransparency='false' src='" + source + "'></iframe>";  
     modalWindow.open();  
};

var openUsageModal = function(source)  
{
     modalWindow.close();
     modalWindow.windowId = "myModal";  
     modalWindow.width = 630;  
     modalWindow.height = 300;  
     modalWindow.content = "<iframe width='651' height='300' frameborder='0' scrolling='no' allowtransparency='false' src='" + source + "'></iframe>";  
     modalWindow.open();  
};

function menuItem_Click() {
    $(this).toggleClass("selected");

    // addition sets filter value for form-handler
    var ctrl = $("#filterIDs");
    ctrl.val(ctrl.val().replace($(this).attr('id') + "|", ""));
    if ($(this).hasClass("selected")) {
        ctrl.val(ctrl.val() + $(this).attr('id') + "|");
    }

    // proceed
    var clearFilter = $(".f-clear-btn", $(this).parents(".filter-wrap"));
    var clearAll = $("#f-clear");

    if ($(".filter-wrap ul li a").hasClass("selected")) {
        clearFilter.show();
        clearAll.show();} 
    else {
        clearFilter.hide();
        clearAll.hide();
    }

    // call update
    hpRefresh();
	return false;
}

function clearAll_Click() {
	$('.hidden ul li a').removeClass("selected");
    resetSlider($("#slider-time-range"), 0, 600);
    resetSlider($("#slider-tempo-range"), 40, 180);
	$('.f-clear-btn').hide();

    // clear settings
	$("#filterIDs").val("|");

	// call update
	hpRefresh();
	return false;
}

function clearFilters_Click() {
    $('ul li a', $(this).parents(".filter-wrap")).removeClass("selected");

    // clear filter settings fro this group
    var ctrl = $("#filterIDs");
    $('ul li a', $(this).parents(".filter-wrap")).each(function () {
        ctrl.val(ctrl.val().replace("|" + $(this).attr('id') + "|","|"));
    });
    $(this).hide();
    checkFilters();

    // call update
    hpRefresh();
    return false;
}

function resetSlider(elem, lower, upper) {
    $(".slider-lower", $(elem).parent()).val(lower);
    $(".slider-upper", $(elem).parent()).val(upper);
    $(elem).slider("option","values",[lower, upper]).trigger("change");
}

function checkFilters() {
    if (!$('#f-theme-clear:visible').size() && !$('#f-style-clear:visible').size() && !$('#f-mood-clear:visible').size() && !$('#f-effect-clear:visible').size() && !$('#f-instrument-clear:visible').size() && !$('#f-tempo-clear:visible').size() && !$('#f-time-clear:visible').size()) {
        $('#f-clear').hide();
    }

    // call update
    hpRefresh();
}

function tempoSlider_SlideChange(e, ui) {
    var lower = ui.values[0];
    var upper = ui.values[1];
    var stored = $("#tempo_selected");

    $("#amount-slow").html(lower);
    $("#amount-fast").html(upper);
    $("#text-amount-fast").html(upper);
    $("#text-amount-slow").html(lower);

    if (lower > 40 || upper < 180) {
        $("#f-clear, #f-tempo-clear").show();
        $(".t-note").addClass("f-note");
        $(".no-tf").hide();
        $(".tf").show();
        stored.val(lower + ':' + upper);
    }
    else {
        $("#f-tempo-clear").hide();
        $(".t-note").removeClass("f-note");
        $(".tf").hide();
        $(".no-tf").show();
        stored.val('');
    }

    checkFilters();
}

function tempoSlider_OnCreate(e, ui) {
    $("#amount-slow, #text-amount-slow").val(iTempoMin).html(iTempoMin);
    $("#amount-fast, #text-amount-fast").val(iTempoMax).html(iTempoMax);
    
    if (iTempoMin > 40 || iTempoMax < 180) {
        $("#f-clear, #f-tempo-clear").show();
        $(".t-note").addClass("f-note");
        $(".no-tf").hide();
        $(".tf").show();
    }
    else {
        $("#f-tempo-clear").hide();
        $(".t-note").removeClass("f-note");
        $(".tf").hide();
        $(".no-tf").show();
    }
}

function timeSlider_SlideChange(e, ui) {
    var mins0 = Math.floor(ui.values[0] / 60);
    var secs0 = ui.values[0] - (mins0 * 60);
    var mins1 = Math.floor(ui.values[1] / 60);
    var secs1 = ui.values[1] - (mins1 * 60);
    var stored = $("#time_selected");

    shortTime = getTime(mins0, secs0);
    longTime = getTime(mins1, secs1);
    $("#amount-short").html(shortTime);
    $("#amount-long").html(longTime);
    $("#text-amount-short").html(shortTime);
    $("#text-amount-long").html(longTime);

    if (ui.values[0] > 0 || ui.values[1] < 600) {
        $("#f-time-clear").show();
        $("#f-clear").show();
        $(".n-note").addClass("f-note");
        $(".no-f").hide();
        $(".f").show();
        stored.val(ui.values[0] + ":" + ui.values[1]);
    } else {
        $("#f-time-clear").hide();
        $(".n-note").removeClass("f-note");
        $(".f").hide();
        $(".no-f").show();
        stored.val('');
    }
    checkFilters();
}

function timeSlider_OnCreate(e, ui) {
    var mins0 = Math.floor(iTimeMin / 60);
    var secs0 = iTimeMin - (mins0 * 60);
    var mins1 = Math.floor(iTimeMax / 60);
    var secs1 = iTimeMax - (mins1 * 60);

    $("#amount-short, #text-amount-short").val(getTime(mins0,secs0)).html(getTime(mins0,secs0));
    $("#amount-long, #text-amount-long").val(getTime(mins1, secs1)).html(getTime(mins1, secs1));

    if (iTimeMin > 0 || iTimeMax < 600) {
        $("#f-time-clear").show();
        $("#f-clear").show();
        $(".n-note").addClass("f-note");
        $(".no-f").hide();
        $(".f").show();
    } else {
        $("#f-time-clear").hide();
        $(".n-note").removeClass("f-note");
        $(".f").hide();
        $(".no-f").show();
    }

    // dynamic function should occur after final create
    setFilterTabs();
}

function getTime(mins, secs) {
    secs = secs + "";
    if (secs.length == 1) { secs = "0" + secs; }
    return mins + ":" + secs + " ";
}

function hpRefresh() {
    var formData = "f=" + $("#filterIDs").val() + "&tempo=" + $("#tempo_selected").val() + "&time=" + $("#time_selected").val();
    doFilterSwitch("/handlers/ajax/filter_set_redraw.aspx", formData, "#f_script");
    ajaxSwitch("/handlers/ajax/hpresults.aspx", formData, "#tracks_list");
}

function playMP3(id, name, artist) {
    // close out like icon-on and click-action first, the keywords will reset accordingly
    $("#curr_track").html('' + id);
    if ($("#like").hasClass('on')) {
        $("#like").removeClass('on');
    }

    // mp3 player media set
    $("#title").html('<marquee behavior="scroll" scrollamount="1" direction="left" width="129">' + name + '</marquee>');
    $("#artist").html(artist);
    $("#jquery_jplayer_1").jPlayer("setMedia", { mp3: '/music/play/' + id + '/' + active_key + '.mp3' });
    $("#jquery_jplayer_1").jPlayer("play");

    // refresh keywords
    $("#track_keywords").html('<p>Loading keywords...</p>');
    quietSwitch("/handlers/ajax/track_keywords.aspx", "t=" + id, "#track_keywords");
}

function check() {
	var c = $("input#agreement[type=checkbox]").is(":checked")
	if (c == false) {
		alert("Please agree to the terms and conditions.")
	};
}

function checkUT() {
    if ($("#territory").html() == 'Set Location' || $("#usage").html() == 'Set Usage') {
        if (!$("#tracks_sorter").hasClass('highlighted')) {
            $("#tracks_sorter").addClass('highlighted');
        }
    }
    else {
        if ($("#tracks_sorter").hasClass('highlighted')) {
            $("#tracks_sorter").removeClass('highlighted');
        }
    }
}


