function AddButt(id)
{
	$('overlay').setStyles({
		'opacity': 0.5,
		'display' : 'block'
	});
	$('over_ground').setStyles({
		'display' : 'block'
	});
	$('head_q').innerHTML = 'Zapytaj sprzedawce o produkt';
	$('product_a').value = id;
	
}

function request(button)
{
	var req = new Request({
		method: 'post',
		url: $(button).get('href'),
		data: { 'do' : '1' },
		onRequest: function() { 
			$('main').set('src','media/images/ajax-loader.gif');
			$('maintxt').innerHTML = '';
			$('price').innerHTML = ''; 
		},
		onComplete: function(responset, responsex){
			var obj = JSON.decode(responset);
			$('main').set('src', 'user_files/products/'+obj.pid+'/small/'+obj.file);
			$('maintxt').innerHTML = obj.name;
			$('price').innerHTML = 'cena: <font style="color:red">'+obj.price.toFixed(2)+'</font> zł';
			var nxt = obj.id.toInt() + 1;
			$('nxt').set('href', 'products/promotions/' + nxt);
			var prv = obj.id.toInt() - 1;
			$('prv').set('href', 'products/promotions/' + prv);
			$('click').addEvent('click', function () {
				window.location = 'opis-produktu/'+obj.pid;
			})
			
		}	
			 
		
	});
	return req;
}

function requestN(button)
	{
		var req = new Request({
			method: 'post',
			url: $(button).get('href'),
			data: { 'do' : '1' },
			onRequest: function() { 
			},
			onComplete: function(responset, responsex){
				var obj = JSON.decode(responset);
					$('content').setStyle('height','auto');
					$('body').setStyle('height','auto');
					
					$('text').innerHTML = obj.text;
					
				
		}	
			 
		
	});
	return req;
}

window.addEvent('domready',function(){		
		//background loginu
		if ($('submit_online')) {
			var form = $('payment_online');
			$('submit_online').addEvent('click', function(){
				var order_id = $('order_id').value;
				
				var req = new Request.JSON({
					url: 'payments_online/SendForm/' + order_id,
					onSuccess: function(response){
					
						$each(response, function(b, a){
							form.adopt(new Element('input', {
											type: 'hidden',
											name: a,
											value: b
										}));

						});
						
						form.submit();
					}
				});
				req.send();
			});
		};
		$$('#inputLogin,#inputLoginPassword').each(function(obj,id){
			if (obj.value.length == 0) {
			obj.addClass(obj.id + '-blur');
			}
		
			obj.addEvents({
				'focus': function(){obj.removeClass(obj.id + '-blur');},
				'blur': function(){if(obj.value.length == 0)obj.addClass(obj.id + '-blur');}
			});
		});
		var sear = $('inputSearch');
		if(sear.value.length == 0)
		{
			sear.setStyle('background' ,'url(media/images/search_back.jpg) no-repeat #eeefcf');
		}
		sear.addEvents({
			'focus': function(){sear.removeClass(sear.id + '-blur');sear.setStyle('background','#eeefcf');},
			'blur': function(){if(sear.value.length == 0)sear.addClass(sear.id + '-blur');sear.setStyle('background' ,'url(media/images/search_back.jpg) no-repeat #eeefcf');}
		});
		
		
		new Swiff('media/images/top.swf', { width: 986, height: 210, container: 'flash'});
		//przeladowanie captchy
		
			//overlay
			if($chk($('close'),$('overlay'),$('step2Send')))
			{
				$('close').addEvent('click', function(){
					$('overlay').setStyle('display','none');
					$('over_ground').setStyle('display','none');
				});
			
				$('overlay').addEvent('click', function () {
					$('overlay').setStyle('display','none');
					$('over_ground').setStyle('display','none');
				});
				
				
				$('step2Send').addEvent('submit', function(e){
					e.stop();
					this.set('send', {onComplete: function(response) { 
						
						$('msg').setStyle('display','block');
						$('msg').innerHTML = response;
						k++;
						$('captchaImg').src = 'captcha/default/' + k;
						$('captchaImg').style.display = 'block';
				}});
						this.send();
				});
			
			}
			
			
			//Promocja
			request('nxt').send();
			setInterval("request('nxt').send();",5000);
			$('nxt').addEvent('click', function(event) {
				event.stop();
				request('nxt').send();
			});
			
			$('prv').addEvent('click', function(event) {
				event.stop();
				request('prv').send();
			});
			//Opis produktu
			if($chk($('prod'),$('filename2'),$('filename')))
			{
				var id = $('prod').get('value');
				var filename2 = $('filename2').get('value');
				var filename = $('filename').get('value');
				if(filename.length > 1){
					$('big').set('src','user_files/products/' + id + '/big/' + filename);
					$('chImg').set('href','user_files/products/' + id + '/giga/' + filename);
					$('chImg').set('rel','milkbox:[gall1]');
					milkbox.reloadGalleries();
				
				$('img1').addEvent('click', function(){
					$('big').set('src','user_files/products/' + id + '/big/' + filename);	
					$('chImg').set('href','user_files/products/' + id + '/giga/' + filename);
					$('chImg').set('rel','milkbox:[gall1]');
					milkbox.reloadGalleries();
					
				})
				
				$('img2').addEvent('click', function(){
					$('big').set('src','user_files/products/' + id + '/big/' + filename2);
					$('chImg').set('href','user_files/products/' + id + '/giga/' + filename2);
					$('chImg').set('rel','milkbox:gall2');
					milkbox.reloadGalleries();
					
				})
				
				}
				
			}
			
			//strony static
			
			if($chk($('pl'),$('en')))
			{
				requestN('pl').send();
			
				$('pl').addEvent('click', function(event) {
					event.stop();
					requestN('pl').send();
				});
				$('en').addEvent('click', function(event) {
					event.stop();
					requestN('en').send();
				});
			}									
	});
