/**
 * Javascript Sources
 * @author Frederic GINIOUX
 */

$(document).ready(function(e){
	
});

var ptmdl = {};
ptmdl.tools = {};

ptmdl.tools.isEnterKey = function(e){
	var characterCode;
	if(e && e.which){
  	e = e;
   	characterCode = e.which;
  } else if(arguments[0]){
	  e = event;
	  characterCode = e.keyCode;
	}
		
	if(characterCode == 13) return true;
	else return false;	
};

ptmdl.tools.stopEvent = function(evt){
	if(window.event){ //IE
		evt = window.event;
		evt.returnValue = false;
	}
	try {
		evt.cancelBubble = true;
		if(evt.preventDefault) evt.preventDefault();
		if(evt.stopPropagation) evt.stopPropagation();
		if(evt.stopped) evt.stopped = true;
	} catch(e) {}		
	return evt;
};

ptmdl.videoPlayer = function(container, options){
	this._defaultOptions = {videoUrl: null, params: {}, attributes: {}, sizes: {width: 0, height: 0}};
	this.options = $.extend({}, this._defaultOptions, options);
	this.container = $(container);
	this.thumbnail = $('.thumbnail', this.container);
	this.videoContainer = $('.video_container');
	this.instance = null;
	return this.initialize();
};

$.extend(ptmdl.videoPlayer.prototype, {
	initialize: function(){
		var that = this;
		$(this.thumbnail).bind('click', function(e){
			that.embedPlayer();
		});	
		
		return this;		
	},
	
	embedPlayer: function(){
		var container = $('div:eq(0)', this.videoContainer);
		if(container.is('div') && !this.instance){
			var ID = container.attr('id');
			swfobject.embedSWF(this.options.videoUrl, "video_container", this.options.sizes.width, this.options.sizes.height, "8", null, null, this.options.params, this.options.attributes);
		}
		this.thumbnail.css({display: 'none'});
		this.videoContainer.css({display: 'block'});
		return this;
	},
	
	removePlayer: function(){
		if(this.instance){
			try{
				this.instance.mute();
				this.instance.stopVideo();
			} catch(e) {}
		}
		this.thumbnail.css({display: 'block'});
		this.videoContainer.css({display: 'none'});
		if(!$.browser.safari){
			this.instance = null;
			this.videoContainer.html('<div id="video_container">&nbsp;</div>');
		}
		return this;
	}	
});


