var motywGraficzny = false;

function inicjujJs() {
	if ($(document.body).getStyle('unicode-bidi') == 'bidi-override') {motywGraficzny = true;}
	document.documentElement.className = "jsOn";
	if (motywGraficzny === true) { 
		zglosBlad();
		popup();
		menuRozwijane();
		animacjaGlowna();
		faqScroll();
		drobnePoprawkiWygladu();
		polecZnajomemu();
	}
	inputyZTekstem();
	tabOnMaxlength();
	formularzInputHover();
	
}

function faqScroll() {
	$$('.faqPytanie').each(function(item){
		item.addEvent('click', function(klik){
			klik.preventDefault();
			var toEl = item.getElement('a').get('href').replace('#', '');
			new Fx.Scroll(window).toElement(toEl);
		});
	});
}

function animacjaGlowna() {
	var idRodzica = 'animacjaGlowna';
	flashvars = '';
	var params = {};
	params.wmode = "transparent";
	params.menu = 'false';

	if (document.getElementById(idRodzica)) {
		swfobject.embedSWF("/g/animacjaGlowna.swf", idRodzica, "712", "500", "9.0.0",false, flashvars, params);
	}
}

function inputyZTekstem() {
	var pola = [];
	var klasaNieaktywnego = 'inputNieaktywny';
	pola['newsletterEmail'] = 'Twój e-mail';
	pola['fraza'] = 'szukaj ...';
	
	for (var el in pola) {
		ustawValue(el,pola[el]);
	}
	// przy błędach logowania pole hasła ma pozostawać puste, jeżeli nie zostało wypełnione przez internautę
	var poleHaslo = document.getElementById('haslo');
	if (poleHaslo 
		&& maKlase(poleHaslo, 'inputerror') 
		&& poleHaslo.value == pola['haslo']) {
			poleHaslo.value = '';
	}
	
	function ustawValue(id,Value) {
		if ($(id)) { 
			Value = ($(id).get('title')) ? $(id).get('title') : Value;
			if ($(id).value == '' || $(id).value == Value) {
				$(id).value = Value;
				$(id).addClass(klasaNieaktywnego);
			}
			$(id).addEvents({
				'focus': function() {if($(id).value == Value) {$(id).removeClass(klasaNieaktywnego); $(id).value = '';}},
				'blur': function() {if($(id).value == '') {$(id).addClass(klasaNieaktywnego); $(id).value = Value;}}
			});
			
			if ($(id).getParent('form')) {
				$(id).getParent('form').addEvent('submit', function(e) {
					e.preventDefault();
					if ($(id).value == Value) {$(id).value = '';}
					$(id).getParent('form').submit();
				});				
			}
		}
	}
}


function menuRozwijane() {
	var selektorMenu = '#menuWrap ul';
	var nieAnimujWewnatrz = '#panelWrap';
	
	$$(selektorMenu).each(function(menu,index) {
		menu.getElements('a').each(function(pozycjaMenu) {
			// ie7 + white-space + display:block dziecko + ... ie7
			var szerokosc = pozycjaMenu.getParent('li').offsetWidth - 18; // uwzglednic padding
			pozycjaMenu = $(pozycjaMenu);
			pozycjaMenu.setStyle('width', szerokosc+'px');
			// / ie7
			
			if(pozycjaMenu.getParent('li').getElement('div')) {
				var animowany = pozycjaMenu.getParent('li').getElement('div');
				animowany = $(animowany);
				if (pozycjaMenu.getParent(nieAnimujWewnatrz)) {
					animowany.setStyle('display', 'none');
					pozycjaMenu.addEvents({
						'click': function(e) {
							animowany.style.display = (animowany.style.display == 'none') ? '' : 'none';
							e.preventDefault();
						}
					});	
				} else {
					animowany.fade('hide');
					pozycjaMenu.getParent('li').addEvents({
						'mouseenter': function() {
							animowany.fade('in');
						},
						'mouseleave': function() {
							animowany.fade('out');
						}
					});				
				}
			}

		});
	});
}

function zglosBlad() {
		$$('.zglosBlad').each(function(item,index) {
			item.addEvent('click', function() {  
			$('zgloszenie').toggleClass('ukryty');
			});
		});	
}
function popup() {
	var link;
	$$('.popup').each(function(item,index) {
		item.addEvent('click', function() {
			link = item.getAttribute('href');
			noweOkno(link, 'okno_pomocy', '773', '650');
            return false;
		});
	});
}

function noweOkno(link,name,x,y) {
	window.open(link, name, 'menubar=0,scrollbars=0,resizable=1,width=850,height=600');
}

function bannerStat(rekid, url) {	
	//zlicza kliknięcia bannerów i ew. przekierowuje
	var myRequest = new Request({
		method: 'get', 
		url: '/bannery/statystyki/',
			onSuccess: function( responseText ) {
					document.location.href = url;
			}
	}).send('id='+rekid); 
	
}


function drobnePoprawkiWygladu() {
	$$('.ulatwienia').setStyle('display', 'none');
	$$('.domyslnieUkryty').setStyle('display', 'none').fade('hide');

	
	$$('.odkryjNastepnyBlok').each(function(item, index) {
		if(item.getNext('.doOdkrycia')) {
			var odkrywany = item.getNext('.doOdkrycia');
		} else if(item.getParent().getNext('.doOdkrycia')) {
			var odkrywany = item.getParent().getNext('.doOdkrycia');
		}
		if (typeof(odkrywany) != 'undefined') {
			odkrywany = $(odkrywany);
			var slideElement = new Fx.Slide(odkrywany);
			
			if((item.getElement('a') && window.location.hash == item.getElement('a').get('href')) || maKlase(odkrywany, 'domyslnieOdkryty')) {
				slideElement.show();
			} else {
				slideElement.hide();
			}
			item.addEvent('click', function(e) {
				slideElement.toggle();
				item.toggleClass('jsOdkryty');
				return false;
			});		
		}
	});
	$$('a.gora').addEvent('click',function(e){
		var myFx = new Fx.Scroll(window).toElement(window);
		e.preventDefault();
	});
	
	window.addEvent('domready', infoDymki);
	
}

function infoDymki() {
	var dymki = new Tips($$('.infoDymek'), {
		className: 'tipDymek'
	});
}


function dodajdoulubionych(){

    if(window.sidebar) //Firefox
    {
        window.sidebar.addPanel(document.location, document.location, "");
    }
    else if(window.external) //IE
    {
        window.external.addFavorite(document.location, document.location);
    }
    else if(window.opera ) //opera
    {
        var link = document.getElementById('dodajDoUlubionych');
        var a = new Element('a', {
            href: document.location,
            title: document.location,
            rel: 'sidebar',
            events: {
                click: function(e){
                    e.preventDefautl();
                }
            }
        });
        $('a').replaces($(link));
    }
}
function polecZnajomemu() {
	var tresc;
	if (document.getElementById('napisz_email')) {
		tresc = $('napisz_email').getParent('.zawartosc');
		//tresc = $(tresc);
		var komunikat = new Komunikat();
		var polecTytulTekst = (polecNasZnajomym != '') ? polecNasZnajomym : 'Poleć nas znajomym';
		komunikat.setTytul(polecTytulTekst);
		
		$$('a[href$=polec_znajomemu]').each(function(item){
			item.addEvent('click', function(e){
				komunikat.setTresc(tresc);
				komunikat.pokaz();
				e.preventDefault();
			});
		});
		
		if (tresc.getElement('.komunikat')) {
			komunikat.setTresc(tresc);
			komunikat.pokaz();
		}
	}
}

Komunikat = function() {
	var zamknijTekst = (window.zamknijTekst != '') ? '<span>'+window.zamknijTekst+'</span>' : '<span>zamknij</span>';
	var wrap = new Element('div', {'id': 'komunikatWrap', 'class': 'komunikatUkryty'});
		var tlo = new Element('div', {'id': 'komunikatFixedTlo'});
		var box = new Element('div', {'class': 'box niebieski', 'id': 'komunikatFixed'});
			var tytul = new Element('h2', {'class': 'tytul'});
			var ukryjKomunikat = new Element('a', {'id': 'ukryjKomunikat', 'html': zamknijTekst, 'href': '#'});
				ukryjKomunikat.addEvent('click', function(e){
					this.ukryj();
					e.preventDefault();
					if($('komunikatPolecZnajomemu')){
						$('komunikatPolecZnajomemu').setStyle('display', 'none');
					}
				}.bind(this));
			var zawartosc = new Element('div', {'class': 'zawartosc'});
	var elZastepczy = new Element('span');
	elZastepczy.setStyle('display', 'none');
	var trescKomunikatu;
	
	tytul.inject(box);
	zawartosc.inject(box);
	tlo.inject(wrap);
	box.inject(wrap);
	wrap.inject($(document.body));
	wrap.fade('hide');
	tlo.tween('opacity', 0.7).setStyles({'height': document.body.offsetHeight});
	
	this.pokaz = function() {
		wrap.removeClass('komunikatUkryty').fade('in');
		this.trescKomunikatu.inject(zawartosc);
		window.addEvent('keypress',function(e) { 
			if(e.key == 'esc') { 
				this.ukryj();
			} 
		}.bind(this));
		return this;
	};
	this.ukryj = function() {
		wrap.fade('out').addClass('komunikatUkryty');
		if (this.trescKomunikatu.retrieve('styleInline')) {
			this.trescKomunikatu.set('style', this.trescKomunikatu.retrieve('styleInline'));
		}
		this.trescKomunikatu.inject(elZastepczy, 'before');
		return this;
	};
	this.usun = function() {wrap.destroy();};
	this.setTytul = function(tytulHtml) {tytul.set('html', tytulHtml); ukryjKomunikat.inject(tytul, 'bottom'); return this;};
	this.setTresc = function(trescHtml) {
			if(typeof(trescHtml) == 'string') {
				zawartosc.set('html', trescHtml);
			} else if (typeof(trescHtml) == 'object') {
				zawartosc.set('html', '').removeClass('zawartosc');
				elZastepczy.inject(trescHtml, 'after');
				if (trescHtml.get('style')) {
					trescHtml.store('styleInline', trescHtml.get('style'));
					trescHtml.removeAttribute('style');
				}
				trescHtml.inject(zawartosc);
			}
			this.trescKomunikatu = trescHtml;
			return this;
	};

}
function maKlase (obj, klasaCss) {
	var re;
	
    if (typeof obj == 'undefined' || obj==null || !RegExp) {
    	return false;
    }
    re = new RegExp("(^|\\s)" + klasaCss + "(\\s|$)");
    if (typeof(obj)=="string") {
      return re.test(obj);
    } else if (typeof(obj)=="object" && obj.className) {
      return re.test(obj.className);
    }
	return false;
	
}

// przestawia focus na następny input jeśli jego wartość osiągnie długość atrybutu maxlength.
function tabOnMaxlength() {
	var selektorRodzica = '.tabOnMaxlength'; // wspólny rodzic inputów z "przeskakujacym" focusem
	var selektorPol = 'input[type=text][maxlength]'; // selektor pól wewnątrz rodzica. Pole musi mieć "maxlength".
	
	$$(selektorRodzica).each(function(grupa){
		var pola = grupa.getElements(selektorPol);
		if (pola[1]) { // istnieją co najmniej dwa pola w grupie
			grupa.store('pola', pola);
			pola.each(function(pole, index){
				var maxLength = Number(pole.get('maxlength'));
				pole.addEvents({
					'keyup': function(){	
						// jeśli wpisany znak powinien być ostatni, przestaw focus dalej
						if(pole.value.length >= maxLength && grupa.retrieve('pola')[index+1]) {
							// ustaw focus na następnym polu
							grupa.retrieve('pola')[index+1].focus();
						}
					},
					'focus': function(){pole.select();} // ctrl + a za pomocą js
				});
			});
		}
	});
}
function formularzInputHover() {
	var klasaCss = 'formularzInputHover';
	var klasaCssPodgladu = 'formularzInputHover2';
	$$('.wykaz3 input, .wykaz3 textarea, .wykaz3 select').addEvents({
		'focus': function(){	
			this.getParent('.wykaz3').getElements('tr').removeClass(klasaCss);
			this.getParent('tr').addClass(klasaCss);
		}
	});
	$$('.wykazPodglad tbody tr').addEvents({
		'mouseenter': function(){this.addClass(klasaCssPodgladu);},
		'mouseleave': function(){this.removeClass(klasaCssPodgladu);}
	});
}



