// JavaScript Document

Number.implement({

	/*
	Property: numberFormat
		Format a number with grouped thousands.

	Arguments:
		decimals, optional - integer, number of decimal percision; default, 2
		dec_point, optional - string, decimal point notation; default, '.'
		thousands_sep, optional - string, grouped thousands notation; default, ','

	Returns:
		a formatted version of number.

	Example:
		>(36432.556).numberFormat()  // returns 36,432.56
		>(36432.556).numberFormat(2, '.', ',')  // returns 36,432.56
	*/

	numberFormat : function(decimals, dec_point, thousands_sep) {
		decimals = Math.abs(decimals) + 1 ? decimals : 2;
		dec_point = dec_point || '.';
		thousands_sep = thousands_sep || ',';
	
		var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
		var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
		return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + 
				(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
	}
});

var Site = new Class ({

	initialize : function() {

		this.init();
		this.prepareAddToFav();
		this.prepareSearch();
		this.prepareQuantityItems();
		this.prepareGallery();
		this.prepareAdoToBasketRegular();
		this.prepareViewProductCart();
		this.prepareOrderForms();
		this.prepareNewsletterSwitch();
		this.userModalsShow();
		this.prepareContactUs();
		this.prepareForceShow();
		this.prepareOrderRate();
		this.prepareOrderRateForm();
		this.prepareShipmentsActions();
		this.attachLinksToProductPhotos();
		this.correctHomeSectionHeight();
		this.prepareReferUs();
		this.prepareReferProduct();
		this.preparMenuOverlays();
		this.printInfo();
		this.showMoreInfo();
		this.prepareIndexAnim();
		this.noBirthDate();
		this.prepareUserComments();
	},
	init: function() {
		this.myMask = new Mask(document.body, {
			'hideOnClick': false
		});
		this.modalWindow = $('fb-modal');
		this.ajaxResponseTarget = $('modal_ajax_request_response');

		var mySmoothScroll = new Fx.SmoothScroll({
			links: '.smoothAnchors',
			wheelStops: false
		});

		if($defined(this.modalWindow)) {
			this.modalWindow.setStyles({
				opacity:0,
				display:'block'
			});
		}
	},
	prepareIndexAnim: function() {

		if($defined($('d1_h')))
			var dh1 = new noobSlide({
				box: $('d1_h'),
				items: $$('#d1_h .image_holder'),
				size: 326,
				fx: 4000,
				interval: 5000,
				autoPlay: true,
				fade: true
			});

		if($defined($('d2_h')))
			var dh1 = new noobSlide({
				box: $('d2_h'),
				items: $$('#d2_h .image_holder'),
				size: 326,
				fx: 4000,
				interval: 5000,
				autoPlay: true,
				fade: true
			});

		if($defined($('d3_h')))
			var dh3 = new noobSlide({
				box: $('d3_h'),
				items: $$('#d3_h .image_holder'),
				size: 326,
				fx: 4000,
				interval: 5000,
				autoPlay: true,
				fade: true
			});

		if($defined($('d4_h')))
			var dh4 = new noobSlide({
				box: $('d4_h'),
				items: $$('#d4_h .image_holder'),
				size: 326,
				fx: 4000,
				interval: 5000,
				autoPlay: true,
				fade: true
			});

	},
	prepareAddToFav: function() {
		if($defined($('fav'))) {
			var aURL ='http://www.inspirujacydom.pl';
			var aTitle ='www.InspirujacyDom.pl';

			$('fav').addEvents({

				'click': function(event) {
					var event = new Event(event);
					event.preventDefault();
					if (document.all) {
						window.external.AddFavorite(aURL,aTitle);
					} else {
						if(window.sidebar) {
							window.sidebar.addPanel(aTitle,aURL,"");
						} /* else call bookmark for NS&FF */
					}
					return false;
				}
			});
		}
	},
	prepareQuantityItems: function() {
		$$('.quantity').each( function(item) {

			var quantity_input = item;
			if(quantity_input.getAttribute('type') !== 'hidden') {
				var div_outer = new Element('div', {
					'class':'enhanced_number_input'
				});

				var div_minus = new Element('div', {
					'class':'minus',
					'events': {
						'click': function() {
							if(quantity_input.value > 0) {
								quantity_input.set('value', parseInt(quantity_input.get('value')) - 1);
							}
						}
					}
				});

				var div_plus = new Element('div', {
					'class':'plus',
					'events': {
						'click': function() {
							quantity_input.set('value', parseInt(quantity_input.get('value')) + 1);
						}
					}
				});

				div_outer.inject(quantity_input, 'after');
				div_outer.adopt(quantity_input);
				div_minus.inject(quantity_input, 'before');
				div_plus.inject(quantity_input, 'after');

				quantity_input.addEvent('change', function() {

					if (isNaN(parseInt(this.get('value'))))
						this.set('value',1);
					this.set('value',parseInt(this.get('value')));
				});
			}

		});
	},
	prepareGallery: function() {
		if($defined($('myGallery'))) {

			var myGallery = new gallery($('myGallery'), {
				timed: false,
				defaultTransition: "crossfade",
				showArrows: false,
				useExternalCarousel: true,
				carouselElement: $('carousel'),
				carouselHorizontal: false,
				thumbHeight: 90,
				thumbWidth: 90,
				activateCarouselScroller: true,
				showCarouselLabel: false,
				textPreloadingCarousel: "Wczytuję...",
				thumbIdleOpacity: 0.7,
				fadeDuration: 300
			});

			
			var slf = this;
				
			var myGalleryPopup = new gallery($('myGalleryPopup'), {
				timed: false,
				defaultTransition: "crossfade",
				showArrows: false,
				useExternalCarousel: true,
				carouselElement: $('carousel_popup'),
				carouselHorizontal: false,
				thumbHeight: 90,
				thumbWidth: 90,
				activateCarouselScroller: true,
				showCarouselLabel: false,
				textPreloadingCarousel: "Wczytuję...",
				thumbIdleOpacity: 0.7,
				fadeDuration: 300
			});
				
			$$('#myGallery a.open').addEvent('click',function(event){
				event.preventDefault();
				slf.myMask.show();
				$('gallery_popup').setStyle('display','block');
			});
			
			function closeGalleryPopup(event){
				event.preventDefault();
				slf.myMask.hide();
				$('gallery_popup').setStyle('display','none');
			}
			
			$$('#myGalleryPopup a.open').addEvent('click',closeGalleryPopup);
			$$('#gallery-close-x').addEvent('click',closeGalleryPopup);
			document.addEvent('keydown',function(event){
				if(event.code == 27){//Escape
					closeGalleryPopup(event);
				}
			});
		}
		

	},
	
	questionFromSubmit: function(event) {

		var slf= this;

		//	event.preventDefault();
		new Event(event).preventDefault();

		$('contact_form_q').set('send', {
			onRequest: function() {
				slf.ajaxResponseTarget.set('spinner', {
					msg: 'proszę czekać...'
				});
				slf.modalWindow.getElement('.dialog_summary').set('text','Trwa przetwarzanie... proszę czekać...');
				slf.ajaxResponseTarget.get('spinner').show(true);
			},
			onSuccess: function(resp) {

				if(resp) {
					var responseJSON = JSON.decode(resp);
				}

				slf.modalWindow.getElement('.dialog_summary').set('text',responseJSON.box_title);
				slf.ajaxResponseTarget.set('html',responseJSON.box_info);

				if(responseJSON.auto_hide == 1) {
					(function() {
						slf.ajaxResponseTarget.empty();
						slf.modalWindow.fade('out');
						slf.myMask.hide();
					}).delay(1800);
				}
			},
			onFailure: function() {
				slf.modalWindow.getElement('.dialog_summary').set('text','Wystąpił błąd...');
				slf.myMask.hide();
				slf.modalWindow.fade('out');
			},
			onComplete: function(response) {
				slf.ajaxResponseTarget.get('spinner').hide(true);
			}
		});

		$('contact_form_q').send();
	},
	prepareContactUs: function() {

		var slf= this;

		if($defined($('contact_form_container'))) {

			var cform =	$('contact_form_container').getElement('form');

			$('contact_form_container').getElement('.btn_anuluj').addEvent('click', function(event) {
				event.preventDefault();

				slf.myMask.hide();
				slf.modalWindow.fade('out');
				//$('contact_form_container').adopt(slf.ajaxResponseTarget.getElement('form').clone(true,true));
			});
			
			slf.modalWindow.getElement('#fb-close-x').addEvent('click', function(event) {
				event.preventDefault();
				slf.modalWindow.fade('out');
				slf.myMask.hide();
				$('fb-close-x').removeEvent('click');
			});
			
			document.addEvent('keydown',function(event){
				var visibility = slf.modalWindow.getStyle('visibility');
				if( visibility=='visible' && event.code==27){//Escape
					event.preventDefault();
					slf.modalWindow.fade('out');
					slf.myMask.hide();
				}
			});
			
			
			
			
			$$('.show_contact_form').addEvent('click', function(event) {

				//	event.preventDefault();
				new Event(event).preventDefault();

				cform.setAttribute('id', 'cfold');

				slf.myMask.show();
				slf.modalWindow.getElement('.dialog_summary').set('html','Skontaktuj się z nami');
				slf.ajaxResponseTarget.empty();
				slf.modalWindow.fade('in');

				var clonedform = $('contact_form_container').getElement('form').clone();

				clonedform.setAttribute('id','contact_form_q');

				clonedform.inject(slf.ajaxResponseTarget);

			});
		}

	},
	referusFormSubmit: function(event) {

		var slf= this;

		//	event.preventDefault();
		new Event(event).preventDefault();

		$('referus_form_q').set('send', {
			onRequest: function() {
				slf.ajaxResponseTarget.set('spinner', {
					msg: 'proszę czekać...'
				});
				slf.modalWindow.getElement('.dialog_summary').set('text','Trwa przetwarzanie... proszę czekać...');
				slf.ajaxResponseTarget.get('spinner').show(true);
			},
			onSuccess: function(resp) {

				if(resp) {
					var responseJSON = JSON.decode(resp);
				}

				slf.modalWindow.getElement('.dialog_summary').set('text',responseJSON.box_title);
				slf.ajaxResponseTarget.set('html',responseJSON.box_info);

				if(responseJSON.auto_hide == 1) {
					(function() {
						slf.ajaxResponseTarget.empty();
						slf.modalWindow.fade('out');
						slf.myMask.hide();
					}).delay(1800);
				}
			},
			onFailure: function() {
				slf.modalWindow.getElement('.dialog_summary').set('text','Wystąpił błąd...');
				slf.myMask.hide();
				slf.modalWindow.fade('out');
			},
			onComplete: function(response) {
				slf.ajaxResponseTarget.get('spinner').hide(true);
			}
		});

		$('referus_form_q').send();
	},
	prepareReferUs: function() {

		var slf= this;

		if($defined($('referus_form_container'))) {

			var cform =	$('referus_form_container').getElement('form');

			$('referus_form_container').getElement('.btn_anuluj').addEvent('click', function(event) {
				event.preventDefault();

				slf.myMask.hide();
				slf.modalWindow.fade('out');
				//$('contact_form_container').adopt(slf.ajaxResponseTarget.getElement('form').clone(true,true));
			});
			
			slf.modalWindow.getElement('#fb-close-x').addEvent('click', function(event) {
				event.preventDefault();
				slf.modalWindow.fade('out');
				slf.myMask.hide();
				$('fb-close-x').removeEvent('click');
			});
			
			document.addEvent('keydown',function(event){
				var visibility = slf.modalWindow.getStyle('visibility');
				if( visibility=='visible' && event.code==27){//Escape
					event.preventDefault();
					slf.modalWindow.fade('out');
					slf.myMask.hide();
				}
			})
			
			$$('.show_referus_form').addEvent('click', function(event) {

				event.preventDefault();

				cform.setAttribute('id', 'cfold');

				slf.myMask.show();
				slf.modalWindow.getElement('.dialog_summary').set('html','Poleć nas znajomym');
				slf.ajaxResponseTarget.empty();
				slf.modalWindow.fade('in');

				var clonedform = $('referus_form_container').getElement('form').clone();

				clonedform.setAttribute('id','referus_form_q');

				clonedform.inject(slf.ajaxResponseTarget);

			});
		}

	},
	referProductFormSubmit: function(event) {

		var slf= this;

		//	event.preventDefault();
		new Event(event).preventDefault();

		$('referproduct_form_q').set('send', {
			onRequest: function() {
				slf.ajaxResponseTarget.set('spinner', {
					msg: 'proszę czekać...'
				});
				slf.modalWindow.getElement('.dialog_summary').set('text','Trwa przetwarzanie... proszę czekać...');
				slf.ajaxResponseTarget.get('spinner').show(true);
			},
			onSuccess: function(resp) {

				if(resp) {
					var responseJSON = JSON.decode(resp);
				}

				slf.modalWindow.getElement('.dialog_summary').set('text',responseJSON.box_title);
				slf.ajaxResponseTarget.set('html',responseJSON.box_info);

				if(responseJSON.auto_hide == 1) {
					(function() {
						slf.ajaxResponseTarget.empty();
						slf.modalWindow.fade('out');
						slf.myMask.hide();
					}).delay(1800);
				}
			},
			onFailure: function() {
				slf.modalWindow.getElement('.dialog_summary').set('text','Wystąpił błąd...');
				slf.myMask.hide();
				slf.modalWindow.fade('out');
			},
			onComplete: function(response) {
				slf.ajaxResponseTarget.get('spinner').hide(true);
			}
		});

		$('referproduct_form_q').send();
	},
	prepareReferProduct: function() {

		var slf= this;

		if($defined($('referproduct_form_container'))) {

			var cform =	$('referproduct_form_container').getElement('form');

			$('referproduct_form_container').getElement('.btn_anuluj').addEvent('click', function(event) {
				event.preventDefault();

				slf.myMask.hide();
				slf.modalWindow.fade('out');
				//$('contact_form_container').adopt(slf.ajaxResponseTarget.getElement('form').clone(true,true));
			});
			
			slf.modalWindow.getElement('#fb-close-x').addEvent('click', function(event) {
				event.preventDefault();
				slf.modalWindow.fade('out');
				slf.myMask.hide();
				$('fb-close-x').removeEvent('click');
			});
			
			document.addEvent('keydown',function(event){
				var visibility = slf.modalWindow.getStyle('visibility');
				if( visibility=='visible' && event.code==27){//Escape
					event.preventDefault();
					slf.modalWindow.fade('out');
					slf.myMask.hide();
				}
			});
			
			$$('.show_referproduct_form').addEvent('click', function(event) {

				//	event.preventDefault();
				new Event(event).preventDefault();

				cform.setAttribute('id', 'cfold');

				slf.myMask.show();
				slf.modalWindow.getElement('.dialog_summary').set('html','Poleć ten produkt');
				slf.ajaxResponseTarget.empty();
				slf.modalWindow.fade('in');

				var clonedform = $('referproduct_form_container').getElement('form').clone();

				clonedform.setAttribute('id','referproduct_form_q');

				clonedform.inject(slf.ajaxResponseTarget);

			});
		}

	},
	prepareViewProductCart: function() {

		var slf= this;

		var add_to_basket_product_types = $$('.add_to_basket_product_types');

		if(add_to_basket_product_types.length) {

			$$('.add_to_basket_product_types a').each( function(item,index) {
				if(!$defined(item.getParent().getElement('div.enhanced_number_input'))) {
					item.addClass('adb_im_btn');
					item.store('tip:title', '');
					item.store('tip:text', 'Zobacz rodzaje produktu');
				}
			});
			var tipz = new Tips('.adb_im_btn');
		}

	},
	prepareAdoToBasketRegular: function() {

		var slf= this;

		var add_to_basket_forms = $$('.add_to_basket_form');

		if(add_to_basket_forms.length) {

			$$('.add_to_basket_form input[type=image]').each( function(item,index) {
				if(!$defined(item.getParent().getElement('div.enhanced_number_input'))) {
					item.addClass('adb_im_btn');
					item.store('tip:title', '');
					item.store('tip:text', 'Dodaj do koszyka');
				}
			});
			var tipz = new Tips('.adb_im_btn');

			//	slf.myMask.setModalOptions({'hideOnClick': false, modalStyle:{'background-color':'#8FD0EA', opacity:0.5,'z-index':300}});

			$$('.add_to_basket_form').addEvent('submit', function(event) {

				//	event.preventDefault();
				new Event(event).preventDefault();

				this.set('send', {
					onRequest: function() {
						slf.modalWindow.getElement('.dialog_summary').set('text','Aktualizacja koszyka');
						slf.myMask.show();
						slf.modalWindow.fade('in');

						if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
							$('ajax_basket').spin();

						new Element('div', {
							'class':'wait_anim'
						}).inject(slf.ajaxResponseTarget.empty());
					},
					onComplete: function(resp) {

						if(resp) {
							var responseJSON = JSON.decode(resp);

							slf.ajaxResponseTarget.set('html',responseJSON.modal_response);
							$('ajax_basket').empty().set('html',responseJSON.basket_response);

							$('dialog_buttons_panel').slide('hide');

							if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
								$('ajax_basket').unspin();
								
							$('fb-close').addEvent('click', function() {
								slf.modalWindow.fade('out');
								slf.myMask.hide();
							});
							
							$('fb-close-x').addEvent('click', function() {
								slf.modalWindow.fade('out');
								slf.myMask.hide();
							});
					
							document.addEvent('keydown',function(event){
								var visibility = slf.modalWindow.getStyle('visibility','visible');
								if( visibility=='visible' && event.code==27){//Escape
									slf.modalWindow.fade('out');
									slf.myMask.hide();
								}
							});
						
						}
					},
					onSuccess: function() {
						$('dialog_buttons_panel').slide('in');
						if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
							$('ajax_basket').unspin();

					},
					onError: function() {
						$('dialog_buttons_panel').slide('in');
						if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
							$('ajax_basket').unspin();
					}
				}).send();

			});
		}
	},
	prepareOrderForms: function() {
		this.prepareCompanySwitch();
		this.prepareInvoiceSwitch();
		this.preparePasswordSwitch();

		if($defined($('receiver_name'))) {
			$('receiver_name').addEvent('keyup', function() {
				$('contact_name').set('value',this.value)
			});
		}

		if($defined($('receiver_company'))) {
			$('receiver_company').addEvent('keyup', function() {
				$('invoice_company_name').set('value',this.value)
			});
		}

		if($defined($('receiver_phone'))) {
			$('receiver_phone').addEvent('keyup', function() {
				$('contact_phone').set('value',this.value)
			});
		}
	},
	
	prepareCompanySwitch: function(){
		var switchCompany = $('switch_company_name');
		if(switchCompany){
			
			var switchContent = $('receiver_company');
									
			switchCompany.addEvent('click', function() {
				if(switchCompany.checked === true) {
					switchContent.setStyle('display','block');
				} else {
					switchContent.setStyle('display','none')
				}
			});
		}
	},

	prepareInvoiceSwitch: function() {
		var invoiceAddressTheSame = $('invoice_address_the_same');
		if(invoiceAddressTheSame){
			
			var switchContent = $('invoice_address_special');
			invoiceAddressTheSame.addEvent('click', function() {
				if(invoiceAddressTheSame.checked === false) {
					switchContent.setStyle('display','block');
				} else {
					switchContent.setStyle('display','none')
				}
			});
		}	
	},

	preparePasswordSwitch: function() {
		var hasAccountYes = $('has_account_yes');
		var hasAccountNo = $('has_account_no');
		if(hasAccountYes && hasAccountNo){
			
			var switchContent = $('password');
			
			if(hasAccountNo.checked === true) {
				switchContent.setStyle('display','none');
			}
									
			hasAccountYes.addEvent('click', function() {
				if(hasAccountYes.checked === true) {
					switchContent.setStyle('display','block');
					
					var passwordError = $$('.password_error');
					if(passwordError){
						passwordError.setStyle('display','block');
					}
				}
			});
			
			hasAccountNo.addEvent('click', function() {
				if(hasAccountNo.checked === true) {
					switchContent.setStyle('display','none');
					
					var passwordError = $$('.password_error');
					if(passwordError){
						passwordError.setStyle('display','none');
					}
				}
			});
		}	
	},
	
	prepareNewsletterSwitch: function() {

		var noMessages = $('no_messages');

		$$('input[name="grupa[]"]').each(function(el) {
					
			if(noMessages.checked === true){
				el.disabled = true;
			} else {
				el.disabled = false;
			}
			
		});

		if(noMessages){
			noMessages.addEvent('click', function(){
			
				$$('input[name="grupa[]"]').each(function(el) {
					
					if(noMessages.checked === true){
						el.disabled = true;
					} else {
						el.disabled = false;
					}
					
				});
			});
		}
		
		var form = $$('.summary form');
		
		form.addEvent('submit', function(){
			$$('input[type="checkbox"]').each(function(el) {
				el.disabled = false;
			});
		});
	},
	
	userModalsShow: function() {

		var slf= this;

		if($$('.modal_show').length) {

			$$('.modal_show').each( function(item) {

				item.addEvent('click', function(event) {
					new Event(event).preventDefault();
					slf.myMask.show();
					slf.modalWindow.getElement('.dialog_summary').set('text','Zmiana danych');
					slf.ajaxResponseTarget.empty();
					slf.modalWindow.fade('in');
					slf.ajaxResponseTarget.adopt($(item.getAttribute('rel')).getElement('form'));
					
					slf.modalWindow.getElement('.anuluj_btn').addEvent('click', function() {
						slf.modalWindow.fade('out');
						$(item.getAttribute('rel')).adopt(slf.ajaxResponseTarget.getElement('form'));
						slf.myMask.hide();
					});
				});
			
			});
			
			slf.modalWindow.getElement('#fb-close-x').addEvent('click', function(event) {
				event.preventDefault();
				var rel = slf.modalWindow.getElement('form').getAttribute('rel');
				slf.modalWindow.fade('out');
				$(rel).adopt(slf.ajaxResponseTarget.getElement('form'));
				slf.myMask.hide();
				$('fb-close-x').removeEvent('click');
			});
			
			document.addEvent('keydown',function(event){
				var visibility = slf.modalWindow.getStyle('visibility');
				if( visibility=='visible' && event.code==27){//Escape
					event.preventDefault();
					var rel = slf.modalWindow.getElement('form').getAttribute('rel');
					slf.modalWindow.fade('out');
					$(rel).adopt(slf.ajaxResponseTarget.getElement('form'));
					slf.myMask.hide();
				}
			});
			
		}

	},
	
	prepareForceShow: function() {
		if($$('.force_show').length) {

			$$('.force_show').each( function(item) {

				var rel = item.getAttribute('id');

				$('content').getElements('.modal_show[rel='+rel+']').fireEvent('click');
			});
		}
	},
	
	prepareShipmentsActions: function() {
		if($defined($('shipment_types'))) {
			$('order_cost').store('cost',$('order_cost').getAttribute('alt').toFloat());

			$$('#shipment_types input[type=radio]').each( function(item) {
				item.store('additional_cost',item.getAttribute('alt').toFloat()).removeAttribute('alt');
				item.addEvent('change', function() {
					if(item.checked==true) {
						$('current_shipment_cost').set('text',(item.retrieve('additional_cost')==0)?'gratis':item.retrieve('additional_cost').toFixed(2).replace('.',',') + ' zł').getParent('li').highlight('#FFF7C0');
						$('current_order_total').set('text',(item.retrieve('additional_cost')+$('order_cost').retrieve('cost')).toFixed(2).replace('.',',') + ' zł').getParent('li').highlight('#FFF7C0');
					}
				});
			});
		}
	},
	attachLinksToProductPhotos: function() {
		$$('.product-list .product').each( function(item) {

			var photo = item.getElement('.photo');
			photo.set('style','cursor:pointer');
			var plink = item.getElement('.desc h5 a');
			var href = plink.href;

			photo.addEvent('click', function() {
				window.location = href;
			});
		});
	},
	correctHomeSectionHeight: function() {

		if($$('.sp_i').length) {

			var sp = $$('.section_presentation .subcategories');

			if(sp[0].getSize().y > sp[1].getSize().y) {
				sp[1].setStyle('height',(sp[0].getSize().y-16)+'px');
			} else {
				sp[0].setStyle('height',(sp[1].getSize().y-16)+'px');
			}

			if(sp[2].getSize().y > sp[3].getSize().y) {
				sp[3].setStyle('height',(sp[2].getSize().y-16)+'px');
			} else {
				sp[2].setStyle('height',(sp[3].getSize().y-16)+'px');
			}

		}
	},
	printInfo: function() {
		if($defined($('print_info'))) {

			$('print_info').addEvent('click', function(event) {

				var event = new Event(event);
				event.preventDefault();

				var okno_sz = 900;
				var okno_w = 600;
				var polecokno = window.open(this.href, 'drukowanie', 'toolbar=no,menubar=no,scrollbars=yes,resizable=no,height='+okno_w+',width='+okno_sz+'' );
				var widthR = window.getWidth();
				var heightR = window.getHeight();

				posx = widthR/2 - (okno_sz/2);
				posy = heightR/2 - (okno_w/2);

				polecokno.moveTo(posx, posy);
				polecokno.focus();
			});
		}
	},
	form_question_cancel : function() {
		var slf = this;
		$('fb-modal').getElement('.dialog_summary').set('text','');
		$('modal_ajax_request_response').empty();
		$('fb-modal').fade('out');
		slf.myMask.hide();

	},
	form_referus_cancel : function() {
		var slf = this;
		$('fb-modal').getElement('.dialog_summary').set('text','');
		$('modal_ajax_request_response').empty();
		$('fb-modal').fade('out');
		slf.myMask.hide();

	},
	form_referproduct_cancel : function() {
		var slf = this;
		$('fb-modal').getElement('.dialog_summary').set('text','');
		$('modal_ajax_request_response').empty();
		$('fb-modal').fade('out');
		slf.myMask.hide();

	},
	prepareSearch: function() {

		$('top_search_btn').addEvent('click', function(event) {
			event.preventDefault();

			$('top_search_box').submit();

		});
		$('top_search_box_text').addEvent('keypress', function(e) {
			if(e.key == 'enter' && this.value.length < 3) {
				e.stop();
				this.focus();
			}
		});
	},
	prepareOrderRate: function() {
		$$('.rate').each( function(element,i) {
			element.addEvent('click', function(event) {
				event.preventDefault();
				var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar'];
				myStyles.each( function(myStyle) {
					if(element.getParent().hasClass(myStyle)) {
						element.getParent().removeClass(myStyle)
					}
				});
				myStyles.each( function(myStyle, index) {
					if(index == element.getAttribute("rel")) {
						element.getParent().toggleClass(myStyle);
						$(element.getParent().id + '_h').set('value',element.getAttribute("rel"));
					}
				});
			});
		});
	},
	prepareOrderRateForm: function() {

		if($defined($('rate_us'))) {

			$('odp1').addEvent('blur', function() {
				if($('odp1').get('value').trim() != '' && $('rt1_h').value == 0) {
					alert('Pamiętaj o zaznaczeniu ilości gwiazdek');
				}
			});
			$('odp2').addEvent('blur', function() {
				if($('odp2').get('value').trim() != '' && $('rt2_h').value == 0) {
					alert('Pamiętaj o zaznaczeniu ilości gwiazdek');
				}
			});
			$('odp3').addEvent('blur', function() {
				if($('odp3').get('value').trim() != '' && $('rt3_h').value == 0) {
					alert('Pamiętaj o zaznaczeniu ilości gwiazdek');
				}
			});

			$('opinia_rez').addEvent('click', function(event) {

				event.preventDefault();

				var myRequest = new Request({
					method: 'get',
					url: this.href,
					onRequest: function() {
						if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
							$('rate_us').spin();
					},
					onSuccess: function(response) {
						if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
							$('rate_us').unspin();
						$('leave_us_comment').set('html',response);

					}
				});
				myRequest.send();

			});
			$('rate_us').addEvent('submit', function(event) {
								
				//	event.preventDefault();
				new Event(event).preventDefault();

				if( $('rt1_h').value > 0 ||  $('rt2_h').value > 0 ||  $('rt3_h').value > 0 ) {

					this.set('send', {
						onRequest: function() {
							if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
								$('rate_us').spin();
						},
						onSuccess: function(response) {
							if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
								$('rate_us').unspin();
							$('leave_us_comment').set('html',response);

						},
						onError: function() {
							if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4))
								$('rate_us').unspin();
						}
					}).send();

				} else {
					alert('Formularz nie został wypełniony, jeżeli nie chcesz podzielić się z nami swoimi spostrzeżeniami, kliknij przycisk "Nie chcę wyrażać opinii"');
				}

			});
		}
	},
	preparMenuOverlays: function() {
		$$('#main-menu li').each( function(item,index) {

			if(index < 4) {

				if(item.get('id') == 'current_menu') {
					item.addClass('current_temp');
				}

				item.addEvent('mouseenter', function() {

					var ttt = $('current_menu');

					if($defined(ttt) && ttt != item) {
						ttt.id = 'current_menu_t';
					}

					$$('.menu_overlay').setStyle('display','none');
					item.getElement('a').addClass(item.get('class')+'overlayed');
					$('m'+(index + 1) +'_overlay').setOpacity(0.99).setStyle('display','block');
				});
				item.addEvent('mouseleave', function() {
					item.getElement('a').removeClass(item.get('class')+'overlayed');
					$$('.menu_overlay').setStyle('display','none');

					var ttt = $('main-menu').getElement('.current_temp');

					if($defined(ttt)) {
						ttt.id = 'current_menu';
					}

				});
			}

		}
		);
	},
	showMoreInfo: function() {
		if($defined($('show_more_description')) && $defined($('more_description_box'))) {

			$('show_more_description').addEvent('click', function(event) {
				event.preventDefault();
				if($('more_description_box').hasClass('swd_hidden')) {
					$('show_more_description').empty().set('html','mniej...');
					$('more_description_box').removeClass('swd_hidden');
				} else {
					$('show_more_description').empty().set('html','więcej...');
					$('more_description_box').addClass('swd_hidden');
				}

			});
		}
	},
	noBirthDate: function() {
		if($defined($('newsletter_page_form'))) {
			if($('no_birth_date').checked == true) {
				$('birth_day').disabled = true;
				$('birth_month').disabled = true;
			} else {
				$('birth_day').disabled = false;
				$('birth_month').disabled = false;
			}

			$('no_birth_date').addEvent('click', function() {
				if(this.checked == true) {
					$('birth_day').disabled = true;
					$('birth_month').disabled = true;
				} else {
					$('birth_day').disabled = false;
					$('birth_month').disabled = false;
				}
			})
		}
	},
	prepareUserComments: function() {
		
		var content = $('opinie-content');
		var scrollbar = $('opinie-scrollbar');
		var handle = $('opinie-handle');
		
		content.setStyle('overflow','hidden');
		content.setStyle('width','223px');
		scrollbar.setStyle('display','block');
				
		var steps = content.getScrollSize().y - content.getSize().y;

		var slider = new Slider(scrollbar, handle, {	
			steps: steps,
			mode: 'vertical',
			onChange: function(step){
				// Scrolls the content element in x or y direction.
				var x = 0;
				var y = step;
				content.scrollTo(x,y);
			}
		}).set(0);

		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
		
		// Stops the handle dragging process when the mouse leaves the document body.
		$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
	}
});

window.addEvent('domready', function() {
	IDsite = new Site();
});
var questionFromSubmit = function(event) {
	IDsite.questionFromSubmit(event);
};
var form_question_cancel = function() {
	IDsite.form_question_cancel();
}
var referusFormSubmit = function(event) {
	IDsite.referusFormSubmit(event);
};
var form_referus_cancel = function() {
	IDsite.form_referus_cancel();
}
var referProductFormSubmit = function(event) {
	IDsite.referProductFormSubmit(event);
};
var form_referproduct_cancel = function() {
	IDsite.form_referproduct_cancel();
}
function prepare_window(object,button,overlay) {

	button.addEvent('click', function() {

		object.setStyle('display','block').setStyle('visibility','visible').setStyle('top',$(window).getScroll().y + 50).setStyle('left',($(window).getSize().x - 500) / 2);

	}.bind(this));
	object.getElement('.m_close').addEvent('click', function() {

		object.setStyle('display','none');

	});
}

window.addEvent('load', function() {
	new OverText($('top_search_box_text'));
});
