//
//function g()
//{
//	if(console != undefined && console.log){
//		for(i=0,l=arguments.length;i<l;i++){
//			console.log(arguments[i]);
//		}
//	}
//}
////var debug = {log: g};
//
//if(typeof debug == 'undefined'){var debug = {"log":function(){return false;}}}


var route=function(path){
  return new route.fn.init(path);
}
route.fn = route.prototype = {
		
  init: function(path) {
			
	
    /* lazy init window['routes'] */
	if(typeof window['routes'] == 'undefined'){window['routes']={};}

    /* lazy init path */
    if(typeof path=='undefined'){path='';}

	/* lazy init route args hash */
	if(typeof args == 'undefined'){var args = {};} 

	/* lazy init instance events */
	if(typeof this.events == 'undefined'){this.events= new Array();} 

    /* lazy init bindPath, we need the original path sent as to not confuse .bind() when route pattern matching occurs */
    if(typeof bindPath=='undefined'){bindPath='';}

	/* assign args and path to route instance */
	this.args = args;
	this.path = path;	
	this.bindPath = path;


 	if(typeof window['routes'][this.bindPath]=='undefined'){
	  window['routes'][this.bindPath]={};
	}
 	if(typeof window['routes'][this.bindPath].events=='undefined'){
	  window['routes'][this.bindPath].events=new Array();
	}
	if(typeof window['routes'][this.bindPath].args=='undefined'){
	  window['routes'][this.bindPath].args={};
	}

	/* remove trailing slash from path if it exists */
	this.path = this.path.replace('//','/');
	if(this.path[this.path.length-1]=='/'){this.path = this.path.substring(0,this.path.length-1);}
	
	
	
	while(this.path[0] == '#' || this.path[0] == '/'){
		this.path = this.path.substring(1);
	}    	
	
	  
	/* a literal route match was found */
	
	// match direct event
	if(typeof window['routes'][this.path]=='object'){ 
			    
	  /* window['routes'][this.path].args = args; */
	  for(var i=0; i<window['routes'][this.path].events.length; i++){
	    this.events.push(window['routes'][this.path].events[i]);
	  }
	}else{
	  /* check if route matches any known route patterns (using the : operator) */
		for(r in window['routes']){						
			var A = this.path.split('/');			
			if(A[0] == 'feed'){ // this is not cool..
				var X = this.path.indexOf('/');
				var Y = this.path.substring(0,X);
				var Z = this.path.substring(X+1);
				A  = [];A[0] = 'feed';A[1] = Z;				
			}					
			var B = r.split('/');					
			var D = false;
			var E = []; // params
								
			for(i=0;i<A.length;i++){				
				var C = A[i];							
				if(C == B[i]){
					D = true;
					continue;				
				}			
				if(D){							
					if(B[i] && B[i].indexOf(':') > -1 ){					
						var F = B[i].replace(/:/,'');
						E[F] = C;						
					}
				}							
			}				
			if(D){
				/* rebind instance variables */
				this.args = E;
				this.path = r;
				
				for(k=0;k<window['routes'][r].events.length;k++){
					this.events.push(window['routes'][r].events[k]);
				}
				break;
			}
		 
	   }
	}
	return this;
  },

  bind: function(fn) {

	if(this.bindPath==''){
	  return 'nothing to bind';
	}
	if(typeof fn != 'function'){
	  return 'fn is invalid and cannot bind to route';
	}	
	window['routes'][this.bindPath].events.push(fn);
   	
	return 'fn bound to route';	
	  
  },
  run: function(A) {

	  
	if(this.events.length==0){
	  return false;
	}

	var B = (typeof window['routes']['last_args'] == 'undefined')?new Array():window['routes']['last_args']; 
	
	
	for(var i=0; i<this.events.length; i++){
	  var C = (window['routes']['last_path'] == this.path);
	  
	  this.events[i](this.args,C,B,A);
	  window['routes']['last_args'] = this.args;
	}
	
	window['routes']['last_path'] = this.path;
	return true;
  }

};
route.fn.init.prototype = route.fn;