$(document).ready(function () {

	$(".expand").click(function(){
		if($(this).hasClass('colapse')){
			$(this).text('+').removeClass('colapse');
			$(this).closest('li').find('ul:first').addClass('hide');
		}
		else{
			$(this).text('-').addClass('colapse');
			$(this).closest('li').find('ul:first').removeClass('hide');
		}
	});
	$("#savepriority").click(function(){
		var category=new Array();
			$("#categoryList li").each(function(index){
				category.push({categoryid: $(this).attr('category'), priority:index});
			});
			var data=new Object();
			data.category=category;
			$.ajax({
				type:"POST",
				url:GLOBAL_BASE_PATH + "/productcategory/ajaxSavePriority/",
				data:data,
				success:function (msg) {
					jAlert(lblCategoryPrioritySuccess);
					$("#savepriority").attr('disabled','disabled');
				}
			}); 
	});
	
	$(".categoryList").sortable({
		items: "> li:not(.ui-state-disabled)",
		placeholder:'dd-placeholder',
		cursor:'move',
		update:function(){
			$("#savepriority").removeAttr('disabled');
		}
	});
    $('#ListStatusLink').click(function () {
        if ($("#ListStatusSelection").is(':visible'))
            $("#ListStatusSelection").slideUp();
        else {
            $("#ListStatusSelection").slideDown();
            $("#table_productcategories").children().children().show();
        }
    });
    $(document).on('click', "#ListStatusSelection a", function () {
        $(".ListAll").prop('checked',false);
        var curr_class = $(this).attr("rel");
        $("." + curr_class).prop("checked", true);
        var ListAllCount = $(".ListAll").size();
        var ListAllCheckedCount = $(".ListAll:checked").size();
        if (parseInt(ListAllCount) == parseInt(ListAllCheckedCount)) {
            $(".ListStatusLinkCheckbox,#ListStatusLinkCheckbox").prop('checked',true);
        } else {
            $(".ListStatusLinkCheckbox,#ListStatusLinkCheckbox").prop('checked',false);
        }
        $("#ListStatusSelection").slideUp();
    });
    $(document).on('click', ".ListAll", function () {
        var ListAllCount = $(".ListAll").size();
        var ListAllCheckedCount = $(".ListAll:checked").size();
        if (parseInt(ListAllCount) == parseInt(ListAllCheckedCount)) {
            $(".ListStatusLinkCheckbox,#ListStatusLinkCheckbox").prop('checked',true);
        } else {
            $(".ListStatusLinkCheckbox,#ListStatusLinkCheckbox").prop('checked',false);
        }
    });

    $(document).on('click', ".ListStatusLinkCheckbox,#ListStatusLinkCheckbox", function () {
        if ($(this).prop("checked") == true) {
            $(".ListAll").prop("checked",true);
            $("#ListStatusSelection").slideUp();

        } else {
            $(".ListAll").prop('checked',false);
            $("#ListStatusSelection").slideUp();
        }
    });
    $(document).on('click', ".expandable", function () {
        var id = $(this).attr("id");
        $("#div" + id).fadeIn();
        $(this).removeClass("expandable").addClass("collapsable");
    });
    $(document).on('click',".collapsable",function () {
        var id = $(this).attr("id");
        $("#div" + id).fadeOut();
        $(this).removeClass("collapsable").addClass("expandable");
    });
    $('.delete_button').click(function () {
        var thehref = $(this).attr("rel");
        $('#popup_title').html("Confirm");
        jConfirm(msgConfirmDelete, deleteText, function (r) {
            if (r == true) {
                window.location.href = GLOBAL_BASE_PATH + thehref;
                return true;
            } else {
                return false;
            }
        });
    });
    $("#submitBulkStatus").click(function () {
        if ($(".ListAll:checked").size() == 0) {
            jAlert(lblSelectItem, msgAlert);
            return false;
        }
        if ($("#bulkStatus").val() == '') {
            jAlert(msgSelectAStatus, msgAlert);
            return false;
        }
    });
    $(document).on("click", ".status-active,.status-inactive", function () {
        var obj = $(this);		
        var data = new Object();		
		if(!$(this).attr('categoryid')){
			 return false;
		}			
        data.categoryid = $(this).attr('categoryid');
        if ($(this).hasClass('status-active')) {
            data.categorystatus = 'Inactive';

        } else if ($(this).hasClass('status-inactive')) {
            data.categorystatus = 'Active';
        }
        $.ajax({			
            type: "POST",
            url: GLOBAL_BASE_PATH + "/productcategory/ajaxchangestatus/",
            data: data,			
            success: function (msg) {
                if (msg.trim() == "") {			
					location.reload();			
                }
            }
        });
    });
})