var SiteClass = new Class({

	initialize: function(){

		hs.registerOverlay(
			{
				thumbnailId: null,
				overlayId: 'controlbar',
				position: 'top right',
				hideOnMouseOut: true
			}
		);
		
		hs.graphicsDir = 'public/images/highslide/graphics/';
		hs.outlineType = 'rounded-white';
		hs.align = 'center';
		hs.captionEval = 'this.thumb.title';

		window.ff3 = (window.navigator.userAgent.indexOf('Firefox/3.') != -1) ? true : false;
				
	}, // initialize

	
	domReady: function() {

		this.activateInputs();
		this.activateGoToTop();
		this.activateJobForm();
		this.activateOfferTree();
		
	}, // domReady

	activateOfferTree: function() {
		if($('product-search-link')) {
			$('product-search-link').addEvent('click', function() {
				var scrollFx = new Fx.Scroll(window, {
					transition: Fx.Transitions.Quad.easeInOut
				});
				scrollFx.toTop();
				$('q').focus();
			});
		}
		if($('product-search-link-2')) {
			$('product-search-link-2').addEvent('click', function() {
				var scrollFx = new Fx.Scroll(window, {
					transition: Fx.Transitions.Quad.easeInOut
				});
				scrollFx.toTop();
				$('q').focus();
			});
		}
		if($('offer-categories')) {
			$$('div#offer-categories ul li').each(function(li) {
				try {
					// dodawanie elementów drzewka
					if(li.getParent().getLast() === li && li.getParent().getParent().get('tag') != 'div') li.addClass('last');
					
					// dodawanie 'zamykaczy' drzewka
					uls = li.getChildren("ul");
					if(uls.length) {
						// ustalam, czy dana galaź jest u samej góry, jeżeli tak to nie zamyka się jej
						var top_level = (li.getParent().getParent().getParent().get('tag') == 'div');
						uls.each(function(ul){
							if(!top_level) ul.addClass('hidden');
						});
						var span = new Element('span');
						if(top_level) span.addClass('open');
						span.inject(li, 'top');
						span.addEvent('click', function(){
							if(this.hasClass('open')) {
								this.removeClass('open');
								this.getNext().getNext().addClass('hidden');
							} else {
								this.addClass('open');
								this.getNext().getNext().removeClass('hidden');
							}
						});
					}
					// ustawianie aktywnej kategorii
					li.getChildren('a').each(function(a) {
						var href = location.href.replace(/\/\d+\/?$/, ""); // usuwanie pagera z adresu
						if($('offer-details')) href = location.href.replace(/\/[^\/]*\/?$/, ""); //gdy szczegoly obcinanie nazwy produkty z adresu
						if(href.substr(href.length - a.get('href').length) == a.get('href')) {
							a.addClass('active');
							var img = new Element('img', {
								'src': 'public/images/ico_arrow_small.gif'					  
							});
							img.inject(a);
						}
						if(!a.getPrevious()) {
							a.setStyle('float', 'none');
						}
					});
				} catch (ex) { $('offer-list').innerHTML += 'ERROR' + ex + '<br><br>';  }
			});
		}

		$$('div#offer-categories ul li a.active').each(function(a) {
			try {
				// otwieramy aktualna kategorie
                if(a.getNext()) a.getNext().removeClass('hidden');
                if(a.getPrevious()) a.getPrevious().addClass('open');
				
				// wycieczka ku gorze aby otworzyć wcześniej zamkniete gałęzie drzewka...
				while(a.getParent().getParent().get('tag') == 'ul') {
					ul = a.getParent().getParent();
					ul.removeClass('hidden');
					ul.getPrevious().getPrevious().addClass('open');
					a = ul.getPrevious();
				}
			} catch (ex) {  }
		});
	}, //activateOfferTree
	
	activateJobForm: function(){
		if($('job-form')) {
			this.activateFileInputs();
			$('job-form').addEvent('submit', function() {
				$('job-form-loader').setStyle('display', 'block');
				var inputs = $$('form#job-form input[type=text]'); //, form#job-form input[type=file]
				var errors = 0;
				var regex;
				for(var i = 0; i < inputs.length; i++) {
					// sprawdzanie czy wogóle ktoś coś wpisał
					if(inputs[i].value == inputs[i].title && inputs[i].id) {
						inputs[i].getParent().addClass('error'); 
						errors++;
					} else {
						regex = false;
						switch(inputs[i].name) {
							case 'name': regex = /^.{3,100}$/; break;
							case 'phone': regex = /^[-+\.w0-9 ]{9,30}$/; break;
							case 'email': regex = /^[0-9a-zA-Z._-]+\@[0-9a-zA-Z_-]+\.[0-9a-zA-Z.]{2,100}$/; break;
							//case 'cv': regex = /^.+$/; break;
							//case 'lm': regex = /^.+$/; break;
						}
						if(regex) {
						  
							if(regex.test(inputs[i].value) == false) {
								inputs[i].getParent().addClass('error');
								errors++;
							} else {
								inputs[i].getParent().removeClass('error');
							}
						}
					}
				}

				if(!$('cv').value && !$('lm').value) {
					$('cv').getParent().addClass('error');
					//$('lm').getParent().addClass('error');
					errors++;	
				} else {
					$('cv').getParent().removeClass('error');
					//$('lm').getParent().removeClass('error');
				}
				if(errors > 0) {
					$('job-form-loader').setStyle('display', 'none');
					alert('Wypełnij podświetlone pola');
					return false;
				}
			});
		}
	
	}, //activateJobForm
	
	activateGoToTop: function() {
		$('gotop').addEvent('click', function() {
			var scrollFx = new Fx.Scroll(window, {
				transition: Fx.Transitions.Quad.easeInOut
			});
			scrollFx.toTop();
		});
	}, //activateGoToTop

	activateInputs: function() {
		if($('search-form')) $('search-form').addEvent('submit',  function() { if($('q').value == $('q').title) return false; });
		var inputs = $$('input[type=text]');
		for(var i = 0; i < inputs.length; i++) {
			if(inputs[i].value == '') inputs[i].value = inputs[i].title;
			inputs[i].addEvent('focus', function() { if(this.value == this.title) this.value = ''; if(window.ie) this.addClass('focus'); });
			inputs[i].addEvent('blur',  function() { if(this.value == '') this.value = this.title; if(window.ie) this.removeClass('focus'); });
		}
	}, //activateInputs
	
	activateFileInputs: function() {
		if($('cv') && $('lm')) {
			$('cv').addEvent('change', function() { $('cv_name').style.display = 'none'; });
			$('lm').addEvent('change', function() { $('lm_name').style.display = 'none'; });
		}
		
		if (!(document.createElement && document.getElementsByTagName)) return;
		var fakeFileUpload = document.createElement('div');
		fakeFileUpload.className = 'fakefile';
		fakeFileUpload.appendChild(document.createElement('input'));
		var button = document.createElement('span');
		button.innerHTML='Przeglądaj';
		fakeFileUpload.appendChild(button);
		var x = document.getElementsByTagName('input');
		for (var i=0;i<x.length;i++) {
			if (x[i].type != 'file') continue;
			if (x[i].getAttribute('noscript')) continue;
			if (x[i].parentNode.className != 'file') continue;
			x[i].className = 'hidden';
			//x[i].disabled = 'disabled';
			var clone = fakeFileUpload.cloneNode(true);
			x[i].parentNode.appendChild(clone);
			x[i].relatedElement = clone.getElementsByTagName('input')[0];
			x[i].onchange = x[i].onmouseout = function () {
				this.relatedElement.value = this.value;
			}
			if (x[i].value) {
				x[i].onchange();
			}
		}
	}, //activateFileInput
	
	showMail: function(user, domain, contry, attr) {
		var start = "<a href=\"mailto:" + user + "@" + domain + "." + contry + "?subject=Mail ze strony WWW\"" + attr + ">";
		var end = "</a>";
		document.write(start + user + "@" + domain + "." + contry + end);
		return false;
	}, // showMail
	
	stopScroll: function(){
		var scroll;
		$$("body").each(function(b){
			scroll = b.getScroll();
		});
		window.scrollTo(0,scroll.y);
	} //stopScroll
	
});

var Site = new SiteClass();
window.addEvent('domready', function(){ Site.domReady(); });
