$(document).ready(function() {
	setup_playlist();

        $("img[@id^='recommend_btn_']").css("cursor", "pointer").click(function() {
                var song_id = $(this).attr("id").replace("recommend_btn_", "");

                if($("#recommend_song_id").length<1)
                        $(document.body).append("<input type='hidden' id='recommend_song_id' value='"+ song_id +"' />");
                else
                        $("#recommend_song_id").val(song_id);

                if($("#member_logged_in").val()=="yes")
                {
                        show_recommend_dialog();
                } else
                {
                        show_login_dialog("show_recommend_dialog");
                }
        });

        $("img[@id^='wishlist_btn_']").css("cursor", "pointer").click(function() {
                var song_id = $(this).attr("id").replace("wishlist_btn_", "");

                if($("#wishlist_song_id").length<1)
                        $(document.body).append("<input type='hidden' id='wishlist_song_id' value='"+ song_id +"' />");
                else
                        $("#wishlist_song_id").val(song_id);

                if($("#member_logged_in").val()=="yes")
                {
                        show_wishlist_dialog();
                } else
                {
                        show_login_dialog("show_wishlist_dialog");
                }
        });

});

function show_playlist_dialog()
{
        var playlist_song_id = $("#playlist_song_id").val();

        $.ajax({
                type: "POST",
                url: "/common/dialog/playlist/",
                data: {
                        playlist_song_id: playlist_song_id,
                        request_type: "show_playlist_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function show_wishlist_dialog()
{
        var wishlist_song_id = $("#wishlist_song_id").val();

        $.ajax({
                type: "POST",
                url: "/common/dialog/wishlist/",
                data: {
                        wishlist_song_id: wishlist_song_id,
                        request_type: "show_wishlist_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function show_recommend_dialog()
{
        var recommend_song_id = $("#recommend_song_id").val();
        $.ajax({
                type: "POST",
                url: "/common/dialog/recommend/",
                data: {
                        recommend_song_id: recommend_song_id,
                        request_type: "show_recommend_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function mouseleave() {
	$("TR.parent", this).removeClass("row-highlight");
	//$("TR.parent .bt_more", this).hide();
	$("TR.parent .bt_type", this).show();
}

function mouseenter() {
	$("TR.parent", this).addClass("row-highlight");
	$("TR.parent .bt_type", this).hide();
	//$("TR.parent .bt_more", this).show();
}

function setup_playlist() {
	$(".playlist .child").hide();
	//$(".playlist THEAD:first-child TR.parent TD").addClass("b-top");
	if($("#playlist_no_border").val()!="yes")  $(".playlist-container LI:first-child").addClass("b-top");
	//clicking more expand
	$("input[name=bt_more]").toggle(
		function() {
			$("TR.parent TD", $(this).parents("THEAD")).css("border-bottom", "0px");
			$("TR.child", $(this).parents("THEAD")).toggle();
			$(this).attr("src", "/images/common/bt_arrow-up.png");
			$(this).parents("THEAD").unbind("mouseleave");
		},
		function() {
			//$("TR.parent TD", $(this).parents("THEAD")).css("border-bottom", "1px dotted #E2E2E2");
			show_bottom_border($("TR.parent TD", $(this).parents("THEAD")));
			$(".tabs div[class^=tab_]", $(this).parents("THEAD")).hide();	
			$("TR.child", $(this).parents("THEAD")).toggle();
			$(this).attr("src", "/images/common/bt_arrow-down.png");
			$(this).parents("THEAD").bind("mouseleave", mouseleave);
		}
	);
	
	//clicking more expand
	$("input[name=bt_more-small]").toggle(
		function() {
			$("TR.parent TD", $(this).parents("THEAD")).css("border-bottom", "0px");
			$("TR.child", $(this).parents("THEAD")).toggle();
			$(this).attr("src", "/images/common/bt_arrow-up.png");
			$(this).parents("THEAD").unbind("mouseleave");
		},
		function() {
			$("TR.parent TD", $(this).parents("THEAD")).css("border-bottom", "1px dotted #E2E2E2");
			$(".tabs div[class^=tab_]", $(this).parents("THEAD")).hide();	
			$("TR.child", $(this).parents("THEAD")).toggle();
			$(this).attr("src", "/images/common/bt_arrow-down.png");
			$(this).parents("THEAD").bind("mouseleave", mouseleave);
		}
	);

	$("img[name=close_tab]").click(function() {
		$(this).parents("div.tab_items").hide();
	});
	
	//playlist tabs
	$(".actions a[name=bt_video]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();
		$(".tab_video", $(this).parents("TR.child")).show();
		return false;
	});
	
	$(".actions a[name=bt_playlist]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											 
		$(".tab_playlist", $(this).parents("TR.child")).show();
		return false;
	});
	
	$(".actions a[name=bt_recommend]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_recommend", $(this).parents("TR.child")).show();
		return false;
	});
	
	$(".actions a[name=bt_wishlist]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_wishlist", $(this).parents("TR.child")).show();
		return false;
	});
	
	$(".actions a[name=bt_cart]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_cart", $(this).parents("TR.child")).show();
		$(".tab_cart .pointer-up", $(this).parents("TR.child")).css("margin-left", "283px");
		return false;
	});
	
	$(".actions a[name=bt_gift]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_gift", $(this).parents("TR.child")).show();
		$(".tab_gift .pointer-up", $(this).parents("TR.child")).css("margin-left", "333px");
		return false;
	});
	
	$(".actions a[name=bt_cart-small]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_cart", $(this).parents("TR.child")).show();
		$(".tab_cart .pointer-up", $(this).parents("TR.child")).css("margin-left", "153px");
		return false;
	});
	
	$(".actions a[name=bt_cart-feed]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_cart", $(this).parents("TR.child")).show();
		$(".tab_cart .pointer-up", $(this).parents("TR.child")).css("margin-left", "228px");
		return false;
	});
	
	$(".actions a[name=bt_gift-small]").click(function() {
		$(".tabs div[class^=tab_]", $(this).parents("TR.child")).hide();											  
		$(".tab_gift", $(this).parents("TR.child")).show();
		$(".tab_gift .pointer-up", $(this).parents("TR.child")).css("margin-left", "203px");
		return false;
	});

	//playlist mouseover
	$(".playlist THEAD").bind("mouseenter", mouseenter).bind("mouseleave", mouseleave);

	// remove button
	$(".playlist .bt_remove").css("cursor", "pointer").click(function() {
		var callback = $(this).parents("TABLE").attr("remove_cb");

		if (!eval('(typeof ' + callback + '==\'function\');')) {
			$(this).parents("THEAD").hide();
		} else {
			window[callback]($(this).attr("song_id"), $(this));
		}
	});
}

function show_bottom_border(obj)
{
	if (!obj.parents("TABLE").hasClass("playlist-inner")) obj.css("border-bottom", "1px dotted #E2E2E2");
}

function playlist_add2playlist(id)
{
	if($("#playlist_song_id").length<1)
		$(document.body).append("<input type='hidden' id='playlist_song_id' value='"+ id +"' />");
	else
		$("#playlist_song_id").val(id);

	if($("#member_logged_in").val()=="yes")
	{
		show_playlist_dialog();
	} else
	{
		show_login_dialog("show_playlist_dialog");
	}
}

function playlist_add2wishlist(song_id)
{

                if($("#wishlist_song_id").length<1)
                        $(document.body).append("<input type='hidden' id='wishlist_song_id' value='"+ song_id +"' />");
                else
                        $("#wishlist_song_id").val(song_id);

                if($("#member_logged_in").val()=="yes")
                {
                        show_wishlist_dialog();
                } else
                {
                        show_login_dialog("show_wishlist_dialog");
                }

}

function playlist_add2recommend(song_id)
{
                if($("#recommend_song_id").length<1)
                        $(document.body).append("<input type='hidden' id='recommend_song_id' value='"+ song_id +"' />");
                else
                        $("#recommend_song_id").val(song_id);

                if($("#member_logged_in").val()=="yes")
                {
                        show_recommend_dialog();
                } else
                {
                        show_login_dialog("show_recommend_dialog");
                }
}

function add_to_cart_alt(type, id)
{
	if($("#cart_item_type").length>0)
		$("#cart_item_type").val(type);
	else
		$(document.body).append('<input type="hidden" id="cart_item_type" name="cart_item_type" value="'+type+'" />');

	if($("#cart_item_id").length>0)
		$("#cart_item_id").val(id);
	else
		$(document.body).append('<input type="hidden" id="cart_item_id" name="cart_item_id" value="'+id+'" />');

	if($("#member_logged_in").val()=="yes")
	{
		show_cart_dialog();
	} else
	{
		show_login_dialog("show_cart_dialog");
	}
}

function show_cart_dialog()
{
        var item_type = $("#cart_item_type").val();
        var item_id = $("#cart_item_id").val();

        $.ajax({
                type: "POST",
                url: "/common/dialog/add-cart/",
                data: {
                        cart_item_id: item_id,
                        cart_item_type: item_type,
                        request_type: "show_cart_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function confirm_add_cart()
{
        var cart_item_type = $("#cart_item_type").val();
        var cart_item_id = $("#cart_item_id").val();

	$.ajax({
		type: "POST",
		url: "/common/dialog/add-cart/",
		data: {
			cart_item_type:	cart_item_type,
			cart_item_id: cart_item_id,
			is_dialog: "1",
			request_type: "add_to_cart"
		},
		beforeSend: function(){
			show_dialog_loading();
		},
		success: function(xml){
			if($("#cart_item_box").length>0) reload_cart_list();

                        $("#dialog_box").html(xml);

                        setTimeout(function() {
                                $("#dialog_box").fadeOut(function() {
                                        $("#dialog_box").remove();
                                        $("#dim_overlay").remove();
                                        if($("#cart_item_box").length>0) reload_cart_list();
					reload_hd_cart_count();
                                });
                        }, 1000);
		}
	});
}

function add_to_cart(type, id, obj)
{
	var cart_box_id = type+"_cart_"+id;

	if($("#"+cart_box_id).css("display")=="none")
	{
        	$(document.body).append('<input type="hidden" id="cart_item_type" name="cart_item_type" value="'+type+'" />');
	        $(document.body).append('<input type="hidden" id="cart_item_id" name="cart_item_id" value="'+id+'" />');

        	if($("#member_logged_in").val()=="yes")
	        {
        	        check_add_cart();
	        } else
        	{
                	show_login_dialog("check_add_cart");
	        }
	} else
	{
		return false;
	}
}

function check_add_cart()
{
        var cart_item_type = $("#cart_item_type").val();
        var cart_item_id = $("#cart_item_id").val();

	var cart_box_id = cart_item_type+"_cart_"+cart_item_id;

	if($("#"+cart_box_id).html().replace(/^\s+|\s+$/g,"").length>0)
	{
		if(cart_item_type=="song" && $("#"+cart_box_id).parents("TR.child").css('display')=="none") $("#bt_more_"+cart_item_id).trigger('click');
		$("#"+cart_box_id).siblings("div.tab_items").hide();
		$("#"+cart_box_id).show();
	} else
	{
                $.ajax({
                        type: "POST",
                        url: "/common/dialog/add-cart/",
                        data: {
                                cart_item_type: cart_item_type,
                                cart_item_id: cart_item_id,
                                request_type: "add_to_cart"
                        },
                        beforeSend: function(){
                                $("#"+cart_box_id).parents("TR.child").show();
                                $("#"+cart_box_id).html('<div style="width:100%;text-align:center;"><img src="/images/common/loading.gif" /> Loading</div>').slideDown();
                        },
                        success: function(xml){
                                $("#"+cart_box_id).html(xml);

                                if($("#cart_item_box").length>0) reload_cart_list();

                                setTimeout(function() {
                                        $("#"+cart_box_id).fadeOut(function() {
                                                $("#"+cart_box_id).parents("TR.child").hide();
                                                $("#"+cart_box_id).html('');
                                        });
                                }, 2000);
                        }
                });
	}

        $("#cart_item_type").remove();
        $("#cart_item_id").remove();
}

function reload_cart_list()
{
	if(urldecode($("#request_uri").val())=="/buy/checkout/")
	{
		var checkout = 1;
	} else
	{
		var checkout = 0;
	}

        $.ajax({
                type: "POST",
                url: "/common/dialog/add-cart/",
                data: {
			checkout: checkout,
                        request_type: "reload_cart_list"
                },
                beforeSend: function(){
                },
                success: function(xml){
                        $("#cart_item_box").html(xml);
			if(checkout==1)
			{
				var cart_count = parseInt($("#cart_count").val());
				if(cart_count<1) 
				{
					$("#cart_payment_box").hide();
					$("#pay_prepaid_box").hide();
				}
			}
                }
        });
}

function reload_hd_cart_count()
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/add-cart/",
                data: {
                        request_type: "reload_hd_cart_count"
                },
                beforeSend: function(){
                },
                success: function(xml){
                        $("#hd_cart_count").html(xml);
                }
        });
}

function remove_cart_item(id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/remove-cart-item/",
                data: {
			cart_item_id: id,
                        request_type: "load_remove_cart_item_dialog"
                },
                beforeSend: function(){
			show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });	
}

function add_gift_item(type, id, member_id)
{
	if($("#gift_item_type").length>0)
		$("#gift_item_type").val(type);
	else
		$(document.body).append('<input	type="hidden" id="gift_item_type" name="gift_item_type"	value="'+type+'" />');

	if($("#gift_item_id").length>0)
		$("#gift_item_id").val(id);
	else
		$(document.body).append('<input	type="hidden" id="gift_item_id"	name="gift_item_id" value="'+id+'" />');

	var gift_member_id;
	if(member_id.length>0)
	{
		gift_member_id = member_id;
	} else
	{
		gift_member_id = 0;
	}

	if($("#gift_member_id").length>0)
		$("#gift_member_id").val(gift_member_id);
	else
		$(document.body).append('<input type="hidden" id="gift_member_id" name="gift_member_id" value="'+gift_member_id+'" />');

	if($("#member_logged_in").val()=="yes")
	{
		show_gift_dialog();
	} else
	{
		show_login_dialog("show_gift_dialog");
	}

	$("#gift_item_type").remove();
	$("#gift_item_id").remove();
}

function show_gift_dialog()
{
	var item_type = $("#gift_item_type").val();
	var item_id = $("#gift_item_id").val();
	var member_id = $("#gift_member_id").val();
	$.ajax({
		type: "POST",
		url: "/common/dialog/add-gift-item/",
		data: {
			gift_item_id: item_id,
			gift_item_type: item_type,
			gift_member_id: member_id,
			request_type: "show_gift_dialog"
		},
		beforeSend: function(){
			show_dialog_loading();
		},
		success: function(xml){
			if($("#dim_overlay").length<1)
			{
				$(document.body).append("<div id='dim_overlay'></div>");
				$("#dim_overlay").show();
			}
			if($("#dialog_box").length<1)
			{
				$(document.body).append("<div id='dialog_box' style='display:none;'></div>");
			}
			$("#dialog_box").html(xml).show();
		}
	});
}

function download_track(song_id)
{
	window.location = "/common/artist/ajax/download-track/?d=" + song_id;
}
