(function($){

	EYE.mainMenu = function(){
		var currentItem,
			timer,
			visible = false,
			enter = function(){
				currentItem = this;
				clearTimeout(timer);
				if (visible) {
					show();
				} else {
					timer = setTimeout(show, 120);
				}
			},
			show = function(){
				visible = true;
				$('#subMenu ul')
					.removeClass('active')
					.filter('ul[rel='+$(currentItem).attr('rel')+']')
						.addClass('active');
			},
			leave = function(){
				clearTimeout(timer);
				timer = setTimeout(clear, 80);
			},
			clear = function(){
				currentItem = null;
				visible = false;
				$('#subMenu ul')
					.removeClass('active')
					.filter('[rel='+$('#mainMenu li.active').attr('rel')+']')
						.addClass('active');
			},
			enterSub = function() {
				if (visible) {
					clearTimeout(timer);
				}
			};
		return {
			init: function(){
				$('#mainMenu li')
					.bind('mouseenter', enter)
					.bind('mouseleave', leave);
				$('#subMenu')
					.bind('mouseenter', enterSub)
					.bind('mouseleave', leave);
			}
		};
	}();
	EYE.register(EYE.mainMenu.init, 'init');

	EYE.search = function() {
		var focusEv = function() {
			if ($('#searchQuery').val() == $('#searchQuery').attr('rel')) {
				$('#searchQuery').val('');
			}
		};
		var el;
		var blurEv = function() {
				if ($('#searchQuery').val() == '') {
					$('#searchQuery').val($('#searchQuery').attr('rel'));
				}
		};
		return {
			init: function() {
				el = $('#searchQuery')
					.bind({
						'focus': focusEv,
						'blur': blurEv
					});
				blurEv();
			}
		};
	}();
	EYE.register(EYE.search.init, 'init');

	EYE.order = function() {
		var timer,
			visible = false;
		return {
			init: function() {
				$('#orderProductsByOptions').bind('change', function(){
					window.location.href = this.value;
				});
			}
		};
	}();
	EYE.register(EYE.order.init, 'init');


	EYE.productCombos = function () {		var el,
		attachEv = function(){
			$('#productFormCombos select:last').bind('change', changeOption);
		},
		changeOption = function(ev){
			$(this).parent().next().find('select').remove();
			buildSelects();
		},
		buildSelects = function(){
			var stopProccess = false;
			var selects = $('#productFormCombos select').filter(function(){
				return this.value != 0;
			});
			if (selects.size() > 0) {
				var availOptions = {};
				var comboSize = 0;
				$.each(EYE.product.combos, function(nr, attr){
					availOptions[nr] = {
						id: attr.id,
						options: {}
					};
					comboSize ++;
				});
				$.each(EYE.product.combo, function(nr, combo){
					var hasOptions = 0;
					selects.each(function(){
						var attrId = $(this).attr('rel');
						if (combo.attributes[attrId] == this.value) {
							hasOptions++;
						}
					});
					if (hasOptions == selects.size()) {
						//availOptions[attrId].options[this.value] = EYE.product.combos[attrId].options[this.value];
						$.each(combo.attributes, function(nr2, opt){
							opt = parseInt(opt, 10);
							availOptions[nr2].options[opt] = EYE.product.combos[nr2].options[opt];
						});
					}
					if (hasOptions == comboSize) {
						$(el).val(nr);
						//$('#productPrice').html(EYE.product.combo[nr].price);
					}
				});
			} else {
				var availOptions = EYE.product.combos;
			}
			$.each(availOptions, function(nr, attr){
				if (stopProccess == false) {
					var isSelect = $('#combo_'+attr.id).find('select');
					if (isSelect.size() != 1) {
						stopProccess = true;
						var html = ['<select rel="', attr.id, '"><option value="0">kies</option>'];
						$.each(attr.options, function(nr2, opt){
							html.push('<option value="');
							html.push(opt.id);
							html.push('">');
							html.push(opt.name);
							html.push('</option>');
						});
						html.push('</select>');
						$('#combo_' + attr.id).append(html.join(''));
						attachEv();
					} else if (isSelect.val() == 0) {
						stopProccess = true;
					}
				}
			});
		};
		return {
			init: function(){
				if ((el = document.getElementById('productCombo'))) {
					buildSelects();
				}
			}
		};

	}();
	EYE.register(EYE.productCombos.init, 'init');

	EYE.productImages = function() {
		return {
			init: function() {
				if (document.getElementById('productGallery')) {
					$(document).piroBox({
						  my_speed: 300, //animation speed
						  bg_alpha: 0.5, //background opacity
						  radius: 4, //caption rounded corner
						  scrollImage : true, // true == image follows the page _|_ false == image remains in the same open position
											   // in some cases of very large images or long description could be useful.
						  slideShow : 'false', // true == slideshow on, false == slideshow off
						  slideSpeed : 3, //slideshow
						  pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
						  pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
						  close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
						  });
				}
			}
		};
	}();
	EYE.register(EYE.productImages.init, 'init');

	EYE.general = function() {
		var accntForm;
		if ((accntForm = document.getElementById('accountForm'))) {
			var swicthAccount = function(ev){
				var el = $(this);
				el
					.parent()
					.parent()
						.find('input, select')
							.not(el)
							.attr('disabled', el.attr('checked'));
			};
			swicthAccount.apply($('input[name=use_contact_info]', accntForm)
				.bind('click', swicthAccount).get(0));
		}

	};
	EYE.register(EYE.general, 'init');

	EYE.checkout = function(){
		var el,
			asCompany = function(ev){
				if ($(this).attr('checked')) {
					$('p.asCompany', el).show();
					$('p.asPerson', el).hide();
				} else {
					$('p.asCompany', el).hide();
					$('p.asPerson', el).show();
				}
			},
			useBilling = function(){
				$(this).parent().parent().find('input, select').not(this).attr('disabled', $(this).attr('checked'));
			};
		return {
			init: function(){
				if ((el = document.getElementById('checkoutInfo'))) {
					asCompany.apply($('#as_company').bind('click', asCompany).get(0));
					useBilling.apply($('#use_billing').bind('click', useBilling).get(0));
				}
			}
		};
	}();
	EYE.register(EYE.checkout.init, 'init');

	EYE.productInfo = function() {
		var click = function() {
			if ($(this).parent().attr('id') == 'productMoreInfo') {
				$('#productForm, #productFormCombos, #productMoreInfo').addClass('hidden');
				$('#requestInfo').removeClass('hidden');
			} else {
				$('#productForm, #productFormCombos, #productMoreInfo').removeClass('hidden');
				$('#requestInfo').addClass('hidden');
			}
			return false;
		};
		return {
			init: function() {
				if (document.getElementById('productMoreInfo')) {
					$('#productMoreInfo a, #productBackCart a')
						.bind('click', click);
				}
			}
		};
	}();
	EYE.register(EYE.productInfo.init, 'init');

	EYE.overlay = function(){
		var currentBind = false;
		var resizeEv = function() {
			$('#overlay').css({
				width: $(document).width(),
				height: $(document).height()
			});
		};
		return {
			show: function() {
				$('#overlay').css({
					width: $(document).width(),
					height: $(document).height(),
					display: 'block'
				});
				$(window).bind('resize', resizeEv);
			},
			hide: function() {
				$('#overlay').hide();
				if (currentBind) {
					$('#overlay').unbind(currentBind);
					currentBind = false;
				}
				$(window).unbind('resize', resizeEv);
			},
			bind: function(type, fnc) {
				currentBind = type;
				$('#overlay').bind(type, fnc);
			}
		};
	}();

	EYE.window = function() {
		var close = function() {
			$('#window').hide();
			EYE.overlay.hide();
			return false;
		};
		var resizeEv = function() {
			$('#windowContent').css('height', Math.max($(window).height() - 180, 300));
		};
		return {
			init: function() {
				$('#windowHeader a').bind('click', close);
			},
			show: function(title, content) {
				$('#windowHeader span').text(title);
				$('#windowContent').html(content);
				resizeEv();
				EYE.overlay.show();
				EYE.overlay.bind('click', close);
				$('#window').show();
				$(window).bind('resize', resizeEv);
			}
		}
	}();
	EYE.register(EYE.window.init, 'init');

	EYE.productExtra = function() {
		var clickEv = function(ev) {
			var targetEl = $(ev.target).closest('a');
			if (targetEl.size() == 1) {
				EYE.window.show(targetEl.text(), $('#'+targetEl.attr('rel')).html());
				return false;
			}
		};
		return {
			init: function() {
				if (document.getElementById('productExtra')) {
					$('#productExtra').bind('click', clickEv);
				}
			}
		};
	}();
	EYE.register(EYE.productExtra.init, 'init');

	EYE.cart = function(){
		var clickEv = function(ev) {
			var targetEl = $(ev.target).closest('a');
			if (targetEl.size() == 1) {
				if (targetEl.is('.changeModel')) {
					targetEl.hide().parent()
						.find('ul').hide().end()
						.find('select, a.changeModelBack').show();
					return false;
				} else if (targetEl.is('.changeModelBack')) {
					targetEl.hide().parent()
						.find('select').hide().end()
						.find('ul, a.changeModel').show();
					return false;
				} else if (targetEl.is('.cartDelete')) {
					$('#cartForm tbody:first input:checkbox').attr('checked', false);
					targetEl.closest('tr').find('input:checkbox').attr('checked', true);
					$('#cartForm tfoot:first button:first').trigger('click');
					return false;
				}
			}
		};
		return {
			init: function() {
				$('#cartForm tbody:first').bind('click', clickEv);
			}
		};
	}();
	EYE.register(EYE.cart.init, 'init');

	EYE.brands = function() {
		var current = 0, timer, seconds = 4, perview = 9, max, views;
		var goTo = function() {
			clearTimeout(timer);
			current ++;
			if (current >= views) {
				current = 0;
			}
			$('#brands ul').stop().animate({
				left: - Math.min(
							current * perview,
							max - perview
						) * 105
			}, 400);
			timer = setTimeout(goTo, seconds * 1000);
		};
		return {
			init: function() {
				max = $('#brands li').size();
				views = Math.ceil(max/perview);
				timer = setTimeout(goTo, seconds * 1000);
			}
		};
	}();
	EYE.register(EYE.brands.init, 'init');

})(jQuery);
