;WelShop = {
	showLoader: function() {
		new Element('div')
			.set('id', 'welshop-loader-layer')
			.setStyles({
				'position': 'fixed',
				'top': 0,
				'left': 0,
				'width': window.getSize().x + 'px',
				'height': window.getSize().y + 'px',
				'background-color': '#fff',
				'opacity': '.5',
				'z-index': 50000
			})
			.inject(document.body);
		
		new Element('div')
			.set('id', 'welshop-loader')
			.setStyles({
				'position': 'fixed',
				'top': ((window.getSize().y / 2) - 8) + 'px',
				'left': ((window.getSize().x / 2) - 8) + 'px',
				'z-index': 50001
			})
			.set('html', '<img src="/images/dentwayLoader.gif" />')
			.inject(document.body);
	},
	
	activeLinks : function () {
    $$('ul li a').each( function(a) {
    	if (a.get('href') != null) {
    		if (a.get('href').length <= window.location.pathname.length) {
					if (Browser.Engine.trident) {
						//$('log').set('text', window.location.pathname)
						str = window.location.pathname;
						str = str.replace(/%D6/,'%C3%96');
						str = str.replace(/%F6/,'%C3%B6');
						str = str.replace(/%C5/,'%C3%85');
						str = str.replace(/%E5/,'%C3%A5');
						str = str.replace(/%C4/,'%C3%84');
						str = str.replace(/%E4/,'%C3%A4');						
						//$('log').set('text', $('log').get('text') + " | " + str)
					} else {
				    str = window.location.pathname;
				  }
					str = decodeURI(str).substring(0, a.get('href').length);

		    } else {
			    if (Browser.Engine.trident) {
						str = window.location.pathname;
						str = str.replace(/%D6/,'%C3%96');
						str = str.replace(/%F6/,'%C3%B6');
						str = str.replace(/%C5/,'%C3%85');
						str = str.replace(/%E5/,'%C3%A5');
						str = str.replace(/%C4/,'%C3%84');
						str = str.replace(/%E4/,'%C3%A4');
			    } else {
			    	str = window.location.pathname;
			    }
			    str = decodeURI(str);
		    }
	      if ((a.get('href') == str && str != '') 
	        || (a.get('href') == '/' && window.location.pathname == '/')) {
				if(a.get('href') == '/' && window.location.pathname.length > '1'){
		        	
	      		}
	      		else{
	      			a.getParent('li').addClass('active');
	    	    	a.getParent('li').addClass('locked');
	      		}
	      }
	    }
    });
    }
};

var fclock = false;
function fcautofilled(el) {
	if (fclock)
		return true;
	
	fclock = true;
	
	if (el.get('value') == fcDefaults['fields'][el.get('name')]) {
		el.errors.push(fcDefaults['msg']);
		
		fclock = false;
		return false;
	}
	
	fclock = false;
	return true;
}

window.addEvent('domready', function() {
	WelShop.activeLinks();
	/*$$('a').addEvent('click', function(e) {
		WelShop.showLoader();
	});*/
	
	var formhtml, infohtml;
	var currentInputValue = '';
	if ($defined($('customer-pay-details'))) {
		$('customer-pay-details').getElements('input').addEvents({
			'focus': function() {
				if (!this.hasClass('value-has-changed')) {
					currentInputValue = this.get('value');
					this.set('value', '');
				}
			},
			'blur': function() {
				if (!this.hasClass('value-has-changed')) {
					if (this.get('value') == '') {
						this.set('value', currentInputValue);
					} else {
						this.addClass('value-has-changed');
					}
				}
			}
		});
		
		new FormCheck('customer-details-form', {
			tipsClass: 'validate'
		});
	}
	
	if ($defined($('getCustomerCredentials'))) {
		$('getCustomerByBirthday').addEvent('keydown', function(e) {
			if (e.key == 'enter')
				return false;
		});
		$('getCustomerByEmail').addEvent('keydown', function(e) {
			if (e.key == 'enter')
				return false;
		});
		
		$('getCustomerCredentials').addEvent('click', function(e) {
			new Event(e).stop();
			
			new Request.JSON({
				'url': this.get('href'),
				'method': 'post',
				'data': {
					'getCustomerByEmail': $('getCustomerByEmail').value,
					'getCustomerByBirthday': $('getCustomerByBirthday').value
				},
				onRequest: function() {
					WelShop.showLoader();
				},
				onComplete: function(response) {
					$('welshop-loader').dispose();
					$('welshop-loader-layer').dispose();
					
					if (response && !response.error) {
						formhtml = $('customer-pay-details').get('html');
						
						$('json_firstName').set('value', response.firstName);//.set('disabled', 'disabled');
						$('json_street').set('value', response.address);//.set('disabled', 'disabled');
						$('json_city').set('value', response.postalCity);//.set('disabled', 'disabled');
						$('json_birthday').set('value', response.birthday);//.set('disabled', 'disabled');
						$('json_email').set('value', response.email);//.set('disabled', 'disabled');
						$('json_surname').set('value', response.surname);//.set('disabled', 'disabled');
						$('json_zip').set('value', response.postalCode);//.set('disabled', 'disabled');
						$('json_country').set('value', response.country);//.set('disabled', 'disabled');
						$('json_tel').set('value', response.tel);//.set('disabled', 'disabled');
						
						if (parseInt(response.newsletter) == 0)
							$('json_newsletter').set('checked', '');
						else
							$('json_newsletter').set('checked', 'checked');
						
						infohtml = $('customer-pay-get').get('html');
						$('customer-pay-get').removeClass('columnLogin');
						
						$('customer-pay-get').empty();
						new Element('h2')
							.set('text', 'RESET')
							.setStyle('cursor', 'pointer')
							.inject($('customer-pay-get'))
							.addEvent('click', function(e) {
								$('customer-pay-details').set('html', formhtml);
								$('customer-pay-get').set('html', infohtml);
								$('customer-pay-get').addClass('columnLogin');
							});
					}
				}
			}).send();
		});
	}
	
	$$('.startProductsBox .item').addEvents({
		mouseenter: function() {
			/*if(this.get('rel') != 'open'){
				$$('.startProductsBox .item[rel=open]').each(function(el){
	    			var closeBox = new Fx.Morph(el.getElement('.top'), {duration: 0, transition: Fx.Transitions.Sine.easeOut});
					closeBox.start({'height': 68});
					el.set('rel', '');
				});
			}*/
			//$$('.startProductsBox .item .top[rel=open]').morph({'height': '68px'});
			
			this.getElement('.top').set('morph', {duration: 400, transition: 'Cubic:in'});
			this.getElement('.top').morph({height: 130});
			this.getElement('.top').get('morph'); //The Fx.Morph instance.
			
		},
		mouseleave: function(){
			this.getElement('.top').set('morph', {duration: 300, transition: 'Cubic:out'});
			this.getElement('.top').morph({height: 68});
			this.getElement('.top').get('morph'); //The Fx.Morph instance.
		}
	});
	
	$$('.addedProduct').set('morph',{duration: 500, transition: 'Quad:out'});
	(function(){ $$('.addedProduct').morph({'height':'80px'}); }).delay(200);
	(function(){ $$('.addedProduct').morph({'height':'1px'}); }).delay(3000);
	
		
	if ($defined($('basketAmount'))) {
		var ccSet = false;
		$('payByCard').addEvent('click', function(e) {
			if (!ccSet) {
				var amount = parseFloat($('basketAmount').get('text'));
				amount    -= parseFloat($('get-cc-discount').get('text'));
				$('basketAmount').set('text', amount);
				ccSet      = true;
			}
		});
		$('payByInvoice').addEvent('click', function(e) {
			if (ccSet) {
				var amount = parseFloat($('basketAmount').get('text'));
				amount    += parseFloat($('get-cc-discount').get('text'));
				$('basketAmount').set('text', amount);
				ccSet      = false;
			}
		});
	
	
		$('payByCard').fireEvent('click', this);
	}
	
	if ($defined($('coupon-code'))) {
		var hasAddedCoupon = false;
		$('coupon-code').addEvent('focus', function(e) {
			if (!hasAddedCoupon) {
				this.set('value', '');
				hasAddedCoupon = true;
			}
		});
	}
	
	if ($defined($('show-terms'))) {
		$('show-terms').addEvent('click', function(e) {
			new Event(e).stop();
			
			window.open(this.get('href'), 'termsWindow', "toolbar=0,directories=0,menubar=0,resizeable=1,location=0,status=0,scrollbars=1,width=400,height=500");
		});
	}
	
	$$('.product-quantifier').addEvent('keydown', function(e) {
		if (e.key == 'backspace' || e.key == 'delete' || parseInt(e.key) >= 0 || parseInt(e.key) <= 9) {
			
		} else {
			return false;
		}
	});
	
	var current, url, quantity;
	$$('.product-quantifier').addEvent('keyup', function(e) {
		if (parseInt(e.key) >= 0 || parseInt(e.key) <=9) {
			var me = this;
			(function() {
				WelShop.showLoader();
				location.href = me.get('name') + '/' + me.get('value');
			}).delay(300);
		}
	});
	
	$$('.startProductsBox .item .button').addEvents({
		mouseenter: function() {
			this.toggleClass('active');
		},
		mouseleave: function() {
			this.toggleClass('active');
		}
	});
	
	$$('.middleMenu li').addEvents({
		mouseenter: function() {
			if(!this.hasClass('locked')) {
				this.toggleClass('active');
			}
		},
		mouseleave: function() {
			if(!this.hasClass('locked')) {
				this.toggleClass('active');
			}
		}, 
		click: function(){
			document.location = this.getElement('a').get('href');	
		}
	});
	
	$$('#topMenu li').addEvents({
		mouseenter: function() {
			if(!this.hasClass('locked')) {
				if(this.hasClass('left')) {
					this.toggleClass('active');
					this.getParent('div').getElement('.left').toggleClass('active');
				} else {
					this.toggleClass('active');
				}
			}
		},
		mouseleave: function() {
			if(!this.hasClass('locked')) {
				if(this.hasClass('left')) {
					this.toggleClass('active');
					this.getParent('div').getElement('.left').toggleClass('active');
				} else {
					this.toggleClass('active');
				}
			}
		},
		click: function(){
			document.location = this.getElement('a').get('href');	
		}
	});
	$$('#topMenu li.left.active').getParent('div').getElement('.left').toggleClass('active');
	
	new Wel.Box('welboxed-product', WelBoxed.AJAX, {
		'background-opacity': '.5',
		'loader': '/images/dentwayLoader.gif'
	});
	new Wel.Box('welboxed-new-dentway', WelBoxed.AJAX, {
		'background-opacity': '.5',
		'loader': '/images/dentwayLoader.gif',
		'width': 400,
		'height': 200
	});
	
	if ($defined($('wb-inline-me'))) {
		Wel.Box.inline({
			'element': $('wb-inline-me'),
			'width': 352,
			'height': 142
		});
	}
});