


(function($){
	$.fn.attachParams = function(options) {

		/*		
		*
		*  This will turn a query string into a json object
		*
		*/
		
		function getURLParams(query) {
			var search = query.split('?').pop().split('&');
			var params = {};
			for (var i in search) {
				if (search[i]) {
					var temp = search[i].split('=');
					params[temp[0]] = temp[1];
				}
			}
			return params;
		}
		
		
		return this.each(function() {
			if (document.location.search) {	// no need to do stuff if there's no params in the URL
//				var atags = $(this).find('a');
				var atags = $(this);
				var a, newhref;
				atags.each(function(i,a){
					var href = $(a).attr('href') ;
					
					if (href.indexOf('?')>-1) {	
						if (options&&options.dupes) {
							var urlparams = getURLParams(document.location.search);
							var hrefparams = getURLParams($(a).attr('href'));
							var newparams = {};
							switch(options.dupes) {
								case 'url' :   newparams = $.extend(hrefparams, urlparams);  break; 
								case 'href' :  newparams = $.extend(urlparams, hrefparams);  break;
							}
							newhref = href.split('?').shift()+'?'+$.param(newparams);
						} else {
							newhref = href+'&'+document.location.search.substring(1);
						}
					} else {
						newhref = href+document.location.search;
					}
					$(a).attr('href',newhref);
				});
			}
		});
	};
})(jQuery);