var WTA = {};

WTA.videoPlayer = function(){

	var pl; 	// Player
	var ct; 	// Controller
	var el;	// Div container element
	
	// Private functions
	function _init(p,iv,e)
	{
		pl = document.getElementById(p);
		_initPlayer(iv);
		$(function(){
			_initController(e);
		})
	}
	
	function _initController(e)
	{
		// costruisco il controller
		ct = $('<div id="controller"></div>');
		ct.append("<ul><li><a rel='play' id='btn_play'>play</a></li><li><a rel='stop' id='btn_stop'>stop</a></li><li><a rel='volUp' id='btn_unmute'>Vol +</a></li><li><a rel='volDown' id='btn_mute'>Vol -</a></li>");
		ct.css({'display':'none'});
		
		// metto in posizione
		el = $(e);
		el.append(ct);
		
		// Attivo le interazioni
		$('a[rel=play]', el).live('click',_playVideo);
		$('a[rel=stop]', el).live('click',_pauseVideo);
		$('a[rel=volUp]', el).live('click',_unMuteVideo);
		$('a[rel=volDown]', el).live('click',_muteVideo);
		
		el.hover(
			function(){ ct.show(150); },
			function(){ ct.hide(150); }
		);
		
		$('a.videoSelect[rel]').live('click', function(){
			_setVideo($(this).attr('rel'));
		})
	}
	
	function _initPlayer(iv)
	{
		pl.addEventListener("onStateChange", "_onPlayerStateChange");
		pl.addEventListener("onError", "_onPlayerError");
  	
		pl.cueVideoById(iv);
	}
	
	// This function is called when an error is thrown by the player
	function _onPlayerError(errorCode) {
	  alert("An error occured of type:" + errorCode);
	}
	
	// This function is called when the player changes state
	function _onPlayerStateChange(newState) {
	  alert(newState);
	}
	
	function _playVideo() {
		if (pl) {
    		pl.playVideo();
		}
	}

	function _pauseVideo() {
		if (pl) {
    	pl.pauseVideo();
		}
	}

	function _muteVideo() {
		if(pl) {
			pl.mute();
		}
	}
	
	function _unMuteVideo() {
		if(pl) {
			pl.unMute();
		}
	}
	
	function _setVideo(vd)
	{
		if(pl) {
			pl.cueVideoById(vd);
			$(window).scrollTo(el, 300, {offset: -95});
			pl.playVideo();
		}
	}
	
	// Public functions
	return {
		init: 		function(p,iv,e){ _init(p,iv,e) },
		getPlayer: 	function(){ return pl; },
		setVideo: 	function(vd){ _setVideo(vd) },
		play:			_playVideo,
		pause:		_pauseVideo,
		mute:			_muteVideo,
		unmute:		_unMuteVideo
	}
}();



WTA.slideShow = function(){
	// Private vars
	var el, url;
	
	var opts = {
		width: 620,
		height: 310
	}
	
	// Private functions
	function _init(e,u,o)
	{
		el 		= $(e);
		url		= u;
		$.extend(opts, o);
		
		var flashvars 		= { dataFile: url};
		var params 			= { menu: "false" };
		var attributes 	= { };
 
		swfobject.embedSWF("http://www.sfilatadamoreemoda.com/assets/swf/monoslideshow.swf", el.attr('id'), opts.width, opts.height, "9.0.0","http://www.sfilatadamoreemoda.com/assets/swf/expressInstall.swf", flashvars, params, attributes);
	}
	
	// Public functions
	return {
		init: function(e,u,o) { _init(e,u,o) }
	}
}();
