/*
 * Ajuntament de Barcelona - Javascript Image Replacement (JIR)
 * www.bcn.cat / www.bcn.es
 *
 * Revision: jquery-jir.js v2 | nº1, 2008-12-17 17:00h
*/


$(document).ready( function() {
	if ($.fn.jir) {
		$.fn.jir($.jirOptions);
	}
});

(function($) {
		  
	$.extend($.fn, {
		jir : function(opts) {
			var config = {
				lang		: $("html").attr("lang") || $("html").attr("xml:lang") || 'ca',
				selector	: '.jir'
			};
			var options = $.extend({
				pathRoot	: '',
				pathImg		: '',
				pathMov		: '',
				typeImg		: 'png',
				langMov		: true
			}, opts || {});
			
			if (options.pathRoot != '' && !options.pathRoot.match(/(\/)$/))
				options.pathRoot += '/';
			if (options.pathImg != '' && !options.pathImg.match(/(\/)$/))
				options.pathImg += '/';
			if (options.pathMov != '' && !options.pathMov.match(/(\/)$/))
				options.pathMov += '/';
			
			$.fn.type = function(e) {
				return e.split('.')[1];
			}
			
			$.fn.fileName = function(e) {
				var x		= false;
				var type	= options.typeImg;
				var types	= new Array('gif', 'jpg', 'png', 'swf');
		
				for (var i = 0; i < types.length && !x; i++) {
					if (types[i] == e[e.length-1]) {
						type = e.pop();
						x = true;
					}
				}
				e = e.join('-') + '.' + type;
				return e;
			}
			
			$.fn.mov = function(p, s, w, h , i) {
				var imgAlt	= i.replace(/(\.swf)/gi, '.'+options.typeImg);
				
				if (w && h) {
					$('.'+s).flash ({
						src: p,
						width: w,
						height: h }, { version: 6 }, '', '', imgAlt
					);
				}else{
					$('.'+s).flash ({
						src: p }, { version: 6 }, '', '', imgAlt
					);
				}
			}
			
			return $(config.selector).each( function() {
				var eClass	= $(this).attr('class').split(/\s+/);
				var eText	= $(this).text();
				var eFile	= $(this).fileName(eClass[1].split('-'));
				var eSize	= (eClass[2])?eClass[2].split('x'):'';

				var eImg	= '<img src="'+options.pathRoot+options.pathImg+config.lang+'/'+eFile+'" alt="'+eText+'" />';
				var eMov	= options.pathRoot + options.pathMov;
					eMov	+= (options.langMov)?eFile+'?idioma='+config.lang:config.lang+'/'+eFile;
				
				if ($(this).type(eFile)!='swf') $(this).html(eImg);
				else $(this).mov(eMov, eClass[1], eSize[0], eSize[1], eImg);
			});
		}
	});
	
})(jQuery);


/*
 * Flash (http://jquery.lukelutman.com/plugins/flash)
 * A jQuery plugin for embedding Flash movies.
 * 
 * Version 1.0
 * November 9th, 2006
 *
 * Copyright (c) 2006 Luke Lutman (http://www.lukelutman.com)
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-license.php
*/ 
 
(function(){
	
	var $$;
	$$ = jQuery.fn.flash = function(htmlOptions, pluginOptions, replace, update, imgAlt) {
		// Set the default block.
		var block = replace || $$.replace;
		
		// Merge the default and passed plugin options.
		pluginOptions = $$.copy($$.pluginOptions, pluginOptions);
		
		// Detect Flash.
		if(!$$.hasFlash(pluginOptions.version)) {
			// Use Express Install (if specified and Flash plugin 6,0,65 or higher is installed).
			if(pluginOptions.expressInstall && $$.hasFlash(6,0,65)) {
				// Add the necessary flashvars (merged later).
				var expressInstallOptions = {
					flashvars: {  	
						MMredirectURL: location,
						MMplayerType: 'PlugIn',
						MMdoctitle: jQuery('title').text() 
					}					
				};
			// Ask the user to update (if specified).
			} else if (pluginOptions.update) {
				// Change the block to insert the update message instead of the flash movie.
				htmlOptions.img = imgAlt;
				block = update || $$.imgAlt;
			// Fail
			} else {
				// The required version of flash isn't installed.
				// Express Install is turned off, or flash 6,0,65 isn't installed.
				// Update is turned off.
				// Return without doing anything.
				return this;
			}
		}
		
		// Merge the default, express install and passed html options.
		htmlOptions = $$.copy($$.htmlOptions, expressInstallOptions, htmlOptions);
		
		// Invoke $block (with a copy of the merged html options) for each element.
		return this.each(function(){
			block.call(this, $$.copy(htmlOptions));
		});
	};
	
	/**
	 *
	 * @name flash.copy
	 * @desc Copy an arbitrary number of objects into a new object.
	 * @type Object
	 * 
	 * @example $$.copy({ foo: 1 }, { bar: 2 });
	 * @result { foo: 1, bar: 2 };
	 *
	**/
	$$.copy = function() {
		var options = {}, flashvars = {};
		for(var i = 0; i < arguments.length; i++) {
			var arg = arguments[i];
			if(arg == undefined) continue;
			jQuery.extend(options, arg);
			// don't clobber one flash vars object with another
			// merge them instead
			if(arg.flashvars == undefined) continue;
			jQuery.extend(flashvars, arg.flashvars);
		}
		options.flashvars = flashvars;
		return options;
	};
	
	/*
	 * @name flash.hasFlash
	 * @desc Check if a specific version of the Flash plugin is installed
	 * @type Boolean
	 *
	**/
	$$.hasFlash = function() {
		// look for a flag in the query string to bypass flash detection
		if(/hasFlash\=true/.test(location)) return true;
		if(/hasFlash\=false/.test(location)) return false;
		var pv = $$.hasFlash.playerVersion().match(/\d+/g);
		var rv = String([arguments[0], arguments[1], arguments[2]]).match(/\d+/g) || String($$.pluginOptions.version).match(/\d+/g);
		for(var i = 0; i < 3; i++) {
			pv[i] = parseInt(pv[i] || 0);
			rv[i] = parseInt(rv[i] || 0);
			// player is less than required
			if(pv[i] < rv[i]) return false;
			// player is greater than required
			if(pv[i] > rv[i]) return true;
		}
		// major version, minor version and revision match exactly
		return true;
	};
	
	/**
	 *
	 * @name flash.hasFlash.playerVersion
	 * @desc Get the version of the installed Flash plugin.
	 * @type String
	 *
	**/
	$$.hasFlash.playerVersion = function() {
		// ie
		try {
			try {
				// avoid fp6 minor version lookup issues
				// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
				var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
				try { axo.AllowScriptAccess = 'always';	} 
				catch(e) { return '6,0,0'; }				
			} catch(e) {}
			return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
		// other browsers
		} catch(e) {
			try {
				if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
					return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
				}
			} catch(e) {}		
		}
		return '0,0,0';
	};
	
	/**
	 *
	 * @name flash.htmlOptions
	 * @desc The default set of options for the object or embed tag.
	 *
	**/
	$$.htmlOptions = {
		width: 320,
		height: 240,
		type: 'application/x-shockwave-flash',
		pluginspage: 'http://www.adobe.com/go/getflashplayer',
		src: '#',
		img: '',
		flashvars: {}
	};
	
	/**
	 *
	 * @name flash.pluginOptions
	 * @desc The default set of options for checking/updating the flash Plugin.
	 *
	**/
	$$.pluginOptions = {
		expressInstall: false,
		update: true,
		version: '6.0.65'
	};
	
	/**
	 *
	 * @name flash.replace
	 * @desc The default method for replacing an element with a Flash movie.
	 *
	**/
	$$.replace = function(htmlOptions) {
		//this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
		jQuery(this)
			.addClass('flash-replaced')
			.html($$.transform(htmlOptions));
	};
	
	/**
	 *
	 * @name flash.imgAlt
	 * @desc The default method for replacing an element with an update message.
	 *
	**/
	$$.imgAlt = function(htmlOptions) {
		jQuery(this)
			.addClass('flash-update')
			.html(htmlOptions.img);
	};
	
	/**
	 *
	 * @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')
				s += key+'="'+this[key]+'" ';
		return s;		
	};
	
	/**
	 *
	 * @desc Convert a hash of flashvars to a url-encoded string, using Function.apply(). 
	 * @example toFlashvarsString.apply(flashvarsObject)
	 * @result foo=bar&foo=bar
	 *
	**/
	function toFlashvarsString() {
		var s = '';
		for(var key in this)
			if(typeof this[key] != 'function')
				s += key+'='+escape(this[key])+'&';
		return s.replace(/&$/, '');		
	};
	
	/**
	 *
	 * @name flash.transform
	 * @desc Transform a set of html options into an embed tag.
	 * @type String 
	 *
	 * @example $$.transform(htmlOptions)
	 * @result <embed src="foo.swf" ... />
	 *
	 * Note: The embed tag is NOT standards-compliant, but it 
	 * works in all current browsers. flash.transform can be
	 * overwritten with a custom function to generate more 
	 * standards-compliant markup.
	 *
	**/
	$$.transform = function(htmlOptions) {
		var st = '';
		var aOptions = new Array();
		
		htmlOptions.toString = toAttributeString;
		if(htmlOptions.flashvars) htmlOptions.flashvars.toString = toFlashvarsString;
		
		aOptions = String(htmlOptions).split(' ');
		aOptions[4] = aOptions[4].replace('src=','data=');
		aOptions[6] = aOptions[4].replace('data=','value=');
		
		st += '<object '+aOptions[2]+' '+aOptions[4]+' '+aOptions[0]+' '+aOptions[1]+'>';
		st += '<param name="movie" '+aOptions[6]+' />';
		st += '<param name="quality" value="high" />';
		st += '<param name="autoplay" value="false" />';
		st += '<param name="allowScriptAccess" value="always">';
		st += '</object>';
	
		return(st);
	};
	
	/**
	 *
	 * Flash Player 9 Fix (http://blog.deconcept.com/2006/07/28/swfobject-143-released/)
	 *
	**/
	if (window.attachEvent) {
		window.attachEvent("onbeforeunload", function(){
			__flash_unloadHandler = function() {};
			__flash_savedUnloadHandler = function() {};
		});
	}
})();

