﻿jQuery(function($) {



    $("#jplayer").jPlayer({
        ready: function() {
            $("#jp_container .track-default").click();
        },

        supplied: "m4a, mp3, mv4"
    });

    /*
    //add play buttons for songs with IDs
    $("a[metadata]").append("<img style='vertical-align:middle;' src='/images/ic_menu_play.png'/>");

    //get hrefs to tracks
    $("a[metadata]").each(function(index, elem) {
    var id = $(elem).attr("metadata");
    $.getJSON("http://itunes.apple.com/lookup?id=" + id + "&callback=?",
    function(data) {

                $(elem).attr("href", data.results[0].previewUrl);
    }
    );


    });
    */
    // Create click handlers for the different tracks
    var current;
    $(".play").click(function(e) {
        var filename = $(this).attr("href");
        var ext = filename.split('.').pop();

        if (ext == "m4a") {
            $("#jplayer").jPlayer("setMedia", {
             m4a: $(this).attr("href")
            });
        }
        else {
            $("#jplayer").jPlayer("setMedia", {
                mp3: $(this).attr("href")
            });

        }


        if (current == this) {
            $(current).empty();
            $(current).append("<img style='vertical-align:middle;' border='0' src='/images/ic_menu_play.png'/>");
            $("#jplayer").jPlayer("pause");
            current = null;
            return false;

        }
        $("#jplayer").jPlayer("play");

        if (current != null) {
            $(current).empty();
            $(current).append("<img style='vertical-align:middle;' border='0' src='/images/ic_menu_play.png'/>");
        }
        $(this).empty();
        $(this).append("<img style='vertical-align:middle;' border='0' src='/images/ic_menu_pause.png'/>");
        current = this;
        return false;
    });


    $("#commentForm").dialog({ autoOpen: false, modal: true, resizable: false, width: "320px" });
    $("#favoriteForm").dialog({ autoOpen: false, modal: true, resizable: false });
    $("#joinForm").dialog({ autoOpen: false, modal: true, resizable: false });
    $("#featureForm").dialog({ autoOpen: false, modal: true, resizable: false });
    $("#featureFormNo").dialog({ autoOpen: false, modal: true, resizable: false });

    $("#addComment").bind("click", function() {
        var userName = $("input[name='userName']").val();
        if (userName == "")
            $("#joinForm").dialog("open");
        else
            $("#commentForm").dialog("open");

    });
    $("#addFavorite").bind("click", function() {
        var userName = $("input[name='userName']").val();
        var canFeature = $("input[name='canFeature']").val();

        if (userName == "")
            $("#joinForm").dialog("open");
        else if (canFeature == false)
            $("#favoriteFormNo").dialog("open");
        else
            $("#favoriteForm").dialog("open");

    });
    $("#addFeature").bind("click", function() {
        var userName = $("input[name='userName']").val();
        if (userName == "")
            $("#joinForm").dialog("open");
        else
            $("#featureForm").dialog("open");

    });



    $("#btnSubmitComment").click(function() {
        $("#commentForm").css("cursor", "wait");
        $("#btnSubmitComment").attr("disabled", "true")
        var mixId = $("input[name='mixId']").val();
        var comment = $("textarea[name='comment']").val();
        var userName = $("input[name='userName']").val();
        var memberId = $("input[name='memberId']").val();
        var data = "memberId=" + memberId + "&mixId=" + mixId + "&comment=" + encodeURIComponent(comment);
        $.post(
                    "postcomment.aspx", data, function() {
                        self.location = "getContents2.aspx?strMixId=" + mixId;
                    }
                );

    });


    $("#btnSubmitFeature").click(function() {
        $("#featureForm").css("cursor", "wait");
        $("#btnSubmitFeature").attr("disabled", "true")
        var mixId = $("input[name='mixId']").val();
        var comment = $("textarea[name='commentFeature']").val();
        var data = "mixId=" + mixId + "&comment=" + encodeURIComponent(comment);
        $.post(
                    "postFeature.aspx", data, function() {
                        self.location = "getContents2.aspx?strMixId=" + mixId;
                    }
                );

    });



    $("#btnSubmitFavorite").click(function() {
        $("#favoriteForm").css("cursor", "wait");
        $("#btnSubmitFavorite").attr("disabled", "true")
        var mixId = $("input[name='mixIdFav']").val();
        var comment = $("textarea[name='commentFav']").val();
        var data = "mixId=" + mixId + "&comment=" + encodeURIComponent(comment);
        $.post(
                    "postFavorite.aspx", data, function() {

                        $("#favoriteForm").css("cursor", "default");
                        $("#btnSubmitFavorite").attr("disabled", "false");
                        $("#favoriteForm").dialog("close");
                    }
                );

    });

});

function fb_popup(url) {
    window.open(url, 'fb_popup', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=626,height=436');
}

function myspace_popup(T, C, U, L)
{
var targetUrl = "http://www.myspace.com/Modules/PostTo/Pages/?" + "t=" + encodeURIComponent(T)
+ "&c=" + encodeURIComponent(C) + "&u="+ encodeURIComponent(U) + "&l=" + L;
window.open(targetUrl);
}

