(function($) {
		$(document).ready(function(){
				$("#scroller").simplyScroll({
					autoMode: 'loop',
					pauseOnHover: false,
					frameRate: 30
				});
				var	decor	=	{
						$sideForm:			null,
						$callDesigner:	null,
						$calculator:		null,
						$calculate:			null,
						$closeCalc:			null,
						$gray:					null,
						$lightbox:			null,
						init:function(){
								$sideForm	=		$('.wpcf7');
								$lightbox	=		$('.lightbox');
								$gray	=		$('.gray');
								$calculator	=		$('.calculator').click(function(){
										$gray.fadeIn(200,function(){
												$lightbox.css('margin-top','-'+$lightbox.height()/2+'px').fadeIn(200);
										});
								});
								$calculate = $('.btn:not(.send)',$lightbox).click(function(){
									if(decor.calculator.validate())
										decor.calculator.init();	
								});
								$callDesigner	=	$('.call-designer').click(function(){
										$sideForm.slideToggle(300);
								});
								$closeCalc = $('.close',$lightbox).click(function(){
										decor.calculator.closeCalc();
								});
								
								this.calculator.checkForInputKeyPress();	
								this.calculator.preCommitValidation();

						},
						calculator:{
								cornice:				null,
								corniceWidth:		null,
								corniceHeight:	null,
								corniceStyle:		null,
								priceRange:			null,
								numberOfPanels:	null,
								$corniceWidth:	null,
								$corniceHeight:	null,
								$corniceStyle:	null,
								$cornice:				null,
								$price:					null,
								$submit:				null,
								$form:					null,
								$calculate:			null,
								$numOfPanels:		null,
								$miniForm:			null,
								init:function(){
										$corniceWidth		=	$('[name=width-karniz]',$lightbox);
										$corniceHeight	=	$('[name=height-karniz]',$lightbox);
										$corniceStyle		=	$('.radio-297	input:checked',$lightbox);
										$cornice				=	$('[menu-99] option:selected',$lightbox);
										$price					=	$('.radio-957	input:checked',$lightbox);
										$form						=	$('form',$lightbox);
										$calculate			= $('span.btn',$lightbox);
										$numOfPanels		= $('.numberOfPanels',$lightbox);
										$miniForm				= $('.miniform',$lightbox);
										this.setParameters();
										
								},
								validate:function(){
									if(
										((!!$('[name=width-karniz]').val() && !!$('[name=height-karniz]').val())
											&& $('.numberOfPanels').is(':visible') && !!$('.numberOfPanels').val() && $('.numberOfPanels').val() !== $('.numberOfPanels').attr('title'))
										 	|| $('.numberOfPanels').is(':hidden'))
										 {
											return true;
									} else {
										return false;
									}
								},
								preCommitValidation:function(){
									$('.radio-297 input').click(function(){
										var style = decor.calculator.returnTranslatedStyle(this.value);
										
										if(style === 'japanese'){
											$('.numberOfPanels').show();
										} else {
											$('.numberOfPanels').hide();
										}
										
										if(style === 'classic'){
											$('.menu-99').hide();
										} else {
											$('.menu-99').show();
										}
										
									});
								},
								fixInputKeys:function(value){
									value = value.replace(',','.');
									return value;
								},
								checkForInputKeyPress:function(){
									$('input[type=text]', $lightbox).keyup(function(e){
										if(e.keyCode === 188){
											this.value = decor.calculator.fixInputKeys(this.value);
										}
									});	
								},
								showContactForm:function(message){
										$form.slideUp(200);
										$lightbox.prepend('<p class="message">'+message+'</p>');						
								},
								setParameters:function(){
										cornice	=	$cornice.index() ==	0	?	true : false;
										corniceWidth = parseFloat(this.fixInputKeys($corniceWidth.val()));
										corniceHeight	=	parseFloat(this.fixInputKeys($corniceHeight.val()));
										corniceStyle = $corniceStyle.val();
										numberOfPanels = parseFloat($numOfPanels.val());
										priceRange = $price.parent('span').index() == 0 ? 'economy' : ($price.parent('span').index() == 1 ? 'middle' : 'premium');
										this.checkIfBig();
								},
								returnTranslatedStyle:function(style){
										switch(style)
										{
										case window.corniceNames.classic:
										  return style = 'classic';
										  break;
										case window.corniceNames.roman:
										  return style = 'roman';
										  break;
										case window.corniceNames.french:
										  return style = 'french';
										  break;
										case window.corniceNames.japanese:
										  return style = 'japanese';
										  break;
										}
								},
								checkIfBig:function(){
										corniceStyle = this.returnTranslatedStyle(corniceStyle);
										var message = window.message[corniceStyle];
										var toBig = false;
										if((corniceStyle == 'classic' && corniceHeight > 2.9) || (corniceStyle == 'roman' && corniceWidth > 1.3) || (corniceStyle == 'french' && corniceHeight > 1.5) || (corniceStyle == 'japanese' && corniceWidth > 1.2)){
											toBig = true;			
										} else {
											toBig = false;
										}
										if(!!toBig){
												this.showContactForm(message);	
										} else {
												var finalPrice = Math.round(this.calculate[corniceStyle][priceRange]());
												this.showMiniForm(corniceStyle,cornice,finalPrice);
										}			
								},
								calculate:{
										classic:{
												economy:function(){
														return 128.695 * corniceWidth + 60.442 * corniceWidth * corniceHeight + 53.721 * corniceHeight + 84.7476;//33.5*(2.5*corniceWidth +0.2) +93.2*(corniceWidth/0.6 +1)*(corniceHeight+0.2);	
														
												},
												middle:function(){
														return 203.125 * corniceWidth + 92.592 * corniceWidth * corniceHeight + 82.296 * corniceHeight + 97.263;//68.5*(2.5*corniceWidth +0.2) +133.2*(corniceWidth/0.6 +1)*(corniceHeight +0.2);
												},
												premium:function(){
														return 327.843 * corniceWidth + 176.182 * corniceWidth * corniceHeight + 156.591 * corniceHeight + 122.922;//158.5*(2.5*corniceWidth +0.2) +173.2*(corniceWidth/0.6 +1)*(corniceHeight +0.2);	
												}	
										},
										roman:{
												economy:function(){
														if(!cornice){
																return 68*(corniceHeight +0.2) +32*(corniceWidth*corniceHeight);	
														} else {
																return 68*(corniceHeight +0.2) +32*(corniceWidth*corniceHeight) +129*corniceWidth;	
														}
												},
												middle:function(){
														if(!cornice){
																return 108*(corniceHeight +0.2) +32*(corniceWidth*corniceHeight);	
														} else {
																return 108*(corniceHeight +0.2) +32*(corniceWidth*corniceHeight) +129*corniceWidth;	
														}
												},
												premium:function(){
														if(!cornice){
																return 148*(corniceHeight +0.2) +32*(corniceWidth*corniceHeight);
														} else {
																return 148*(corniceHeight +0.2) +32*(corniceWidth*corniceHeight) +129*corniceWidth;	
														}	
												}											
										},
										french:{
												economy:function(){
														if(!cornice){
																return 50*(0.2*corniceHeight +0.5) +20 *(corniceWidth*corniceHeight) +9*corniceWidth;
														} else {
																return 50*(0.2*corniceHeight +0.5) +135 *(corniceWidth*corniceHeight) +9*corniceWidth;
														}	
												},
												middle:function(){
														if(!cornice){
																return 90*(0.2*corniceHeight +0.5) +20 *(corniceWidth*corniceHeight) +9*corniceWidth;
														} else {
																return 90*(0.2*corniceHeight +0.5) +135 *(corniceWidth*corniceHeight) +9*corniceWidth;
														}	
												},
												premium:function(){
														if(!cornice){
																return 130*(0.2*corniceHeight +0.5) +20 *(corniceWidth*corniceHeight) +9*corniceWidth;
														} else {
																return 130*(0.2*corniceHeight +0.5) +135 *(corniceWidth*corniceHeight) +9*corniceWidth;
														}	 	
												}
										},
										japanese:{
												economy:function(){
														if(!cornice){
																return 50*numberOfPanels* (corniceHeight+0.2) +20 *numberOfPanels +10 *corniceHeight;
														} else {
																return 50*numberOfPanels* (corniceHeight+0.2) +20 *numberOfPanels +10 *corniceHeight + 84 *corniceWidth;
														}		
												},
												middle:function(){
														if(!cornice){
																return 90*numberOfPanels* (corniceHeight+0.2) +20 *numberOfPanels +10 *corniceHeight;
														} else {
																return 90*numberOfPanels* (corniceHeight+0.2) +20 *numberOfPanels +10 *corniceHeight + 84 *corniceWidth;
														}		
												},
												premium:function(){
														if(!cornice){
																return 130*numberOfPanels* (corniceHeight+0.2) +20 *numberOfPanels +10 *corniceHeight;
														} else {
																return 130*numberOfPanels* (corniceHeight+0.2) +20 *numberOfPanels +10 *corniceHeight + 84 *corniceWidth;
														}		
												}
										}
								},
								showMiniForm:function(corniceStyle,cornice,finalPrice){
										$miniForm.before('<p class="message">'+message.details+'</p>');
										$miniForm.slideDown(200);
										this.checkAndSend(corniceStyle,cornice,finalPrice);
								},
								checkAndSend:function(corniceStyle,cornice,finalPrice,$miniForm){
										var $btnSend = $('.send',$miniForm).click(function(){
												if(!!$('input#name',$miniForm).val() && !!$('input#email',$miniForm).val()){
														var name = $('input#name',$miniForm).val();
														var email = $('input#email',$miniForm).val();
														sendForm(corniceStyle,cornice,finalPrice,name,email);	
												}
										});
								},
								closeCalc:function(){
										$lightbox.fadeOut(200,function(){
												$gray.fadeOut(200);
												decor.calculator.calcReset();
										});	
										
								},
								calcReset:function(){
										$('.message').remove();
										$('.miniform').hide();
										$('.lightbox form').show();
								}
						}
				}
				var sendForm = function(corniceStyle,cornice,finalPrice,name,email){
							cornice = !!cornice ? '-carnice' : '-no-carnice';
							cornice = corniceStyle == 'classic' ? '' : cornice;
							var $form = $('#forms .'+corniceStyle+cornice+' form');
							$('.your-name input',$form).val(name);
							$('.your-email input',$form).val(email);
							$('.your-price input',$form).val(finalPrice);
							$('.your-style input',$form).val(corniceStyle);
							$form.submit();
							decor.calculator.closeCalc();
				}
				decor.init();
				
				window.decor = decor;
		});
})(jQuery);
