function CoreVideoControls(parentDiv) {
	this.parentDiv = $(parentDiv);
	this.buttons = new Array();
	this.scrubbers = new Array();
	this.clocks = new Array();
	this.amScrubbing = false;
	this.duration = null;
	this.initList = new Array();
}

CoreVideoControls.prototype.initialize = function(callbackMethod) {
	for (var i = 0; i < this.initList.length; i++) {
		this.initList[i]();
	}
	callbackMethod();
}

CoreVideoControls.prototype.handleXMPEvent = function(p_method, p_arg) {
		var returnVal;
		switch(p_method) {
			case 'setEmailEnabled':
				if (p_arg == true)
					this.enableEmail();
				if (p_arg == false)
					this.disableEmail();
				break;				
			case 'setVolume':
				this.scrubbers['volume'].scrubberPointer.setValue((p_arg/100));
				break;
			case 'setTime':
				this.setTime(p_arg);
				break;
			case 'setDuration':
				this.setDuration(p_arg);
				break;
			case 'togglePlay':
				if (p_arg == true) {
					this.enablePause();
				} else {
					this.enablePlay();
				}
				break;
			case 'setScrubberEnabled':
				if (p_arg == true) {
					this.enableTimelineScrubber();
				} else {
					this.disableTimelineScrubber();
				}
				break;
			case 'setLoadedPercent':
				this.setTimelineScrubberBuffer(p_arg);
				break;
			case 'getVolume':
				returnVal = 100 * this.scrubbers['volume'].scrubberPointer.value;
				break;
		}
		return returnVal;
}

/* 	**********************************************

	End		-	Initialization 
	Start 	- 	Play

	********************************************** */
	
function CoreVideoControls_Play() { }

CoreVideoControls_Play.prototype.swapPlayPause = function(hide,show) {
	$(hide).style.display = 'none';
	$(show).style.display = 'block';
}

CoreVideoControls_Play.prototype.disablePlay = function() {
	if ($('playLayer')) {
	this.swapPlayPause('pauseLayer','playLayer');
	$('playLayer').className = this.buttons['play'].classes['disabled'];
	$('playLayer').onclick = function() { }
	$('playLayer').onmouseover = function() { }
	$('playLayer').onmouseout = function() { }
	}
}

CoreVideoControls_Play.prototype.enablePlay = function() {
	if ($('playLayer')) {
	this.swapPlayPause('pauseLayer','playLayer');
	$('playLayer').className = this.buttons['play'].classes['activeOff'];
	$('playLayer').onclick = this.buttons['play'].clickEvent;
	$('playLayer').onmouseover = function() { this.className = XMPPlaylistManager.getInstance().controller.buttons['play'].classes['activeOn']; }
	$('playLayer').onmouseout = function() { this.className = XMPPlaylistManager.getInstance().controller.buttons['play'].classes['activeOff']; }
	}
}

CoreVideoControls_Play.init = function() {
	// place the play button
	var divObj = document.createElement('div');
	divObj.setAttribute('id','playLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.buttons['play'].classes['disabled'];
	divObj.style.display = 'none';
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
}

CoreVideoControls_Play.clickEvent = function() {
	XMPPlayer.getActivePlayer().handleEvent('playPressed');
}

/* 	**********************************************

	End		-	Play 
	Start 	- 	Pause

	********************************************** */

function CoreVideoControls_Pause() { }

CoreVideoControls_Pause.prototype.disablePause = function() {
	if ($('pauseLayer')) {
	this.swapPlayPause('playLayer','pauseLayer');
	$('pauseLayer').className = this.buttons['pause'].classes['disabled'];
	$('pauseLayer').onclick = function() { }
	$('pauseLayer').onmouseover = function() { }
	$('pauseLayer').onmouseout = function() { }
	}
}

CoreVideoControls_Pause.prototype.enablePause = function() {
	if ($('pauseLayer')) {
	this.swapPlayPause('playLayer','pauseLayer');
	$('pauseLayer').onclick = this.buttons['pause'].clickEvent;
	$('pauseLayer').onmouseover = function() { this.className = XMPPlaylistManager.getInstance().controller.buttons['pause'].classes['activeOn']; }
	$('pauseLayer').onmouseout = function() { this.className = XMPPlaylistManager.getInstance().controller.buttons['pause'].classes['activeOff']; }
	}
}

CoreVideoControls_Pause.init = function() {
	// place the pause disabled button
	var divObj = document.createElement('div');
	divObj.setAttribute('id','pauseLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.buttons['pause'].classes['disabled'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
}

CoreVideoControls_Pause.clickEvent = function() {
	XMPPlayer.getActivePlayer().handleEvent('pausePressed');
}

/* 	**********************************************

	End		-	Pause/Play 
	Start 	- 	Email

	********************************************** */
function CoreVideoControls_Email() { }

CoreVideoControls_Email.prototype.disableEmail = function() {
	$('emailLayer').className = this.buttons['email'].classes['disabled'];
	$('emailLayer').onclick = function() { }
	$('emailLayer').onmouseover = function() { }
	$('emailLayer').onmouseout = function() { }
}

CoreVideoControls_Email.prototype.enableEmail = function() {
	$('emailLayer').className = this.buttons['email'].classes['activeOff'];
	$('emailLayer').onclick = this.buttons['email'].clickEvent;
	$('emailLayer').onmouseover = function() { this.className = XMPPlaylistManager.getInstance().controller.buttons['email'].classes['activeOn']; }
	$('emailLayer').onmouseout = function() { this.className = XMPPlaylistManager.getInstance().controller.buttons['email'].classes['activeOff']; }
}

CoreVideoControls_Email.init = function() {
	// place the email disabled button
	var divObj = document.createElement('div');
	divObj.setAttribute('id','emailLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.buttons['email'].classes['disabled'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
}

CoreVideoControls_Email.clickEvent = function() {
	XMPPlayer.getActivePlayer().handleEvent('sharePressed');
}

/* 	**********************************************

	End		-	Email 
	Start 	- 	Timeline

	********************************************** */

function CoreVideoControls_Timeline() { }

CoreVideoControls_Timeline.prototype.enableTimelineScrubber = function() {
	if ($('timelineTrackBackground')) {
	this.scrubbers['timeline'].scrubberPointer.disabled = false;
	$('timelineHandler').className = this.scrubbers['timeline'].classes['handlerEnabled'];
	}
}

CoreVideoControls_Timeline.prototype.disableTimelineScrubber = function() {
	if ($('timelineTrackBackground')) {
	this.scrubbers['timeline'].scrubberPointer.disabled = true;
	$('timelineHandler').className = this.scrubbers['timeline'].classes['handlerDisabled'];
	}
}

CoreVideoControls_Timeline.prototype.setTimelineScrubberBuffer = function(val) {
	if ($('timelineTrackBackground')) {
	$('timelineTrackBuffer').style.width = ('' + (val * 100) + '%');
	}
}

CoreVideoControls_Timeline.prototype.doneTimelineScrubbing = function(val) {
	if ($('timelineTrackBackground')) {
	XMPPlayer.getActivePlayer().getMediaPlayer().seek(Math.floor(this.duration * val));
	}
}

CoreVideoControls_Timeline.init = function() {
	// place the background layer for the handle
	var divObj = document.createElement('div');
	divObj.setAttribute('id','timelineTrackBackground');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].classes['trackBackground'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);

	// fill the background layer
	var divObj = document.createElement('div');
	divObj.setAttribute('id','timelineTrackBuffer');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].classes['trackBuffer'];
	$('timelineTrackBackground').appendChild(divObj);

	// create the handle encloser
	var divObj = document.createElement('div');
	divObj.setAttribute('id','timelineHandlerBacker');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].classes['handlerBacker'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
	
	// create the handle
	var divObj = document.createElement('div');
	divObj.setAttribute('id','timelineHandler');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].classes['handlerDisabled'];
	$('timelineHandlerBacker').appendChild(divObj);

	XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].scrubberPointer = new Control.Slider('timelineHandler', 'timelineHandlerBacker', {
		onSlide: XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].slideEvent,
		onChange: XMPPlaylistManager.getInstance().controller.scrubbers['timeline'].changeEvent,
		disabled: true
	});
}

CoreVideoControls_Timeline.slideEvent = function(val) {
	XMPPlaylistManager.getInstance().controller.amScrubbing = true;
}

CoreVideoControls_Timeline.changeEvent = function(val) {
	XMPPlaylistManager.getInstance().controller.amScrubbing = false;
	XMPPlaylistManager.getInstance().controller.doneTimelineScrubbing(val);
}

/* 	**********************************************

	End		-	Timeline 
	Start 	- 	Volume Button

	********************************************** */

function CoreVideoControls_VolumeButton() { }

CoreVideoControls_VolumeButton.prototype.setVolumeButton = function(val) {
	this.buttons['volume'].isMuted = false;
	if (val < 0.3333 ) {
		$('volumeLayer').className = this.buttons['volume'].classes['disabled'];
	} else if (val >= 0.3333 && val < 0.6666) {
		$('volumeLayer').className = this.buttons['volume'].classes['activeOff'];
	} else if (val >= 0.6666) {
		$('volumeLayer').className = this.buttons['volume'].classes['activeOn'];
	}
	XMPPlayer.getActivePlayer().handleEvent('volumeChanged');
}

CoreVideoControls_VolumeButton.init = function() {
	// place the volume mutted button
	XMPPlaylistManager.getInstance().controller.buttons['volume'].isMuted = false;
	XMPPlaylistManager.getInstance().controller.buttons['volume'].lastDrop = 0;
	var divObj = document.createElement('div');
	divObj.setAttribute('id','volumeLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.buttons['volume'].classes['disabled'];
	divObj.onclick = function() { XMPPlaylistManager.getInstance().controller.buttons['volume'].clickEvent(); }
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
}

CoreVideoControls_VolumeButton.clickEvent = function() {
	var c = XMPPlaylistManager.getInstance().controller;
	if (c.buttons['volume'].isMuted) {
		c.scrubbers['volume'].scrubberPointer.setValue(c.buttons['volume'].lastDrop);
		c.setVolumeButton(c.buttons['volume'].lastDrop);
	} else {
		c.buttons['volume'].lastDrop = c.scrubbers['volume'].scrubberPointer.value;
		c.scrubbers['volume'].scrubberPointer.setValue(0);
		c.setVolumeButton(0);
		c.buttons['volume'].isMuted = true;
	}
}

/* 	**********************************************

	End		-	Volume Button 
	Start 	- 	Volume Scrubber

	********************************************** */

function CoreVideoControls_VolumeScrubber() { }

CoreVideoControls_VolumeScrubber.slideChangeEvent = function(val) {
	XMPPlaylistManager.getInstance().controller.setVolumeButton(val);
}

CoreVideoControls_VolumeScrubber.init = function() {
	// place the background layer for the handle
	var divObj = document.createElement('div');
	divObj.setAttribute('id','volumeTrackBackground');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['volume'].classes['trackBackground'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);

	// create the handle encloser
	var divObj = document.createElement('div');
	divObj.setAttribute('id','volumeHandlerBacker');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['volume'].classes['handlerBacker'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
	
	// create the handle
	var divObj = document.createElement('div');
	divObj.setAttribute('id','volumeHandler');
	divObj.className = XMPPlaylistManager.getInstance().controller.scrubbers['volume'].classes['handlerEnabled'];
	$('volumeHandlerBacker').appendChild(divObj);

	XMPPlaylistManager.getInstance().controller.scrubbers['volume'].scrubberPointer = new Control.Slider('volumeHandler', 'volumeHandlerBacker', {
		onSlide: XMPPlaylistManager.getInstance().controller.scrubbers['volume'].slideEvent,
		onChange: XMPPlaylistManager.getInstance().controller.scrubbers['volume'].changeEvent
	});
}

/* 	**********************************************

	End		-	Volume Scrubber
	Start 	- 	Time and Duration

	********************************************** */

function CoreVideoControls_Clock() { }

/*CoreVideoControls_Clock.prototype.calculateTime = function(val) {
	if ($('timeLayer')) {
		if (val > 59) {
			var minutes = Math.floor(val / 60);
			seconds = (val % 60);
			var seconds = val - (60 * minutes);
			if (seconds < 10) seconds = ('' + "0") + seconds;
			return (minutes + ":" + seconds);
		} else {
			if (val < 10) val = ('' + "0") + val;
			return ("0:" + val);
		}
	}
}*/
CoreVideoControls_Clock.prototype.calculateTime = function(val) {
	var hours = minutes = seconds = 0;
	if ( $('timeLayer') ) {
		if ( val > 3599 ) {
			hours = Math.floor( val / 3600 );
			val = val - ( hours * 3600 ); 
		}
		if ( val > 59 ) {
			minutes = Math.floor(val / 60);
			seconds = val - (60 * minutes);
			if (seconds < 10) seconds = "0" + seconds;
			if (minutes < 10 && hours) minutes = "0" + minutes;
			return ( hours ? hours +':' : '' ) + minutes + ":" + seconds;
		}
		else {
			if (val < 10) val = "0"+ val;
			return "0:" + val;
		}
	}
}

CoreVideoControls_Clock.prototype.setDuration = function(val) {
	if ( $('timeLayer') ) {
		if ( val ) {
			this.duration = val;
			$('durationLayer').innerHTML = this.calculateTime(val);
		}
	}
}

CoreVideoControls_Clock.prototype.setTime = function(val) {
	if ( $('timeLayer') ) {
		$('timeLayer').innerHTML = this.calculateTime(val);
		if (this.duration != null && this.amScrubbing == false) {
			this.scrubbers['timeline'].scrubberPointer.setValueExternally(val/this.duration);
		}
	}
}

CoreVideoControls_Clock.prototype.resetTimeDuration = function() {
	if ( $('timeLayer') ) {
		this.setTime(0);
		this.setDuration(0);
	}
}

CoreVideoControls_Clock.init = function() {
	var divObj = document.createElement('div');
	divObj.setAttribute('id','timeLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.clocks['time and duration'].classes['time'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
	$('timeLayer').innerHTML = "0:00";

	var divObj = document.createElement('div');
	divObj.setAttribute('id','sepLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.clocks['time and duration'].classes['sep'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
	$('sepLayer').innerHTML = "/";

	var divObj = document.createElement('div');
	divObj.setAttribute('id','durationLayer');
	divObj.className = XMPPlaylistManager.getInstance().controller.clocks['time and duration'].classes['duration'];
	XMPPlaylistManager.getInstance().controller.parentDiv.appendChild(divObj);
	$('durationLayer').innerHTML = "0:00";
}

/* 	**********************************************

	End		-	Time and Duration 
	Start 	- 	add<object> abstracts

	********************************************** */

CoreVideoControls.prototype.addButton = function(buttonName,buttonInit,disabledClass,activeOffClass,activeOnClass,clickEvent) {
	this.buttons[buttonName] = new CoreVideoControls.Button(disabledClass,activeOffClass,activeOnClass,clickEvent);
	this.initList[this.initList.length] = buttonInit;
}

CoreVideoControls.prototype.addClock = function(clockName,clockInit,timeClass,durationClass,sepClass) {
	this.clocks[clockName] = new CoreVideoControls.Clock(timeClass,durationClass,sepClass);
	this.initList[this.initList.length] = clockInit;
}

CoreVideoControls.prototype.addScrubber = function(scrubberName,scrubberInit,trackBackground,trackBuffer,handlerBacker,handlerDisabled,handlerEnabled,slideEvent,changeEvent) {
	this.scrubbers[scrubberName] = new CoreVideoControls.Scrubber(trackBackground,trackBuffer,handlerBacker,handlerDisabled,handlerEnabled,slideEvent,changeEvent);
	this.initList[this.initList.length] = scrubberInit;
}

CoreVideoControls.inheritAll = function() {
	for (var i = 1; i < arguments.length; i++) {
		for (x in arguments[i].prototype) {
			arguments[0].prototype[x] = arguments[i].prototype[x];
		}
	}
}

/* 	**********************************************

	End 	- 	add<object> abstracts
	Start 	- 	<object> abstracts

	********************************************** */

CoreVideoControls.Button = function(disabledClass,activeOffClass,activeOnClass,clickEvent) {
	this.classes = new Array();
	this.classes['disabled'] = disabledClass;
	this.classes['activeOff'] = activeOffClass;
	this.classes['activeOn'] = activeOnClass;
	this.clickEvent = clickEvent;
}

CoreVideoControls.Clock = function(time,duration,sep) {
	this.classes = new Array();
	this.classes['time'] = time;
	this.classes['duration'] = duration;
	this.classes['sep'] = sep;
}

CoreVideoControls.Scrubber = function(trackBackground,trackBuffer,handlerBacker,handlerDisabled,handlerEnabled,slideEvent,changeEvent) {
	this.classes = new Array();
	this.classes['trackBackground'] = trackBackground;
	this.classes['trackBuffer'] = trackBuffer;
	this.classes['handlerBacker'] = handlerBacker;
	this.classes['handlerEnabled'] = handlerEnabled;
	this.classes['handlerDisabled'] = handlerDisabled;
	this.slideEvent = slideEvent;
	this.changeEvent = changeEvent;
}

Control.Slider.prototype.setValueExternally = function(sliderValue, handleIdx){
	if(!this.active) {
		this.activeHandleIdx = handleIdx || 0;
		this.activeHandle    = this.handles[this.activeHandleIdx];
		this.updateStyles();
	}
	handleIdx = handleIdx || this.activeHandleIdx || 0;
	if(this.initialized && this.restricted) {
		if((handleIdx>0) && (sliderValue<this.values[handleIdx-1]))
			sliderValue = this.values[handleIdx-1];
		if((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1]))
			sliderValue = this.values[handleIdx+1];
	}
	sliderValue = this.getNearestValue(sliderValue);
	this.values[handleIdx] = sliderValue;
	this.value = this.values[0]; // assure backwards compat
	
	this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] = 
	this.translateToPx(sliderValue);
	
	this.drawSpans();
}

CoreVideoControls.inheritAll(CoreVideoControls,CoreVideoControls_Play,CoreVideoControls_Pause,CoreVideoControls_Email,CoreVideoControls_Timeline,CoreVideoControls_VolumeButton,CoreVideoControls_VolumeScrubber,CoreVideoControls_Clock);
