var animacja = Class.create({
	initialize: function(element, style)
    {
    	this.element = element;
    	this.style = style;
    },
    animate: function(pixel)
    {
    	this.element.style[this.style] = pixel+'px';
    }
});

var Controller = Class.create({
	// Dane o modelu strony
	links: ['oferta', 'realizacje', 'kontakt'],
	firstSelection: true,
	selection: -1,
	slide: 0,
	slides: [1, 12, 1],
	initialize: function()
	{
		for(i = 0; i < this.links.length; i++)
		{
			
			$(this.links[i]).observe('click', this.linkNotification.bind(this, this.links[i]));
			$(this.links[i]+'Top').observe('click', this.linkNotification.bind(this, this.links[i]));
		}
		
		for(i = this.slides[1]-1; i >= 0 ; i--)
		{
			a = new Element('a', {
				'id': 'menuDotItem'+i,
				'class': 'linkDot',
				'href': '#'
			});
			
			$('menuDot').appendChild(a);
			$('menuDotItem'+i).observe('click', this.gotoSlide.bind(this, i));
		}
		
		$('nextSlide').observe('click', this.nextSlide.bind(this));
		$('prevSlide').observe('click', this.previousSlide.bind(this));
		new Effect.Opacity('nextSlide', {from: 1, to: 0, duration: 0.01});
		new Effect.Opacity('prevSlide', {from: 1, to: 0, duration: 0.01});
		new Effect.Opacity('menuDot', {from: 1, to: 0, duration: 0.01});
	},
	linkNotification: function(selection, event)
	{
		// Dla pierwszego kliknięcia robimy odpowiednie animacje
		if(this.firstSelection)
		{
			this.firstSelection = false;
			new Effect.Tween(new animacja($('menu'), 'paddingTop'), 300, 80, 'animate');
			new Effect.Tween(new animacja($('container'), 'height'), 0, 550, 'animate');
		}
		
		// Dla każdego kolejnego kliknięcia robimy odpowiednie rzeczy
		for(i = 0; i < this.links.length; i++)
		{
			
				if(selection == this.links[i] && this.selection != i)
				{
					$(this.links[i]).style.backgroundPosition = '0px 0px';
					new Effect.Tween('text', $('text').scrollLeft, 0, 'scrollLeft');
					new Effect.Tween('text', $('text').scrollTop, 500*i, 'scrollTop');
					if(this.slides[i] > 1 && (this.slides[this.selection] < 2 || this.selection == -1))
					{
						new Effect.Opacity('nextSlide', {from: 0, to: 1, duration: 2});
						new Effect.Opacity('prevSlide', {from: 0, to: 1, duration: 2});
						new Effect.Opacity('menuDot', {from: 0, to: 1, duration: 2});
					}
					else
					{
						if(this.slides[this.selection] > 1)
						{
							new Effect.Opacity('nextSlide', {from: 1, to: 0, duration: 2});
							new Effect.Opacity('prevSlide', {from: 1, to: 0, duration: 2});
							new Effect.Opacity('menuDot', {from: 1, to: 0, duration: 2});
						}
					}
					this.selection = i;
				}
				else
				{
					$(this.links[i]).style.backgroundPosition = '0px -107px';
				}
				
				this.slide = 0;
				this.lightDot(0);	
		}
		
	},
	gotoSlide: function(index)
	{
		if(index < this.slides[this.selection])
		{
			new Effect.Tween('text', $('text').scrollLeft, index*914, 'scrollLeft');
			this.slide = index;
			this.lightDot(index);
		}
	},
	lightDot: function(index)
	{	
		for(img = 0; img < this.slides[1]; img++)
		{
			if(index == img)
			{
				$('menuDotItem'+img).style.backgroundPosition = '-36px 0px';
			}
			else
			{
				$('menuDotItem'+img).style.backgroundPosition = '0px 0px';
			}
		}
	},
	nextSlide: function()
	{
		if(this.slide <= this.slides[this.selection]-2)
		{
			this.gotoSlide(this.slide+1);
		}
		else
		{
			this.gotoSlide(0);
		}
	},
	previousSlide: function()
	{
		if(this.slide > 0)
		{
			this.gotoSlide(this.slide-1);
		}
		else
		{
			this.gotoSlide(this.slides[this.selection]-1);
		}
	}
});

var Form = Class.create({
	initialize: function()
	{
		$('formSubmit').observe('click', this.submit.bind(this));
		$('formTypTelefoniczny').observe('click', this.typeChange.bind(this));
		$('formTypMailowy').observe('click', this.typeChange.bind(this));
	},
	typeChange: function()
	{
		if($('formTypTelefoniczny').checked == true)
		{
			$('prefTel').style.display = 'block';
			$('prefMail').style.display = 'none';
		}
		else
		{
			$('prefTel').style.display = 'none';
			$('prefMail').style.display = 'block';
		}
	},
	submit: function(event)
	{
		message = 'Musisz wypełnić pola: \n';
		error = false;
		// Imie i nazwisko
		if($('formImieNazwisko').value.length < 1)
		{
			error = true;
			message += '- Imie i nazwisko \n';
			this.addBorder($('formImieNazwisko'));
			
		}
		
		//Typ kontaktu
		if($('formKontaktTel').value.length < 1 && $('formTypTelefoniczny').checked == true)
		{
			error = true;
			message += '- Numer kontaktowy \n';
			this.addBorder($('formKontaktTel'));
		}
		
		if($('formTypMailowy').checked == true && $('formKontaktMail').value.length < 1)
		{
			
			error = true;
			message += '- Mail kontaktowy \n';
			this.addBorder($('formKontaktMail'));
		}
		
		if($('formTypMailowy').checked == true && $('formWiadomosc').value.length < 1)
		{
		    error = true;
		    message += '- Treść wiadomości \n';
		    this.addBorder($('formWiadomosc'));
		}
		
		//Jakie zainteresowanie
		isOK = false;
		for(i = 1; i<= 11; i++)
		{
			if($('formI'+i).checked == true)
			{
				isOK = true;
			}
		}

		if(isOK == false)
		{
			error = true;
			message += '- Interesująca część oferty \n';
		}
		
		if(error)
		{
			alert(message);
		}
		else
		{
			// Czas wysłać dane
			lista = new Hash();
			
			lista.set('imie', $('formImieNazwisko').value);
			lista.set('firma', $('formFirma').value);
			if($('formTypTelefoniczny').checked == true)
			{
				lista.set('tel', $('formKontaktTel').value);
				lista.set('godzina', $('formHour').value);
				lista.set('miesiac', $('formMonth').value);
				lista.set('dzien', $('formData').value);
			}
			else
			{
				lista.set('mail', $('formKontaktMail').value);
				lista.set('wiadomosc', $('formWiadomosc').value);
			}
			
			for(i = 1; i <= 11; i++)
			{
				if($('formI'+i).checked == true)
				lista.set('option'+i, 1);
			}
			
			new Ajax.Request('/formularz', {
				method: 'post',
				parameters: lista,
				onComplete: this.submitNotification.bind(this)
			});
		}
		
	},
	submitNotification: function(transport)
	{
		if(transport.status == 200)
		{
			alert("Wiadomość została wysłana. Dziękujemy za zgłoszenie");
		}
		else
		{
			
		}
	},
	removeBorder: function(obj)
	{
		obj.style.border = '0px';
	},
	addBorder: function(obj)
	{
		obj.style.border = '1px solid #da2800';
		obj.observe('focus', this.removeBorder.bind(this, obj));
	},
	message: function(message)
	{
	
	}
});

var Gal = Class.create({
	galleryNo: [4,4,4,4,4,4,4,3,4,3,4,4],
	initialize: function()
	{
		for(i = 1; i <= this.galleryNo.length; i++)
		{
			for(j = 1; j <= this.galleryNo[i-1]; j++)
			{
				$('galleryPic'+j+'_'+i).observe('click', this.selectPicture.bind(this, i, j));
			}
		}
	},
	selectPicture: function(gal, pic)
	{
		$('galleryMain'+gal).src = '/img/black/'+gal+'/'+pic+'.jpg';
		$('galleryPicR_'+gal).style.left = (39+(pic-1)*(68+24))+'px';
	}
});

var controller, form, gallery;

window.onload = function()
{
	gallery = new Gal();
	controller = new Controller();
	form = new Form();
	$('loading').style.display = 'none';
}