$(function() {
	var player = document.getElementById("audio-player");
	var discoListener = new Object();
	var currentTrackId = null;
	
	discoListener.onUpdate = function(isPlaying){
		console.log("discoListener.onUpdate = " + isPlaying);
		if(!isPlaying){
			$("#stop-"+currentTrackId).hide();
			$("#play-"+currentTrackId).show();
		}
	};
	
	audioListener.addListener(discoListener);
	
	$(".audio-play").click(function(e){
		player.SetVariable("method:setUrl", $(this).attr("href"));
    	player.SetVariable("method:play", "");
    	player.SetVariable("enabled", "true");
		
		if(currentTrackId!=null){
			$("#stop-"+currentTrackId).hide();
			$("#play-"+currentTrackId).show();
		}
		
		currentTrackId = $(this).attr("target");
		$("#stop-"+currentTrackId).show();
		$("#play-"+currentTrackId).hide();
		
		e.preventDefault();
	});
	
	$(".audio-stop").click(function(e){
		player.SetVariable("method:stop", "");
		
		$("#stop-"+currentTrackId).hide();
		$("#play-"+currentTrackId).show();
		currentTrackId = null;
		
		e.preventDefault();
	});
	
	$("#dialog").dialog({width: 1024, modal: true, autoOpen: false, resizable: false});
	
	$("#cart-items").load("Pages/Common/cart.jsp");
	$("#cart-container").hover(function(){
		$("#cart-items").show();
	},function(){
		$("#cart-items").hide();
	});
	
	$("#link-logout").click(function(e){
		$.get("services/web/logout", function(data){
			location.reload();
		});
		e.preventDefault();
	});
	
	$(".link-login").click(function(e){
		var title = $(this).attr("title");
		$("#dialog").dialog("close");
		$("#dialog").load("Pages/Account/login.jsp", function(){
			$("#dialog").dialog({title: title, width: 1024});
			$("#dialog").dialog("open");
		});
		e.preventDefault();
	});
	
	$(".link-favorit").click(function(e){
		var trackId = $(this).attr("href");
		var link = $(this);
		$.post("services/web/addToFavorit", {id: trackId}, function(){
			link.hide();
		});
		e.preventDefault();
	});
	
	$(".link-purchase").click(function(e){
		var title = $(this).attr("title");
		var subscriptionId = $(this).attr("href");
		$("#dialog").dialog("close");
		$("#dialog").load("Pages/Common/purchaseSubscription.jsp", {id: subscriptionId}, function(){
			$("#dialog").dialog({title: title, width: 1024});
			$("#dialog").dialog("open");
		});
		e.preventDefault();
	});
		
	$(".link-cart").click(function(e){
		var itemId = $(this).attr("href");
		var itemType = $(this).attr("type");
		$.post("services/web/addCartItem", {id: itemId, type: itemType}, function(){
			$("#cart-items").load("Pages/Common/cart.jsp", function(){
				$("#cart-items").fadeIn(500);
				$("#cart-items").fadeOut(500);
			});
		});
		e.preventDefault();
	});
	
	$(".link-autoprod").click(function(e){
		var title = $(this).attr("title");
		$("#dialog").dialog("close");
		$("#dialog").load("Pages/Account/autoprod.jsp", function(){
			$("#dialog").dialog({title: title, width: 1024});
			$("#dialog").dialog("open");
		});
		e.preventDefault();
	});
	
	$("#more-releases").click(function(e){
		$("#more-titles").show();
		$("#less-releases").show();
		$(this).hide();
		e.preventDefault();
	});
	
	$("#less-releases").click(function(e){
		$("#more-titles").hide();
		$("#more-releases").show();
		$(this).hide();
		e.preventDefault();
	});
	
	$("#language").change(function(){
		$.getJSON("services/web/changeLanguage",{lang:$(this).val()},function(json){
			if(json.success){
				location.reload();
			}
		});
	});
	
	$(".album-details").click(function(e){
		var title = $(this).attr("title");
		var albumId = $(this).attr("href");
		$("#dialog").dialog("close");
		$("#dialog").load("Pages/Audio/album.jsp", {id: albumId}, function(){
			$("#dialog").dialog({title: title, width: 1024});
			$("#dialog").dialog("open");
		});
		e.preventDefault();
	});
	
	$(".link-playlist").click(function(e){
		var title = $(this).attr("title");
		var trackId = $(this).attr("href");
		$("#dialog").dialog("close");
		$("#dialog").load("Pages/Common/selectPlaylist.jsp", {id: trackId}, function(){
			$("#dialog").dialog({title: title, width: 300});
			$("#dialog").dialog("open");
		});
		e.preventDefault();
	});
	
	$(".link-contact").click(function(e){
		var title = $(this).attr("title");
		$("#dialog").dialog("close");
		$("#dialog").load("Pages/Common/contact.jsp", function(){
			$("#dialog").dialog({title: title, width: 600});
			$("#dialog").dialog("open");
		});
		e.preventDefault();
	});
});
