(function() {
	EYE.register(function(){
		var prodTabsEl, prodTabEl, prodRatings, prodReviews, accntForm, checkoutEl;
		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));
		}
		if ((checkoutEl = document.getElementById('shippingAddress'))) {
			$('select:first', checkoutEl)
				.bind('change', function(){
					$('fieldset.addressGroup').hide().eq(this.selectedIndex).show();
				})
				.each(function(){
					$('fieldset.addressGroup').hide().eq(this.selectedIndex).show();
				})
		}
		 $("div.prodDetails a").not('.productBrandImage').fancybox({ 'zoomSpeedIn': 0, 'zoomSpeedOut': 0, 'overlayShow': true });
		 $('#currencies select').bind('change', function(){
		 	window.location.href = $(this).val();
		 });
	}, 'init');
	
	EYE.localOverlay = function (){
		var inited = false;
		return {
			init: function (){
				if (inited == false) {
					inited = true;
					$('#localOverlay').css('opacity', 0.6);
				}
			},
			show: function (el) {
				var offset = $(el).offset();
				$('#localOverlay').css({
					top: offset.top,
					left: offset.left,
					width: el.offsetWidth,
					height: el.offsetHeight,
					display: 'block'
				});
			},
			hide: function (el) {
				$('#localOverlay').hide();
			}
		};
	}();
	EYE.register(EYE.localOverlay.init, 'init');
	
	EYE.mainMenu = function(){
		var currentItem,
			enter = function(){
				/*var left = Math.min($(this).offset().left - $('#wrapper').offset().left, 831);
				$(this)
					.find('>a').addClass('active').end()
					.find('>div')
						.css('left', left)
						.show();*/
				$(this)
					.find('>a').addClass('active').end()
					.find('>ul').show();
			},
			leave = function(){
				/*$(this)
					.find('>a').removeClass('active').end()
					.find('>div').hide();*/
				$(this)
					.find('>a').removeClass('active').end()
					.find('>ul').hide();
			};
		return {
			init: function(){
				$('#mainMenu li.mainItem')
					.bind('mouseenter', enter)
					.bind('mouseleave', leave);
				$('ul.topNavigation>li')
					.bind('mouseenter', function(){
						$('>ul', this).show();
					})
					.bind('mouseleave', function(){
						$('>ul', this).hide();
					});
			}
		};
	}();
	EYE.register(EYE.mainMenu.init, 'init');
	
	
	EYE.homepagePromo = function(){
		var el, current = 0, lnks, lnks2, imgs,
			timer,
			goTo = function(ev) {
				clearTimeout(timer);
				imgs.eq(current).stop().animate({opacity: 0}, 500, function(){
					$(this).hide();
				});
				lnks2.eq(current).removeClass('active');
				if (ev){
					current = lnks2.index(this);
				} else {
					current ++;
				}
				if (current < 0) {
					current = lnks2.size()-1;
				} else if(current >= lnks2.size()) {
					current = 0;
				}
				imgs.eq(current).show().stop().animate({opacity: 1}, 500);
				lnks2.eq(current).addClass('active');
				timer = setTimeout(goTo, 5000);
				return false;
			};
		return {
			init: function(){
				if ((el = document.getElementById('homePromo'))) {
					imgs = $('a', el);
					if (imgs.size() > 1) {
						var html = [];
						imgs.each(function(nr){
							html.push('<li><a href="#">');
							html.push(nr+1);
							html.push('</a></li>');
						}).css('opacity', 0).hide().filter(':first').css('opacity',1).show();
						lnks2 = $(el).parent().find('ul').html(html.join(''))
							.find('a').bind('click', goTo)
								.filter('a:first').addClass('active').end();
						timer = setTimeout(goTo, 5000);
					} else {
					}
				}
			}
		};
	}();
	EYE.register(EYE.homepagePromo.init, '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('checkout'))) {
					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.notifier = function(){
		var el, messages = 0, timer = false, available = false,
			hide = function() {
				messages --;
				$('div.noteItem:last', el).remove();
				if (messages > 0) {
					setTimeout(hide, 1000);
				} else {
					timer = false;
				}
			},
			scrll = function(){
				if (messages > 0) {
					$(el).css('top', (window.pageYOffset || document.documentElement.scrollTop || 0));
				}
			};
		return {
			init: function(){
				if ((el = document.getElementById('notifier'))) {
					available = true;
					$(window).bind('scroll', scrll);
				}
			},
			show: function(msg){
				if (available) {
					$(el).prepend('<div class="noteItem">' + msg + '</div>');
					messages++;
					scrll();
					if (timer === false) {
						timer = setTimeout(hide, 1000);
					}
				}
			}
		};
	}();
	EYE.register(EYE.notifier.init, 'init');
	
	EYE.compareProducts = function(){
		var add = function(ev){
				this.blur();
				var title = $(this).parent().parent().find('h2 a');
				var id = $(this).attr('rel');
				var dat = {
					id: id,
					link: title.attr('href'),
					name: title.text()
				};
				if ($('#sideCompareProducts ul a.deleteCompareItem[rel=' + id + ']').size() == 0) {
					$('#sideCompareProducts ul').append(tmpl('sideCompareItem', dat));
					$.ajax({
						type: 'POST',
						data: $.param({
							plugin: 'addProductToCompare',
							prod: id
						}),
						complete: function(){
						},
						success: function(xmlResponse)
						{
						},
						url : '/ajaxserver.php'
					});
					EYE.notifier.show(EYE.translations.notify_add_compare);
				} else {
					EYE.notifier.show(EYE.translations.notify_already_compare);
				}
				if ($('#sideCompareProducts ul li').size() > 0) {
					$('#sideCompareProducts a:last, #sideCompareProducts a:first').show();
					$('#sideCompareProducts p:last').hide();
				}
				return false;
			},
			remove = function(){
				$.ajax({
					type: 'POST',
					data: $.param({
						plugin: 'removeProductFromCompare',
						prod: $(this).attr('rel')
					}),
					complete: function(){
					},
					success: function(xmlResponse)
					{
					},
					url : '/ajaxserver.php'
				});
				EYE.notifier.show(EYE.translations.notify_remove_compare);
				$(this).parent().remove();
				if ($('#sideCompareProducts ul li').size() == 0) {
					$('#sideCompareProducts a').hide();
					$('#sideCompareProducts p:last').show();
				}
				return false;
			},
			view = function(){
				this.blur();
				$.ajax({
					type: 'POST',
					data: $.param({
						plugin: 'getProductsFromCompare'
					}),
					complete: function(){
					},
					success: function(xmlResponse)
					{
						var el = $('#compareProductsWrapper').html($('compare',xmlResponse).text()).css('width', 'auto');
						$('#compareProducts').show();
						var width = el.get(0).offsetWidth;
						el.css('width', Math.min(900, width));
						overlay();
						$('#compareProducts').css('marginLeft', -(el.get(0).offsetWidth+22)/2);
						window.scrollTo(0,0);
					},
					url : '/ajaxserver.php'
				});
				return false;
			},
			hide = function(){
				this.blur();
				$('#compareProductsWrapper').html('');
				$('#compareProducts, #overlay').hide()
				return false;
			},
			overlay = function() {
				$('#overlay').hide().css({
					width: $(document).width(),
					height: $(document).height(),
					display: 'block'
				});
			},
			scrll = function(){
				$('#compareProducts').css('top', 30+(window.pageYOffset || document.documentElement.scrollTop || 0));
			},
			empty = function(){
				var ids = [];
				$('#sideCompareProducts li a.deleteCompareItem').each(function(){
					ids.push($(this).attr('rel'));
				}).parent().remove();
				$.ajax({
					type: 'POST',
					data: $.param({
						plugin: 'removeProductFromCompare',
						prod: ids.join(',')
					}),
					complete: function(){
					},
					success: function(xmlResponse)
					{
					},
					url : '/ajaxserver.php'
				});
				EYE.notifier.show(EYE.translations.notify_empty_compare);
				if ($('#sideCompareProducts ul li').size() == 0) {
					$('#sideCompareProducts a').hide();
					$('#sideCompareProducts p:last').show();
				}
				return false;
			};
		return {
			init: function(){
				$('div.prodList').find('a.addToCompare').bind('click', add);
				$('#sideCompareProducts li a.deleteCompareItem').live('click', remove);
				$('#sideCompareProducts a:last').bind('click', view);
				$('#sideCompareProducts a:first').bind('click', empty);
				$('#compareProductsClose, #overlay').bind('click', hide);
			}
		};
	}();
	EYE.register(EYE.compareProducts.init, 'init');
	
	EYE.brands = function(){
		var left = 0, minLeft = 0,
			navigate = function(ev) {
				this.blur();
				var newleft = Math.max(minLeft, Math.min(0, left + 940*(this.id=='brandsLeft' ? 1 : -1)));
				if (left != newleft) {
					left = newleft;
					$('#brandsWrapper').stop().animate({
						left: left
					}, 700);
				}
				return false;
			};
		return {
			init: function(){
				$('#brandsLeft, #brandsRight').bind('click', navigate);
				minLeft = -94 * ($('#brands div.brandsItem').size() - 10);
			}
		};
	}();
	EYE.register(EYE.brands.init, 'init');
})();

