/**
 * cifer
 * NEasy text replacement with flash
 * by Sebastian Romano / seba@envero.org 
 *
 */

// plugin
jQuery.fn.cifer = function(options){

	var self = this;
	self.options = options;	
	
	// each cifer
	this.each(function(){
	
		var parent = this;

		// opciones del objeto
		this.options = { src: 'cifer.swf', fit: 'exact' };
		// parámetros a pasarle al flash
		this.params = { linkURL: '#'};
		// seteo las opciones pasadas por el usuario
		for(a in this.options){
			parent.options[a] = (self.options[a])?self.options[a]:parent.options[a];
		}
	
		// contador general
		this.counter = (new Date()).getTime() * 1;
	
		// Agrega parámetros para el flash
		this.addParam = function(name, value){
	
			if(value){
				switch(name){
					case 'wordAlign':
						parent.params[name] = value;
						break;
					case 'wordSpacing':
					case 'charSpacing':
						if(value.indexOf('px') != -1){
							parent.params[name] = value.substring( 0, value.indexOf('px') );
						}
						break;
					case 'fontSize':
					case 'lineHeight':
						if(value.indexOf('px') != -1){
							parent.params[name] = value.substring( 0, value.indexOf('px') );
						}
						break;
					case 'fontColor':
						if(value.indexOf('rgb') != -1){ // ff safari
							// convierto RGB en haxadecimal
							tempString = value.substring(value.indexOf('(') +1 , value.length );
							tempString = tempString.substring(0, tempString.indexOf(')') );
							tempString = tempString.split(',');
							parent.params[name] = parent.rgb2hex(tempString[0]*1, tempString[1]*1, tempString[2]*1);
						}else{ // hexadecimal
							parent.params[name] = value.substring(value.indexOf('#')+1).toString();
						}
						break;
					case 'texto':
						parent.params[name] = value;
						break;
					default:
						parent.params[name] = value;
				}
			}
				
		}
	
		this.rgb2hex = function(red, green, blue){
		    var decColor = red + 256 * green + 65536 * blue;
	    	return decColor.toString(16);
		}	
		
		// 
		var oldContent = $(this).html();
		
		// flag de error
		var error = false;
		
		var textito = '%20';
		
		// texto
		if( $('a', $(this)).length > 0 ){
			
			if( $('a:first', $(this) ).html() == '' || $('a:first', $(this) ).html() == undefined){
				error = true;
				parent.addParam('texto', ' ');
			}else{
				textito = $('a:first', $(this) ).text();
				//parent.addParam('texto', $('a:first', $(this) ).text() );
				//parent.addParam('texto', textito );
				// si tiene link
				parent.addParam('linkURL', $('a:first', $(this) ).attr('href') );
				// target del link
				parent.addParam('linkTarget', $('a:first', $(this) ).attr('target') );
			}
			
		}else{
		
			if( $(this).html() == '' || $(this).html() == undefined){
				error = true;
				parent.addParam('texto', ' ');
			}else{           
				textito = $(this).text();
		    	//parent.addParam('texto', $(this).text() );
		    	//parent.addParam('texto', textito);
		    }
		}
		
		textito = textito.replace('&', '%26');
		//textito = textito.replace('+', '&#43;');
		textito = textito.replace('\'', '’');
		
		//alert(Base64.encode(textito));

		// tamaño de la fuente
		parent.addParam('fontSize', $(this).css('font-size') );
		// alto de línea
		parent.addParam('lineHeight', $(this).css('line-height') );
		// Color del texto
		parent.addParam('fontColor', $(this).css('color') );
		// Color del link
		parent.addParam('fontHoverColor', $(this).css('color') );	
	
		// Espacio entre letras
		parent.addParam('wordSpacing', $(this).css('word-spacing') );		
		// Espacio entre letras
		parent.addParam('charSpacing', $(this).css('letter-spacing') );
		// Alineamiento del texto
		parent.addParam('wordAlign', $(this).css('text-align') );
		// si tiene upper, convierto el texto a mayúsculas
		if( $(this).css('text-transform') == 'uppercase' ){
			//parent.params['texto'] = parent.params['texto'].toUpperCase();
			textito = textito.toUpperCase();
		}

		// medidas exactas (incluye padding)
		switch(parent.options['fit']){
			case 'inner':
				flashWidth = $(this).innerWidth();
				flashHeight = $(this).innerHeight();			
				break;
			default:
				flashWidth = $(this).width();
				flashHeight = $(this).height();
		}

		parent.addParam('wrapperWidth', flashWidth.toString() );
		parent.addParam('wrapperHeight', flashHeight.toString() );
		
		// debugger
		if(window.location.href.indexOf('debugcifer') != -1){
			var output = '';
			for (a in parent.params){
				output = output + a + ": " + parent.params[a] + "\n";
			}
			alert(output);
		}
		
		if(!error){
				
			$(this).html('<div id="Cifer' + parent.counter + '" style="position: relative;"></div><span style="width:0;height;0;position:absolute;top:0; overflow: hidden;">' + oldContent + '</span>');
			var properties = { wmode: 'transparent'};
			swfobject.embedSWF( self.options['src'] + '?texto='+textito, "Cifer" + parent.counter, flashWidth, flashHeight, "8.0.0", "expressInstall.swf",  parent.params, properties, null);
			parent.counter++;
		
		}
		
	});
	

}