//
// Functionality common to all Help Center pages
//

Behaviour.register({
	//
	// ensure some text is entered into a search field
	//
	'form.search' : function (el) {
		el.onsubmit = function() {
			if (el.q.value.strip() == '') {
				alert('Please enter some search terms.');
				return false;
			}
			return true;
		}
	},
	'#bookmark' : function (el) {
		if (BookmarkController.canBookmarkFromJS()) {
			var img = document.createElement('img');
			img.src = '/domain-help/images/btn_bookmark.gif';
			img.alt = img.title = 'Bookmark this Page';
			img.onclick = function() {
				bookmark(window.location, document.title);
				return false;
			}
			el.appendChild(img);
		}

	
		var magnoliaImg = document.createElement('img');
		magnoliaImg.src = '/domain-help/images/btn_magnolia.gif';
		magnoliaImg.alt = magnoliaImg.title = 'Post to ma.gnolia';
		magnoliaImg.onclick = function() {
			location.href = 'http://ma.gnolia.com/bookmarklet/add?url=' +
				encodeURIComponent(location.href) + '&title=' +
				encodeURIComponent(
					document.title.strip().replace(/\s+/g, ' '))
		}
		el.appendChild(magnoliaImg);
	
		var deliciousImg = document.createElement('img');
		deliciousImg.src = '/domain-help/images/btn_delicious.gif';
		deliciousImg.alt = deliciousImg.title = 'Post to del.icio.us';
		deliciousImg.onclick = function() {
			location.href = 'http://del.icio.us/post?v=4;url=' + 
				encodeURIComponent(location.href) + ';title=' +
				encodeURIComponent(
					document.title.strip().replace(/\s+/g, ' '));
		}
		el.appendChild(deliciousImg);
	}
});

(function() {
	function gup(name) {
		var result = (new RegExp('[\\?&]' + name + '=([^&#]*)'))
			.exec(window.location.href);
		if (result == null) { return null; }
		return result[1];
	}

	function queryStringToArray(query) {
		if (!query || query.length == 0) { return {}; }
		var params = query.split('&');
		var query = {};
		for (var i = 0; i < params.length; ++i) {
			var param = params[i].split('=');
			query[param[0]] = param[1] ? param[1] : null;
		}
		return query;
	}

	function buildQueryString(queryString) {
		if (!queryString || queryString.length == 0) { return ''; }
		
		var query = [];
		for (var i in queryString) {
			query.push(i + '=' + queryString[i]);
		}

		return '?' + query.join('&');
	}

	//
	// if app_hdr was included in the query string, we want to make sure
	// any local links have this query string as well.
	//

	var app_hdr = gup('app_hdr');

	if (app_hdr) {
		Behaviour.register({
			'a' : function(a) {
				var href = a.href;
				if (href.match(/^https?:\/\/help\./)) {
					var hash = null, queryString = null, path = null;
					var match = href.match(/^([^?]*)(?:\?([^#]*))?(?:#(.*))?$/);
					path = match[1];
					queryString = queryStringToArray(match[2]);
					hash = match[3];
					queryString['app_hdr'] = app_hdr;
	
					a.href = path + buildQueryString(queryString);
	
					if (hash) { a.href += '#' + hash; }
				}
			}
		});
	}
})();

