document.observe('dom:loaded', function(){
	new Utility();
	
	//Cufon.replace('p', { fontFamily: 'chevin', textShadow: '0px 2px #000' });
	document.title = document.title.toUpperCase();
	hands.up();
	
	$(document.body).insert('<div id="player"></div>');
	
	try{
		tracker = _gat._getTracker("UA-7916262-2");
		tracker._trackPageview();
	} catch(err) {}
	
	colors = $A(['r','o', 'y', 'g', 'b', 'i', 'v']);
	cycler = new Cycle(colors.length);
	
	$$('*[title]').each(function(link){
		link._title = link.title;
		link.title = '';
	});
	
	$$('p, h1, h2').each(function(p, i){
		p.addClassName(colors[cycler.next()]);
	});
	
	cycler.reset();
	
	$$('a').each(function(link, i){
		link.addClassName(colors[cycler.next()]);
	});
	
	//$('story').hide().setStyle({marginTop: '400px'});
	//$(document.body).hide();
	
});

Event.observe(window, "unload", function(){});

var Utility = Class.create({
	initialize: function(){
		
		document.observe('click', function(event){
			event.stop();
			el = $(event.element());
			if(el.match('a')){
				var url = this.getRelativeURL(el.href);
				var title = el.title;
				//tracker._trackPageview('/'+url);
									event.stop();
				if(el.hasAttribute('rel') && el.readAttribute('rel') == 'bookmark'){
					embed_player(url, title);
				}
			} else if (el.up('a')){
				var e = el.up('a');
				
				var url = this.getRelativeURL(e.href);
				var title = e.title;
				//tracker._trackPageview('/'+url);
									event.stop();
				if(e.hasAttribute('rel') && e.readAttribute('rel') == 'bookmark'){
					embed_player(url, title);
				}
			}
		}.bind(this))		
	},
	
	getRelativeURL: function(url){
		// pulls protocol, host and path from url to return location
		if(url.startsWith(window.location.protocol+'//'+window.location.host)){
			return url.sub(window.location.protocol+'//'+window.location.host+window.location.pathname, '');
		} else if (url.startsWith('/')){
			return url.sub('/', '', 1);
		} else {
			return url;
		}
	}
});

Array.prototype.shuffle = function(){
	for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
}

var Cycle = Class.create({
	
	currentId: 0,
	range: 0,
	
	initialize: function(range, val){
		this.range = range;
		this.currentId = val || 0;
	},
	// increase and return
	next: function(by){
		by = by || 1;
		var increment = this.currentId + by;
		return this.currentId = increment % this.range;
	},
	// decrease and return
	previous: function(by){
		by = by || 1;
		var increment = this.currentId - by;
		return this.currentId = Math.abs(increment % this.range);
	},
	
	// return value
	value: function(){
		return this.currentId;
	},
	
	// resets to zero by default, accepts value, returns new value
	reset: function(to){
		return this.currentId = to || 0;
	},
	
	// returns a copy of cycle object
	copy: function(){
		var neo = new Object();
		for(var i in this){
			neo[i] = this[i];
		}
 		return neo;
	}
});

var hands = {
	count : 0,
	fumble : 800,
	fingers : [
	],

	up : function(){
		document.body.className = '';
		
		the_title = document.title;
		var that = this;
		that.count = the_title.length;
		setInterval(function(){
			
			if(that.count >= 1){
				document.title = the_title.slice(0, that.count--).unescapeHTML();
			} else {
				document.title = the_title.unescapeHTML();
				that.count = the_title.length;
			}
			
			//document.title = that.fingers[( that.count )].unescapeHTML();
		}, that.fumble);
	}
};

function embed_player(_url, _title){
	
	// JAVASCRIPT VARS
	// cache buster
	var cacheBuster = "?t=" + Date.parse(new Date());		
	
	// stage dimensions
	var stageW = 600;//"100%";
	var stageH = 80;//"100%";
	
	
	// ATTRIBUTES
    var attributes = {};
    attributes.id = 'player';
    attributes.name = attributes.id;
    
	// PARAMS
	var params = {};
	params.wmode = "transparent";
	params.allowfullscreen = "true";
	params.allowScriptAccess = "always";			
	params.bgcolor = "#ffffff";
	

    /* FLASH VARS */
	var flashvars = {};				
	
	/// if commented / delete these lines, the component will take the stage dimensions defined 
	/// above in "JAVASCRIPT SECTIONS" section or those defined in the settings xml			
	flashvars.componentWidth = stageW;
	flashvars.componentHeight = stageH;
	
	/// path to the content folder(where the xml files, images or video are nested)
	/// if you want to use absolute paths(like "http://domain.com/images/....") then leave it empty("")
	flashvars.pathToFiles = "/";
	flashvars.xmlPath = "settings.xml";
	
	// other vars
	flashvars.artistName = "6e";
	flashvars.songName = _title;
	flashvars.songURL = _url;			
				
	
	/** EMBED THE SWF**/
	swfobject.embedSWF("/preview.swf"+cacheBuster, attributes.id, stageW, stageH, "9.0.124", "js/expressInstall.swf", flashvars, params);
}