//Requires swfobject2
function Player(par, options, closebtn, show) {
	//PRIVATE
	var par,
		options = (options == null ? true : options),
		closebtn = (closebtn == null ? true : closebtn),
		show = (show == null ? false : show);
	var self = this;
	
	//PUBLIC
	this.flashVersion = (typeof swfobject == 'object' ? swfobject.getFlashPlayerVersion() : {'major':0});
	this.html5Works = canPlayMP4(document.createElement('video'));
	this.playerRel = document.createElement('div');
	this.playerOuter = document.createElement('div');
	this.optionsArea = document.createElement('div');
	this.optionsPane = null;
	this.optionsOpen = false;
	this.player = null;
	this.closeButton = document.createElement('a');
	this.optionsButton = document.createElement('a');
	this.optionsClick = null;
	this.defaults = {'flash': '', 'html5': ''};
	this.playingURL = '';
	this.setVideoState = '';
	this.hideContentOnOpen = false;
	this.useControls = true;
	this.isIOS = (/iPhone|iPod|iPad/i).test(navigator.userAgent);
	this.closeFunction = '';
	this.flashScaling = '';
	
	//METHODS
	this.init = function() {
		if(typeof par == 'string')
			par = document.getElementById(par);
		if(typeof par == 'object') {
			par.innerHTML = '';
			this.playerOuter.style.width = "100%";
			this.playerOuter.style.height = "100%";
			this.playerRel.className = 'rel';
			this.playerRel.appendChild(this.playerOuter);
			par.appendChild(this.playerRel);
			if(this.flashVersion['major'] != 0)
				this.buildFlash();
			else if(this.html5Works)
				this.buildHTML5();
			if(closebtn)
				this.setupCloseButton();
			if(options && !this.isIOS)
				this.setupOptions();
			//No HTMl5 or Flash? Show options.
			if(this.flashVersion['major'] == 0 && this.html5Works == false)
				this.toggleOptions();
			if(show)
				this.togglePlayer(true);
		}
	}
	
	this.setupCloseButton = function(term) {
		var term = (term === '' || term === null ? term = 'Close' : term);
		this.closeButton.className = 'close-player';
		this.closeButton.innerHTML = term;
		this.closeButton.href = '#';
		this.playerRel.appendChild(this.closeButton);
		this.setupCloseButtonEvents();
	}
	
	this.setupCloseButtonEvents = function() {
		if(this.closeFunction != '') {
			addListener(this.closeButton, 'click', function(e) {
				preventDefaults(e);
				window[self.closeFunction]();
			}, false);
		}
		else {
			addListener(this.closeButton, 'click', function(e) {
				preventDefaults(e);
				self.togglePlayer();
			}, false);
		}
	}
	
	this.setupOptions = function() {
		this.optionsArea.className = 'bottom';
		this.optionsButton.id = 'viewing-options';
		this.optionsButton.href = '#';
		this.optionsButton.innerHTML = 'Viewing Options';
		addListener(this.optionsButton, 'click', function(e) {
			preventDefaults(e);
			self.toggleOptions();
		}, false);
		this.optionsArea.appendChild(this.optionsButton);
		this.playerRel.appendChild(this.optionsArea);
		//Now that the button is setup, let's grab the actual options' content
		this.optionsPane = $('playerOptions');
		if(this.optionsPane != null) {
			this.playerRel.appendChild(this.optionsPane);
			addListener($('options-close'), 'click', function(e) {
				preventDefaults(e);
				self.toggleOptions();
			}, false);
		}
	}
	
	this.toggleOptions = function() {
		var item = this;
		if(this.optionsPane != null) {
			switch(this.optionsPane.style.display) {
				case 'block':
					this.optionsPane.style.display = 'none';
					setTimeout(function() { item.optionsPane.className = ''; }, 200);
					this.videoState('play');
					this.optionsOpen = false;
					break;
				case 'none':
				default:
					this.optionsPane.style.display = 'block';
					setTimeout(function() { item.optionsPane.className = 'active'; }, 200);
					this.videoState('pause');
					this.optionsOpen = true;
					break;
			}
		}
	}
	
	this.buildFlash = function() {
		this.playerOuter.id = par.id + "flashVideo";
		this.player = document.createElement('div');
		this.player.id = par.id + 'flashPlayer';
		this.playerOuter.appendChild(this.player);
		var params = {
			allowscriptaccess:"always",
			allowfullscreen:"true",
			wmode:"transparent",
			bgcolor:"#000000"
		};		
		swfobject.embedSWF("media/videoplayer.swf", par.id + "flashPlayer", "100%", "100%", "9.0.0", "", {}, params, {id: par.id + "flashPlayer"});
	}
	
	this.buildHTML5 = function() {
		this.playerOuter.id = par.id + "html5Video";
		this.player = document.createElement('video');
		if(this.html5Works) {
			this.player.id = par.id + "html5VideoObject";
			this.player.style.width = "100%";
			this.player.style.height = "100%";
			if(this.useControls)
				this.player.controls = "controls";
			this.playerOuter.appendChild(this.player);
		}
	}
	
	this.setControls = function(state) {
		var state;
		this.useControls = (state !== '' && state !== null ? state : this.useControls);
		switch(this.playerOuter.id) {
			case par.id + 'flashVideo':
				if(typeof this.player.sendToVideoPlayer == 'function') {
					if(this.useControls == false)
						this.player.showControls('false');
					else
						this.player.showControls('showControls');
				}
				else
					setTimeout(function(){ self.setControls(self.useControls); }, 500); //Keep trying until it's recognized
				break;
			case par.id + 'html5Video':
				if(this.useControls == false)
					this.player.controls = "";
				else
					this.player.controls = "controls";
				break;
		}
	}
	
	this.playVideo = function(url) {
		var url;
		this.setVideoState = 'play';
		switch(this.playerOuter.id) {
			case par.id + 'flashVideo':
				url = (url == null ? this.defaults['flash'] : url);
				this.player = document.getElementById(par.id + 'flashPlayer');
				this.playingURL = url;
				if(typeof this.player.setVideoPlayerURL == 'function') {
					if(this.closeFunction != '')
						this.player.setCompleteFunction(this.closeFunction);
					this.player.showReplay('false');
					this.player.setVideoPlayerURL(url);
					this.player.sendToVideoPlayer('play');
				}
				else
					setTimeout(function(){ self.playVideo(); }, 1000); //Keep trying until it's recognized
				break;
			case par.id + 'html5Video':
				url = (url == null ? this.defaults['html5'] : url);
				this.playingURL = url;
				this.player.src = url;
				if(this.closeFunction != '')
					this.player.addEventListener('ended', this.html5Close, false);
				this.player.load();
				this.player.play();
				break;
		}
		//alert('play '+this.playerOuter.id+': '+this.playingURL);
	}
	
	this.html5Close = function() {
		if(self.closeFunction != '')
			window[self.closeFunction]();
		self.player.removeEventListener('ended', self.html5Close, false);
	}
	
	this.setFlashScaling = function(state) {
		var state = (state == null || (state != 'true' && state != 'false') ? 'false' : state);
		this.flashScaling = state;
		if(typeof this.player.setScaling === 'function')
			this.player.setScaling(this.flashScaling);
		else
			setTimeout(function(){ self.setFlashScaling(self.flashScaling); }, 1000); //Keep trying until it's recognized
	}
	
	this.videoState = function(state) {
		var state = (state == null || (state != 'play' && state != 'pause') ? 'pause' : state);
		this.setVideoState = state;
		switch(this.playerOuter.id) {
			case par.id + 'flashVideo':
				if(typeof this.player.sendToVideoPlayer == 'function')
					this.player.sendToVideoPlayer(state);
				else
					setTimeout(function(){ self.videoState(self.setVideoState); }, 1000); //Keep trying until it's recognized
				break;
			case par.id + 'html5Video':
				this.player.pause();
				break;
		}
	}
	
	this.togglePlayer = function(how) {
		var how, state, cls, vis;
		if((par.style.display == 'block' && how != true) || how == 'hide' || how == false) {
			how = 'none';
			state = 'close';
			cls = '';
			vis = 'visible';
		}
		else {
			how = 'block';
			state = 'play';
			cls = 'active';
			vis = 'hidden';
		}
		if(!this.optionsOpen) {
			self.videoState(state);
			par.style.display = how;
			setTimeout(function() { par.className = 'video '+cls; }, 200);
			if(self.hideContentOnOpen) {
				var hideTT = $('no-bg');
				if(hideTT != null && hideTT.className != 'active')
					hideTT.style.display = how;
				var hideContent = $('static');
				if(hideContent != null)
					hideContent.style.visibility = vis;
			}
		}
	}
	
	//START
	swfobject.switchOffAutoHideShow();
	this.init();
}

function canPlayMP4(v) {
	var v;
	return !!(v.canPlayType && v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/, ''));
}

function isIE(){ return !+"\v1"; }
