function setCookie (name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +	((expires) ? "; expires=" + expires : "; expires=Mon, 07-Dec-2020 00:00:00 GMT") +	((path) ? "; path=" + path : "; path=/") +	((domain) ? "; domain=" + domain : "") +	((secure) ? "; secure" : "");
	}
	
;(function(b,c){b('*').hover(function(){b(this).data(c,1)},function(){b(this).data(c,0)}).data(c,0);b[c]=function(a){return b(a)[c]()};b.fn[c]=function(a){a=0;b(this).each(function(){a+=b(this).data(c)});return a>0}})(jQuery,'isHovered');

$(function() {
	var config = {
		animationTimeout:250
		}
		
	function animateTogetherProducts(direction) {
		var $products=$('.product-page .buy .products'),
			ml=parseInt($products.css('margin-left')),
			pl=parseInt($products.css('padding-left'));
			
		if(direction=='right') {
			ml-=130;
			pl+=130;
			}
		else {
			pl-=130;
			}
		
		if(direction=='left') {
			$products.find('.product').eq($products.find('.product').length-1).detach().prependTo($products);
			$products.css({marginLeft:ml-130});
			}
		
		$products.animate(
			{marginLeft:ml}, config.animationTimeout, function() {
				if(direction=='right') {
					$products.find('.product').eq(0).detach().appendTo($products);
					$products.css({paddingLeft:pl})
					}
				});
				
		setTimeout(function() {
			$('.product-page .buy .arrow-'+direction).one('click',function() {
				animateTogetherProducts(direction);
				})
			},config.animationTimeout);
				
		}
		
	function animateAccessories(direction) {
		var $products=$('.product-page .accessories .accessories-elements'),
			ml=parseInt($products.css('margin-left')),
			pl=parseInt($products.css('padding-left'));
			
		if(direction=='right') {
			ml-=130;
			pl+=130;
			}
		else {
			pl-=130;
			}
		
		if(direction=='left') {
			$products.find('.accessories-element').eq($products.find('.accessories-element').length-1).detach().prependTo($products);
			$products.css({marginLeft:ml-130});
			}
		
		$products.animate(
			{marginLeft:ml}, config.animationTimeout, function() {
				if(direction=='right') {
					$products.find('.accessories-element').eq(0).detach().appendTo($products);
					$products.css({paddingLeft:pl})
					}
				});
				
		setTimeout(function() {
			$('.product-page .accessories .arrow-'+direction).one('click',function() {
				animateAccessories(direction);
				})
			},config.animationTimeout);
				
		}

	$('.product-page .buy .arrow-left').one('click',function() {
		animateTogetherProducts('left');
		});
	
	$('.product-page .buy .arrow-right').one('click',function() {
		animateTogetherProducts('right');
		});
	
	$('.product-page .accessories .arrow-left').one('click',function() {
		animateAccessories('left');
		});
	
	$('.product-page .accessories .arrow-right').one('click',function() {
		animateAccessories('right');
		});
		
	$('.middle-info .photo a[rel="slideshow"]').fancybox();
	
	 $('.middle-info .photo.youtube a, a[rel="youtube"]').click(function() {
       $.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
			
		return false;
		});

		
	$('.contacts').delegate('.country a','click',function() {
		var $country=$(this).parents('.country'),
			countryName=$(this).text(),
			countryClass;
		
		$country.addClass('active');
		$country.html('<span>'+countryName+'</span>');
		
		if($country.hasClass('ru'))
			countryClass='ru';
		else
			countryClass='ua';
			
		$('.value-'+countryClass).show();
		setCookie('country',countryClass);
		
		$country=$country.siblings('.country').eq(0);
		countryName=$country.find('span').text();
		
		$country.removeClass('active');
		$country.html('<a href="#">'+countryName+'</a>');
		
		if($country.hasClass('ru'))
			countryClass='ru';
		else
			countryClass='ua';
			
		$('.value-'+countryClass).hide();
		
		return false;
		});
		
	function animateSlide(direction) {
		var $slides=$('.slides-container .slide'),
			slidesCount=$slides.length,
			activeSlide=$slides.index($('.slide.active')),
			$prevSlide=$slides.eq(activeSlide),
			prevSlideLeft;
		
		if(direction=='left') {
			activeSlide--;
			prevSlideLeft=150;
			}
		else {
			activeSlide++;
			prevSlideLeft=-150;
			}
			
		activeSlide=activeSlide%slidesCount;
		
		var $newSlide=$slides.eq(activeSlide);
		
		
		$prevSlide.removeClass('animating');
		$prevSlide.css('opacity','1').animate({
			opacity:0,
			left:prevSlideLeft
			},800,'easeInOutQuint',function() {
				$prevSlide.removeClass('active');	
				$prevSlide.removeClass('animating');
				});
				
		setTimeout(function() {
			$newSlide.css('left',-1*prevSlideLeft).css('opacity','0')
			$newSlide.addClass('active').addClass('animating');
			$newSlide.css('opacity','0').animate({
				opacity:1,
				left:0
				},600,'easeInOutQuint',function() {
					$newSlide.removeClass('animating');
					}
					);},200);
					
		setTimeout(function(){$('.slides-container .arrow-'+direction).one('click',function() {
			animateSlide(direction);
			})},900);
		}
		
		
	$('.slides-container .arrow-left').one('click',function() {
		animateSlide('left');
		});
		
	$('.slides-container .arrow-right').one('click',function() {
		animateSlide('right');
		});
		
		
	var menuHover=false;
	
	$('.menu-categories .has-dropdown').mouseenter(function() {
		$('.dropdown-menu').css('visibility','visible');
		
		var left=$('.menu-categories .has-dropdown').offset().left-$('.dropdown-menu').width()+$('.menu-categories .has-dropdown').width()+34,
			top=$('.menu-categories .has-dropdown').offset().top+$('.menu-categories .has-dropdown').height();
		
		$('.dropdown-menu').css({
			left:left,
			top:top
			})
		
		$(this).addClass('pseudo-hover');
		});
	
	$('.dropdown-menu').mouseenter(function() {
		menuHover=true;
		});
	
	$('.dropdown-menu').mouseleave(function() {
		$(this).css('visibility','hidden');
			
		menuHover=false;
		$('.menu-categories .has-dropdown').removeClass('pseudo-hover');
		});
		
	$('.menu-categories .has-dropdown').mouseleave(function() {
		setTimeout(function() {
			if(!menuHover) {
				$('.dropdown-menu').css('visibility','hidden');
				$('.menu-categories .has-dropdown').removeClass('pseudo-hover');
				}
			},100);
		});
		
	$('.catalog .models-types a').click(function() {
		var $parentLi=$(this).parents('li');
		
		if(!$parentLi.hasClass('active')) {
			$parentLi.addClass('active').siblings().removeClass('active');
			
			var $products=$('.catalog .product'),
				type=$parentLi.data('type'),
				numberVisible=0;
				
			$('.catalog .left .seperator').remove();
			
			if(type=='all') {
				$products.fadeIn('fast');
				$('.catalog .product:odd').after('<div class="seperator"></div>');
				numberVisible=$products.length;
				}
			else {
				
				$products.each(function() {
					var types=$(this).data('types').split(' '),
						show=false;
					
					for (var i in types) {
						if(types[i]==type) 
							show=true;
						}
						
					if(show) {
						$(this).fadeIn('fast');
						numberVisible++;
						if(numberVisible%2==0)
							$(this).after('<div class="seperator"></div>');
						}
					else
						$(this).fadeOut('fast');
					});
					
				}
			if(numberVisible%2==0)
				$('.catalog .left .seperator').last().remove();
				
			}
			
		return false;
	});
		

	$(function(){
		$("div.b-input-hint > input, div.b-input-hint > textarea")
		.focus(function()
		{
			$(this).parent().find('label').hide();
			if ($(this).hasClass('error_input'))
			{
				$(this).removeClass('error_input').next().hide().remove();			
			}
		})
		.blur(function()
		{
			if ($(this).attr('value') == "")
				$(this).parent().find('label').show();
		})
		.mouseover(function()
		{
			if ($(this).attr('value') != "")
				$(this).parent().find('label').hide();
		}).bind('change',function()
		{
			if ($(this).attr('value') != "")
				$(this).parent().find('label').hide();
		}).trigger('change');
	});	

	$('div.contacts-form .submit > input').click(function()
	{
		if ($("div.contacts-form input").hasClass('error_input') || $("div.contacts-form textarea").hasClass('error_input'))
			return false;

		$.ajax({
			url: $("div.contacts-form form").attr('action'),
			data: $("div.contacts-form form").serializeArray(),
			dataType: "json",
			type: "POST",
			beforeSend: function(XMLHttpRequest) {
				$("div.contacts-form .submit > input").hide();
				$("div.contacts-form .submit > .loader").show();
			},
			complete: function(XMLHttpRequest, textStatus) {
				$("div.contacts-form .submit > .loader").hide();
				$("div.contacts-form .submit > input").show();
			},
			success: function (data, textStatus) {
				if (data.status == 'ok'){
					$("div.contacts-form > div.status").fadeIn('slow');
					$("div.contacts-form > div.form").fadeOut('fast');
				}else if(data.status == 'error'){
					for (var key in data.field) {
						$('#' + data.field[key].obj).addClass(data.field[key].errorclass).parent().append(data.field[key].error);
					}
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Не удалось выполнить запрос. Возможно сайт временно не доступен или у вас проблемы с интернетом.');
			}
		});	
		return false;
	});
	
	$(function(){
		$("#buy-form input, #buy-form textarea").focus(function()
		{
			$(this).parent().find('label').hide();
			if ($(this).hasClass('error_input'))
			{
				$(this).removeClass('error_input').next().hide().remove();			
			}
		})
	});

	$('#buy-form button.buy').click(function()
	{
		if ($("#buy-form input").hasClass('error_input') || $("#buy-form textarea").hasClass('error_input'))
			return false;

		$.ajax({
			url: $("#buy-form form").attr('action'),
			data: $("#buy-form form").serializeArray(),
			dataType: "json",
			type: "POST",
			beforeSend: function(XMLHttpRequest) {
				$("#buy-form .submit > input").hide();
				$("#buy-form .submit > .loader").show();
			},
			complete: function(XMLHttpRequest, textStatus) {
				$("#buy-form .submit > .loader").hide();
				$("#buy-form .submit > input").show();
			},
			success: function (data, textStatus) {
				if (data.status == 'ok'){
					$("#buy-form div.status").fadeIn('slow');
					$("#buy-form form").fadeOut('fast');
				}else if(data.status == 'error'){
					for (var key in data.field) {
						$('#' + data.field[key].obj).addClass(data.field[key].errorclass).parent().append(data.field[key].error);
					}
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Не удалось выполнить запрос. Возможно сайт временно не доступен или у вас проблемы с интернетом.');
			}
		});	
		return false;
	});

	$('#buy-form button.gray').click(function()
	{
		$.fancybox.close();
	});
	
	$('a.buy-form-href').fancybox({
		onStart:function(caller) {
			var $form=$('#buy-form'),
				$productFirst=$form.find('div.product').eq(0),
				$product=$form.find('div.product').eq(1),
				buyingTogether=false;
			
			if($(caller).parents('.accessories-element').length!=0) {
					var $productBuy=$(caller).parents('.accessories-element');
					$productFirst.find('.img img').attr('src',$productBuy.find('img').attr('src'));
					$productFirst.find('.desc .name').html($productBuy.find('span.name').html());
					$productFirst.find('.price span.value').html($productBuy.find('.price span:visible').html());
					
					var $productPrice = $productBuy.find('.price span:visible strong');
				}

			else if($(caller).parents('.action-products').length!=0) {
					var $productBuy=$(caller).parents('.action-products .sub-total');
					$productFirst.find('.img img').attr('src',$('img.action-img').attr('src'));
					$productFirst.find('.desc .name').html($('h1').html());
					$productFirst.find('.price span.value').html($productBuy.find('.price span:visible').html());
					
					var $productPrice = $productBuy.find('.price span:visible strong');
				}
			

			else {
					var $productBuy=$(caller).parents('.product-page');
					$productFirst.find('.img img').attr('src',$productBuy.find('div.big-photo > img').attr('src'));
					$productFirst.find('.desc .name').html($productBuy.find('h1').html());
					$productFirst.find('.price span.value').html($productBuy.find('div.buy div.price span:visible').html());
					
					var $productPrice = $productBuy.find('.buy .price span:visible strong');
				}
			
			if($(caller).parents('.together').length==0) {
				$('#buy-form div.product').eq(1).hide();
				//$('#buy-form h1 span').hide();
				$form.find('#order_product').val($form.find('div.product').find('.name').text() + ', ' + $productBuy.find('.price span:visible').html() );
				$form.find('input.together').val('');
				}
				
			else {
				$('#buy-form div.product').eq(1).show();
				//$('#buy-form h1 span').show();
				buyingTogether=true;
				
				var $together=$('.together div.product').eq(0),
					economy;

				economy=parseInt($together.find('.old-price span:visible').html().toString().replace(' ',''));
				economy-=parseInt($together.find('.new-price span:visible strong').html().toString().replace(' ',''));				
					
				$product.find('.img img').attr('src',$together.find('img').attr('src'));
				$product.find('.desc .name').html($together.find('span.name').html());
				$form.find('#order_product').val($form.find('div.product').eq(0).find('.name').text() + ', ' + $('.product-page .buy .price span:visible').html() );
				$form.find('input.together').val($together.find('span.name').html() + ', ' + $together.find('.new-price span:visible').html() );
				$product.find('.old-price span').html($together.find('.old-price span:visible').html())
				$product.find('.price span.value').html($together.find('.new-price span:visible').html())
				$product.find('.economy strong').html(economy);
				}
			
			var summary=parseInt(
				$productPrice.html().toString().replace(' ',''));
			
			if(buyingTogether)
				summary+=parseInt($form.find('div.product').eq(1).find('.price span strong').html().toString().replace(' ',''));
			
			$form.find('.summary-price strong').html(summary);
			},
			
		onComplete:function() {
			$('#buy-form input[name="name"]').focus()
			},
		onClosed:function() {
			$("#buy-form div.status").hide();	
			$("#buy-form form").show();
			}
		});
	
	$('a.feedback-form-href').fancybox();
	
	$('#buy-form .ajax').live('click', function() {
		$(this).hide();
		$('#buy-form .comment').show().focus();
		
		return false;
		})
		
	$('#buy-form .gift-box input').change(function() {
		var $wrapping=$('#buy-form .wrapping');
		
		if($wrapping.css('visibility')=='hidden')
			$wrapping.css('visibility','visible')
		else
			$wrapping.css('visibility','hidden');
		});
});



