/* == jQuery Sifr Plugin (with selectors) == */
jQuery.fn.sifr = function (prefs) {

	/* == lock and load our preferences == */
	var p = jQuery.extend((prefs === false) ? { unsifr: true } : {}, arguments.callee.prefs, prefs);

	/* == if necessary, save our prefs == */
	if (p.save) {
		arguments.callee.prefs = jQuery.extend(p, { save: false });
	}

	/* == we're done if there's no sIFR specified == */
	if (this[0] === document) {
		return;
	}

	/* == if necessary, run a custom function before we begin == */
	if (!p.unsifr && typeof p.before === 'function') {
		p.before.apply(this, [p]);
	}

	/* == do this function on every element we've selected == */
	this.each(function () {

		/* == 't' will mean the jQuery version of 'this' == */
		var t = jQuery(this);

		/* == 'a' will mean the possible '.sIFR-alternate' child of 't' == */
		var a = t.children('.sIFR-alternate');

		/* == if 'a' exists, then it's time to unSifr == */
		if (a) {
			t.html(a.html());

			/* == if unsifr was called, then it's time to go == */
			if (p.unsifr) {
				return;
			}
		}

		if (typeof p.beforeEach === 'function') {
			p.beforeEach.apply(this, [t, p]);
		}

		/* == 's' will mean the newly created child of 't', and it's what will be sIFRed == */
		
		
		if(t.hasClass("sifr-hover")== false) {
		t.addClass("sIFR-trans").css({visibility:"visible",position:"relative"});
		}
		
		
				
		var s = t.html('<span class="flash-replaced sIFR-replaced">' + (p.content || t.html()).replace(/^\s+|\s+$/g, '') + '</span>').children();

    //var s2 = t.append('' + (p.content || t.html()).replace(/^\s+|\s+$/g, '') + '');		
$("div#foot").attr({rel:$("div#foot").attr("rel")+","+s.height()});	
		/* == 'a' will mean another newly created child of 't', and it's where the alternate text goes == */
		
		var f = t.prepend(s.children().clone().text("").addClass("afix").css({width:s.width(),height:t.height()}));
		
		if(t.hasClass("sifr-hover")== false) {
		
		if($(this).children().is("a")){
		
		
		if($(this).parents("div.text-vpravo:first").length || $(this).parents("div.text-vpravo2:first").length) {
      addcss = "right:0;";
      $(this).children("a").css({right:0});

    }else{
      addcss = "left:0;";
      $(this).children("a").css({left:0});

      
    }
    
      s2 = t.append('<span class="sifr-hover" style="position:absolute;   z-index:10; '+addcss+'  ">' + s.text() + '</span>').children('.sIFR-alternate');
    }
		a = t.append('<span class="alt sIFR-alternate">' + s.html() + '</span>').children('.sIFR-alternate');
		}
    		
 //   var s2 = t.prepend('<span class="flash-replaced hover sIFR-replaced">' +s.children().text()+'</span>');        
    
    
   /*$(".afix").html('<img src="http://www.framehouse.sk/uploads/images/framehouse/contacts/mapa.gif" style="width:200px; height:100px;">');*/
    
    if(s.children().is("a")) {
      s.children("a").removeAttr("rel");
    }
    

		/* == if 'a' isn't hidden already, make it secret, make it safe == */
		if (a.css('display') !== 'none') {
			a.css('display', 'none');
		}

		/* == make a function which converts "most" colors into the 6 digit hexidecimal equivelent == */
		var toHex = function (c) {
			var h = function (n) {
				if (n === 0 || isNaN(n)) {
					return '00';
				}
				n = Math.round(Math.min(Math.max(0, n), 255));
				return '0123456789ABCDEF'.charAt((n - n % 16) / 16) + '0123456789ABCDEF'.charAt(n % 16);
			};
			c = (c) ? c.replace(/rgb|\(|\)|#$/g, '') : false;
			if (!c) {
				return false;
			}
			if (c.indexOf(',') > -1) {
				c = c.split(', ');
				return '#' + h(c[0]) + h(c[1]) + h(c[2]);
			}
			if (c.search('#') > -1 && c.length <= 4) {
				c = c.split('');
				return '#' + c[1] + c[1] + c[2] + c[2] + c[3] + c[3];
			}
			return c;
		};

		/* == this is a very special addition for textTransform support == */
		if (p.textTransform) {
			if (p.textTransform.toLowerCase() === 'uppercase') {
				s.html(s.html().toUpperCase());
			}
			if (p.textTransform.toLowerCase() === 'lowercase') {
				s.html(s.html().toLowerCase());
			}
			if (p.textTransform.toLowerCase() === 'capitalize') {
				var c = s.html().replace(/\>/g, '> ').split(' ');
				for (var i = 0; i < c.length; i = i + 1) {
					c[i] = c[i].charAt(0).toUpperCase() + c[i].substring(1);
				}
				s.html(c.join(' ').replace(/\> /g, '>'));
			}
		}
		
		/* == 'f' will mean the flash plugin htmloptions == */		
		var f = {
			flashvars: jQuery.extend({
				h: s.height() * (p.zoom || 1),
				offsetLeft: p.offsetLeft || undefined,
				offsetTop: p.offsetTop || undefined,
				textAlign: p.textAlign || (/(left|center|right)/.exec(t.css('textAlign')) || ['center'])[0],
				textColor: toHex(p.color || t.css('color')) || undefined,
				txt: p.content || s.html(),
				underline: (p.underline || (p.underline !== false && t.css('textDecoration') === 'underline')) ? true : undefined,
				w: (p.width || s.width()) * (p.zoom || 1)
			}, p.flashvars),
			height: p.height || s.height(),
			src: (p.path || '') + ((p.path && p.path.substr(p.path.length - 1) !== '/') ? '/' : '') + (p.font || '') + ((p.font && p.font.indexOf('.swf') === -1) ? '.swf' : ''),
			width: p.width || s.width(),
			wmode: 'transparent'
		};

			

		/* == hand the mic over to luke lutman and his big flash band == */
		t.children().flash(
			jQuery.extend(f, p.embedOptions),
			jQuery.extend({
				expressInstall: p.expressInstall || false,
				version: p.version || 7,
				update: p.update || false
			}, p.pluginOptions),
			function (f) {

				/* == now remember how wide and tall things are == */
				var preHeight = t.height();
				var preWidth = t.width();
				/* == the flash bang == */
			s.html(jQuery.fn.flash.transform(f));    
      		
              			

				/* == 'e' will mean the embedded flash == */
				var e = s.find(':first');				

				e.css({
					verticalAlign: 'text-top',
					display: 'inline',
					width: p.width,
					height: p.height
				});
				
				var marginBottom = preHeight - t.height();
				var width = parseInt(e.css('width'), 10) + parseInt(preWidth - t.width(), 10);

				/* == now enforce these non-changes == */
				if (!p.height) {
					e.css({
						marginBottom: marginBottom
					});
				}
				if (!p.width) {
					e.css({
						width: width
					});
				}

				/* == special and strange middle vertical alignment == */
				if (p.height && p.verticalAlign === 'middle') {
					e.css({
						marginTop: Math.floor((p.height - s.height()) / 2),
						marginBottom: Math.round((p.height - s.height()) / 2),
						height: s.height()
					});
					e.attr('height', s.height());
				}

				/* == special and strange bottom vertical alignment == */
				if (p.height && p.verticalAlign === 'bottom') {
					var a = t.find('.sIFR-alternate');
					e.css({
						marginTop: (p.height - s.height()),
						height: s.height()
					});
					e.attr('height', s.height());
				}

				if (p.css) {
					e.css(p.css);
				}
			}
		);
		
		

		if (typeof p.afterEach === 'function') {
			p.afterEach.apply(this, [t, p]);
		}
	});

	/* == if necessary, run a custom function after we're done == */
	if (!p.unsifr && typeof p.after === 'function') {
		p.after.apply(this, [p]);
	}

};

/* == jQuery Sifr Plugin (without selectors) == */
jQuery.sifr = function (prefs) {
	jQuery().sifr(jQuery.extend({
		save: true
	}, prefs));
};

/* == jQuery Sifr Plugin (as unSifr) == */
jQuery.fn.unsifr = function () {
	return this.each(
		function () {
			jQuery(this).sifr(false);
		}
	);
};



