//setup the namespace
if (typeof crumbs == "undefined" || !crumbs) {
    var crumbs = {};
}

crumbs.Filter = Class.create({
	initialize: function() {
		this.tagModalContentId = "pager-filter-tags";
		this.button = $("pager-filter-button");
		this.tags = null;
		this.indicator = $('toolbar-loading');

		this.modal = new crumbs.Modal.TagCloud(this);
		this.eventShowModal = this.modal.showModal.bindAsEventListener(this.modal);
		Event.observe(this.button, "click", this.eventShowModal);
		
		var cont = $(this.tagModalContentId).select(".modal-cont");
		var initialContent = $(cont[0]);
		var popularTagLinks = initialContent.select(".linktag");
		if (popularTagLinks.length > 0) {
			this.addTagEvent = this.addTag.bindAsEventListener(this);
			for (var i=0; i<popularTagLinks.length; i++) {
				Event.observe(popularTagLinks[i], "click", this.addTagEvent)
			}
		}

		this.url = "/catalog/category/filterlist/";
		this.categoryId = currentCategory;
		if (this.categoryId)
			this.url += "category/" + this.categoryId;

		//add the content to the modal
		this.modal.setInitialContent(initialContent);
		this.initFilterTags();	
	},

	getUrl: function (append) {
		if (!Object.isString(append)) 
			append = "";
		else
			append = "/" + append;

		return this.url + append;
	},

	getButton: function () {
		return this.button;
	},

	initFilterTags: function() {
		this.tags = initialTagFilters;
		this.fillFilterList();
		this.modal.setFilterTags(this.getFilterNodes());
	},
	loadFilterTags: function(action, paramName, paramValue) {
		var append = "tagAction/" + action;
		if (paramName && paramValue)
			append += "/" + paramName + "/" + paramValue;
		var url = this.getUrl(append);
		//console.log("Filter URL: " + url);

		this.eventSuccess = this.loadSuccess.bindAsEventListener(this);
		this.eventFailure = this.loadFailure.bindAsEventListener(this);
		var options = {
			method: 'get',
			onSuccess: this.eventSuccess,
			onFailure: this.eventFailure
		};
		this.ajaxRequest = new crumbs.Ajax.Request(url, options);
		this.showIndicator();
	},
	loadSuccess: function(transport) {
		var result = eval(transport.responseText);		
		this.tags = result[0];
		this.fillFilterList();
		this.hideIndicator();
		this.modal.setFilterTags(this.getFilterNodes());
		screenManager.getCatalog().updateProducts(result[1]);
		screenManager.getLocation().addTags(this.getTagArray(), true);
	},
	loadFailure: function(transport) {
		this.hideIndicator();
		//what do we do on failure?
	},
	fillFilterList: function() {
		var filterList = $('pager-filter-tags-current');
		filterList.innerHTML = "";
		if(this.tags.length==0) {
			return false;
		}
		var newFilterContent = this.getFilterNodes();
		filterList.appendChild(newFilterContent);
	},
	getFilterNodes: function() {
		var nodeParent = document.createElement('div');
		if(!this.tags || !this.tags.length) {
			return nodeParent;
		}
		for(var i=0; i<this.tags.length; i++) {
			var node = document.createElement('span');
			node.appendChild(document.createTextNode(this.tags[i].name));
			var alink = document.createElement("a");
			alink.setAttribute("for", this.tags[i].name);
			this.removeTagEvent = this.removeTag.bindAsEventListener(this);
			Event.observe(alink, "click", this.removeTagEvent);
			alink.appendChild(document.createTextNode(" [X]"));
			node.appendChild(alink);
			nodeParent.appendChild(node);
			if(this.tags[i+1]) {
				var plusSign = document.createTextNode(" + ");
				nodeParent.appendChild(plusSign);
			}
		}
		var divider = document.createElement("span");
		divider = $(divider);
		divider.addClassName("filter-divider");
		divider.innerHTML = "|";
		nodeParent.appendChild(divider);

		var reset = $(document.createElement("button"));
		reset.addClassName("filter-reset");
		Event.observe(reset, "click", this.resetTags.bindAsEventListener(this));
		nodeParent.appendChild(reset);

		return nodeParent;
	},

	getTagArray: function () {
		var tagArray = new Array();
		for (var i = 0; i < this.tags.length; i++) {
			tagArray.push(this.tags[i].name.toLowerCase());
		}
		return tagArray;
	},

	hasTag: function (tagName) {
		for (var i = 0; i < this.tags.length; i++) {
			if (this.tags[i].name.toLowerCase() == tagName.toLowerCase()) 
				return true;
		}
		return false;
	},

	removeTag: function(event) {
		var elm = Event.element(event);
		this.loadFilterTags("remove", "tagId", elm.getAttribute('for'));
		$(elm.parentNode).addClassName("removing");
	},

	addTag: function(event) {
		var elm = Event.element(event);
		this._addTag(elm);
	},

	_addTag: function (element) {
		var tagName = element.getAttribute("for");
		screenManager.trackEvent("Category Tag Filter", "Tag Added", tagName);
		this.loadFilterTags("add", "tagId", tagName);
		this.modal.hideWindow(true);
	},

	resetTags: function (event) {
		this.loadFilterTags("reset");
		this.modal.hideWindow(true);
	},
	
	addExclusiveTag: function (element) {
		this.loadFilterTags("addExclusive", "tagId", element.getAttribute('for'));
	},

	addManyTags: function (tagArray) {
		for (var i = 0; i < tagArray.length; i++) {
			if (!this.hasTag(tagArray[i])) { 
				this.loadFilterTags("addMany", "tagIds", tagArray.join(","));  // only load if new tags found.
				return;
			}
		}
	},

	showIndicator: function () {
		this.getIndicator().show();
	},

	hideIndicator: function () {
		this.getIndicator().hide(); 
		this.modal.hideIndicator();
	},

	getIndicator: function () {
		return this.indicator;
	}
});