var LeftCategoryToggle = Class.create();
LeftCategoryToggle.prototype = {
	initialize: function(productsArray, options, productCategories, debug) {
  		this.debug = debug
		if (this.debug || true) {
			this.createDebug();
		}
        this.productsArray = productsArray;
        this.options = Object.extend(this.prepareElementsIdsNames(), options);

		this.product = {};
		this.no_empty = {};
		this.toggle = {};
		this.toggleAll = {};

        this.currentCategoryId = this.options.currentCategory;
        this.currentCategoryProductId = this.options.currentCategoryProduct;
        this.productCategories = productCategories;

        $A( this.productsArray).each(function(workProduct) {
			this.prepareAllListsForProduct(workProduct.id)
		}.bind(this))

        $A( this.productsArray).each(function(workProduct) {
			this.attachObservers(workProduct)
		}.bind(this))

		this.setupAllGroups();
	},
	setupAllGroups: function() {
		if (this.options.currentProduct != "") {
			setCookie("left_product_" + this.options.currentProduct, "all")
		}
	} ,
	createDebug: function() {
		this.status = new Element('div',
		{
			style:'position:absolute; z-index:1290;padding:0;margin:0;background:orange;border:0; ' +
				  'color:#fff; font-weight:bold;top:0;'
		});
		document.body.appendChild(this.status);
		this.status.update("")
	} ,

    putDebug: function(s) {
		$(this.status).update($(this.status).innerHTML + s)
	} ,

	prepareElementsIdsNames: function() {
		return {
			currentProduct : "",
			productGroup: {
				prefix: "product",
				suffix: "_group"
			},
			noEmptyGroup: {
				prefix: "no_empty_product",
				suffix: "_group"
			},
			toggleAll: {
				prefix: "product",
				open_suffix: "_open_all",
				close_suffix: "_close_all"
			},
			toggle: {
				prefix: "product",
				open_suffix: "_open",
				close_suffix: "_close"
			}
		}
	},

	prepareAllListsForProduct: function(workProductNumber) {
		this.product[workProductNumber] = $(this.options.productGroup['prefix'] + workProductNumber + this.options.productGroup['suffix'])

		this.toggle[workProductNumber] = {}
		this.toggleAll[workProductNumber] = {}

		try {
			this.toggle[workProductNumber]["close"] = $(this.options.toggle['prefix'] + workProductNumber + this.options.toggle['close_suffix'])
			this.toggle[workProductNumber]["open"] = $(this.options.toggle['prefix'] + workProductNumber + this.options.toggle['open_suffix'])

		} catch(e) {

		}

		if (this.debug) {
			this.putDebug(workProductNumber)
		}

		this.toggleAll[workProductNumber]['close'] = $(this.options.toggleAll['prefix'] + workProductNumber + this.options.toggleAll['close_suffix'])
		this.toggleAll[workProductNumber]['open'] = $(this.options.toggleAll['prefix'] + workProductNumber + this.options.toggleAll['open_suffix'])
	},

	attachObservers: function(work_product) {
		this.attachObserversForTopToggleButton(work_product);
		this.attachObserversForBottomToggleButton(work_product);
	},

    existElement:function(mainDiv, childDivClass){
        if (mainDiv){
            var globalDivChildren = mainDiv.childNodes;
            for (var j=0; j<globalDivChildren.length; j++){
                if (globalDivChildren[j].className == childDivClass){
                    return true;
                }
            }
        }
        return false;
    },

    drawCategories : function(categoryObjects, product) {
        this.globalProductDiv = document.getElementById("product" + product.id + "_group");
        this.categoriesLinks = [];
        var link = [];

        this.categoriesDiv = document.createElement("div");
        this.categoriesDiv.className = "product_categories";
        for (var i = 0; i < categoryObjects.length; i++) {
            link[i] = "/product/" + product.name + "/"+ categoryObjects[i].cRN + "/";
            if (categoryObjects[i].cId == this.currentCategoryId && product.id == this.currentCategoryProductId){
                this.categoriesLinks[i] = Builder.node('span', {className: "current"}, [categoryObjects[i].cN]);
            } else {
                if (categoryObjects[i].nofollow){
                    this.categoriesLinks[i] = Builder.node('a', {href:link[i], rel:'nofollow'}, [categoryObjects[i].cN]);
                } else {
                    this.categoriesLinks[i] = Builder.node('a', {href:link[i]}, [categoryObjects[i].cN]);  
                }
            }

            this.categoriesDiv.appendChild(this.categoriesLinks[i]);
        }
        if (!this.existElement(this.globalProductDiv, "product_categories")){
            if (this.productCategories[product.id].length != 0){
                this.globalProductDiv.style.display = "block";
                this.globalProductDiv.appendChild(this.categoriesDiv);
                if (document.getElementById("product" + product.id + "_hide")){
                    document.getElementById("product" + product.id +"_hide").style.display="block";
                }
            }
         }
    },

    removeCategories:function(productNumber){
        this.globalProductDiv = document.getElementById("product" + productNumber + "_group");
        if (this.existElement(this.globalProductDiv, "product_categories")){
            var globalDivChildren = this.globalProductDiv.childNodes;
            for (var j = 0; j < globalDivChildren.length; j++) {
                this.globalProductDiv.removeChild(globalDivChildren[j]);    
            }
        }
    },

	attachObserversForTopToggleButton: function(work_product) {

		this.toggleAll[work_product.id]["close"].observe('click', function() {
			Effect.BlindUp(this.product[work_product.id], {duration:0.05});
			this.toggleAll[work_product.id]["close"].hide();
			this.toggleAll[work_product.id]["open"].show();

			try {
				this.toggle[work_product.id]["open"].hide();
				this.toggle[work_product.id]["close"].hide();
			} catch(e) {
			}
            this.removeCategories(work_product.id);
            if (document.getElementById("product" + work_product.id + "_hide")){
                document.getElementById("product" + work_product.id + "_hide").style.display="none";
            }
            
			setCookie("left_product_" + work_product.id, "none");

		}.bind(this));

		this.toggleAll[work_product.id]["open"].observe('click', function() {
            var outer = this;
            var productDiv = document.getElementById("product_id_" + work_product.id);
            var loaderDivName = "product"+work_product.id+"_loader";

            if (this.productCategories[work_product.id] != null) {
                outer.drawCategories(outer.productCategories[work_product.id], work_product);
            }

         this.toggleAll[work_product.id]["open"].hide();
			this.toggleAll[work_product.id]["close"].show();

			try {
				this.toggle[work_product.id]["open"].hide();
				this.toggle[work_product.id]["close"].show();
			} catch(e) {
			}

			setCookie("left_product_" + work_product.id, "all");

		}.bind(this));
	},

	attachObserversForBottomToggleButton: function(work_product) {
		try {
			this.toggle[work_product.id]["close"].observe('click', function() {
              	Effect.BlindUp(this.product[work_product.id], {duration:0.05});
            	this.toggleAll[work_product.id]["close"].hide();
				this.toggleAll[work_product.id]["open"].show();
                this.removeCategories(work_product.id);
                document.getElementById("product" + work_product.id + "_hide").style.display="none";
				setCookie("left_product_" + work_product.id, "", -1);

			}.bind(this));

			this.toggle[work_product.id]["open"].observe('click', function() {

				Effect.BlindDown(this.product[work_product.id], {duration:0.05});
				this.toggle[work_product.id]["open"].hide();
				this.toggle[work_product.id]["close"].show();

				this.toggleAll[work_product.id]["open"].hide();
				this.toggleAll[work_product.id]["close"].show();

                document.getElementById("product"+ work_product.id + "_hide").style.display="block";

                setCookie("left_product_" + work_product.id, "all");
			}.bind(this));
		} catch(e) {
		}
	}

}
