var runningPlayer;

$(document).ready(function()
{
/*	if($.cookie('wasHere') != 'wasHere')
    {
        $("body").append("<a class=\"fancyPopUp\" href=\"#fancyPopUp\"></a>");
        $.cookie('wasHere', 'wasHere');
        $("a.fancyPopUp").fancybox(
        {
            'titleShow'		: false,
            'scrolling'		: 'no',
            'transitionIn'	: 'elastic',
            'transitionOut'	: 'elastic',
            'autoDimensions': false,
            'width'			: 900,
            'height'		: 700,
            'content'		: "<div id=\"fancyPopUp\"><a id=\"beatportLink\" href=\"http://www.beatport.com/\"  target=\"_blank\"></a></div>"
        });
        $("a.fancyPopUp").click();
    }	*/
	
    $("a.fancyCover").fancybox({
        'titleShow'		: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic'
    });
    
    $("form").submit(function()
    {
        var inputs = $("form").serialize();
//        alert(inputs);
        jQuery.ajax(
        {
            data: inputs,
            dataType: "xml",
            url: "plugins/mc/mcLabelBoss.php",
            type: "POST",
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
//                alert("XMLHttpRequest: " + XMLHttpRequest);
//                alert("textStatus: " + textStatus);
//                alert("errorThrown: " + errorThrown);
            },
            success: function(xml)
            {
//            	$(xml).find('response').each(function()
//                {
//                    for (key in werte) {
//					werte[key] = $(this).find(key).text();
//					fehler[key]	= $(this).find(key + 'fehler').text();
//				}
//			});
            },
            complete: function()
            {
//                f = false;
//			for (key in fehler) {
//				if (!(fehler[key] == "kein Fehler")) {
//                    //alert(fehler[key]);
//					f = true;
//				}
//			}
//			if (f == false) {
//				for (key in werte) {
//					setWert(key, werte[key]);
//				}
//				Overlay.getInstance().remove();
//			}
            }
        });
        $("input[name='eMail']").val("");
        return false;
    });
    
    var ids = new Array();
    $(".flashContainer").each(function()
    {
        var id;
        while(id == undefined || isAlreadyUsed(ids, id))
        {
            id = "flashContainer" + Math.round(Math.random() * 1000).toString();
        }
        ids.push(id);
        $(this).attr("id", id);
    });
    
	$(".contentElement ol.tracks li a[class='play']").click(function()
    {		
        $(this).parent().find(".flashContainer").stop(true, true).slideDown('normal', function()
		{
			if(($(this).children()).length == 0)
			{
				stopRunningPlayer();
				var id = $(this).attr("id");
				runningPlayer = id;
				var url = $(this).parent().find("a[class='play']").attr("href");
				$(this).flash(
				{
					flashvars:
                    {
						autoplay:true,
                    	id:id,
						url:url
                    },
                    hasVersion:10,
                    height:40,
                    params:
                    {
                        allowScriptAccess:'sameDomain',
                        menu:'false',
                        quality:'best',
                        wmode:'transparent'
                    },
                    swf:WP_PLUGIN_URL + '/mc/mcLabelBossPlayer.swf',
                    width:449
                })
            }
		});
		return false;
	});
	
    $(".contentElement ol.tracks li").hover(function()
    {
        $(this).css("background", "#D5D3D2");
    },
    function()
    {
        if($(this).find(".flashContainer").attr("id") != runningPlayer)
        {
            $(this).find(".flashContainer").stop(true, true).slideUp('normal', function()
            {
                $(this).empty();
            }).parent().css("background", "transparent");
        } else if($(this).find(".flashContainer").length == 0)
		{
			$(this).css("background", "transparent");
		}
    });
    
    function isAlreadyUsed(ids, id)
    {
        for(var i = 0; i < ids.length; i++)
        {
            if(id == ids[i])
            {
                return true;
            }
        }
        return false;
    }
});

    function stopRunningPlayer()
    {
        if(runningPlayer != undefined)
        {
            $("#" + runningPlayer).children().externalInterface(
            {
                method:'stop',
				success:function(data)
				{
				},
				error:function(error)
				{
				}
            }).parent().stop(true, true).slideUp('normal', function()
            {
                $(this).empty();
            }).parent().css("background", "transparent");
        }
    }    
    
    function playerWantStartPlaying(id)
    {
        $("#" + id).children().externalInterface(
        {
            method:'play',
			success:function(data)
			{
			},
			error:function(error)
			{
			}
        });
    }
