/** ((START UTILITIES)) **/
YAHOO.ympyui.util.Dom.setStyles = function(el, styles) {
	for (var i in styles) {
		YAHOO.ympyui.util.Dom.setStyle(el, i, styles[i]);
	}
};

/**
*/
var CookieCutter = function(options) {};
CookieCutter.prototype = {
	
	/**
	*/
	get: function(key) {
		var start = 0, end = 0;
		if (key && (document.cookie.length > 0)) {
			start = document.cookie.indexOf(key + '=');
			if (start != -1) {
				start = start + key.length+1; 
				end = document.cookie.indexOf(';', start);
				if (end == -1) {
					end = document.cookie.length;
				}
				return unescape(document.cookie.substring(start, end));
			} 
		}
		return '';
	},
	
	/**
	*/
	set: function(key, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = key + '=' + escape(value) + ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString() + ';path=/;');
	},
	
	/**
	*/
	check: function(key) {
		var cookie = this.get(key);
		return (cookie && ((cookie != null) && (cookie != ''))) ? true : false;
	}
	
};


/**
MD5 Hash
*/
var hex_chr = "0123456789abcdef";
function rhex(num) { str = ""; for (j = 0; j <= 3; j++) { str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F); } return str; }
function str2blks_MD5(str) { nblk = ((str.length + 8) >> 6) + 1; blks = new Array(nblk * 16); for(i = 0; i < nblk * 16; i++) { blks[i] = 0; } for(i = 0; i < str.length; i++) { blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8); } blks[i >> 2] |= 0x80 << ((i % 4) * 8); blks[nblk * 16 - 2] = str.length * 8; return blks; }
function add(x, y) { var lsw = (x & 0xFFFF) + (y & 0xFFFF); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); return (msw << 16) | (lsw & 0xFFFF); }
function rol(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)); }
function cmn(q, a, b, x, s, t) { return add(rol(add(add(a, q), add(x, t)), s), b); }
function ff(a, b, c, d, x, s, t) { return cmn((b & c) | ((~b) & d), a, b, x, s, t); }
function gg(a, b, c, d, x, s, t) { return cmn((b & d) | (c & (~d)), a, b, x, s, t); }
function hh(a, b, c, d, x, s, t) { return cmn(b ^ c ^ d, a, b, x, s, t); }
function ii(a, b, c, d, x, s, t) { return cmn(c ^ (b | (~d)), a, b, x, s, t); }
function MD5(str) { x = str2blks_MD5(str); a =  1732584193; b = -271733879; c = -1732584194; d =  271733878; for (i = 0; i < x.length; i += 16) { olda = a; oldb = b; oldc = c; oldd = d; a = ff(a, b, c, d, x[i+ 0], 7 , -680876936); d = ff(d, a, b, c, x[i+ 1], 12, -389564586); c = ff(c, d, a, b, x[i+ 2], 17,  606105819); b = ff(b, c, d, a, x[i+ 3], 22, -1044525330); a = ff(a, b, c, d, x[i+ 4], 7 , -176418897); d = ff(d, a, b, c, x[i+ 5], 12,  1200080426); c = ff(c, d, a, b, x[i+ 6], 17, -1473231341); b = ff(b, c, d, a, x[i+ 7], 22, -45705983); a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416); d = ff(d, a, b, c, x[i+ 9], 12, -1958414417); c = ff(c, d, a, b, x[i+10], 17, -42063); b = ff(b, c, d, a, x[i+11], 22, -1990404162); a = ff(a, b, c, d, x[i+12], 7 ,  1804603682); d = ff(d, a, b, c, x[i+13], 12, -40341101); c = ff(c, d, a, b, x[i+14], 17, -1502002290); b = ff(b, c, d, a, x[i+15], 22,  1236535329); a = gg(a, b, c, d, x[i+ 1], 5 , -165796510); d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632); c = gg(c, d, a, b, x[i+11], 14,  643717713); b = gg(b, c, d, a, x[i+ 0], 20, -373897302); a = gg(a, b, c, d, x[i+ 5], 5 , -701558691); d = gg(d, a, b, c, x[i+10], 9 ,  38016083); c = gg(c, d, a, b, x[i+15], 14, -660478335); b = gg(b, c, d, a, x[i+ 4], 20, -405537848); a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438); d = gg(d, a, b, c, x[i+14], 9 , -1019803690); c = gg(c, d, a, b, x[i+ 3], 14, -187363961); b = gg(b, c, d, a, x[i+ 8], 20,  1163531501); a = gg(a, b, c, d, x[i+13], 5 , -1444681467); d = gg(d, a, b, c, x[i+ 2], 9 , -51403784); c = gg(c, d, a, b, x[i+ 7], 14,  1735328473); b = gg(b, c, d, a, x[i+12], 20, -1926607734);  a = hh(a, b, c, d, x[i+ 5], 4 , -378558); d = hh(d, a, b, c, x[i+ 8], 11, -2022574463); c = hh(c, d, a, b, x[i+11], 16,  1839030562); b = hh(b, c, d, a, x[i+14], 23, -35309556); a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060); d = hh(d, a, b, c, x[i+ 4], 11,  1272893353); c = hh(c, d, a, b, x[i+ 7], 16, -155497632); b = hh(b, c, d, a, x[i+10], 23, -1094730640); a = hh(a, b, c, d, x[i+13], 4 ,  681279174); d = hh(d, a, b, c, x[i+ 0], 11, -358537222); c = hh(c, d, a, b, x[i+ 3], 16, -722521979); b = hh(b, c, d, a, x[i+ 6], 23,  76029189); a = hh(a, b, c, d, x[i+ 9], 4 , -640364487); d = hh(d, a, b, c, x[i+12], 11, -421815835); c = hh(c, d, a, b, x[i+15], 16,  530742520); b = hh(b, c, d, a, x[i+ 2], 23, -995338651); a = ii(a, b, c, d, x[i+ 0], 6 , -198630844); d = ii(d, a, b, c, x[i+ 7], 10,  1126891415); c = ii(c, d, a, b, x[i+14], 15, -1416354905); b = ii(b, c, d, a, x[i+ 5], 21, -57434055); a = ii(a, b, c, d, x[i+12], 6 ,  1700485571); d = ii(d, a, b, c, x[i+ 3], 10, -1894986606); c = ii(c, d, a, b, x[i+10], 15, -1051523); b = ii(b, c, d, a, x[i+ 1], 21, -2054922799); a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359); d = ii(d, a, b, c, x[i+15], 10, -30611744); c = ii(c, d, a, b, x[i+ 6], 15, -1560198380); b = ii(b, c, d, a, x[i+13], 21,  1309151649); a = ii(a, b, c, d, x[i+ 4], 6 , -145523070); d = ii(d, a, b, c, x[i+11], 10, -1120210379); c = ii(c, d, a, b, x[i+ 2], 15,  718787259); b = ii(b, c, d, a, x[i+ 9], 21, -343485551); a = add(a, olda); b = add(b, oldb); c = add(c, oldc); d = add(d, oldd); } return rhex(a) + rhex(b) + rhex(c) + rhex(d); }
 

/** ((END UTILITIES)) **/

/** ((START TRACK SEEK)) **/
/**
Yahoo Media Player - TrackSeek extension

Displays a visual position indicator and allows the user to control the seek position

@author: Eric Fehrenbacher
@company: Yahoo!
@version 0.1.2
*/
var TrackSeek = function() {
	
	// reference to YMP
	this.player = YAHOO.MediaPlayer;
	
	// position of the left edge of the slider relative to the page
	this.seekControlX = 0;
	
	// reference to the currently playing track
	this.track = null;
	
	// the elapsed time of the currently playing track
	this.elapsed = 0;
	
	// the duration of the currently playing track
	this.duration = 0;
	
	// tells us if the track is playing or not
	this.paused = true;
	
	// tells us if we are dragging the slider or not
	this.dragging = false;
	
	// tracks the percentage of movement from the left edge of the control
	this.position = 0;
	
	// setup of delegates, subscribers, and UI
	this.init();
	
};
TrackSeek.create = function(args) {
	YAHOO.mediaplayer.TrackSeek = new TrackSeek(args[0]);
	return YAHOO.mediaplayer.TrackSeek;
};
TrackSeek.attachElement = 'ymp-meta-album-title';
TrackSeek.prototype = {
	
	/**
	Fires during the contruction of this object
	@method init
	*/
	init: function() {
		
		// 
		this.setupUI();
		
		// some ui specs
		this.ui = {
			// define the total width
			width: function() {
				return parseInt(YAHOO.ympyui.util.Dom.getStyle('ymp-seek', 'width'));
			},
			// define the left edge
			left: function() {
				return Math.round(parseInt(YAHOO.ympyui.util.Dom.getStyle('ymp-seek-thumb', 'width')) / 2);
			},
			// define the right edge
			right: function() {
				return this.width() - this.left();
			}
		};
		
		// initialize default seek position...
		this.onPositionChange(1);
		
		// handle mousedown on the position control
		YAHOO.ympyui.util.Event.on('ymp-seek', 'mousedown', this.seekStartDrag, this, true);
		
		// handle clicks on the position control
		YAHOO.ympyui.util.Event.on('ymp-seek', 'click', this.stopEvent);
		
		// track hash of active track when play is clicked
		this.player.onTrackStart.subscribe(this.onPlay, this, true);
		
		// update position based on elapsed time of active track
		this.player.onProgress.subscribe(this.onProgress, this, true);
		
		//
		this.player.onTrackPause.subscribe(this.onPause, this, true);
		
		// 
		YAHOO.ympyui.util.Event.addListener(['ymp-next','ymp-prev'], 'click', function() {
			this.onPositionChange(1);
		}, this, true);
		
	},
	
	/**
	We just setup the GUI here.
	@method setupUI
	*/
	setupUI: function() {
		
		// grab the volume control
		var displaced = document.getElementById(TrackSeek.attachElement);
		YAHOO.ympyui.util.Dom.setStyles(displaced, {display: 'none'});
		
		// create slider
		var seekContainer = document.createElement('div');
		seekContainer.id = 'ymp-seek';
		YAHOO.ympyui.util.Dom.setStyles(seekContainer, {
			position: 'absolute',
			width: '190px',
			height: '15px',
			zIndex: '2'
		});
		displaced.parentNode.insertBefore(seekContainer, displaced);
		/*
        // we need to be notified when the timer changes size so we can readjust
        // or better yet, why do we need to readjust the timer should be doing that
        var seekRegion = YAHOO.ympyui.util.Dom.getRegion(seekContainer);
        var seekWidth = seekRegion.right - seekRegion.left;
        YAHOO.ympyui.util.Dom.setStyle(seekContainer, 'width', (seekWidth - 10) + 'px');
        */
        
		var seekCover = document.createElement('div');
		seekCover.id = 'ymp-seek-cover';
		YAHOO.ympyui.util.Dom.setStyles(seekCover, {
			display: 'block',
			position: 'absolute',
			overflow: 'hidden',
			top: '5px',
			left: '0px',
			width: '100%',
			height: '4px',
			backgroundColor: '#D6D6D6'
		});
		seekContainer.appendChild(seekCover);
		
		var seekCoverElapsed = document.createElement('div');
		seekCoverElapsed.id = 'ymp-seek-cover-elapsed';
		YAHOO.ympyui.util.Dom.setStyles(seekCoverElapsed, {
			display: 'block',
			position: 'absolute',
			visibility: 'hidden',
			overflow: 'hidden',
			top: '0px',
			left: '0px',
			width: '100%',
			height: '4px',
			backgroundColor: '#929392'
		});
		seekCover.appendChild(seekCoverElapsed);
		YAHOO.ympyui.util.Event.addListener(seekContainer, 'mouseover', function() {
			this.style.backgroundColor = '#CEFD0D';
		}, seekCoverElapsed, true);
		YAHOO.ympyui.util.Event.addListener(seekContainer, 'mouseout', function() {
			this.style.backgroundColor = '#929392';
		}, seekCoverElapsed, true);
		
		var seekThumb = document.createElement('div');
		seekThumb.id = 'ymp-seek-thumb';
		YAHOO.ympyui.util.Event.addListener(seekThumb, 'mouseover', function() {
			this.style.borderColor = '#CEFD0D';
		}, seekThumb, true);
		YAHOO.ympyui.util.Event.addListener(seekThumb, 'mouseout', function() {
			this.style.borderColor = '#DDDDDD #B2B2B2 #B2B2B2 #DDDDDD';
		}, seekThumb, true);
		YAHOO.ympyui.util.Dom.setStyles(seekThumb, {
			display: 'block',
			position: 'absolute',
			overflow: 'hidden',
			top: '3px',
			left: '0px',
			width: '4px',
			height: '7px',
			cursor: 'pointer',
			backgroundColor: '#E9E8E8',
			borderWidth: '1px',
			borderStyle: 'solid',
			borderColor: '#DDDDDD #B2B2B2 #B2B2B2 #DDDDDD'
		});
		seekContainer.appendChild(seekThumb);
		
	},
	
	/**
	Provides a shortcut for stopping an event
	@method stopEvent
	@param {DOMEvent} evt
	*/
	stopEvent: function(evt) {
		YAHOO.ympyui.util.Event.stopEvent(evt);
	},
	
	/**
	Event handler for when the user starts to drag the position slider
	@method seekStartDrag
	@param {Object} eventObj The HTML event object.
	*/
	seekStartDrag: function(evt) {
		if (this.paused) {
			return;
		}
		
		this.stopEvent(evt);
		this.dragging = true;
		
		// get the current position of the left edge of the slider relative to the page
		this.seekControlX = YAHOO.ympyui.util.Dom.getX('ymp-seek');
		
		// notify everyone about position change
		this.notifySeekChange(evt);
		
		YAHOO.ympyui.util.Event.on(document, 'mousemove', this.notifySeekChange, this, true);
		YAHOO.ympyui.util.Event.on(document, 'mouseup', this.seekMouseUp, this, true);  
	},
	
	/**
	Event handler when the user releases the mouse after dragging the position slider. Remove the appropriate event listeners.
	@method seekMouseUp
	@param {Object} eventObj The HTML event object
	*/                                                    
	seekMouseUp: function(evt) {
		this.dragging = false;
		this.stopEvent(evt);
		this.elapsed = this.duration - (this.position * this.duration);
		
		YAHOO.ympyui.util.Event.removeListener(document, 'mousemove', this.notifySeekChange);
		YAHOO.ympyui.util.Event.removeListener(document, 'mouseup', this.seekMouseUp);
		
		if (this.paused) {
			
			this.player.pause();
			return;
			
			// reduce the volume before we play so that no one here's the brief noise
			var volume = this.player.getVolume();
			this.player.setVolume(0);
			this.player.play(this.track, this.elapsed);
			/*
			it would be nice to be able to move the position while paused, but...
			for some reason this hack won't work here, haven't figured out why yet
			*/
			/*
			this.playCheck = YAHOO.ympyui.lang.later(100, this, function(volume) {
				if (this.player.getPlayerState() == 2) {
					this.playCheck.cancel();
					//this.updateSeekPosition();
					this.onProgress({elapsed: this.elapsed, duration: this.duration})
					console.log("asddddddddd");
					this.player.pause();
					//this.player.setVolume(volume);
				}
			}, [volume], true);
			*/
		} else {
			this.player.play(this.track, this.elapsed);
		}
		
	},
	
	/**
	Get the right requested position based on where the user has dragged the position slider and then fire the position change request event for that position
	@method notifySeekChange
	@param {DOMEvent} evt
	*/
	notifySeekChange: function(evt) {
		this.stopEvent(evt);
		var newMouseX = YAHOO.ympyui.util.Event.getPageX(evt);
		var xDiff = (newMouseX - this.seekControlX);
		xDiff -= 0;
		var thumbLeft;
		
		// calculate position percentage (0 - 1) and fire the event to notify whoever is listening
		if ((xDiff >= this.ui.left()) && (xDiff < this.ui.right())) {
			// mouse is within the constraint
			thumbLeft = xDiff - this.ui.left();
		} else if (xDiff >= this.ui.right()) {
			// mouse is way below the position, so cap the thumb at the maximum x it is allowed to go
			thumbLeft = this.ui.right() - this.ui.left();
		} else if (xDiff < this.ui.left()) {
			// mouse is way above the position, so cap the x at 0
			thumbLeft = 0;
		}
		
		// percentage of movement from the left edge of the control
		this.position = 1 - (thumbLeft / (this.ui.right() - this.ui.left()));
		
		this.onPositionChange(this.position);
	},
	
	/**
	Event handler for onPlay. Record the track as a mediaObject for reference.
	@method onPlay
	@param {Object} track The currently playing track
	*/
	onPlay: function(track) {
		this.paused = false;
		this.track = track.mediaObject;
	},
	
	/**
	This function updates the seek position based on the elapsed time of the currently playing track
	@method onProgress
	@param {Object} options
	*/
	onProgress: function(options) {
		this.elapsed = options.elapsed;
		this.duration = options.duration || (YAHOO.mediaplayer.TrackResume && YAHOO.mediaplayer.TrackResume.duration) || 0;
		if (!this.dragging && (this.duration != 0)) {
			var thumbLeft;
			
			// convert thumb position into seek position
			var xDiff = Math.ceil((Math.ceil(this.elapsed) * this.ui.right() - this.ui.left()) / Math.ceil(this.duration));
			xDiff -= 0;
			
			// calculate position percentage (0 - 1) and fire the event to notify whoever is listening
			if ((xDiff >= this.ui.left()) && (xDiff < this.ui.right())) {
				// seek is within the constraint
				thumbLeft = xDiff - this.ui.left();
			} else if (xDiff >= this.ui.right()) {
				// seek is way below the position, so cap the thumb at the maximum x it is allowed to go
				thumbLeft = this.ui.right() - this.ui.left();
			} else if (xDiff < this.ui.left()) {
				// seek is way above the position, so cap the x at 0
				thumbLeft = 0;
			}
			
			// percentage of seek position from the left edge of the controle
			var position = 1 - (thumbLeft / (this.ui.right() - this.ui.left()));
			
			this.onPositionChange(position);
		}
	},
	
	/**
	Event handler for onPlay. Record the track as a mediaObject for reference.
	@method onPause
	@param {Object} track The currently playing track
	*/
	onPause: function(track) {
		this.paused = true;
	},
	
	/**
	Event handler for onPositionChange. If the seek position is changed through API update the view appropriately
	@method onPositionChange
	@param {Object} seek The new position [0-1]
	*/
	onPositionChange: function(position) {
		
		// convert seek position into thumb position
		var thumbLeft = (1 - position) * (this.ui.right() - this.ui.left());
		
		// adjust seek thumb position
		YAHOO.ympyui.util.Dom.setStyle('ymp-seek-thumb', 'left', thumbLeft + 'px');
		
		// adjust elapsed position
		YAHOO.ympyui.util.Dom.setStyle('ymp-seek-cover-elapsed', 'visibility', 'visible');
		YAHOO.ympyui.util.Dom.setStyle('ymp-seek-cover-elapsed', 'left', -((this.ui.right() - this.ui.left()) - thumbLeft) + 'px');
		
	}
	
};

/**
This creates a new TrackSeek object and attaches to the MediaPlayer
*/
(typeof YAHOO != 'undefined') && YAHOO.ympyui.util.Event.onAvailable(TrackSeek.attachElement, TrackSeek.create, TrackSeek);

/** ((END TRACK SEEK)) **/


/** ((START BUY BUTTON )) **/
/*Slackers.Plugin.RenderBuyButton = function(){
	var self = this;
	var Player = YAHOO.MediaPlayer;
	
	this.init = function(){
		self.initEvents();
	};
	
	this.initEvents = function(){
		Player.onTrackStart.subscribe(function(){
			//console.log('w');
		});
	};
	
	this.init();
};

YAHOO.MediaPlayer.onAPIReady.subscribe(function(){
	new Slackers.Plugin.RenderBuyButton();
});*/

/*Event.onDOMReady(function(){
	var bb = 
});*/

/** ((END BUY BUTTON )) **/