﻿        $(document).ready(
            function()
            {
                //TOP NAV:
	            $("#nav li ul").hide(); 

	            $("#nav li.nav").hover(
                    function () {
		            $(this).children("ul").fadeIn("fast");
                    },function(){
		            $(this).children("ul").fadeOut("fast");
	            });
	            
	            //THEME IMAGES:
	            $("img.theme").click(
                    function () {
                        if ($(this).css("width") == "auto")
    		                $(this).css("width", "100px").css("position", "block");
    		            else
    		                $(this).css("width", "auto").css("position", "relative");
	                }
	            );
	            
	            //DROPDOWN LIST WITH ADD NEW:
	            $("input.tbAddNew").hide();
	            $(".groupDropdown").change(
	                function() {
	                    changeCat($(this));
	                }
	            );
	            $(".groupDropdown").each(
	                function (i) {
	                    changeCat(this);
	                }
	            );
	            $(".catDropdown").change(
	                function() {
	                    loadCatGroups(this);
	                }
	            );
	            $(".catDropdown").each(
	                function() {
	                    loadCatGroups(this);
	                }
	            );
	                        
                $("#txtNewItem").keyup(
	                function(event) {
	                    if (event.keyCode == 13)
	                        saveUserFeed(this, "0");
	                }
	            );


	            //HIDDEN ELEMENTS:
	            $("div.notification").hide();
	            
	            
	        }
	    );
	    
	    function loadCatGroups(dd)
	    {
	        //Get groups if a cat was selected, otherwise show textbox for new
	        changeCat(dd);
            if ($(dd).val() != "0")
            {
                $(dd).siblings(".tbAddNew").fadeOut("fast");
	            $.post('/webservice/palinternal.asmx/GetFeedGroupsForCat',
                    {catGuid : $(dd).val()},
                    function (data) {
                        loadFeedGroups(data);
                    },
                    "xml"
	            );	        
            }
            else
            {
                $(dd).siblings(".tbAddNew").fadeIn("fast");
            }
	    }
	    
	    function loadFeedGroups(data)
	    {
	        //Clear it out
            $(".groupDropdown").children().remove();
            //Add groups for this category
	        $(data).find("FeedCatResponse").each(
	            function () {
	                var liGroup = document.createElement("option");
	                var name = $(this).children("Name").text();
	                var val = $(this).children("CatGuid").text();
	                $(liGroup).attr("value", val);
	                $(liGroup).text(name);
	                $(".groupDropdown").append(liGroup);
	            }
	        );
	        //Add "add new" option at the end:
            var liNew = document.createElement("option");
            $(liNew).attr("value", "0");
            $(liNew).text("[Add New...]");
            $(".groupDropdown").append(liNew);
            $(".groupDropdown").change();
	    }

        // Show or hide "add new" textbox for this dropdown:
        function changeCat(dd) {
            if ($(dd).val() != "0")
                $(dd).siblings(".tbAddNew").fadeOut("fast");
            else
                $(dd).siblings(".tbAddNew").fadeIn("fast");
        }

	    function rdoActiveTheme_Click(distID, themeID)
	    {
	        $.post('/webservice/palinternal.asmx/SetActiveDistributorPalTheme',
                {distID : distID, themeID : themeID}
	        );
	    }

	    function aSendWelcomeEmail_Click(userGuid)
	    {
	        $.post('/webservice/palinternal.asmx/SendWelcomeEmail',
                {guid : userGuid},
                function(data) {
                    $("div.notification").fadeIn();
                    setTimeout('$("div.notification").fadeOut();', 5000);
                }
	        );
	    }
	    
/* my feeds admin */

    function addToUserFeedsList(txt, id)
    {
        var li = $("#liTemplate").clone()[0];
        $(li).children("#spanItem").html(txt);
        $(li).children("#txtItem").val(txt);
        $(li).children("#divID").html(id);
        $(li).children("#aValidateUserFeed").attr('href', 'http://validator.w3.org/feed/check.cgi?url=' + txt);
        $(li).fadeOut();
        $("#rssList").append($(li));
        $(li).fadeIn();
    }

    function getUserFeeds()
    {
        $.post('/webservice/palinternal.asmx/GetUserFeeds',
            {},
            function (data) {
                $("#rssList").empty();

                $(data).find("Feed").each( function()
                {
                    var url = $(this).children("url")[0];
                    var id = $(this).children("id")[0];
                    addToUserFeedsList($(url).text(), $(id).text());
                })

	            $("a#aDelUserFeed").click(
	                function() {
	                    if (confirm("Are you sure you want to remove this feed?"))
	                    {
	                        $(this).parent().slideUp();
	                        removeUserFeed($(this).siblings("#divID").html());
	                    }
	                }
	            );	            
	            
	            $("div#spanItem").click(
	                function() {
	                    $(this).fadeOut("fast", 
	                        function() {
	                            $(this).siblings("#aDelUserFeed, #aValidateUserFeed").hide();
	                            $(this).siblings("#txtItem, #aCancelUserFeed, #aSaveUserFeed").fadeIn("fast");
	                        });
	                }
	            );

	            $("a#aCancelUserFeed").click(
	                function() {
	                    $(this).siblings("#txtItem").fadeOut("fast", 
	                        function() {
	                            $(this).siblings("#aSaveUserFeed, #aCancelUserFeed").hide();
	                            $(this).siblings("#spanItem, #aDelUserFeed, #aValidateUserFeed").fadeIn("fast");
	                        });
	                }
	            );
	            
	            $("a#aSaveUserFeed").click(
	                function() {
	                    $(this).siblings("#txtItem").fadeOut("fast", 
	                        function() {
	                            var id = $(this).siblings("#divID")[0]
	                            saveUserFeed(this, $(id).text());
	                            $(this).siblings("#aSaveUserFeed, #aCancelUserFeed").hide();
	                            $(this).siblings("#spanItem, #aDelUserFeed, #aValidateUserFeed").fadeIn("fast");
	                        });
	                }
	            );
	            
	            $("input#txtItem").keyup(
	                function(event) {
	                    if (event.keyCode == 13)
	                    {
	                        var id = $(this).siblings("#divID")[0]
	                        $(this).hide();
	                        saveUserFeed(this, $(id).text());
	                    }
	                }
	            );

            }
        );
    }
    
    function saveUserFeed(url, id)
    {
        if (id == "0")
            {
                addToUserFeedsList("[saving...]", "");
            }
        else
            $(url).siblings("#spanItem").hide().html("[saving...]").fadeIn("fast");
        
        $.post('/webservice/palinternal.asmx/SavePalUserFeed',
            { url : url.value, id : id },
            function (data) {
                getUserFeeds();
            }
        );        
    }

    function removeUserFeed(id)
    {
        $.post('/webservice/palinternal.asmx/RemovePalUserFeed',
            { id : id},
            function (data) {
                getUserFeeds();
            }
        );        
    }


/* END my feeds admin */
