(function() {
	EYE.register(function(){
		var prodTabsEl, prodTabEl, prodRatings, prodReviews, accntForm, checkoutEl;
		$('#browseManufacturer, select.productsSort').bind('change', function(){
			if (this.value != 0) {
				window.location.href = this.value;
			}
		});
		if ((prodTabsEl = document.getElementById('productDetailsTabs'))) {
			$(prodTabsEl).bind('click', function(ev){
				if ($(ev.target).is('a')) {
					var tabIndex = $('a', prodTabsEl).index(ev.target); 
					$(prodTabsEl)
						.find('li')
							.removeClass('current')
							.eq(tabIndex)
								.addClass('current')
								.end()
							.end()
						.parent()
							.find('>div')
								.removeClass('current')
								.eq(tabIndex)
									.addClass('current');
					ev.target.blur();
					return false;
				}
			});
			if (window.location.hash && (prodTabEl = $(window.location.hash)).size()>0) {
				if (prodTabEl.is('li') && EYE.isChildOf(prodTabsEl, prodTabEl.get(0), prodTabsEl)) {
					var tabIndex = $('li', prodTabsEl).index(prodTabEl.get(0)); 
					$(prodTabsEl)
						.find('li')
							.removeClass('current')
							.eq(tabIndex)
								.addClass('current')
								.end()
							.end()
						.parent()
							.find('>div')
								.removeClass('current')
								.eq(tabIndex)
									.addClass('current');
				}
			}
		}
		$('#filterCategory').bind('change', function(){
			window.location.href = this.value;
		});
		if ((prodRatings = document.getElementById('reviewsRatings'))) {
			$('select', prodRatings)
				.hide()
				.each(function(){
					var el = $('<div class="starRating" />')
								.appendTo(this.parentNode)
								.bind('click', function(ev){
									var targetEl = $(ev.target);
									if (targetEl.is('a')) {
										$(this)
											.find('div')
												.css('width', ev.target.offsetWidth + 'px')
												.end()
											.parent()
												.find('select')
													.val(targetEl.attr('rel'))
													.end()
												.end()
											.find('a')
												.removeClass('active');
										targetEl.addClass('active').blur();
										return false;
									}
								}),
						cnt = 0;;
					$(this).find('option').each(function(){
						el.prepend('<a href="#" rel="' + $(this).attr('value') + '" class="starRating' + cnt +'">' + cnt +'</a>');
						cnt++;
					});
					$('<div />').css('width', (parseInt(this.value, 10)||0)*20 + 'px').prependTo(el);
				});
		}
		if ((prodReviews = document.getElementById('productReviewsPage'))) {
			$('p.productReviewUsefull a', prodReviews).bind('click', function(){
				window.location.href = $(this).attr('dovote');
				return false;
			});
		}
		if ((accntForm = document.getElementById('accountForm'))) {
			var swicthAccount = function(ev){
				var el = $(this);
				el
					.parent()
					.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));
		}
		$('#addBookmark').bind('click', function () {
			this.blur();
			if (window.sidebar) { 
				window.sidebar.addPanel($('title').text(), window.location.href,""); 
			} else if( document.all ) {
				window.external.AddFavorite( window.location.href, $('title').text());
			} else if( window.opera && window.print ) {
				return true;
			}
			return false;
		});
		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();
				})
		}
	}, '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.productComment = function (){
		var inited = false, el, review, container,
			hide = function (ev) {
				$('#commentComment').val('');
				$('#productCommentForm ul').remove();
				$('#overlay, #productCommentForm').hide();
				if (ev) {
					this.blur();
				}
				return false;
			},
			overlay = function() {
				$('#overlay').css({
					display: 'block',
					width: $(document).width(),
					height: $(document).height()
				});
			},
			show = function(ev){
				container = $(this).parent().parent();
				review = container.attr('rel');
				container = container.find('div.productReviewComments');
				var offset = $(this).offset();
				$('#productCommentForm').css({
					display: 'block',
					top: offset.top,
					left: offset.left
				});
				overlay();
				this.blur();
				return false;
			},
			send = function (ev){
				EYE.localOverlay.show(document.getElementById('productCommentForm'));
				$.ajax({
					type: 'POST',
					data: $.param({
						plugin: 'postComment',
						name: $('#commentName').val(),
						comment: $('#commentComment').val(),
						email: $('#commentEmail').val(),
						product: EYE.product,
						review: review
					}),
					complete: EYE.localOverlay.hide,
					success: function(xmlResponse)
					{
						var responseErros = $('errorResponse', xmlResponse);
						if (responseErros.size() > 0) {
							var html = ['<ul>'];
							responseErros.each(function(){
								html.push('<li>');
								html.push($(this).text());
								html.push('</li>');
							});
							html.push('</ul>');
							$('#productCommentForm ul').remove();
							$('#productCommentForm h3').after(html.join(''));
							overlay();
						} else {
							hide();
							var dat = {
								name: $('name', xmlResponse).text(),
								email: $('email', xmlResponse).text(),
								date: $('date', xmlResponse).text(),
								text: $('text', xmlResponse).text()
							};
							container.find('>div.productReviewCommentsWrapper').append(tmpl("reviewItem", dat));
							$.cookie('comment_name', dat.name, {
								expires: 7,
								path: '/'
							});
							$.cookie('comment_email', dat.email, {
								expires: 7,
								path: '/'
							});
							var cnt = container.find('h4 span');
							cnt.text(parseInt(cnt.text(), 10) + 1);
						}
					},
					url : '/ajaxserver.php'
				});
				this.blur();
				return false;
			},
			toggle = function() {
				$(this).parent().parent().find('div.productReviewCommentsWrapper').toggle();
				this.blur();
				return false;
			};
		return {
			init: function(){
				if (inited == false && (el = document.getElementById('productReviewsPage'))) {
					inited = true;
					$('a.addComment').bind('click', show);
					$('#overlay').css('opacity', 0.5);
					$('#overlayIframe').css('opacity', 0);
					$('#productCommentForm a:last').bind('click', hide);
					$('#productCommentForm a:first').bind('click', send);
					$('div.productReviewComments h4 a', el).bind('click', toggle);
				}
			}
		}
	}();
	EYE.register(EYE.productComment.init, 'init');
		
	EYE.priceRange = function () {
		var el, cont, width, priceDiff, knob1, knob2,
			origX, mouseX, knob, minPrice, maxPrice;
			mouseDown = function(ev){
				if ($(ev.target).is('span')) {
					knob = ev.target;
					$(document).bind('mousemove', mouseMove).bind('mouseup', mouseUp);
					origX = knob.offsetLeft;
					mouseX = ev.pageX;
				}
			},
			mouseMove = function(ev){
				knob.style.left = Math.min(Math.max(0, origX + ev.pageX - mouseX), width) + 'px';
				showPrice();
			},
			mouseUp = function(ev){
				$(document).unbind('mousemove', mouseMove).unbind('mouseup', mouseUp);
			},
			showPrice = function(){
				var minP = EYE.priceRangeMin + parseInt(priceDiff * knob1.offsetLeft / width, 10);
				var maxP = EYE.priceRangeMin + parseInt(priceDiff * knob2.offsetLeft / width, 10);
				EYE.priceRangeRef[1] = Math.min(minP, maxP);
				EYE.priceRangeRef[3] = Math.max(minP, maxP);
				minPrice.text(EYE.priceRangeRef[1]);
				maxPrice.text(EYE.priceRangeRef[3]);
				$('#priceRangeCurrent a').attr('href', EYE.priceRangeRef.join(''));
			};
		return {
			init: function(){
				if ((el = document.getElementById('priceRange'))) {
					cont = $('#priceRangeSlider').bind('mousedown', mouseDown);
					width = cont.width() - cont.find('span:last').width();
					cont
						.find('span:last')
							.css('left', width);
					minPrice = $('#priceRangeCurrent span:first');
					maxPrice = $('#priceRangeCurrent span:last');
					knob1 = document.getElementById('priceRangeSlider1');
					knob2 = document.getElementById('priceRangeSlider2');
					priceDiff = EYE.priceRangeMax - EYE.priceRangeMin;
					showPrice();
				}
			}
		}
	}();
	EYE.register(EYE.priceRange.init, 'init');
	
	
})();
