﻿///<reference path="/Scripts/BondiTouch.Core.js" />

_b.Utility = {
	///<summary>This object will handle only stateless, non-business logic related methods.</summary>


	/*______________________________________________________________________________________________________________________________________________________________________________________*/
	makeProxyUrl: function (url, data) {
		///<summary>Used for remote solution debugging</summary>
		var started = false; 
		if (data) {
			$.each(data, function(key, value) {
				url += (started ? "&" : "?")
					+ key
					+ "="
					+ value;
				
				started = true;
			});
		}
	
		if (BondiTouch.Uri.proxy)
			return BondiTouch.Uri.proxy + escape(url);
		
		return url;
	},

	/*______________________________________________________________________________________________________________________________________________________________________________________*/
	reduceUrlFragments: function (url) {
		///<summary>Converts a url to an array of fragments, splitting and removing : / # & ! characters, then removing the empty entries

		//TODO: utilize parseUri

		var fragments = url.split(/[:///#/!/&]/);
		return _.reject(fragments, function (fragment) { return fragment.length === 0});
	},

	/*______________________________________________________________________________________________________________________________________________________________________________________*/
	getPageNameFromUrl: function () {
		///<summary>Get the first url hash fragment that is not an empty string or a url delimiter</summary>
		if (!location.hash) {
			return "";
		}
		var hashFragments = this.reduceUrlFragments(location.hash); // => expect that the first result is the page name
		return hashFragments[0];
	},
	
	/*______________________________________________________________________________________________________________________________________________________________________________________*/
	setBgImageOnce: function($elmt, url){
		///<summary>Sets the given element's background image to the given URL only if it isn't the given URL already. This prevents multiple fetches.</summary>
		if (!/^url\(/.test(url)) {
			// important: don't put quotes around the URL, because browsers don't,
			// so this lets us accurately test in the future if the URL is set.
			url = "url(" + url + ")";
		}
		if ($elmt.css('background-image') !== url) {
			$elmt.css('background-image', url);
		}
		return $elmt;
	},
	
	/*______________________________________________________________________________________________________________________________________________________________________________________*/
	clearBgImage: function($elmt, url){
		$elmt.css('background-image', '');
		return $elmt;
	}

};
