// JavaScript Document
$(document).observe('dom:loaded', function(e){
	$('container').observe('click', function(ee) {
		var v = $(ee.element());
		if(v.tagName.toUpperCase()!=('A') && v.up("a")) {
			v = v.up("a");
		}
		if(v.tagName.toUpperCase()==('A') && v.rel && v.rel.indexOf("redir:")>-1) {
			//syntax
			//rel="redir:p+n=v|p-n=v|h+v|h-
			//p+ adds a name value pair
			//p- removes a name value pair
			//h+ changes the hash
			//h- removes the hash
			
			//disect current href
			var url = {q:[]}
			var parts = v.href.split(/\?|\#/)
			url.d = parts.shift()
			while(parts.length > 0){
							var p = parts.shift()
							if (p.indexOf('=') > -1)
											url.q = p.split('&')
							else
											url.h = p
			}
			//append redir
			parts = v.rel.replace('redir:','').split('|')
			while(parts.length > 0){
							var p = parts.shift()
							if (p.indexOf('p+') > -1)
											url.q.push(p.replace('p+',''))
							else if (p.indexOf('p-') > -1)
											url.q = $A(url.q).without(p.replace('p-',''))
							else if (p.indexOf('h+') > -1)
											url.h = p.replace('h+','')
							else if (p.indexOf('h-') > -1)
											url.h = false
			}
			
			//change href
			v.href = url.d
			if(url.q.length > 0) v.href += '?' + url.q.join('&')
			if(url.h) v.href += '#' + url.h

		}
	});
});

/*
$(document).observe('dom:loaded', function(e){
                $$('a[rel^="redir:"]').each(function(v){
                        //syntax
                        //rel="redir:p+n=v|p-n=v|h+v|h-
                        //p+ adds a name value pair
                        //p- removes a name value pair
                        //h+ changes the hash
                        //h- removes the hash
                        
                        //disect current href
                        var url = {q:[]}
                        var parts = v.href.split(/\?|\#/)
                        url.d = parts.shift()
                        while(parts.length > 0){
                                var p = parts.shift()
                                if (p.indexOf('=') > -1)
                                        url.q = p.split('&')
                                else
                                        url.h = p
                        }
                        //append redir
                        parts = v.rel.replace('redir:','').split('|')
                        while(parts.length > 0){
                                var p = parts.shift()
                                if (p.indexOf('p+') > -1)
                                        url.q.push(p.replace('p+',''))
                                else if (p.indexOf('p-') > -1)
                                        url.q = $A(url.q).without(p.replace('p-',''))
                                else if (p.indexOf('h+') > -1)
                                        url.h = p.replace('h+','')
                                else if (p.indexOf('h-') > -1)
                                        url.h = false
                        }
                        
                        //change href
                        v.href = url.d
                        if(url.q.length > 0) v.href += '?' + url.q.join('&')
                        if(url.h) v.href += '#' + url.h
                })
        })*/