/**
 * @author Jakub Argasinski
 */

String.prototype.toBoolean = function () {
	return !!parseInt(this);
};
String.prototype.toFloat = function () {
	return parseFloat(this);
};

String.prototype.toInt = function () {
	return parseInt(this);
};

String.prototype.percentToFloat = function () {
	return (this.charAt(this.length-1) === '%' ? this.toFloat()/100.0 : this.toFloat());
};



$(function(){
	// search.tpl: Klikniecie w input w wyszukiwarce usuwa napis zachety
	$('form.vela-module-search input[name="params"]').one('focus', function() {
		$(this).val('');
	});
	
	// search.tpl: Obsluga wyszukiwania w Google 
	$('form.vela-module-search').submit(function() {
		var elements = $(this).find('input,select');
		var params = elements.filter('[name="params"]');
		var phrase = params.val(), defaultPhrase = params.attr('title');
		if (phrase == '' || phrase == defaultPhrase) {
			$(this).find('p.error > span').fadeIn('slow').animate({opacity: 1.0}, 2000).fadeOut('slow');
			return false;
		} 
		if (elements.filter('[name="where"]').val() == 'google') {
			document.location.href = 'http://google.com/search?q='+encodeURI(phrase)+' site:'+elements.filter('[name="site"]').val();
			return false;
		}
		return true;
	});

	// clock.tpl: renderowanie zegarka cyfrowego
	$('.vela-module-clock').each(function() {
		$(this).find('.vela-module-clock-content').jclock();
	});

	// menu_rollover.tpl: menu
	$('ul.vela-rollover-menu a').each(function() {
		var a = $(this);
		// Pomijamy elementy, które mają normalne linki i nie są rozszerzalne, czyli
		// najzwyklejsze na świecie linki 
		if (a.hasClass('expand') === false && a.attr('href') !== '#') return true;
		var li = a.parent();
		// Jeżeli jest rozszerzalny no to rozszerzamy bądź zwijamy
		if (a.hasClass('expand') === true) {
			a.toggle(
				function() {
					$(this).siblings('ul').slideToggle().parent().siblings().children('ul:visible').slideUp();
					return (a.attr('href') === '#') ? false : true;
				},
				function() {
					$(this).siblings('ul').slideToggle().parent().siblings().children('ul:visible').slideUp();
					return (a.attr('href') === '#') ? false : true;
				}
			);
		}
	});
	$('ul.vela-rollover-menu a.selected').each(function() {
		var a = $(this);
		a.parents('ul').show();
	});
	// banners2.tpl: boxobannery
	$('.velacms-banner').each(function() {
		var b = $(this), B = window["eval"]("(" + b.attr('title') + ")");
		var Positions = ['center','fl','fr'];
		B.normalizeWidth = function(container, element) {
			element = element > 1.0 && element > container ? 1.0 : element;   
			element = element <= 1.0 ? container*element : element;
			return element.toString().toInt();
		};
		// Od tej chwili, title jest już niepotrzebne.
		b.removeAttr('title');
		// Pobieramy rozmiar rodzica
		B.containerWidth = b.parent().width();
		// Przekształcamy zmienne na bardziej zjadliwe
		B.active = B.active.toBoolean();
		B.hide = B.hide.toBoolean();
		B.newwindow = B.newwindow.toBoolean();
		B.ext = B.ext.substr(1);
		B.url = (B.url === "" ? "#" : B.url);

		// W sytuacji, gdy B.containerWidth jest = 0, to najprawdopodniej rodzic ma display: none, albo coś podobnego,
		// więc nie ma sensu renderować bannera. Podobnie jeśli B.hide lub B.active (wybrano aby ukryć banner) jest true.
		if (B.active === false || B.hide === true || B.containerWidth === 0) return;
		
		// Po tej magicznej operacji boxwidth i bannerwidth będą zawierać rozmiary bannera i boksa w pikselach.
		B.boxwidth = B.normalizeWidth(B.containerWidth, B.boxwidth.percentToFloat());
		B.bannerwidth = B.normalizeWidth(B.boxwidth, B.bannerwidth.percentToFloat());
		B.bannerheight = B.bannerheight.toInt();
		
		B.boxpos = Positions[B.boxpos.toInt()];
		B.fontsize = B.fontsize.toInt();
		// Tych informacji de facto nie potrzebujemy 
		delete B.hits;
		delete B.shows;
		delete B.timelimit;
		// Tymczasowo:
		delete B.color;
		delete B.rel;
		
		var image, anchor, banner, text;
		
		// Ustawiamy szerokość boxa wedle informacji, które posiadamy, o ile ów rozmiar jest
		// mniejszy od rozmiaru bieżącego, obliczanego automatycznie z faktu, że div reprezentujący ten box
		// jest elementem blokowym, więc "rozciąga się" do kontenera
		if (B.boxwidth < b.width()) {
			b.width(B.boxwidth);
		}
		
		// Tworzymy obrazek, o ile nie jest to animacja Flash.
		if (B.ext !== 'swf') {
			image = $('<img src="'+B.image+'" alt="'+B.name+'" width="'+B.bannerwidth+'" height="'+B.bannerheight+'">');
		}
		
		// 1) kliknięcie nie powoduje niczego
		// 2) zmieniamy wygląd kursora na tym elemencie, 
		function preventUserClick(element) {
			element.click(function(){ return false; });
			element.css('cursor', 'default');
		}
		
		// Tworzymy anchor i doczepiamy do niego obrazek.
		anchor = $('<a class="bl" href="'+B.url+'" title="'+B.name+'"></a>');
		anchor.attr('rel', 'nofollow');
		anchor.width(B.bannerwidth);
		anchor.height(B.bannerheight);
		anchor.focus(function() { $(this).blur(); });
		switch (B.bannerpos) {
			default:
				anchor.addClass('center');
			
		}
		// Jeżeli był przekazany pusty URL, to:
		if (B.url === '#') {
			preventUserClick(anchor);
		}

		if (B.ext !== 'swf') {
			image.appendTo(anchor);
		} else {
			anchor.flashembed(B.image);
		}
		
		// Jeżeli jest tekst, tworzymy tekst.
		if (B.text !== null) {
			text = $('<a href="'+B.url+'">'+B.text+'</a>');
			if (B.url === '#') {
				text.click(function(){ return false; });
			}
		}
		

		
		banner = anchor;
		
		
		// Wyświetlamy.
		b.append(banner);
		if (B.text !== null) {
			b.append(text);
		}
		
	});
	
});
