/**
 * Handles Dashboard
 */

(function(){
    if(!window.cs) {
        return;
    }
    
    var dashboard = window.cs.dashboard = {
        // default options/settings
        empty: null
    };
    
    dashboard.init = function(businessID, eventID) {
        // initialize default actions
        
        // Initiate Tabs for Upcoming Gatherings
        $("#upcomingGatherings").tabs();
        
        // Set up search bar
        $("#searchBar_input").focus(function(){
           if($(this).val() == 'ex: "Drink Specials", "Concert"') {
               $(this).removeClass("unselected");
               $(this).val("");
           }     
        });
        $("#searchBar_input").blur(function(){ 
           if($(this).val() == "") $(this).addClass("unselected").val('ex: "Drink Specials", "Concert"');    
        });                

        // Friend Requests
        $('#sideList .buttons a').click(function(){
            var action = $(this).find(".icon").html();
            var inviterID = $(this).parent().parent().find(".inviterID").val();
            var crewID = $(this).parent().parent().find(".crewID").val();
            var groupID = $(this).parent().parent().parent().find("#requestGroupSelect-"+crewID).val();
            if (groupID == "createGroup") {
                groupID = -1;
            }
            cs.forms.doAcceptInvite(action, inviterID, crewID, groupID, this);
            setTimeout("cs.dashboard.removeSidelist()", 1000);
            return false;
        });
        
        // Create Group form Friend Request
        $('.requestGroupSelect').change(function(){
            if($(this).val() == "createGroup") {
                return cs.lightbox.createGroup($(this).attr('id').substr(19,10), 0);
            }
        });
		if (eventID > 0) {
			cs.lightbox.startEventGathering(eventID);
		}
        else if (businessID != -1) {
            // start the start a gathering lightbox
            cs.lightbox.startGathering(businessID);
        }
    };

    dashboard.removeSidelist = function() {
        var inviteCount = parseInt($('#requests_InviteCount').html());
        if (inviteCount == 0) {
            $('#inviteRequestsCount').remove();
            $('#sideList').remove();
        }
    };

    dashboard.removeUpcomingGatheringParents = function(gatheringID){
        var todayParent = $('.gathering-'+gatheringID).parent().parent("#today");
        var thisWeekParent = $('.gathering-'+gatheringID).parent().parent().parent().parent("#thisWeek");
        var pastParent = $('.gathering-'+gatheringID).parent().parent().parent().parent("#past");
        var futureParent = $('.gathering-'+gatheringID).parent().parent().parent().parent("#future");
        var immediateParent = $('.gathering-'+gatheringID).parent();
        $('.gathering-'+gatheringID).remove();
        $(immediateParent).children('.gathering').each(function(i){
           if(i % 2 == 0) {
               if ($(this).hasClass("alt")) {
                   $(this).removeClass("alt");
               }
           } else {
               if (!$(this).hasClass("alt")) {
                   $(this).addClass("alt");
               }
           }
        });
        
        if (todayParent.length) {
            // Actions on today tab
            if ($(todayParent).children('.gatheringsThisDay').children().length == 0) {
                // No more gatherings, add teaser text
                $(todayParent).html("<p>You have no upcoming gatherings today. Why don't you start one?</p><div class='hr'></div>");
            }
        }
        if (thisWeekParent.length) {
            // Actions on this week tab
            jQuery.each(immediateParent, function(i, val) {
                if ($(val).parent().parent(".gatheringsWeek").length) {
                    if ($(val).children().length == 0) {
                        // No children for this day, remove
                        $(val).prev().remove();
                        $(val).remove();
                    }
                }
            });
        }
        if (pastParent.length) {
            // Actions on past tab
            jQuery.each(immediateParent, function(i, val) {
                if ($(val).parent().parent(".gatheringsPast").length) {
                    if ($(val).children().length == 0) {
                        $(val).prev().remove();
                        $(val).remove();
                    }
                }
            });
        }
        if (futureParent.length) {
            // Actions on future tab
            jQuery.each(immediateParent, function(i, val) {
                if ($(val).parent().parent(".gatheringsFuture").length) {
                    if ($(val).children().length == 0) {
                        $(val).prev().remove();
                        $(val).remove();
                    }
                }
            });
        }
        if ($(thisWeekParent).children('.gatheringsWeek').children('li').children().length == 0) {
            // All days removed, show teaser text
            $(thisWeekParent).html("<p>You have no upcoming gatherings this week. Why don't you start one?</p><div class='hr'></div>");
        }
        if ($(pastParent).children(".gatheringsPast").children('li').children().length == 0) {
            $(pastParent).html("<p>You have no past gatherings. Why don't you start one?</p><div class='hr'></div>");
        }
        if ($(futureParent).children(".gatheringsFuture").children('li').children().length == 0) {
            $(futureParent).html("<p>You have no upcoming gatherings in the future. Why don't you start one?</p><div class='hr'></div>");
        }
        return false;
    };

    dashboard.removeUpcomingGatheringRow = function(gatheringID){
        $.getJSON('/hideGathering.json?gatheringID='+gatheringID,
            function(json){
                if (json.success) {
                    $('.gathering-'+gatheringID).slideUp("slow").hide().removeClass("gathering");
                    setTimeout('cs.dashboard.removeUpcomingGatheringParents('+gatheringID+');', 1000);
                    return false;
                } else {
                    // What to do when remove gathering fails
                    cs.lightbox.message(json.message, 'Error Removing Gathering');
                }
            }
        );
    };
                                      
})();
