// JavaScript Document

(function($){ 


$.fn.milieu = function(options) {
		
		
		/********** options par defaut ***************************************/
				options = $.extend(	{
					parent : window
		 			}
			 	, options);
		/**********************************************************************/
		
		var parent = options.parent;

   		return this.each(function(){
					
				var self = $(this);
				
				// recupere dimensions de l'objet
				var obj_width = self.outerWidth();
				var obj_height = self.outerHeight();
				
			
				var scroll_width = $(parent).scrollLeft();
				var scroll_height = $(parent).scrollTop();
				
			
				// recupere dimensions de la fenetre
				var window_width = $(parent).width(); 
				var window_height = $(parent).height(); 
				
				var posY=( scroll_height+window_height-window_height/2 )-( obj_height/2 );
				var posX=( scroll_width+window_width-window_width/2 )-( obj_width/2 );
				
				
				var cssProp = {	position: 'absolute'};
				cssProp.top = posY;
				cssProp.left = posX;
				
				if(obj_height<window_height)self.css({'top':posY});
				if(obj_width<window_width)self.css({'left':posX});
				
			   });
			
};
	
	
	
	
	$.video = function() {
			
	// declarations
	var play = $("#play");
	var largeur = 680, hauteur = 480;
	var supportVideo = null; var flashDiv = null;
	
		// creation du support click
		$('body').append("<div id='supportVideo' class='supportVideo'></div>");
		supportVideo = $("#supportVideo");
			

		// creation du loginflash
		$('body').append("<div id='flashDiv'></div>");
		flashDiv = $("#flashDiv");
		flashDiv.css({'position':'absolute', 'display':'none', 'z-index':100, 'width':largeur, 'height':hauteur, 'backgroundColor':'transparent'});
		flashDiv.milieu();


	/*********************/
	function _fermer(){
		flashDiv.empty();
		flashDiv.hide();
		supportVideo.hide();
		
		$("#closeVideo").unbind('mousedown', _fermer);
	}
	
	
	function _lireVideo(v){
		
			// attachement du flash
			var unique = "" + new Date().getTime();
			f="http://www.agencepixels.com/VIDEO_LOGICMAX/"+v;
			
			flashDiv.flash({src:root+'videos/player.swf',  width:largeur, height:hauteur, flashvars:{'skin':root+'videos/plSkin.swf','video':f} });
			flashDiv.prepend("<a title='Fermer' id='closeVideo'><span>Close</span></a>");
			
			flashDiv.show();
			supportVideo.show();
			//
			$("#closeVideo").bind('mousedown', _fermer);
			

	}
	/********************/
	
		function _dimSupport(){
				if(supportVideo!=null){// redimensionne le calque support
					var max_width = $(window).width()+$(window).scrollLeft(); // recupere dimensions de la fenetre
					var max_height = $(window).height()+$(window).scrollTop();
					supportVideo.css({"width" : max_width, "height" : max_height});
				}
					
				if(flashDiv!=null)flashDiv.milieu();
			}


	// comportement
	play.bind('mousedown', function(){ _lireVideo( $(this).children('span').text()+".flv" )});
	supportVideo.bind('mousedown', _fermer);
	$(window).bind('scroll resize', _dimSupport); 
	
	this.lireVideo = _lireVideo;

	};

	
})($);



// lancement
$(window).ready(function(){	$.video();}	);

