EEPlayer = function (target_element) {
	var element = (typeof target_element == "string") ? document.getElementById(target_element) : target_element;
	this.instance = EEPlayer.COUNT++;
	this.addChildren(element);
	this.queue = {};
	var instanceHolder = this.instance;
	this.player = new nebulaplayer.Nebula('player_target' + this.instance);

	if (this.player.htmlLayer.addEventListener) {
		this.player.htmlLayer.addEventListener('click', this.delegate(this, this.onOverLayClick), false);
	} else if (this.player.htmlLayer.attachEvent) {
		this.player.htmlLayer.attachEvent('onclick', this.delegate(this, this.onOverLayClick));
	}
	
	
	this.player.addEventListener(nebulaplayer.event.BAD_VERSION, function () {
		document.getElementById('player_target'+instanceHolder).innerHTML = '<div class="videoMatte"><div id="videoOverlayWrapper'+ this.instance + '" class="videoOverlayWrapper getFlashContainer"><div>For the best experience please install the latest version of the free Adobe Flash Player.<br /><br /><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&promoid=BONRN"><span class="getFlash"><!--Button--></span></a></div></div></div>'; 
	});
	this.player.addEventListener(nebulaplayer.event.LOAD, this.delegate(this, this.onLoad));
	this.player.addEventListener(nebulaplayer.event.PLAY, this.delegate(this, this.onPlay));
	this.player.addEventListener(nebulaplayer.event.PAUSE, this.delegate(this, this.onPause));
	this.player.addEventListener(nebulaplayer.event.ADVANCE, this.delegate(this, this.onAdvance));
	this.player.addEventListener(nebulaplayer.event.MUTE, this.delegate(this, this.onMute));
	this.player.addEventListener(nebulaplayer.event.BUFFER, this.delegate(this, this.onBuffer));
	this.player.addEventListener(nebulaplayer.event.SUCCESS, this.delegate(this, this.onSuccess));
	this.player.addEventListener(nebulaplayer.event.COMPLETE, this.delegate(this, this.onComplete));
	this.player.addEventListener(nebulaplayer.event.STREAM_ERROR, this.delegate(this, this.onStreamError));
	
	var scopeRewind = document.getElementById('rewindButton'+this.instance);
	scopeRewind.handler = this;
	scopeRewind.onclick = function (event)  {
		this.handler.handleControl(event, "seek", [0]);
	};
	var scopePlay = document.getElementById('playButton' + this.instance);
	scopePlay.handler = this;
	scopePlay.onclick = function (event)  {
		if (this.handler.queue.length !== "") {
			this.handler.handleControl(event, "play");
		}
	};
	var scopeMute = document.getElementById('muteButton' + this.instance);
	scopeMute.handler = this;
	scopeMute.onclick = function (event) {
		this.handler.handleControl(event, "mute");
	};
	
	this.slider = new mediageneral.components.Slider('slider' + this.instance, 0, 100, 140);
	this.slider.addEventListener("start", this.delegate(this, this.onSliderStart));
	this.slider.addEventListener("change", this.delegate(this, this.onSliderEnd));
};
if (typeof EEPlayer.COUNT == "undefined") { EEPlayer.COUNT = 0; }
EEPlayer.prototype = {
	delegate: function(obj, func) {
		var f = function () {
			var target = arguments.callee.target;
			var func0 = arguments.callee.func;
			return func0.apply(target, arguments);
		};
		f.target = obj;
		f.func = func;
		
		return f;
	},
	onOverLayClick: function (event) {
		this.handleControl(event, "play");
		if (event.preventDefault) {
			event.preventDefault();
			event.stopPropagation();
			
		} else {
			event.returnValue = false;
			event.cancelBubble = true;	
		}
	},
	onSliderStart: function (event) {
		/*console.log('slider.start');
		this.player.pause();*/
	},
	onSliderEnd: function (event) {
		this.player.seek((event.target.value / 100) * this.player.duration);
	},
	onStreamError: function (event) {
		this.message('Requested video was not found');
	},
	onLoad: function (event) {
		var target = event.target;
		this.onMute(event);
		this.message('Ready');
	},
	onPlay: function (event) {
		this.message('Playing');	
		if(document.getElementById('playButton' + this.instance).className == 'playButton'){
			document.getElementById('videoMatte' + this.instance).style.display = 'block';
		}else{
			document.getElementById('videoMatte' + this.instance).style.display = 'none';
		}
	},
	onSuccess: function (event) {
		this.message('Connected');
	},
	onBuffer: function (event) {
		this.message('Buffering...');
	},
	onComplete: function (evt) {
		var target = evt.target;
		this.slider.setPosition(0, 0);
		document.getElementById('playButton'+this.instance).className = 'playButton';
		this.message('Stopped');		
		document.getElementById('videoMatte' + this.instance).style.display = 'block';
	},
	onAdvance: function (evt) {
		var target = evt.target;
		var timeBox = document.getElementById('time' + this.instance);
		if (!this.slider.active) { this.slider.setPosition(0, (target.time / target.duration) * 140); }
		
		var lengthInSecs = Math.round(target.duration) - Math.round(target.time);
		var lengthInMins = Math.floor(lengthInSecs / 60);
		lengthInSecs = lengthInSecs%60;
		
		timeBox.innerHTML = ((lengthInMins < 10) ? "0" + Math.max(0, lengthInMins) : lengthInMins) + ":" + ((lengthInSecs < 10) ? "0" + Math.max(0, lengthInSecs) : lengthInSecs);
	},
	onMute: function (event) {
		var target = event.target;
		var muteButton = document.getElementById('muteButton'+this.instance);
		if (target.muted) {
			muteButton.style.backgroundPosition = '-184px -14px';
		} else {
			muteButton.style.backgroundPosition = '-184px 0px';
		}
	},
	handleControl: function (event, action, params) {
		switch (action) {
			case 'seek':
				this.player.seek.apply(this.player, params);
				break;
			case 'play':
				if (this.player.enabled) {
					if (arguments.length > 2) {
						if (this.player.playing) { this.player.stop(); }
						this.queue.stream = params[0];
						this.queue.preview = params[1];
						try {
							this.queue.title = params[2];
							sendFlashPageView(params[2], params[0]);
						} catch (e) {
							
						}
					}
					var target = document.getElementById('playButton'+this.instance);
					if (this.player.playing) {
						if (!this.player.paused) {
							target.className = 'playButton';
						} else {
							target.className = 'pauseButton';	
						}
						this.player.pause();
					} else {
						target.className = 'pauseButton';
						this.player.play(this.queue.stream);
						try {
							sendFlashPageView(this.queue.title, this.queue.stream);
						} catch (e2) {
							
						}
					}
				}
				break;
			case 'mute':
				this.player.mute();			
				break;
		}
	},
	message: function (msg) {
		document.getElementById('messageDisplay'+this.instance).innerHTML = msg;
	},
	addToQueue: function (src, prv, ptype, plist, title) {
		this.queue = {stream:src, preview:prv, title:title, playertype:ptype, playlist:plist};
		if (this.queue.playertype == 'horizontal'){
			this.player.htmlLayer.innerHTML = '<div id="videoMatte' + this.instance + '" class="videoMatte"><div id="videoOverlayWrapper' + this.instance + '" class="videoOverlayWrapper" style="background:url(' + this.queue.preview + ') no-repeat 50% 50%;"><div id="clickToPlay'+ this.instance +'" class="clickToPlayH"></div><div class="mgPlayerPanelH"><div class="panelAccentH"><!--EMPTY--></div><div class="playlistScrollWrapperH"><div class="prevButtonWrapperH"><a><span id="prevButton' + this.instance + '" class="prevButton"><!--Rollover Button--></span></a></div><div class="playlistClipH"><div id="insertHere' + this.instance + '" class="mgPlayerH"><!--Playlist Content Goes Here--></div></div><div class="nextButtonWrapperH"><a><span id="nextButton' + this.instance + '" class="nextButton"><!--Rollover Button--></span></a></div></div></div></div></div>';
			document.getElementById('insertHere' + this.instance).innerHTML = document.getElementById(this.queue.playlist).innerHTML;
		} else if(this.queue.playertype == 'vertical'){
			this.player.htmlLayer.innerHTML = '<div id="videoMatte' + this.instance + '" class="videoMatte"><div id="videoOverlayWrapper'+ this.instance + '" class="videoOverlayWrapper" style="background:url(' + this.queue.preview + ') no-repeat 50% 50%;"><div id="clickToPlay'+ this.instance +'" class="clickToPlayV"></div><div class="mgPlayerPanelV"><div class="panelAccentV"><!-- Empty --></div><div class="playlistScrollWrapperV"><div class="prevButtonWrapperV"><a><span id="prevButton' + this.instance + '" class="prevButton"><!--Rollover Button--></span></a></div><div class="playlistClipV"><div id="insertHere' + this.instance + '" class="mgPlayerV"><!--Playlist Content Goes Here--></div></div><div class="nextButtonWrapperV"><a><span id="nextButton' + this.instance + '" class="nextButton"><!--Rollover Button--></span></a></div></div></div></div>';
			document.getElementById('insertHere' + this.instance).innerHTML = document.getElementById(this.queue.playlist).innerHTML;
		} else {
			this.player.htmlLayer.innerHTML = '<div id="videoMatte' + this.instance + '" class="videoMatte"><div id="videoOverlayWrapper'+ this.instance + '" class="videoOverlayWrapper" style="background:url(' + this.queue.preview + ') no-repeat 50% 50%;"><div id="clickToPlay'+ this.instance +'" class="clickToPlay"><!--EMPTY--></div></div></div>';
		}
	},
	addChildren: function (element) {
		element.innerHTML = '<div id="player_target' + this.instance + '"></div><div class="playerControls"><div class="playButtonWrapper"><a><span alt="Play" id="playButton' + this.instance + '"  class="playButton"><!--ROLOVER BUTTON--></span></a></div><div class="rewindButtonWrapper"><a><span alt="Rewind" id="rewindButton'+ this.instance +'" class="rewindButton"></span></a></div><div class="horizontal_track" id="slider'+ this.instance + '"><div class="horizontal_slit" >&nbsp;</div><div class="horizontal_slider" name="slider_thumb" id="your_slider_id" style="left: 0px;"><a><span class="slider_button"><!--ROLLOVER BUTTON--></span></a></div></div><div class="time" id="time' + this.instance +'">00:00</div><div alt="Mute" class="muteButtonWrapper" onclick="newNeb.mute();"><a><span id="muteButton'+this.instance+'" class="mute_button_off"><!--ROLLOVER BUTTON--></span></a></div><span id="messageDisplay' + this.instance + '" class="message">Loading...</span></div>';
	}
};




