/*============ stereogum url parser for omniture configuration ========*/
/**
 *  should be pulled in after s_code.js file is included
 *  any other s.propN variables	should not be set on this page
 * 	@param s - omniture s object
 *  @modifies - s.prop1 (page type), s.prop2 (page subcategory), s.prop3 (friendly page name), s.prop11 (url)
*/

var BnOmSetUp={	
	"handlers":{
		/*archive page*/
		"^/archives/cat_([\\w\\-_]+)\\.html":function(matches){
			var page=this.getQry(this.path,"page");
			this.props.prop1="listing";
			this.props.prop2="archive";
			this.props.prop3=matches[1].replace(/_/g," ")+(page?" Page "+page:"");
		},
		/*permalinks and archive pages*/
		"^/archives/(([\\w\\-]+)?/)?(([^\\.]+)\\.html.*)?$":function(matches){
			if(matches[1] && !matches[4]){
				this.props.prop1="listing";
				this.props.prop3=matches[2]+" Archive Page";
			}else{
				this.props.prop1="detail";
				this.props.prop3=(matches[2]?matches[2].replace(/\-/g," ")+" Archive:":"")+matches[4].replace(/\-/g," ");
			}
			if(matches[2])
				this.props.prop2="archive";
		},
		/*b-sides page*/
		"^/b-sides/(([^\\.]+).html)?":function(matches){
			if(!matches[2]||matches[2]=="index"){
				var page=this.getQry(this.path,"page");
				this.props.prop1="listing";
				this.props.prop2="archive";
				this.props.prop3="B-Sides Archive"+(page?" Page "+page:"");
			}else{
				this.props.prop1="detail";
				this.props.prop3="B-Sides Archive: "+matches[2].replace(/\-/g," ");
			}
		},
		/*the gum drop page*/
		"^/the-gum-drop/?(.*)":function(matches){
			var smatch;
			this.props.prop1="gum-drop";
			if(smatch=matches[1].match(/(signup|song)/)){
				this.props.prop2=smatch[1];
				if(smatch[1]=="song"){
					this.props.prop3="Gum Drop: Song:"+this.getQry(this.path,"tid");
				}else{
					this.props.prop3="Gum Drop: Sign Up";
				}
			}
			var reg=new RegExp("^issues/(.*)/");
			if(smatch=matches[1].match(reg)){
				this.props.prop2="issue";
				this.props.prop3="Gum Drop:Issue:"+smatch[1];
			}
			var reg=new RegExp("^giveaway\\.php.*");
			if(smatch=matches[1].match(reg)){
				this.props.prop2="giveaway";
				this.props.prop3="Gum Drop:Giveaway:"+this.getQry(this.path,"cid");
			}
			
		},
		"^/fastsearch":function(matches){
			var ordr,offset;
			this.props.prop1="listing";
			this.props.prop2="search";
			var props=new Array("Search",this.getQry(this.path,"query"));
			if(!this.getQry("order"))
				props[props.length]="by relevance";
			else
				props[props.length]="by "+this.getQry(this.path,"order");
			if(this.getQry(this.path,"offset")){
				var page=this.intval(this.getQry(this.path,"offset"));
				if(page)props[props.length]=" page "+((page/25)+1);
				
			}
			this.props.prop3=props.join(":");
		},
		/*send to friend page*/
		"^/send-to-a-friend.php":function(matches){
			var title=this.getQry(this.path,"title");
			this.props.prop1="send-to-a-friend";
			this.props.prop2="";
			this.props.prop3="Send to friend: "+title;
		},
		/*tag pages*/
		"^/tag/([\\w\\-\\s_]+).*$":function(matches){
			var page=this.intval(this.getQry(this.path,"offset"));
			if(page)page=page/25;
			this.props.prop1="listing";
			this.props.prop2="tag";
			this.props.prop3=matches[1]+" tag"+(page?" Page "+(page+1):"");
		},
		/*mp3s search page*/
		"^/mp3/?(index\.php)?.*":function(matches){
			this.props.prop1="listing";
			this.props.prop2="mp3";
			var sort=this.getQry(this.path,"s");
			var page=this.intval(this.getQry(this.path,"n"));
			var mo=this.getQry(this.path,"m");
			if(page)page=(page/75);
			page=(page+1);
			var props=new Array("MP3");
			if(!sort && !mo){
				props[props.length]="Show All";
			}else{
				if(mo)props[props.length]="Month of "+mo+"";
				if(sort)props[props.length]="Sort by Artist";
			}
			if(page)props[props.length]="Page "+page;
			this.props.prop3=props.join(":");
		},
		/*one-off pages*/
		"^/(about|press)\\.html":function(matches){
			this.props.prop1="one-off";
			this.props.prop3=matches[1]+" page";
		},
		/*embed page*/
		"^/includes/mp3_player/embed_mp3_player\\.html":function(matches){
			this.props.prop1="pop-up";
			this.props.prop3="pop-up: mp3 player embed help page";
		},
		/*pop-up player*/
		"^/includes/mp3_player/popped_mp3_player_300_s\\.html":function(matches){
			this.props.prop1="pop-up";
			this.props.prop3="pop-up: mp3 player";
		}
		
	},
	
	/*homepage (no path)*/
	"homepage":function(){
		var page=this.intval(this.getQry(this.path,"page"));
		this.props.prop1="listing";
		this.props.prop2="homepage";
		this.props.prop3="homepage"+(page?":page "+page:"");
	},

	/*========= end site-specific handlers ====*/
	"debug":function(){
		document.write("<pre>");
		for(e in this.props)
			document.write(e+"="+this.props[e]+"\n")
		document.write("</pre>");
	},
	"getPath":function(p,idx){
		p=p.replace(/\?.*/,"");
		var tp=new Array();p=p.split("/");
		for(i=0;i<p.length;i++)if(p[i])tp[tp.length]=p[i];
		if(typeof(tp[idx])=="undefined")return "";
		return tp[idx];
	},
	"getQry":function(p,qvar){
		if(p.indexOf("?")==-1 || p.indexOf(qvar)==-1)return "";
		p=p.substring(p.indexOf("?")+1);
		if(!p)return "";
		p=p.split("&");
		for(i=0;i<p.length;i++){
			prm=p[i].split("=",2);
			if(!prm[0] || !prm[1])continue;
			if(prm[0]==qvar){
				return unescape(prm[1]);
			}
		}
		return "";
	},
	"intval":function(str){
		str=parseInt(str.replace(/[^\d]+/g,""));
		if(!str || isNaN(str))str=0;
		return str;
	},
	"copy2omiture":function(){//uses global var "s"
		//if(typeof(s)=="undefined")return;//don't bother if no s vars
		for(e in this.props)s[e]=this.props[e];
	},
	"init":function(){
		if(typeof(this.path)=="undefined")this.path=location.pathname+location.search;
		if(typeof(this.props)=="undefined")this.props={prop1:"",prop2:"",prop3:"",prop11:""};
		this.props.prop11=location.pathname;
		var basePath=this.getPath(this.path,0);
		if(!basePath || basePath=="index.html"){
			this.homepage();
		}else{
			for(e in this.handlers){
				var match=unescape(this.path).match(new RegExp(e));
				if(match){
					this.tmp=this.handlers[e];//copy handler function from sub-object
					this.tmp(match);//run handler
					break;
				}
			}
		}
		//this.debug();
		this.copy2omiture();
	}
}
BnOmSetUp.init();
