/******************************************
 * The Window Location class
 * Triggers certain JS events based on the window location hash.
 ******************************************/

if (typeof crumbs == "undefined" || !crumbs) {
    var crumbs = {};
}

crumbs.Location = Class.create({
	initialize: function () {
		this.fieldSeparator = "&";
		this.valueSeparator = ",";
		document.observe("crumbs:load", this.process.bindAsEventListener(this));
	},

	process: function() {
		var hashQuery = this.getHashObject();
		if(!hashQuery) return;
		if (hashQuery.tags) {
			tagArray = hashQuery.tags.split(this.valueSeparator);
			//console.log("Tags found in URL, adding " + hashQuery.tags)
			screenManager.getFilter().addManyTags(tagArray);
		}
		if (hashQuery.product) {
			var catProducts = screenManager.getCatalog().products;
			for (var i=0; i<catProducts.length; i++) {
				if (catProducts[i].properties.id == hashQuery.product) {
					//console.log("Product found in URL, showing popup for " + catProducts[i].properties.name)
					catProducts[i].onClick();
					break;
				}
			}
		} 
		else if (hashQuery.store) {
			var modal = screenManager.getContentModal();
			var link = "index/view/id/"+parseInt(hashQuery.store);
			modal.setUrl(link);
			modal.load();
		}
		else {
			cupcake = this.getHash();
			if (cupcake) {
				var catProducts = screenManager.getCatalog().products;
				for (var i=0; i<catProducts.length; i++) {
					if (catProducts[i].properties.url_key == cupcake) {
						catProducts[i].onClick();
						break;
					}
				}
			}
		}
		
	},

	getHash: function () {
		return window.location.hash.substring(1, window.location.hash.length);
	},

	setHash: function (hash) {
		var hashString = "";
		if (hash.tags) {
			hashString += "tags=" + hash.tags;
		}
		if (hash.product) {
			/*
			if (hashString.length)
				hashString += this.fieldSeparator;
				*/
			//hashString += "product=" + hash.product;
			if (hash.productName) {
				if (hashString.length)
					hashString += this.fieldSeparator;
				hashString += hash.productName;
			}
		}
		if (hash.store) {
			if (hashString.length)
				hashString += this.fieldSeparator;
			hashString += "store=" + hash.store;
		}

		if (hashString.length)
			hashString = "#" + hashString;
		else
			hashString = "#0";
		//console.log("setting hash to: " + hashString);
		window.location.hash = hashString;
	},

	getHashObject: function () {
		//console.dir(this.getHash().toQueryParams(this.fieldSeparator));
		return this.getHash().toQueryParams(this.fieldSeparator);		
	},

	addTag: function (tagName) {
		var hash = this.getHashObject();
		if (hash.tags)
			hash.tags += this.valueSeparator + tagName;
		else
			hash.tags = tagName;

		this.setHash(hash);
	},

	addTags: function (tagArray, clearFirst) {
		var hash = this.getHashObject();
		if (tagArray.length) {
			var tagStr = "";
			if (hash.tags && !clearFirst)
				tagStr = hash.tags + this.valueSeparator;
			hash.tags = tagStr + tagArray.join(this.valueSeparator);
			this.setHash(hash);
		}
		else
			this.clearTag();
	},

	addProduct: function (productId,productName) {
		var hash = this.getHashObject();
		hash.product = productId;
		hash.productName = productName;
		this.setHash(hash);
	},

	clearTag: function (tagName) {
		if (!tagName) {
			this._clearTags();
			return;
		}
		var hash = this.getHashObject();
		if (hash.tags) { 
			var oldTags = $A(hash.tags.split(this.valueSeparator));
			var newTags = oldTags.without(tagName);
			hash.tags = newTags.join(this.valueSeparator);
			this.setHash(hash);
		}
	},

	_clearTags: function () {
		var hash = this.getHashObject();
		if (hash.tags) {
			hash.tags = null;
			this.setHash(hash);
		}
	},
		
	clearProduct: function() {
		var hash = this.getHashObject();
		if (hash.product) {
			hash.product = null;
			
		}
		this.setHash(hash);
	},
	
	addStore: function (storeId) {
		var hash = this.getHashObject();
		hash.store = storeId;
		if (storeId) {
			this.setHash(hash);
		}
	},
		
	clearStore: function() {
		var hash = this.getHashObject();
		if (hash.store) {
			hash.store = null;
			this.setHash(hash);
		}		
	}
});

