var shnSifrCount = 5;

function setSifrHeight (id,size){
	$('#'+id).height(size);
	//alert('zawijam');
	
};

jQuery.sifrSettings = function(settings) {
	arguments.callee.settings = jQuery.extend({		  
			absoluteOffsetX: null, aoX: 0,
			absoluteOffsetY: null, aoY: 0,
			path: null,
			font: null,
			color: null,
			underline: null,
			link: null,
			hover: null,
			backgroundColor: null,
			content: null,
			width: null,
			height: null,
			isresizable:1,
			size: null
		},arguments.callee.settings,settings);
	return arguments.callee.settings;
};

jQuery.fn.sifr = function(settings) {
	var hex = function(N) {
		if(N==null) return "00";
		N = parseInt(N);
		if(N==0 || isNaN(N)) return "00";
		N = Math.max(0, N);
		N = Math.min(N, 255);
		N = Math.round(N);
		return "0123456789ABCDEF".charAt((N - N%16) / 16) + "0123456789ABCDEF".charAt(N%16);
	};
	var hexed = function(color) {
		if(!color) { return false; }
		if(color.search('rgb') > -1) {
			color = color.substr(4,color.length-5).split(', ');
			color = hex(color[0]) + hex(color[1]) + hex(color[2]);
		}
		color = color.replace('#','');
		if(color.length < 6) {
			color = color.substr(0, 1) + color.substr(0, 1) + color.substr(1, 1) + color.substr(1, 1) + color.substr(2, 1) + color.substr(2, 1);
		}
		return color;
	};
	
	return this.each(function() {

		var o = jQuery(this);
		if((o.attr('class')) && o.attr('class').search('sifr') > -1) {
			o.unsifr();
		}
		o.flash(
			{},
			{},
			function(htmlOptions) {
				var s = jQuery.extend({},jQuery.sifrSettings(), settings);

				o.addClass('sifr');
				s.font = s.font || o.css('fontFamily').split(',')[0].replace(/\"|\'/gm , '');
				s.color = s.color; 
				s.color = hexed(s.color);
				s.underline = s.underline ||(o.css('textDecoration')=='underline');
				s.backgroundColor = s.backgroundColor || o.css('backgroundColor'); 
				s.background = hexed(s.background);
				s.textAlign = s.textAlign || 'left';
				s.verticalAlign = s.verticalAlign || 'top';
				o.html('<span style="display:inline;margin:0;padding:0;float:none;width:auto;height:auto;">' + o.html() + '</span>');
				var oc = jQuery(this.firstChild);
				//s.size = s.size || oc.css('fontSize', s.size + 'px');
				s.content = s.content || oc.html();
				s.width = s.width || oc.width();
				s.height = s.height || oc.height();
				/* == Hide == */
				oc.hide();


				htmlOptions.style = 'vertical-align:' + s.verticalAlign + ';';
				htmlOptions.wmode = 'transparent';
				htmlOptions.src = s.path + s.font + '.swf';
				htmlOptions.flashvars.txt = s.content;
				htmlOptions.width = s.width;
				htmlOptions.height = s.height;
				shnSifrCount++;
				//alert(shnSifrCount);
				htmlOptions.id = 'flashUniqId_'+shnSifrCount; 
				
				htmlOptions.flashvars.flashid = 'flashUniqId_'+shnSifrCount; 
				htmlOptions.flashvars.w = s.width;
				htmlOptions.flashvars.h = s.height;
				htmlOptions.flashvars.isresizable = s.isresizable;
				htmlOptions.flashvars.textalign = s.textAlign;
				if(s.color) { htmlOptions.flashvars.textcolor = s.color; }
				if(s.link) { htmlOptions.flashvars.linkcolor = s.link; }
				if(s.hover) { htmlOptions.flashvars.hovercolor = s.hover; }
				if(s.underline) { htmlOptions.flashvars.underline = s.underline; }
				if(s.backgroundColor) { htmlOptions.flashvars.bgcolor = s.backgroundColor; }
				htmlOptions.flashvars.textsize = s.size;

				o.append(jQuery.fn.flash.transform(htmlOptions));

			}
		);
	});
};