/*
 * flash 1.2 - Plugin for jQuery
 * 
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Depends:
 *   jquery.js
 *
 *  Copyright (c) 2008 Oleg Slobodskoi (ajaxsoft.de)
 */

;(function($){

$.fn.flash = function( swf, options ) {
    // don't create instance if wrong flash version installed
    if ( options && options.version && !detectVersion( options.version ) ) return this;
    
    return this.each(function() {
        !$.data(this, 'flash') && $.data( this, 'flash', new flash(this, swf, options)).init();
    });
};

$.fn.flash.defaults = {
    params: {
        allowfullscreen: true,
        allowscriptaccess: 'always',
        quality: 'best',
        wmode: 'transparent',
        bgcolor: null,
        flashvars: null,
        menu: false
    },
    attr: {
        width: '521px',
        height: '247px',
        type: 'application/x-shockwave-flash'
    }    
};

function flash( elem, swf, options ) {

    var s = $.extend(true, {}, $.fn.flash.defaults, options);

    this.init = function ()
    {
        //serialize flashvars if object is given
        if ( s.params.flashvars && typeof s.params.flashvars == 'object' )
            s.params.flashvars = $.param(s.params.flashvars);
        

        var flash = '';
        if ( $.browser.msie ) {
            s.params.movie = swf;
            //create param elements
            $.each(s.params, function(name,val){
                if ( val ) flash+='<param name="'+name+'" value="'+val+'"/>';
            });
            flash = $('<object>'+flash+'</object>').attr(s.attr);
        } else {
            flash = $('<embed/>').attr($.extend({src: swf},s.params, s.attr) );
        };
        
        $(elem).html(flash);
    };


};

function detectVersion ( v )
{
    var descr, pv, maxVersion = 10;
    
    //thats NS, Mozilla, Firefox        
    if (typeof navigator.plugins['Shockwave Flash'] == 'object') {
        descr = navigator.plugins['Shockwave Flash'].description;
        descr = descr.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
        pv = [
            descr.replace(/^(.*)\..*$/, "$1"),
            descr.replace(/^.*\.(.*)\s.*$/, "$1"),
            /r/.test(descr) ? descr.replace(/^.*r(.*)$/, "$1") : 0
        ];
    }
        
    //thats IE
    else if ( typeof ActiveXObject == 'function')
    {
        var ao;
        for(var i = maxVersion; i >= 2; i--)
        {
            try {
                ao = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + i);
                if ( typeof ao == 'object' ) {
                    descr = ao.GetVariable('$version'); 
                    break;
                };
           } catch(e){};
        };
        
        pv = descr.split(' ')[1].split(',');
    };
   

    
    if ( !pv && v ) return false;
                
    function toInt (arr) {    
       return $.map(arr, function(n, i){
          return parseInt(n,10);
        });
    };

    v = toInt( v.split('.') );
    pv = toInt( pv );

    return (pv[0] > v[0] || (pv[0] == v[0] && pv[1] > v[1]) || (pv[0] == v[0] && pv[1] == v[1] && pv[2] >= v[2])) ? true : false;
    
            
};


})(jQuery);        

