(function($) {
    /* media options */
    
    var options = {
	    key:null,
	    playerFile:null	
	}
    
	$.fn.mediaPlayer = function(opts) {
		this.each(function() {
			
			if(opts) {
                $.extend(options, opts);
            }
       
			if(flowplayer && options.playerFile != null)
			{
			    
			    var currentPlaylist = [];
			    var imageElm = $(this).next("img");
			  
			    var mediaFile = $.trim($(this).attr("href"));
                var imageFile = imageElm.attr("src");
			    var reg = new RegExp("\\.mp3$");
			    var isAudio = reg.test(mediaFile);
			    var autoBuffOn = true;
			    
                if(imageFile != null && imageFile.length != 0)
                {
                    currentPlaylist.push({url:imageFile,scaling: 'orig',autoPlay: true});
                    autoBuffOn = false;
                }
                
                if(mediaFile != null && mediaFile.length != 0)
                {
                    currentPlaylist.push({url:mediaFile});
                }
                
                /* clear container */
			    $(this).empty();
			    imageElm.remove();
			    
			    if(isAudio && currentPlaylist.length != 2)
			    {
			        $(this).parents("div.mediaplayer").height(30);
			    }
			     
			    if (options.key != null && options.key.length == 0)
			    {
			        options.key = null;
			    }
			    
			    /* Load flowplayer into container */
                flowplayer(this, options.playerFile, {
                    key:options.key,
                    
                    plugins: {
                        controls: {
                            // control display 
                            autoHide: (isAudio)?'never':'always',
                            borderRadius: '0',
                            backgroundColor: '#212121',
                            backgroundGradient: 'low',
                            height: 30,
                            opacity:0.7,
                            
                            sliderColor: '#000000',
                            sliderGradient: 'none',
                            
                            bufferColor: '#000000',
                            bufferGradient: 'none',
                            
                            progressColor: '#ffffff',
                            progressGradient: 'medium',
                            
                            buttonColor: '#a6a6a6',
                            buttonOverColor: '#919191',
                            timeColor: '#000000',
                            durationColor: '#525252',
                            
                            // control visibility
                            play:true,      
                            volume:true, 
                            mute:true,  
                            time:true,  
                            stop:false, 
                            playlist:false,  
                            fullscreen:false,
                            scrubber: true
                       }
                    },
                    
                    clip:{
                        autoPlay:false,
                        autoBuffering:autoBuffOn, // shows the first frame of video when it loads
                        scaling: 'fit'
                        },
                    playlist: currentPlaylist 
                });
            }
		});
		return this;
	};
})(jQuery);
