$(function() {
	
	// Slider
	$("#slider").easySlider({
		auto: true,
		continuous: true,
		controlsShow: false,
		speed: 1200,
		pause: 9000
	});		
	
	var getCurrency = $.cookie("currency");
	var currency = (getCurrency) ? getCurrency : "usd";
	
	if(currency == "usd") {
		
		$('.price-uah').hide();
		$('.currency').html('USD');
		
		if($('.trade-count').text() != 0)
			$('.basket-currency').html('USD');
	}
	else {
		
		$('.price-usd').hide();
		$('.currency').html('UAH');	
		
		if($('.trade-count').text() != 0)	
			$('.basket-currency').html('UAH');		
	}
	
	$('.currency,.basket-currency').click(function() {
		
		getCurrency = $.cookie("currency");
		currency = (getCurrency) ? getCurrency : "usd";
		
		$.get('/engine/ajax/changeCurrency.php', {currency:currency}, function(data) {
			
			$('.trade-price').html(data);
		});
		
		if(currency == "usd") {
				
			$('.price-usd').hide();
			$('.price-uah').show();
				
			$.cookie("currency", "uah", { expires:7, domain:'fornet.net.ua', path:'/'});
			$('.currency').html('UAH');
			
			if($('.trade-count').text() != 0)
				$('.basket-currency').html('uah');			
		}
		else {
			
			$('.price-uah').hide();
			$('.price-usd').show();
	
			$.cookie("currency", "usd", { expires:7, domain:'fornet.net.ua', path:'/'});
			$('.currency').html('USD');	
			
			if($('.trade-count').text() != 0)	
				$('.basket-currency').html('usd');			
		}
		
		return false;
	});
	
	// фокус на поиск
	$('#poisktext').focus();
	
	// Зебра для таблиц
	$("table#catalogtable tr:nth-child(even)").css('background', '#f7f7f7');	
	
	$('.hided').hide();
	
	$('.arrow').hover(
	function(){
		$('> div', this).stop().show();
	},
	function(){
		$('> div', this).stop().hide();
	});
	
	// ToolTip
	$(".tip-actionfree").each(function(i) {
		
		var imgTip = $(this).next().html();
	   	$(this).simpletip({ content: imgTip });
	});		
	
	$(".tip-featured").each(function(i) {
		
		var imgTip = $(this).next().html();
	   	$(this).simpletip({ content: imgTip });
	});
	
	$(".tip-almostfree").each(function(i) {
		
		var imgTip = $(this).next().html();
		$(this).simpletip({ content: imgTip });
	});		
	
	$(".tip-popular").each(function(i) {
		
		var imgTip = $(this).next().html();
	   	$(this).simpletip({ content: imgTip });
	});		
	
	// Autocomplete
	var cache = {};
	$("#poisktext").autocomplete({
			
		source: function(request, response) {
				if (cache.term == request.term && cache.content) {
					response(cache.content);
					return;
				}
				if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
					response($.ui.autocomplete.filter(cache.content, request.term));
					return;
				}				
			$.ajax({
				url: "/engine/ajax/getAutocomplete.php",
				dataType: "json",
				data: request,
				success: function(data) {
					cache.term = request.term;
					cache.content = data;						
					response(data);
				}
			});
		},
		minLength: 2,
		select: function() {
			
			//('form#searchform').submit();
		}
	});
	
	// setBasket
	$('.order-submit').click(function() {
		
		var count = $('#count').val();
		var product_id = $('#product_id').val();
		
		$.getJSON('/engine/ajax/setBasket.php', {count:count, product_id:product_id}, function(data) {
			
			var isDouble = data["double"];
			
			if(isDouble == "0") {
				
				$('.trade-count').html(data["count"]);
				$('.trade-price').html(data["price"]);
				$('.basket-currency').html(currency);
			}
			$('.trade-result').html(data["message"]);
		})
	});		
	
	// Валидация формы регистрации
	$("#regform").validate();
});
