/**
 * More Module
 * @author Kevin McBriraty
 * More dropdown for the top navigation
 * @dependencies
 * none
 */
function MoreModule()
{

    /** name of this module */
    this._name = MoreModule.NAME;

    this._dataFile = "/static/frag/filters/filters.js";

    this._cont = $(".more-filters-wrapper");

    this._numColumns = 7;

    this._filters = null;

    this._filterURLBase = "/filters/";

    this._embeddableTitle = "Embeddable";
	this._embeddableURL = "/embeddable-games/index.jsp";

    this._filterStatus = null;
    this._state = '';
    this._states= {
        OPEN: "open",
        CLOSED: "closed"
    };

    /**
     * called by the page during initialization
     */
    this.initialize = function()
    {
        this._filterStatus = new CookieUtil();
        this._state = this._filterStatus.getAgStatus("filterNav");

        $("#subnav_toggle_link").bind("click",this,function(e){
            e.preventDefault();
            var module = e.data;
            if ($(this).hasClass("subnav_more")){
                module._onOpenDropDown();
            }else if ($(this).hasClass("subnav_close")){
                module._onCloseDropDown();
            }
        });

        this._loadData();

        if (this._state == this._states.OPEN ){
                  this._onOpenDropDown();
        }
    };


    this._loadData = function(){
       $.getScript(this._dataFile,bindContext(this,this._addFilters));
    };

    this._addFilters = function(data){
        this._filters = eval('('+data.slice(2,-2)+')').filters;  //remove comment-filters and convert string to obj
        var currentColumn = 0;
        for (var i=0;i<this._filters.length;i++){
            var title = this._filters[i].title;
            var url = this._filterURLBase + this._filters[i].url;
            if (title == this._embeddableTitle){url = this._embeddableURL;}
            var moreLink = "<div class='more-layover-link'><a href='"+url+"'>"+title+"</a></div>";

            this._cont.find(".more-layover-columns").children().eq(currentColumn).append(moreLink);
            currentColumn = (currentColumn + 1) % this._numColumns;
        }

    };

    this._onOpenDropDown = function(){
        this._state = this._states.OPEN;
        this._saveStatusCookie();
        $("#subnav_toggle_link").removeClass("subnav_more").addClass("subnav_close").html("Close");
		    $(".columns2").addClass("columns2_open");
        this._cont.show();
    };

    this._onCloseDropDown = function(){
        this._state = this._states.CLOSED;
        this._saveStatusCookie();
        $("#subnav_toggle_link").removeClass("subnav_close").addClass("subnav_more").html("More");
        $(".columns2").removeClass("columns2_open");
        this._cont.hide();
    };

    this._saveStatusCookie = function(){
       this._filterStatus.setAgStatus("filterNav",this._state);
    };
}
MoreModule.NAME = "MoreModule";

MoreModule.prototype = new Module();


