(function($) {
        
        //gestion d'un background swf ou image en cas d'erreur flash
        //centrage automatique du background
         
	$.extend($.philae, {
            
		swfBackground: function(image, swf) {
                     
                    var backgroundWrapper = $('.background-wrapper');
                    var inner = backgroundWrapper.find('.background-wrapper-inner');
                   
                    var initSwfBackground = function () {
                        if (swf.url == null || swf.url == '') {
                            swfBackgroundFail();
                            return;
                        }

                        var resizenow = function () {
                            backgroundWrapper.css({
                                'width' : $(window).width(),
                                //'height' : $(window).height()
                                'height' : $(document).height()
                            });
                            var left =  ($(window).width() - swf.width) / 2;
                            inner.css({
                                'position' :'absolute',
                                'left' : left,
                                'width' : swf.width,
                                'height' : swf.height
                             });
                         }
                      
                          //Adjust swf wrapper when browser is resized
                          $(window).resize(function(){
                            resizenow();
                          });
                          resizenow();
                          //return;
                          
                          var self = backgroundWrapper.find('.background-wrapper-inner').flashembed({
                              src: swf.url, 
                              w3c: true, 
                              wmode: 'transparent',
                              align : 'center',
                              salign : 'top',
                              onFail : swfBackgroundFail
                          });
                      
                          //IF FAILED, FINALY
                          var flashembed = self.data("flashembed");
                          if (flashembed == undefined) {
                              $.log('flashembed == undefined-->failed');
                              swfBackgroundFail();
                              return;
                          }

                          var flash = flashembed.getApi();
                          if (flash == null) {
                              $.log('flash == null-->failed');
                              swfBackgroundFail();
                              return;
                          }
                      
                };
		  
		var swfBackgroundFail = function() {
                         inner.empty();
                         inner.css({
                            'width' : image.width,
			    'height' : image.height,
                            'background-image' : 'url('+image.url+')'
                         });
                         var left =  ($(window).width() - image.width) / 2;
                         var resizenow = function () {
                             $.log('resizenow image');
                                var left =  ($(window).width() - image.width) / 2;
                            $.log('left = ' + left);             
                            //alert(left);
                                inner.css({
                                    'position' :'absolute',
                                    'left' : left,
                                    'width' : image.width,
                                    'height' : image.height
                                  });
			  }
         		 $(window).resize(function(){
                            resizenow();
			 });
			 resizenow();
		  };
		  //try to init swf background the image if it fails.
		  initSwfBackground(); 
              }
	});
	
                 
})(jQuery);
