function addtocompare() {
	$('.addtocompare')
	  .unbind('click')
	  .bind('click', function() {
		$(this).html('<img src="/images/loading.gif" height="12px" weight="12px" /> Добавляю...');
		$.ajax({
			url: $(this).attr("href").replace('/addToCompare/', '/ajaxAddToCompare/'),
			cache: false,
			success: function(html){
				$("#compareBlock").html(html);
			}
		});
		return false;
	});
}

function delfromcompare() {
	$('.delfromcompare')
	  .unbind('click')
	  .bind('click', function() {
		$(this).html('<img src="/images/loading.gif" height="12px" weight="12px" /> Удаление...');
		$.ajax({
			url: $(this).attr("href").replace('/delFromCompare/', '/ajaxDelFromCompare/'),
			cache: false,
			success: function(html){
				$("#compareBlock").html(html);
			}
		});
		return false;
	});
}

$(document).ready(function(){
	$('.addtocart').click (function() {
		if($(this).attr("href")!='/orders/cart/'){
			$(this).html('<img src="/images/loading.gif" height="12px" weight="12px" /> Добавляю...');
			cartLink = $(this);
			$.ajax({
				url: $(this).attr("href").replace('/addToCart/', '/ajaxAddToCart/'),
				cache: false,
				success: function(html) {
					$("#cartBlock").html(html);
					cartLink.text('Перейти в корзину');
					cartLink.attr("href", '/orders/cart/');
				}
			});
			return false;
		}else {
			return true;
		}
		
	});

	addtocompare();
	delfromcompare();

	
	$("input.productCount").click( function() {
		var reg=/\D/;
		var product = $(this).attr('id').replace(/product_/, '');
		if(!$("#loading_"+product).length) {
			var count = parseInt($("#countInput_"+product).val());
			count=reg.test(count)?0:count;
			if (count>0) {
				$("#countInput_"+product).after('<div style="position: absolute; width: 30px; height: 17px; float: left; text-align: center; margin-left: 95px; padding-top: 3px;" id="loading_'+product+'"><img src="/images/arrows.gif"></div>');
				$("#product_"+product).hide();
				$("#countInput_"+product).attr('disabled', true);
				$.ajax({url: '/orders/addAjax/'+product+'/'+count, cache: false, success: function(html) {
					$("#cartBlock").html(html);
					$("#product_"+product).show();
					$("#loading_"+product).remove();
					$("#countInput_"+product).attr('disabled', '');
				}});
			}
		}
		return false;
	});
	
	$('.deletefromcart').click (function() {
		count = parseInt($("#cart_count").html())-1;
		if (count>0) {
			id = $(this).attr("href").replace('/orders/deleteProduct/', '');
			$("#productTr_"+id).remove();
			$("#cart_count").html(count);
		} else {
			$('#cartListLeft').html('<p>Ваша корзина пуста.</p>');
		}
		$.ajax({url: $(this).attr("href").replace('/deleteProduct/', '/ajaxDeleteProduct/'), cache: false, success: function(html){ $("#cartBlock").html(html); }});
		return false;
	});
	
	$('.delfromcompare123').click (function() {
		$(this).parent().hide();
		count = parseInt($("#compare_count").html())-1;
		if (count!=0) $("#compare_count").html(count); else $("#compareBlock").html('');
		$.ajax({url: $(this).attr("href").replace('/delFromCompare/', '/ajaxDelFromCompare/'), cache: false, success: function(html){ if (count!=0) $("#compareBlock").html(html); }});
		return false;
	});
	
	function re_odd() {
		var odd = true;
		$.each($(".compare-fields:visible"), function(i, v) {
			if (odd) {
				$(v).removeClass('odd');
			} else {
				$(v).addClass('odd');	
			}
			odd = !odd;
		});
	}
	
	$('#unique_active').click (function() {
		$(this).addClass('active').removeClass('none');
		$('#unique_all').removeClass('active').addClass('none');
		$('.uniqueTr').hide();
		re_odd()
		return false;
	});
	
	$('#unique_all').click (function() {
		$(this).addClass('active').removeClass('none');
		$('#unique_active').removeClass('active').addClass('none');
		$('.uniqueTr').show();
		re_odd()
		return false;
	});
	$('.uniqueTr').hide();
	re_odd()
	
	$('#link-login').click(function() {
		$('.login-form').fadeIn(1000);
		$('.cart-panel').fadeOut(1000);
		return false;
	});
	
	$('#link-cart').click(function() {
		$('.cart-panel').fadeIn(1000);
		$('.login-form').fadeOut(1000);
		return false;
	}).attr('style', 'float: right; margin-right: 22px;');
});

function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", hideFormText, false);
else if (window.attachEvent)
	window.attachEvent("onload", hideFormText);
	
//slideshow
$(function(){
	initSlideShow();
});

function initSlideShow() {
	$('div.slideshow').fadeGallery({
		slideElements:'ul > li',
		pauseOnHover:true,
		autoRotation:true,
		switchTime:3000,
		duration:650,
		event:'click'
	})
}

jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'ul > li',
		pagerLinks:'ul a',
		btnNext:'a.btn-next',
		btnPrev:'a.btn-prev',
		btnPlayPause:'span.play a',
		pausedClass:'paused',
		playClass:'playing',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		switchTime:3000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;

		// gallery init
		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		if(!_slideCount) return;
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}

		// gallery animation
		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slideCount-1;
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else _currentIndex = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}

