

var veoliaplayer=(function(){

	/*!
	* Boom. Ajax! Ever heard of it!?
	* copyright 2011 @dedfat
	* https://github.com/ded/reqwest
	* license MIT
	*/
	!function (context) {
	  var twoHundo = /^20\d$/,
	      xhr = ('XMLHttpRequest' in window) ?
	        function () {
	          return new XMLHttpRequest();
	        } :
	        function () {
	          return new ActiveXObject('Microsoft.XMLHTTP');
	        };
	
	  function readyState(o, fn) {
	    return function () {
	      if (o && o.readyState == 4) {
	        if (twoHundo.test(o.status)) {
	          fn && typeof fn == 'function' ? fn(o) : fn.success(o);
	        } else {
	          fn && fn.error && fn.error(o);
	        }
	        fn && fn.complete && fn.complete(o);
	      }
	    };
	  }
	
	  function getRequest(o, fn) {
	    var http = xhr();
	    http.open(o.method || 'GET', typeof o == 'string' ? o : o.url, true);
	    http.onreadystatechange = readyState(http, fn || o);
	    http.send(o.data || null);
	    return http;
	  }
	
	  // would be cool if there was some fancy class system out there...
	  function Reqwest(o, fn) {
	    var type = o.type || 'js';
	    fn = fn || function () {};
	    function success(resp) {
	      var r = resp.responseText, val=r;
	      if ( /json$/i.test(type) ) {
	      		if (typeof(JSON)!=="undefined") {
	      			val =JSON.parse(r);
	      		} else {
	      			eval("val="+r);
	      		}
	      }
	          
	      if (/^js$/i.test(type)) eval(r);
	      
	      fn(o);
	      o.success && typeof o.success == 'function' && o.success(val);
	    }
	    this.request = getRequest(o, success);
	    this.retries = o.retries || 0;
	  }
	
	  Reqwest.prototype = {
	    abort: function () {
	      this.request.abort();
	    },
	
	    retry: function () {
	      this.request.send(this.o.data || null);
	    }
	  };
	
	  function reqwest(o, fn) {
	    return new Reqwest(o, fn);
	  }
	
	  var oldJax = context.reqwest;
	  reqwest.noConflict = function () {
	    context.reqwest = oldJax;
	    return this;
	  };
	  context.reqwest = reqwest;
	
	}(this);


	var DEFAULT_CONFIG={}, loading=0, delayed=[];
	
	// OUTILS
	//--------------
	
	// thanks: http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone
	function clone(obj) {
		if (!obj || typeof obj != 'object') { return obj; }		
		var temp = new obj.constructor();	
		for (var key in obj) {	
			if (obj.hasOwnProperty(key)) {
				temp[key] = clone(obj[key]);
			}
		}		
		return temp;
	}
	
	function fuse(obj,src) {
	    if (!obj || typeof obj != 'object') { 
	        return;
	    }
	    if (!src || typeof src != 'object') { 
	        return; 
	    }
	
	    for (var p in src) {
	    	if (!src.hasOwnProperty(p)) continue;
	    	
	    	switch (typeof src[p]) {
	    		case "object":
				if (!obj[p])
					obj[p]={};
				fuse(obj[p],src[p]);
	    			break;
	    		case "string":
	    			obj[p]=src[p].replace(/%(?!25)/g,'%25');
	    			break;
	    		default:
	    			obj[p]=src[p];
	    			break;
	    	}
	    }
	}
	
	// VEOLIA PLAYER
	//--------------
	
	function loadConfig(incoming) {
		if ( flashembed.getVersion()[0]=="0" ) return;
		var $obj=this;

		switch ( typeof(incoming) ) {
			case "string":
				loading++;
				reqwest({
					url: incoming,
					type: 'json',
					method: 'get',
					success: function (resp) {
						fuse(DEFAULT_CONFIG,resp);
					    	loading--;

					    	if (loading>0) return;
					    	
					    	for (i=0, len=delayed.length;i<len;i++)
					    		applyembed.apply($obj,delayed[i]);
					    	delayed=[];
					}
				});
			break;
			
			case "object":
				fuse(DEFAULT_CONFIG,incoming);
			break;
			
			default:break;
		}
	}
	
	
	function embed(container, config, flashparams) {
		if ( flashembed.getVersion()[0]=="0" ) return;
		
		document.getElementById(container).innerHTML='';
		document.getElementById(container).style.display = "block";
		if (loading) {
			delayed.push([container, config, flashparams]);
		} else {
			applyembed(container, config, flashparams);
		}
	}
	
	
	function eventclosure(f) {
		return function(clip, info) {
			if (clip.isSplash) return;
			f.call(this, clip, info);
		};
	}
	
	function applyembed(container, config, flashparams) {

	      	function makeConfig(config)
	      	{
	      		var obj = {};
	      		fuse(obj,DEFAULT_CONFIG);
	      		fuse(obj,config);
	      		
	      		if (!obj.plugins) obj.plugins = {};
				
      			// nettoyage captions si non utilisé
      			if (!obj.clip.captionUrl) {
      				delete obj.plugins.captions;
      				delete obj.plugins.captionsContent;
      			}
	      		
	      		// sharing
	      		if (obj.sharing === false) {
	      			delete obj.plugins.sharing;
	      			delete obj.plugins.dock;
	      		}
	      		
	      		//evenements
	      		if (obj.clip.onResumed) {
	      			obj.clip.onResume=obj.clip.onResumed;
	      			delete obj.clip.onResumed;
	      		}
	      		if (obj.clip.onPaused) {
	      			obj.clip.onPause=obj.clip.onPaused;
	      			delete obj.clip.onPaused;
	      		}
				
	      		// gestion d'une splash image ou d'une vidéo hd
				// avec les playlists
	      		if (obj.splash || obj.clip.hdUrl) {
	      			obj.playlist = [];
	      			
      				var reclip={}, f=null;
				for (var p in obj.clip) {
					if (typeof obj.clip[p] != 'function') continue;
					reclip[p]=eventclosure(obj.clip[p]);
					delete obj.clip[p];
				}
					
      				if (obj.splash) {
      					obj.playlist.push({ 
      						url: obj.splash,
      						isSplash: true
      					});
      					delete obj.splash;
      				}

				var cl = clone(obj.clip);					
				obj.playlist.push( cl );
					
      				if (cl.hdUrl) {
      					cl.isSD = true;
      					var hdClip = clone(obj.clip);
      					hdClip.url = obj.clip.hdUrl;
      					hdClip.isHD = true;
      					delete hdClip.hdUrl;
      					delete cl.hdUrl;
      					obj.playlist.push(hdClip);
      				}
      								
				obj.clip=reclip;
	      		}
				
      			// désactiver le vote si pas de callaback ni d'url
      			if (obj.plugins.vote) {
      				if (!obj.plugins.vote.voteCallback && !obj.plugins.vote.voteUrl) {
      					delete obj.plugins.vote
      				}
      			}
      			
      			// désactiver les suggestions si pas de liste
      			if (obj.plugins.suggestions) {
      				if (!obj.plugins.suggestions.videos) {
      					delete obj.plugins.suggestions;
      				} 
      			}
	      		
	      		return obj;
	      	}
	      	
	      	function makeParams(params) {
	      		if (params) {
	      			var p = clone(params);
	      			params.src = DEFAULT_CONFIG.swf;
	      			return params;
	      		} else {
	      			return DEFAULT_CONFIG.swf;
	      		}
	      	}
	
      		// création du player
      		var cfg = makeConfig(config);
      		var params = makeParams(flashparams);
      		
      		flowplayer(container, params, cfg);
	}

	return {
		embed:embed,
		configure:loadConfig,
		configuration:DEFAULT_CONFIG
	};
})();



