/*
	SimpleStore Utils Module
*/
SS.Utils = {
		
	// function getUrlParams
	// returns params of passed in url in the form of an object hash
	getUrlParams: function(url) {
		var 
			params = url.slice(url.indexOf("?") + 1).split("&"),
			hash = {},
			pair, i, len;
		
		for(i = 0; len = params.length, i < len; i++) {
			pair = params[i].split("=");
			hash[pair[0]] = pair[1];
		}
		
		return hash;
	},
	
	
	// function buildHostUrl
	// returns a url that consists of the host + path of hostUrl
	// and the parameters of paramsUrl
	buildHostUrl: function(hostUrl, paramsUrl) {
		var 
			regExp = /http(s?):\/\/([^\/]*)([^\/]?)/,
			params,
			paramsIndex,
			matched, 
			matchedParamIndex,
			timestamp = (new Date()).getTime();
		
		// Get params after the ? char
		paramsIndex = paramsUrl.indexOf("?");
		
		if ( paramsIndex !== -1 ) {
			params = paramsUrl.slice(paramsIndex) + "&timestamp=" + timestamp;
		} else {
			params = "?timestamp=" + timestamp;
		}
		
		matched = hostUrl.match(regExp)[0];
		
		return  matched + SS.Config.actionPath + params;
	},
	
	
	showAjaxLoader: function() {
		var $loadingCover = $("#loadingCover");
		
		$loadingCover.css("height", $("#wrapper").outerHeight() + "px"); // for IE quirksmode
		$loadingCover.show();
	},
	
	
	hideAjaxLoader: function() {
		$("#loadingCover").hide();
	},
	
	
	preventDefault: function (e) {
		
		if (!e) {
			throw "SS.Utils.preventDefault event parameter is missing";
		}
		
		if ( typeof e.preventDefault === "function" ) {
			e.preventDefault();
		} else {
			e.returnValue = false;
		}
	},
	
	
	stopPropagation: function (e) {
		if (!e) {
			throw "SS.Utils.stopPropagation event parameter is missing";
		}
		
		if ( typeof e.preventDefault === "function" ) {
			e.stopPropagation();
		} else {
			e.cancelBubble = true;
		}
	},
	
	scrollToTop: function() {
		try {
	    	var top = $("#ssContainer").offset().top;
	    	
	    	if ( $(window).scrollTop() > top ) {
	    		document.body.scrollTop = top;
	    	}
	    	$("#ssContainer")[0].scrollTop = 0;
	    } catch (e) {
	    	// ignore error. Code above seems to work well enough, but no time to test
	    }
	},
	
	
	// Initializes custom css/javascript select boxes
	initSelectBoxes: (function() {
		
		// On every selection afterwards we display selection through javascript
		$(document).delegate(".input_select_wrapper select", "change", function(){
			showSelection.apply(this);
		});
		
		// Inner function to show select box selection
		function showSelection() {
			var currencySymbol = $("#currencySymbol").val();
			$(this).find("option").each(function() {
				var optionText = $(this).text();
				if(optionText.indexOf("?")!=-1) {
					$(this).text(optionText.replace('?', currencySymbol));
				}
				
			})
			
			var selectedText = $(this).find("option:selected").text();
			$(this).siblings("span").text(selectedText);
			var priceAttrPosition = $(this).find("option:selected").text().indexOf(" - " + currencySymbol);
			if(priceAttrPosition!=-1) {
				//if this is a price attribute, need to override the product price in the list.
				$(".product_price"+$(this).find("option:selected").context.name.substr(5)).text(selectedText.substr(priceAttrPosition+3));
			
			} 
		}
		
		return function() {
			// Loop through all select boxes in element
			$(".input_select_wrapper select").each(function(){
				var self = this;
			
				// Initialize first selection
				showSelection.apply(self);
			});
		};
	}())
	
};
