/* URL class for JavaScript

 * Copyright (C) 2007 Karl Schellenberg, <info AT 3magine DOT com>

 * http://www.3magine.com/

 *	

 * This program is free software; you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation; either version 2 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 *

 * The GPL is located at: http://www.gnu.org/licenses/gpl.txt

 */



//Helper Functions

function clean(a){	ta=[];	for(x=0;x<a.length;x++)	if(is_array(a[x]))	ta[x]=clean(a[x]);	else if(a[x].trim().length!=0)	ta[x]=a[x];	return ta;	}	//This cleans array of null end empty string values and returns a new array

function is_array(obj){	if(typeof obj=='string' || typeof obj=='undefined') return false;	else	return obj.constructor.toString().indexOf("Array")==-1?false:true;	}

function isEmpty(s){	return (typeof s!='string')?true:s.trim().length==0;	}



var URL = new Class({

	initialize: function(){

		//Extract Params

		this.params=this.parseParams(window.location.search.substring(1));	//Returns {'k','v'}

	},

	getParams: function(keep,add){	//$keep tells which params to keep (removing rest)

		if(!is_array(keep) && keep=='*')				keep='all';

		if(!is_array(keep) && !isEmpty(keep))			keep=this.parseParams(keep);	//If string than parse it into array

		else											keep=[];						//If empty string make it empty array

		if(!is_array(add) && !isEmpty(add))				add=this.parseParams(add);		//If string than parse it into array

		else if(!is_array(add))							add=[];							//If empty string.

		

		//First get rid of params that are not found in $keep

		ta={k:[],v:[],f:[]};	//f is final

		if(keep=='all')

			this.params.k.each(function(e,i){

				ta.k.push(e);

				ta.v.push(this.params.v[i]);

			},this);

		else

			this.params.k.each(function(e,i){

				if(keep.v.contains(e)){

					ta.k.push(e);

					ta.v.push(this.params.v[i]);

				}

			},this);

		

		//Then add all params from $add

		add.k.each(function(e,i){

			ta.k.push(e);

			ta.v.push(add.v[i]);

		},this);

		

		if(ta.v.length==0)	return '';

		else

			ta.k.each(function(e,i){

				ta.f.push(e+'='+ta.v[i]);

			},this);

		return ta.f.join('&');

	},

	parseParams: function(p){	//converts query to array and returns, if any keys found will use them as assoc keys, if key is missing value it removes it

		taK=[];	taV=[];	//Temp Keys, and Vals

		d=p.split('&');	d=(d.length>0 && isEmpty(d[0]))?[]:d;

		

		if(d.length>0){	

			hasKeys=false; for(x=0;x<d.length;x++)	if(d[x].contains('=')){ hasKeys=true; break;}	//If found '=' sing in any param than it has assoc keys

			d.each(function(e,i){

					if(hasKeys){

						if(!isEmpty(e.split('=')[1])){	//Clean Check

							taK.push(e.split('=')[0]);

							taV.push(e.split('=')[1]);

						}

					}else

						if(!isEmpty(e)){	//Clean Check

							taK.push(taK.length); //Push incremental value 0,1,2, etc

							taV.push(e);

						}

			},this);

		}

		return {k:taK,v:taV};

	}

});

url=new URL();



//Disable All Inactive Links

function dil(){	

	$$('a').each(function(e){	

		if(false) //e.href.split('/').getLast()=="#")	

			e.addEvent('click',function(e){ 

				new Event(e).stop();	

			});	

	});

}