var PRODUCT_JSON_URL = "/jsonrpc.json";


document.observe("categoryData:loaded", function() {
    productPage.mpp.init();
});

var params = Object.extend({
        "category" : [CATEGORY_ID],
    	"category_fields" : ["CATEGORY_ID",
                "DISPLAY_NAME",
                "product"]
        }, productPage.DETAIL_VIEW_QUERY.PRODUCT_FIELDS);
params = Object.extend (params,
        productPage.DETAIL_VIEW_QUERY.SKU_FIELDS);
params = [params];

document.observe("dom:loaded", function() {
    CatProdData = new CatProdPageData(params);
});

// We will NOT show a bestseller section for these categories:
var g_SkipBestsellerSection = [
	"CATEGORY22880",
	"CATEGORY21900"
];

productPage.categoryFilterMenus = new Hash({
    CATEGORY4918 : { // Mositurizers
        filters: [ { label: "Skin Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    },
    CATEGORY4900 : { // Foundations
        filters: [ { label: "Form", field: "FORMULA" },
                   { label: "Skin Type", field: "prod_skin_type_string" } ]
    },
    CATEGORY4906 : { // Powder
        filters: [ { label: "Form", field: "FORMULA" },
                   { label: "Coverage", field: "COVERAGE" } ]
    },
    CATEGORY4896 : { // Concealor
        filters: [ { label: "Skin Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" },
                   { label: "Coverage", field: "COVERAGE" } ]
    },
    CATEGORY4897 : { // Eye Liner
        filters: [ { label: "Form", field: "FORMULA" } ]
    },
    CATEGORY4898 : { // Eye Shadow
        filters: [ { label: "Form", field: "FORMULA" } ]
    },
    CATEGORY4901 : { // Lip Gloss
        filters: [ { label: "Benefit", field: "BENEFITS" } ]
    },
    CATEGORY4904 : { // Mascara
        filters: [ { label: "Benefit/Priority", field: "BENEFITS" } ]
    },
    CATEGORY4894 : { // Blush
        filters: [ { label: "Form", field: "FORMULA" } ]
    },
    CATEGORY8227 : { // Bronzer
        filters: [ { label: "Form", field: "FORMULA" } ]
    },
    CATEGORY4903 : { // Lipstick
        filters: [ { label: "Benefit", field: "BENEFITS" },
                   { label: "Coverage", field: "COVERAGE" } ]
    },
    CATEGORY21362: { // Makeup Removers/Cleansers
        filters: [ { label: "Skin Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    },
    CATEGORY21363: { // Exfoliators/Masks
        filters: [ { label: "Skin Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    },
    CATEGORY21364: { // Eye & Lip Care
        filters: [ { label: "Skin Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    },
    CATEGORY21374: { // Hand & Body
        filters: [ { label: "Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    }, 
    CATEGORY22356: { // Men's Skincare
        filters: [ { label: "Skin Type", field: "prod_skin_type_string" } ]
    },
    CATEGORY22357: { // Men's Shave
        filters: [ { label: "Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    },
    CATEGORY22358: { // All Men's Grooming
        filters: [ { label: "Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    },
    CATEGORY21370: { // SPF For Face and Body
        filters: [ { label: "Concern", field: "SKIN_CONCERN_1::SKIN_CONCERN_2::SKIN_CONCERN_3" } ]
    }
});

productPage.categoryFilterMenus.each(function(record) {
    var cat = productPage.categoryFilterMenus.get(record.key);
    cat.sort= [ { label: "Alphabetically" , field: "PRODUCT_NAME" },
                { label: "Bestseller" , field: "DISPLAY_ORDER" } ];
});


productPage.mpp = function() {
    return {
        init: function() {
//            console.log("productPage.mpp.init");

            // product Table object parameters
            var args = {
                containerID: "products-grid-container",
                filter_form_selector: "form.productstable-filters",
                filter_menu_class: "productslist_filter",
                sort_menu_class: "productslist_sort",
                maxItems: 0,
                startIndex: 0
            };
        
            args.tableData = CatProdData.category.products;
            var prodsLength = args.tableData.length;

            //if (prodsLength <= 9) {          
            if (prodsLength <= 9 || g_SkipBestsellerSection.include(CATEGORY_ID) ) {          
////                sort (default = show all, bestseller)
                $('bestseller_accordion').hide();
                $('filter-toolbar').hide();
                productPage.productsTable = new productPage.ProductsTable(args);
            } else {
////                sort (default = show all, bestseller)
                $('bestseller_accordion').style.display = "block";
                $('filter-toolbar').style.display = "block";
                args.maxItems = undefined;
                args.startIndex = 3;
                
                productPage.productsTable = new productPage.ProductsTable(args);
                var hideFilters = true;

				if (productPage.categoryFilterMenus.get(CATEGORY_ID)) {
					var menuMetaData = productPage.categoryFilterMenus.get(CATEGORY_ID);
					productPage.productsTable.createFilterMenus({
						filterMenuContainerNode : $("filter_menu_container"),
						tableData    : args.tableData,
						menuMetaData : menuMetaData,
						menuWidth    : 140 });
                    hideFilters = false;
				}
                var sortNode = this.initSortMenu();
                if (sortNode) {
                    productPage.productsTable.addSortNode(sortNode);
                    hideFilters = false;
                }
                if (hideFilters) {
                    $('filter-toolbar').hide();                
                }
                args.maxItems = 3;
                args.startIndex = 0;
                args.containerID = "bestsellers-container";
                productPage.bestsellerTable = new productPage.ProductsTable(args);
            }
            var bs_accordion = new Accordion('bestseller_accordion');
            bs_accordion.activate($$('#bestseller_accordion .accordion_toggle')[0]);
            $('filter-toolbar').observe('table:filter', function(evt) {
//            	console.log(evt);
                if ($$('#bestseller_accordion .accordion_content')[0].style.display !== 'none') {
                    bs_accordion.deactivate();
                    productPage.productsTable.startIndex = 0;
                }
            })
        },
        initSortMenu : function () {
            if (productPage.categoryFilterMenus.keys().indexOf(CATEGORY_ID) > -1) {
                var cat = productPage.categoryFilterMenus.get(CATEGORY_ID);
                var selectNode = productPage.productsTable.buildSortMenu({
                    containerNode : $("sort_menu_container"),
                    sortMenuArray : cat.sort,
                    menuWidth: 130,
                    selectNodeID : "productstable-sort" });
                niceform_init();
                return selectNode;
            }
        },
        buildMenu: function(args) {
            var containerNode = $("filter_menu_container");

            var slct = new Element("select", {
                'class' : "width_130",
                id    : args.fieldName,
                name  : args.fieldName });
            slct.insert(new Element("option", {
                value    : "",
                selected : "selected" }).insert(args.menuLabel));
            slct.insert(new Element("option", {
                value : "" }).insert("ALL"));
            args.menuOptions.each(function(val) {
                slct.insert(new Element("option", {
                    value : val
                }).insert(val));
            });

            containerNode.insert(slct);
            return slct;
        }
    };
}();

