HD.util.Player = function(config) {
	config = config || {};
	config.renderMode = config.renderMode || 'media'; // media (default) | youtube
	this.model = new HD.util.Player.Model(config);
	this.view = new HD.util.Player.View(this.model);
	this.controller = new HD.util.Player.Controller(this.model, this.view);
	this.model.addObserver(this.view);
};

HD.util.Player.Model = function(config) {
	this.config = config;
	this.observers = [];
};

HD.util.Player.Model.prototype = {

};

HD.extend(HD.util.Player.Model, [HD.util.Observable]);

HD.util.Player.View = function(model){
	this.model = model;
	this.config = this.model.config;
	this.observers = [];
	this.player = null;
	this.playerId = HD.util.Common.generateElementId();
	this.loadTemplates(arguments.callee);
	
	var cb = this;
};

HD.util.Player.View.templates = {
	placeholderText : '<a href="http://get.adobe.com/flashplayer/"><img src="/en_US/Media/images/content/pictorial/dark_custom_v2/img_no_flash.jpg" alt="" /></a>',
	
	getPlaceholderText : function(){
		return this.placeholderText;
	},
	
	getHtml : function() {
		return this.html;
	},
	
	html :  '\
		<div id="${playerId}-container" class="${classes.VIDEO_PLAYER}">\
			<div class="${classes.NO_FLASH}">${templates.getPlaceholderText()}</div>\
		</div>'
};

HD.util.Player.View.prototype = {
	EVENTS : {
		PLAY : "PLAY",
		STOP : "STOP"
	},
	
	setStyle : function(style){		
		this.playerStyle = style;
	},
	
	setListeners : function(data) {
		var parentEl = this.getParent();
		var divEl = parentEl.getElementsByTagName("div")[0];
		var playerId = HD.util.Common.generateElementId();
		
		// Cache the data object (for player event listeners).
		this.data = data || null;
		
		// Embed Flash player.
		var url;
		if (this.config.renderMode == 'youtube') {
			url = data.videoUrl;
		}
		else {
			url = comMediaBaseUrl + '/hd/hd_player/assets/player.swf';
		}
		var swbObj = new SWFObject(url, playerId, this.config.width, this.config.height, '9');
		swbObj.addParam('allowfullscreen','true');
		swbObj.addParam('wmode','transparent');
		swbObj.addParam('allowscriptaccess','always');
		if (this.config.renderMode == 'youtube') {
			swbObj.addParam('flashvars','rel=0' + (this.playerStyle || ''));
		}
		else {
			swbObj.addParam('flashvars','file=' + data.videoUrl + '&image=' + data.thumbUrl + (this.playerStyle || '') + '&type=video');
		}
		swbObj.write(divEl.id);
		HD.util.Player.View.views[playerId] = this;
		this.playerId = playerId;
		
		/*
		var cb = this;
		var dCb = data;
		document.getElementById(this.playerId).addViewListener("PLAY", function() {
			HD.util.Analytics.track(cb.analyticsString.call(cb, dCb, cb.model.config.parentWidget));
		});
		*/
	},
    
    destroy : function () {
    	var parentEl = this.getParent();
    	parentEl.innerHTML = "";
		var views = HD.util.Player.View.views;
		if (views[this.playerId]) {
			delete views[this.playerId];
		}
    },
	
    update : function(eventName, eventData) {
    	if(eventName == "doSomething_Start") {
    		this.loading(true);
    	} else if(eventName == "doSomething_Finish") {
    		this.loading(false);
    		this.render(eventData);
    	}
    },
	
	getHtml : function(data) {
		return this.processTemplate(this.config.templates.getHtml(), {
			playerId : this.playerId
		});
	},
	
	// Called when the player is ready.
	setPlayerListeners : function() {
		// override this method
	},
	
	// Media control methods.
	play: function() {
		if (this.player) {
			var swf = YAHOO.util.Dom.get(this.player.id);
			if (swf && swf.sendEvent) {
				swf.sendEvent('PLAY', 'true');
			}
		}
	},
	pause: function() {
		if (this.player) {
			var swf = YAHOO.util.Dom.get(this.player.id);
			if (swf && swf.sendEvent) {
				swf.sendEvent('PLAY', 'false');
			}
		}
	},
	stop: function() {
		if (this.player) {
			var swf = YAHOO.util.Dom.get(this.player.id);
			if (swf && swf.sendEvent) {
				swf.sendEvent('STOP');
			}
		}
	}
};

HD.extend(HD.util.Player.View, [HD.Widget]);

window.playerReady = function(player) {
	if(player.id != undefined) {
		HD.util.Player.View.views[player.id].player = player;
		HD.util.Player.View.views[player.id].setPlayerListeners();
	}
};

HD.util.Player.View.views = {};
HD.util.Player.View.stopAll = function(playerIds) {
	var views = HD.util.Player.View.views;
	if (playerIds) {
		for (var i = 0; i < playerIds.length; i++) {
			if (views[playerIds[i]]) {
				views[playerIds[i]].stop();
			}
		}
	}
	else {
		for (var id in views) {
			views[id].stop();
		}
	}
};

HD.util.Player.Controller = function(model, view){
	this.model = model;
	this.view = view;
};

HD.util.Player.Controller.prototype = {
	
};

// Fix obscure IE/Flash bug where hashes get injected into the title.
if (YAHOO.env.ua.ie > 0) {
	document.onpropertychange = function() {
		try {
			if (document && typeof document.title !== 'undefined' && document.location.hash && document.title.indexOf(document.location.hash) > -1)  {
				document.title = document.title.replace(document.location.hash, '');
			}
		} catch(e) {}
	}
} 

HD.register("hd_player", HD.util.Player, {version: "1.0", build: "1"});