_wygodne_zakupy = new Class({
	
	initialize:function(){
		this.small_cart_request = '';
		this.cart_visable = 0;
		this.kaucja_sztuk = 0;
		this.koszyk = '';
		if($('register_field_password')) {
			$('register_field_password').value = '';
			$('register_field_password_repeat').value = '';
		}
	},
	dorosly:function(){
		this.small_cart_request = new Request({
			method: 'post', 
			link:'cancel',
			url: 'userprocess.php',
			onRequest:function(){ 
			},
			onComplete:function(res) {
				if(res=='OK'){
					window.location.reload(true);
				}
				else {
					if(Browser.Engine.trident4 || Browser.Engine.trident5){
						alert('Towar dostępny tylko osobom pełnoletnim.');
					}
					else {
						$('alert_data').set('html','Towar dostępny tylko osobom pełnoletnim.');
						SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
					}
				}
			} 
		});
		var odp = 'false';
		if($('wiek')) odp = $('wiek').getProperty('checked');
		this.small_cart_request.send('mu_wygodne_produkt=dorosly&odp='+odp);			
	},
	
	favourite:function(pid){
		this.small_cart_request = new Request({
			method: 'post', 
			link:'cancel',
			url: 'userprocess.php',
			onRequest:function(){ 
			},
			onComplete:function(res) {
				if(res=='USUN'){
					if($('favourite_'+pid)){
						$('favourite_'+pid).set('text','usuń z ulubionych.');
						if(Browser.Engine.trident4 || Browser.Engine.trident5){
							alert('Produkt został dodany do ulubionych.');
						}
						else {
							$('alert_data').set('html','Produkt został dodany do ulubionych.');
							SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
						}
					}
				}
				else if(res=='DODAJ'){
					if($('favourite_'+pid)){
						$('favourite_'+pid).set('text','dodaj do ulubionych..');
						if(Browser.Engine.trident4 || Browser.Engine.trident5){
							alert('Produkt został usunięty z ulubionych');
						}
						else {
							$('alert_data').set('html','Produkt został usunięty z ulubionych.');
							SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
						}
					}					
				}
				else if (res=='ERROR'){
					if(Browser.Engine.trident4 || Browser.Engine.trident5){
						alert('Wystąpił błąd dodania produktu do ulubionych. Spróbuj ponownie.');
					}
					else {
						$('alert_data').set('html','Wystąpił błąd dodania produktu do ulubionych. Spróbuj ponownie.');
						SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
					}
				} 
				else{	
					if(Browser.Engine.trident4 || Browser.Engine.trident5){
						alert('Musisz być zalogowany, aby dodać produkt do ulubionych.');
					}
					else {
						$('alert_data').set('html','Musisz być zalogowany, aby dodać produkt do ulubionych.');
						SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
					}
				}
			} 
		});
		this.small_cart_request.send('mu_wygodne_produkt=ulubione'+'&pid='+pid);			
	},
	dec_prod:function(pid,ile,gram,kaucja,magazyn){
		var el_val = '';
		if($('count_'+pid)) el_val = $('count_'+pid);
		else el_val = $$('input[name=count_'+pid+']');
		if($type(el_val)!='object'){
			var count = 0;
			if (gram==1){
				wartosc = el_val.getProperty('value').toString().replace(',','.');
				wartosc = parseFloat(wartosc);
				wartosc = parseInt(10*wartosc);
				wartosc--;
				count = parseFloat(wartosc/10);
				ile = parseFloat(ile/10);
			}
			else {
				count = parseInt(el_val.getProperty('value'));
				count--;
			}
			if (count>ile && (magazyn!=0 || (magazyn==0 && ile>0))) count = ile;
			if (count<=0) count = 0;
			if (count>19) count = 19;
			if(gram==1) {
				if (count>1.9) count = 1.9;
				str = count.toString();
				count = str.replace('.',',');
			}
			el_val.setProperty('value',count);
			if(kaucja==1) this.kaucja_sztuk = count;
			else this.kaucja_sztuk = 0;
		}
	},
	inc_prod:function(pid,ile,gram,kaucja,magazyn){
		var el_val = '';
		var jm = 'szt.';
		if($('count_'+pid)) el_val = $('count_'+pid);
		else el_val = $$('input[name=count_'+pid+']');
		if($type(el_val)!='object'){
			var count = 0;
			if (gram==1){
				wartosc = el_val.getProperty('value').toString().replace(',','.');
				wartosc = parseFloat(wartosc);
				wartosc = parseInt(10*wartosc);
				wartosc++;
				count = parseFloat(wartosc/10);
				ile = parseFloat(ile/10);
				jm = 'kg';
			}
			else {
				count = parseInt(el_val.getProperty('value'));
				count++;
			}
			if (count>ile && magazyn!=0){
				if(Browser.Engine.trident4 || Browser.Engine.trident5){
					alert('Przepraszamy, ale ten towar jest aktualnie niedostępny w takiej ilości.');
				}
				else {
					$('alert_data').set('html','Przepraszamy, ale ten towar jest aktualnie niedostępny w takiej ilości. Maksymalna ilość to '+ile+' '+jm);
					SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
				}
				count = ile;
			}
			if (count>ile && (magazyn!=0 || (magazyn==0 && ile>0))) count = ile;
			if (count>20) count = 20;
			if(gram==1) {
				if (count>2.0) count = 2.0;
				str = count.toString();
				count = str.replace('.',',');
			}
			el_val.setProperty('value',count);
			if (kaucja==1) this.kaucja_sztuk = count;
			else this.kaucja_sztuk = 0;
		}
	},
	cart_add:function(pid,kaucja,gram){
		if ($type(this.small_cart_request)=='object') { this.small_cart_request.cancel(); }
		var count = 1;
		if($('count_'+pid)){
			if(gram==1) {
				count = parseInt(10*($('count_'+pid).value).replace(',','.'));
			}
			else {
				count = parseInt($('count_'+pid).value);
			}
		}
		this.small_cart_request = new Request({
			method: 'post', 
			link:'cancel',
			url: 'userprocess.php',
			onRequest:function(){ 
				nodeshop.LoaderShow(nodeshop.small_shop_div);
			},
			onComplete:function(res) {
				if (res=='NOMAG'){
					if(Browser.Engine.trident4 || Browser.Engine.trident5){
						alert(nodeshop.string_not_on_mag);
					}
					else {
						$('alert_data').set('html',nodeshop.string_not_on_mag);
						SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
					}
				} else if(res=='NOMAGMORE'){
					if(Browser.Engine.trident4 || Browser.Engine.trident5){
						alert(nodeshop.string_not_on_mag_more);
					}
					else {
						$('alert_data').set('html',nodeshop.string_not_on_mag_more);
						SqueezeBox.open($('node_shop_alert_popup_data'), {size: {x: 340, y: 120},handler: 'clone'});
					}
				} else if(res=='ZERO'){
					//wybrano 0 sztuk
				}
				else {
					$(nodeshop.small_shop_div).innerHTML=res;
					if (nodeshop.use_popup){
						if ($$('.suma_koszyk')) {
							//wytnij tekst z res
							start = res.indexOf('<span>');
							stop = res.indexOf('</span>');
							if (start==stop) suma = '0,00 zł';
							else suma = res.substring(start+6,stop);
							$$('.suma_koszyk').set('text',suma);
						}
						//jesli kaucja to dodaj wpis o tym
						if (kaucja==1) {
							wygodne_zakupy.kaucja_sztuk = count;
							licznik = '<a href="javascript:wygodne_zakupy.dec_prod(11330,'+count+',0,1,0)" class="cart_dec_prod"></a><input value="'+count+'" name="count_11330" maxlength="2" class="cart_count_prod"><a href="javascript:wygodne_zakupy.inc_prod(11330,'+count+',0,1,0)" class="cart_inc_prod"></a>';
							$$('.kaucja_licznik').set('html',licznik);
							$$('.kaucja_popup').setStyle('display','block');
						}
						else{
							wygodne_zakupy.kaucja_sztuk = 0;
							$$('.kaucja_popup').setStyle('display','none');
						}
						if(Browser.Engine.trident4 || Browser.Engine.trident5){
							if (kaucja==1) {
								wygodne_zakupy.kaucja_add(2);
								var k = 0.35*wygodne_zakupy.kaucja_sztuk;
								var s = suma.slice(0,-3);
								s.replace(',','.');
								s.replace(' ','');
								var w = parseFloat(s)+parseFloat(k);
								suma = w+' zł';
								alert("Produkt został dodany do koszyka.\nDodano równiż kaucję za butelki\nWartość koszyka wynosi: "+suma);
							}
							else {
								alert("Produkt został dodany do koszyka.\nWartość koszyka wynosi: "+suma);
							}
						}
						else {
							if ($('node_shop_cart_small_popup_data')){
								if (wygodne_zakupy.koszyk=='' || wygodne_zakupy.koszyk=='undefined'){
									wygodne_zakupy.koszyk = $$('a.node_shop_cart_small_popup_link_order').getProperty('href');
								}
								$$('a.node_shop_cart_small_popup_link_order').setProperty('href','#');
								$$('a.node_shop_cart_small_popup_link_order').setProperty('onclick','wygodne_zakupy.closePopup(1);return false;');
								t1 = $('node_shop_cart_small_popup_data').get('html');
								t2 = t1.replace(/nodeshop.closePopup/g,'wygodne_zakupy.closePopup');
								$('node_shop_cart_small_popup_data').set('html',t2);				
							}
							SqueezeBox.open($('node_shop_cart_small_popup_data'), {handler: 'clone'});
						}
					}
				}
				nodeshop.LoaderHide(nodeshop.small_shop_div);
				wygodne_zakupy.hide_small_cart(300,this.cart_visable);
				wygodne_zakupy.show_small_cart(300);
			} 
		});
		this.small_cart_request.send('mu_wygodne_produkt=cart_add'+'&pid='+pid+'&count='+count+'&nt_callerObj_key='+nodeshop.small_shop_key);			
	},
	cart_remove:function(cid,pid){
		if ($type(this.small_cart_request)=='object') { this.small_cart_request.cancel(); }
		this.small_cart_request = new Request({
			method: 'post', 
			link:'cancel',
			url: 'userprocess.php',
			onRequest:function(){ 
				nodeshop.LoaderShow(nodeshop.small_shop_div);
			},
			onComplete:function(res) {
				nodeshop.LoaderHide(nodeshop.small_shop_div);
				$(nodeshop.small_shop_div).innerHTML=res;
				wygodne_zakupy.hide_small_cart(300,1);
				wygodne_zakupy.show_small_cart(300);
			} 
		});
		this.small_cart_request.send('mu_node_shop_user=cart_remove'+'&cid='+cid+'&nt_callerObj_key='+nodeshop.small_shop_key+'&pid='+pid);			
	},
	kaucja_add:function(link){
		pid = 11330;
		count = this.kaucja_sztuk;
		this.kaucja_request = new Request({
			method: 'post', 
			link:'cancel',
			url: 'userprocess.php',
			onRequest:function(){ 
			},
			onComplete:function(res) {
				$(nodeshop.small_shop_div).innerHTML=res;
				wygodne_zakupy.hide_small_cart(300,this.cart_visable);
				wygodne_zakupy.show_small_cart(300);
				if(link != 2){
					SqueezeBox.close();
				}
				if(link==1){
					window.location.href = wygodne_zakupy.koszyk;
				}
			}
		});
		this.kaucja_request.send('mu_wygodne_produkt=cart_add'+'&pid='+pid+'&count='+count+'&nt_callerObj_key='+nodeshop.small_shop_key);			
	},
	closePopup:function(link){
		//dodaj kaucje
		if(this.kaucja_sztuk>0){
			this.kaucja_add(link);
		}
		SqueezeBox.close();
		if(link==1 && this.kaucja_sztuk==0){
			window.location.href = wygodne_zakupy.koszyk;
		}
		return false;
	},
	striptags:function(str){
	    //wyrażenie regularne na usuwanie tagów i pozostawianie tekstu wewnątrz nich
	    var tagREG=new RegExp('<[a-zA-Z/]{1,15}.*?>','g');
	    return str.replace(tagREG,'');
	},
	foto:function(tytul,obraz,opis){
		if(Browser.Engine.trident4 || Browser.Engine.trident5){
			
		}
		else{
			$('image_popup_title').set('html',tytul);
			$('image_popup_foto').set('html','<img src="'+obraz+'" alt="'+tytul+'" title="'+tytul+'" />');
			if(Browser.Engine.trident) $('image_popup_desc').set('html',this.striptags(opis));
			else $('image_popup_desc').set('html',opis);
 			var delta = 305+13*Math.ceil(opis.length/50);
			SqueezeBox.open($('node_shop_image_popup'), {size: {x: 320, y: delta},handler: 'clone'});
			$('sbox-window').addClass('sbox-window');
		}
	},
	pokaz_menu:function(main,sec){
		var tab = main.split(';');
		for( i=0; i < tab.length; i++ ){
			if($('catid_'+tab[i])) {
				$('catid_'+tab[i]).addClass('active_category');
				$$('li#catid_'+tab[i]+' a').addClass('active_category');
			}
		}
		tab = sec.split(';');
		for( i=0; i < tab.length; i++ ){
			if($('switcher_id_2_'+tab[i])) {
				$('switcher_id_2_'+tab[i]).removeClass('menu_hidden');
			}
			if($('switcher_id_3_'+tab[i])) {
				$('switcher_id_3_'+tab[i]).removeClass('menu_hidden');
			}
			if($('switcher_id_4_'+tab[i])) {
				$('switcher_id_4_'+tab[i]).removeClass('menu_hidden');
			}
			if($('catid_'+tab[i])) {
				$('catid_'+tab[i]).addClass('active_parent_category');
				$$('li#catid_'+tab[i]).getFirst('a').addClass('active_parent_category');
			}
		}
	},
	pokaz_cale_menu:function(list,act){
		var tab = list.split(';');
		for( i=0; i < tab.length; i++ ){
			if($('switcher_id_2_'+tab[i])) {
				$('switcher_id_2_'+tab[i]).removeClass('menu_hidden');
			}
			if($('switcher_id_3_'+tab[i])) {
				$('switcher_id_3_'+tab[i]).removeClass('menu_hidden');
			}
			if($('switcher_id_4_'+tab[i])) {
				$('switcher_id_4_'+tab[i]).removeClass('menu_hidden');
			}
		}
		//var myFx = new Fx.Scroll(window);
		//if($('catid_'+act)) myFx.toElement($('catid_'+act)); 
	},
	replace_wpis:function(){
		if ($('small_cart_wrapper')){
			t1 = $('small_cart_wrapper').get('html');
			t2 = t1.replace(/nodeshop.cart_remove/g,'wygodne_zakupy.cart_remove');
			$('small_cart_wrapper').set('html',t2);
		}
	},
	hide_small_cart:function(czas,pokaz){
		if ($('small_card_close')){
			var myEffect = new Fx.Morph('small_cart_wrapper', {
			    duration: czas,
			    transition: Fx.Transitions.Sine.easeOut
			});
			myEffect.start({
			    'height': 0
			});
			pokaz = pokaz||0;
			this.cart_visable = pokaz;
		}
	},
	show_small_cart:function(czas){
		if (this.cart_visable == 1){
			var myEffect = new Fx.Morph('small_cart_wrapper', {
			    duration: czas,
			    transition: Fx.Transitions.Sine.easeOut
			});
			var elem = $('small_cart_wrapper');
			var tab = elem.getElementsByTagName("table");
			if ( tab[0] == undefined) {
				$('small_cart_wrapper').set('html','<div class=\"pusty_koszyk\"><p>Koszyk jest pusty</p><a title=\"Zwiń\" id=\"small_card_close\" class=\"small_card_close\" href=\"javascript:wygodne_zakupy.hide_small_cart(600)\">Zwiń</a></div>');
				wysokosc = 38;
				wys = '6px';
			}
			else {
				$$('.order_row td').set('html','<a title=\"Zamów\" id=\"small_card_order\" href=\"?node_id=1941&amp;lang=pl\">Zamów</a><a title=\"Zwiń\" id=\"small_card_close\" href=\"javascript:wygodne_zakupy.hide_small_cart(600)\">Zwiń</a>');
				wysokosc = tab[0].offsetHeight;
				wys = '6px';
			}
			myEffect.start({
			    'height': wysokosc+11+'px'
			});
			if(!$('small_cart_wrapper_bottom')) {
				var dol = new Element('div', {
					id: 'small_cart_wrapper_bottom',
					styles: {
       					background: 'url(\'/user_themes/wygodne_zakupy/style//images/maly_koszyk_dol.png\') no-repeat center bottom',
       					width: '220px',
       					height: wys
					}
				});
				dol.inject(elem);
			}
			wygodne_zakupy.replace_wpis();
		}
	},
	przelicz:function(){
		$('nodeshop_button_recount').click();
	},
	termin_dost:function(){
		var termin = new Request({
			method: 'post', 
			link:'cancel',
			url: 'userprocess.php',
			onRequest:function(){ 
			},
			onComplete:function(res) {
				$('termin_dost').set('html',res);
			} 
		});
		termin.send('mu_wygodne_produkt=najblizszy_termin');			
	},
	platnosc:function(war){
		if($('payment_radio')){
			if(war == 72 || war == 67) $('payment_radio').value = 'NDPC';
			else $('payment_radio').value = 'NDPR';
		}
	}
	
});

var wygodne_zakupy = new _wygodne_zakupy();

window.addEvent('domready', function(){

	$$('li.koszyk a').addEvent('mouseenter',function(e){
		if(!$('is_koszyk')){
			wygodne_zakupy.cart_visable = 1;
			wygodne_zakupy.show_small_cart(600);
		}
	});
	
	if($('wiek')){
		$('check_wiek').addEvent('click',function(){
			if($('wiek').checked) $('wiek').setProperty('checked','');
				else $('wiek').setProperty('checked','checked');
		});
	}
	
	if ($('reg_field_fa')){
		var f = $('reg_field_fa');
		if (f.checked){
			$$('tr.register_field_tr_company').each(function(item){
				item.setStyle('display','');
			});
		} else {
			$$('tr.register_field_tr_company').each(function(item){
				item.setStyle('display','none');
			});
		}
		f.addEvent('click',function(e){
			if (f.checked){
				$$('tr.register_field_tr_company').each(function(item){
					item.setStyle('display','');
				});
			} else {
				$$('tr.register_field_tr_company').each(function(item){
					item.setStyle('display','none');
				});
			}
		});
	}
	if($('reg_imie')){
		$('reg_imie').addEvent('keyup', function(e) {
			$('reg_full_name').value = $('reg_imie').value+' '+$('reg_nazwisko').value;
			if ($('reg_imie').value=='' || $('reg_nazwisko').value=='') {
				$('reg_full_name').value = '';
			}
		});
		$('reg_imie').addEvent('blur', function(e) {
			$('reg_full_name').value = $('reg_imie').value+' '+$('reg_nazwisko').value;
			if ($('reg_imie').value=='' || $('reg_nazwisko').value=='') {
				$('reg_full_name').value = '';
			}
		});
		
	}
	if($('reg_nazwisko')){
		$('reg_nazwisko').addEvent('keyup', function(e) {
			$('reg_full_name').value = $('reg_imie').value+' '+$('reg_nazwisko').value;
			if ($('reg_imie').value=='' || $('reg_nazwisko').value=='') {
				$('reg_full_name').value = '';
			}
		});
		$('reg_nazwisko').addEvent('blur', function(e) {
			$('reg_full_name').value = $('reg_imie').value+' '+$('reg_nazwisko').value;
			if ($('reg_imie').value=='' || $('reg_nazwisko').value=='') {
				$('reg_full_name').value = '';
			}
		});
		
	}
	if($('reg_ulica')){
		$('reg_ulica').addEvent('blur', function(e) {
			$('reg_full_street').value = $('reg_ulica').value+' '+$('reg_numer').value;
			if ($('reg_ulica').value=='' || $('reg_numer').value ==''){
				$('reg_full_street').value = '';
			}
		});
		$('reg_ulica').addEvent('keyup', function(e) {
			$('reg_full_street').value = $('reg_ulica').value+' '+$('reg_numer').value;
			if ($('reg_ulica').value=='' || $('reg_numer').value ==''){
				$('reg_full_street').value = '';
			}
		});
	}
	if($('reg_numer')){
		$('reg_numer').addEvent('blur', function(e) {
			$('reg_full_street').value = $('reg_ulica').value+' '+$('reg_numer').value;
			if ($('reg_ulica').value=='' || $('reg_numer').value ==''){
				$('reg_full_street').value = '';
			}
		});
		$('reg_numer').addEvent('keyup', function(e) {
			$('reg_full_street').value = $('reg_ulica').value+' '+$('reg_numer').value;
			if ($('reg_ulica').value=='' || $('reg_numer').value ==''){
				$('reg_full_street').value = '';
			}
		});
	}
	
	if($('termin_dost')){
		wygodne_zakupy.termin_dost();
	}

});

