$(function(){
	/*
	 *	menu
	 */

	var menuactive = $('<li class="selected"><span class="st"></span></li>');
	if ($('ul.menu>li.active').length==0){
		menuactive.css({
			'width': $('ul.menu>li:eq(0)').width(),
			'top': -300,
			'left': 0
		}).appendTo('ul.menu');
		menuactive.animate({
			top: 0
		},500,'swing',function(){
			$('ul.menu>li:eq(0)>a').animate({color: '#fff'},200);
			$('ul.menu>li:eq(0) ul').show(500);
		});
	}else{
		menuactive.css({
			'width': $('ul.menu>li.active').width(),
			'top': 0,
			'left': $('ul.menu>li.active').position().left
		})
		.appendTo('ul.menu');

		$('ul.menu>li.active').removeClass('active').find('>a').css({color: '#fff'})
		.end().find('ul').show();
	}

	$('ul.menu>li>a').click(function(){
		var li = $(this).parent();
		$('ul.menu>li ul').hide();
		$('ul.menu>li a').animate({color: '#000'},200);
		menuactive.animate({
			'left': li.position().left,
			'width': li.width()
		},500,'linear',function(){
			$('ul.menu>li ul').hide();
			$('ul.menu>li>a').css({color: '#000'});
			li.find('>a').animate({color: '#fff'},200);
			li.find('ul').show(200);
		});
		return false;
	});
	
	/*
	 *	cart
	 */
	
	/* 
	$('.content .price a').click(function(){
		var turl = this.href;
		var i = $(this).parents('div.content').find('dl.gallery dt img');
		var poscart = $('.top .cart>a').offset();
		var pos = i.offset();
		var j = i.clone().appendTo($(document.body))
		.css({
			'position':'absolute',
			'left': pos.left,
			'top': pos.top,
			'z-index': 10
		});
		j.animate({
			width: 100,
			height: 100,
			top: poscart.top,
			left: poscart.left-50,
			'opacity': 0
		},600,'',function(){
			$(this).remove();
		});
		$('html,body').animate({
			scrollTop: 0
		},600,'');
	});
	
	*/
	
	/*
	 *	gallery
	 */
	var truer = false;

	function gallery(){
		bgallery();
		var im = $('<img />');
		var iim = $(this).find('img');
		im.load(function(){
			if (!truer){
				$('dl.gallery dt img').animate({
					marginLeft: 640
				},500,'',function(){
					$('dl.gallery dt').empty();
			
					im.appendTo('dl.gallery dt')
					.css({
						'margin-left': 640
					})
					.animate({
						marginLeft: 0
					},500);
					iim.animate({
						marginLeft: -250
					},500,'',rgallery);
				});
				$('dl.gallery dd img').not(iim).animate({
					marginLeft: 0
				},500);
			}
			else{
				truer = false;
				$('dl.gallery dt img').animate({
					marginTop: 640
				},500,'',function(){
					
					$('dl.gallery dt').empty();
					im.appendTo('dl.gallery dt')
					.css({
						'margin-left': 640,
						'margin-top': 0,
					})
					.animate({
						marginLeft: 0
					},500);
					iim.animate({
						marginLeft: -250
					},500,'',rgallery);
				});
				$('dl.gallery dd img').not(iim).animate({
					marginLeft: 0
				},500);
				
			}
		});
		im.attr('src',this.href);
		return false;
	}

	$('dl.gallery dd:eq(2) img').css({
		'margin-left':-250
	});
	$('dl.gallery dd a').click(gallery);
	$('div.colorView a:eq(0)').click(function(){$('dl.gallery dd a:first').click();});
	
	$('div.colorView a:gt(0)').click(function(){

		var im = $('<img />');
		im.attr('src',$(this).attr('img'));
		im.load(function(){
			if (truer)
				$('dl.gallery dt img').animate({
					marginTop: 640
				},500,'',function(){
					$('dl.gallery dt').empty();
			
					im.appendTo('dl.gallery dt')
					.css({
						'margin-top': 640
					})
					.animate({
						marginTop: 0
					},500);
				});
			else {
				truer = true;
				$('dl.gallery dd img').animate({
					marginLeft: 0
				},500);				
				$('dl.gallery dt img').animate({
					marginLeft: 640
				},500,'',function(){
					
					$('dl.gallery dt').empty();
					im.appendTo('dl.gallery dt')
					.css({
						'margin-top': 640,
						'margin-left': 0
					})
					.animate({
						marginTop: 0
					},500);
				});
				
			}
		});
	});
	
	function bgallery(){
		$('dl.gallery dd a').unbind('click');
		$('dl.gallery dd a').click(function(){return false});
	}
	function rgallery(){
		$('dl.gallery dd a').unbind('click');
		$('dl.gallery dd a').click(gallery);
	}
	
	/*
	 *	sort
	 */
	
	if ($('dl.sort').length){
		var noavailable = false;
	
		$('dl.category, dl.sort').css({'visibility':'visible'});
		
		$('dl.category dd:eq(0)').addClass('active');
		$('dl.category dd:gt(0) a').click(function(){
			var cid = this.href.substr(this.href.indexOf('#')+1);
			$('ul.products>li').hide();
			$('ul.products>li.'+cid).show();
			
			$('dl.category dd').removeClass('active');
			$(this).parent().addClass('active');
			
			if (noavailable){
				$('ul.products li.noavailable').hide();
			}
		});
		
		$('dl.category dd:eq(0) a').click(function(){
			$('ul.products>li').show();
			$('dl.category dd').removeClass('active');
			$(this).parent().addClass('active');
		});

		if ($('dl.category').length){
			var ind = location.href.indexOf('#');
			var l = 0;
			if (ind != -1) l = location.href.substring(ind+1);
			if ($('dl.category a[href$=#'+$.trim(l)+']').length){
				$('dl.category a[href$=#'+$.trim(l)+']').click();
			}
		}	
		var products = new Array();
		function prs(){
			products = new Array();
			$('ul.products>li').each(function(i){
				var visits = parseInt($(this).find('h3 a').attr('rel'));
				var price = parseInt($(this).find('.to-cart a').attr('rel'));
				
				products[i] = new Array();
				products[i][0] = visits;
				products[i][1] = price;
				products[i][2] = this;
			});
		}
		prs();
		
		function qsort(nm){
			prs();
			f_compare = function(a, b){
				return ((a == b) ? 0 : ((a > b) ? 1 : -1));
			};
			
			f_change = function(a, i, j){
				var c = a[i];a[i] = a[j];a[j] = c;
			};
		 
			var qs = function(l, r){
				var i = l, 
					j = r,
					x = products[Math.floor(Math.random()*(r-l+1))+l][nm];
		 
				while(i <= j){
					while(f_compare(products[i][nm], x) == -1) {i++;}
					while(f_compare(products[j][nm], x) == 1) {j--;}
					if(i <= j) {f_change(products, i++, j--);}
				};
				if(l < j) {qs(l, j);}
				if(i < r) {qs(i, r);}
			};
		 
			qs(0, products.length-1);
		};
		var sort = new Array();
		sort[0] = true;
		sort[1] = true;
		$('dl.sort dd:not(.nal) a').click(function(){
			var index = $('dl.sort dd a').index(this);
			qsort(index);		
			$('dl.sort dd:not(.nal)').removeClass('active');
			$(this).parent().parent().addClass('active');
			$('dl.sort dd span.cn span').html('');
			
			$(this).parent().find('span').html((sort[index])? ' ↓':' ↑');
			
			$('ul.products').empty();
			if (sort[index]){
				$.each(products,function(i,n){
					$(n[2]).appendTo($('ul.products'));
				});
			}else{
				$.each(products,function(i,n){
					$(n[2]).prependTo($('ul.products'));
				});
			}
			sort[index] = !sort[index];
			return false;
		});
		
		$('dl.sort dd.nal a').click(function(){
			noavailable = !noavailable;
			if (noavailable){
				$(this).parent().parent().addClass('active');
				$('ul.products li.noavailable').hide();
			}else{
				$(this).parent().parent().removeClass('active');
				$('ul.products li.noavailable').show();
				$('dl.category dd.active a').click();
			}
			return false;
		});
	}
	
	if ($('.right .email').length){
		if ($('.right .email input').attr('value') == '') {
			$('.right .email').hide();
		}else{
			$('.right .email').show();
			$('a.show-email').addClass('active');
		}
	
        $('a.show-email').click(function(){
            $(this).toggleClass('active');
            $('.right .email').slideToggle(500);
            if (!$(this).hasClass('active')) {
                $('.right .email input').attr('value', '');
            }
            return false;
        });
	}
	
	//	auth
	
	$('.right .auth>a:not(.exit)').click(function(){
		$('div.auth-form').slideToggle(300);
		return false;
	});
	
	$('div.auth-form span.close').click(function(){
		$('div.auth-form').slideUp(300);
		return false;
	});
	
	$('.s-auth a').click(function(){
		$('.s-auth').hide();
		$('.s-remind').show();
		return false;
	});
	$('.s-remind a').click(function(){
		$('.s-remind').hide();
		$('.s-auth').show();
		return false;
	});
	
	$('dl.noinstock dt a').click(function(){
		$(this).parents('dl.noinstock').find('dd').toggle();
		return false;
	});
	
	//	inputs
	
	var inputs = $('.auth-form input[type=text], input.search');
	
	inputs.each(function(){
		this.value = (this.value=='') ? this.title: this.value;
	});
	
	inputs.blur(function(){
		this.value = (this.value=='') ? this.title: this.value;
	});
	
	inputs.focus(function(){
		this.value = (this.value==this.title) ? '': this.value;
	});
	
	$('a.show-auth').click(function(){
		$('html, body').animate({
			scrollTop: 0
		},300);
		$('div.auth-form').slideDown(300);
		return false;
	});
//* nerevar: add town select
	//$('.ukr-town').hide();
	
	$('.select-town').click(function(){
		if (this.checked) {
            $('.ukr-town').slideDown(500);
		} else {
			$('.ukr-town').slideUp(500);
		}
	});
	$('.select-kiev').click(function(){
		$('.ukr-town').slideUp(500);
	});
	$('#chat').click(function(){
		$("#swimdiv iframe").attr('src',$(this).attr('hrefto'));
		$('.chater').show();
	});
	$('.chater .cheterin .close').click(function(){
		$('.chater').hide();
		$("#swimdiv iframe").attr('src','');
	});
	$('.chater .cheterin .down').click(function(){
		$('.chater').hide();
		$('.chaterdown').show();
	});
	$('.chaterdown .cheterin .down').click(function(){
		$('.chater').show();
		$('.chaterdown').hide();
	});
	$('.chaterdown .cheterin .close').click(function(){
		$('.chater').hide();
		$('.chaterdown').hide();
		$("#swimdiv iframe").attr('src','');
	});
//* /nerevar	
});

    $(function () {
        $('.description').each(function () {
            var distance = 10;
            var time = 250;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = $(this);
            var info = $(this).parent().find('.alldescription').css('opacity', 0);


            $([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;

                    info.css({
                        top: 0,
                        left: 0,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
    });

