﻿/**
 * GifSwitcher
 * Un plugin jQuery qui permet de remplacer des images par
 * du texte de référencement naturel. On utilise le même
 * principe de fonctionnement que pour le plugin jquery.flash.js
 * 
 * Version 1.0
 * December 21st, 2007
 *
 * 
 * Inspired by:
 * jQuery.flash (je me souviens plus du lien dsl)
 * SWFObject (http://blog.deconcept.com/swfobject/)
 * UFO (http://www.bobbyvandersluis.com/ufo/)
 * sIFR (http://www.mikeindustries.com/sifr/)
 *
 **/ 
 
;(function(){
	
var $$;

$$ = jQuery.fn.gifSwitcher = function(gifOptions, linkOptions, replaceFunction) {
    // Récupération des paramêtres utilisateurs
    gifOptions = $$.copy($$.gifOptions, gifOptions);
    linkOptions = $$.copy($$.linkOptions, linkOptions);
        
    // Permet de redéfinir la function de remplacement
    var block = replaceFunction || $$.replace;
    
    // Appel de la function
    block.call(this, linkOptions, gifOptions);
};

/**
 *
 * @desc Merge array or object when possible
 * @example $$.copy($$.gifOptions, gifOptions);
 * @result An object (or an array)
 *
**/
$$.copy = function() {
	var options = {}
	for(var i = 0; i < arguments.length; i++) {
		var arg = arguments[i];
		if(arg == undefined) continue;
		jQuery.extend(options, arg);
	}
	return options;
};

/**
 *
 * @desc Convert a hash of html options to a string of attributes, using Function.apply(). 
 * @example toAttributeString.apply(htmlOptions)
 * @result foo="bar" foo="bar"
 *
**/
function toAttributeString() {
	var s = '';
	for(var key in this)
	    
		if(typeof this[key] != 'function' && this[key] != '')
			s += key+'="'+this[key]+'" ';
			
	return s;		
};

/**
 *
 * @name gifSwitcher.replace
 * @desc The default method for replacing an element with a Flash movie.
 *
**/
$$.replace = function(linkOptions, gifOptions){
    linkOptions.toString = toAttributeString;
    gifOptions.toString = toAttributeString;
    
    this.empty().append('<a '+ String(linkOptions) +' ><img '+ String(gifOptions) +' /></a>');
    return this;
};

/**
 *
 * @name flash.gifOptions
 * @desc The default set of options for the gif.
 *
**/
$$.gifOptions = {
    width: 100,
    height: 100,
    src: '#',
    id: '',
    style: ''
};

/**
 *
 * @name flash.linkOptions
 * @desc The default set of options for the anchor
 *
**/
$$.linkOptions = {
    href: '',
    target: 'blank',
    id: "",
    style: ""
};

})();