// Video Title Control for TubePress
// Questions? Dan Larson - dlarson13@gmail.com
//
// This function object has three possible variables which, if included,
// should be direct children of the vidt object. The possible variables
// are as follows:
//
// remove : [array]
// An array of strings to be removed. Case insensitive. Special characters
// should be double-escaped for RegExp. For example, to remove a square
// bracket ([), the array entry would be "\\[". Alternately, you could use
// 
// removespecial : [boolean]
// Set to true to remove all parentheticals, square brackets, and curly
// brackets in a string.
//
// force : [JSON object]
// This allows you to force a title for any video. The key should be
// defined as the string of the YouTube video ID (watch?v=[id]), and the
// value set for that key should be the string to use as a replacement

vidt={
	removespecial:true,
	remove:["hd","video","official","clip","hq","dreamscape","music"],
	force:{
		"c_H3MWVx6JU":"With a Spirit - 009 Sound System"
	},
	init:function(seek){
		seek=(seek)?seek:".tubepress_thumb";
		jQuery(seek).each(function(){
			var myid=vidt.getid(jQuery(this).children("a").attr("id"));
			var title=vidt.parseit(this,myid);
			jQuery(this).find(".tubepress_meta_title a").text(title);
		});
	},
	parseit:function(elem,myid){
		if("force" in vidt && myid in vidt.force){
			var title=vidt.force[myid];
		}else{
			var title=jQuery(elem).find(".tubepress_meta_title a").text();
			var reparr=vidt.fetch("remove");
			for(i=0;i<reparr.length;i++){
				var st=reparr[i];
				var re=new RegExp(st,"gi");
				title=title.replace(re,"");
			}
			if(vidt.fetch("removespecial")){
				title=title.replace(/[\[\]\(\)\{\}]/g,"")
			}
			title=jQuery.trim(title);
		}
		return title;
	},
	getid:function(myid){
		myid=myid.replace("tubepress_image_","");
		var term=myid.lastIndexOf("_");
		return myid.substr(0,term);
	},
	fetch:function(k){
		if(k in this){return this[k]}else{return false};
	}
}

jQuery(document).ready(function(){
	vidt.init();
});
