var category_id = 0;
var page = 1;
var currency_id = 1;
var vendor_id = 0;
var photo = 1;
var compareSel = new Array();
var type_view = 2;
var search = '';
var delivery_price = 0;
var order_name = 'name';
var order_type = 'asc';

$(document).ready(function()
{
	/** Top Menu **/

	$('.menuTop').live('mouseenter mouseleave', function()
	{
		$(this).toggleClass('menuTop_over');
	})
	.click(function()
	{
		$('.menuTopSel').removeClass('menuTopSel');

		$(this).addClass('menuTopSel');

		var section = $(this).attr('id').substr(7).toLowerCase();

		$.get('index.php?' + section + '&ajax=1', function(data)
		{
			$('#mainBlock').html(data);
		});

		return false;
	});

	$(document).on('click', '.news', function()
	{
		var id = $(this).data('id');

		$.get('index.php?news=' + id + '&ajax=1', function(data)
		{
			$('#mainBlock').html(data);
		});

		return false;
	});
	
	/** Products **/

    /*$().ajaxStart(function() {$("#waiter").show();});
	$().ajaxStop(function() {$("#waiter").hide();});*/

	/** **/

	function removeSelCategory(category_level)
	{
		compareSel = new Array();
		if (typeof(category_level) == undefined) category_level = 2;

		if (category_level == 2) $('.categoryLevel2').removeClass('selectCategory');

		category_id = 0;
		page = 1;
		search = '';
	}

	/** **/

	$('.categoryLevel1').click(function()
	{
		$(this).parent().children('ul').toggleClass('category_hide');

		return false;
	});

	$(document).on('click', '.category', function()
	{
		vendor_id = 0;
		var category_level = 2;
		$('#price_start, #price_end').val('');

		if ($(this).hasClass('categoryLevel3')) category_level = 3;

		removeSelCategory(category_level);

		category_id = $(this).data('category');

		$(this).addClass('selectCategory');

		getProductsTable();

		return false;
	});

	$(document).on('change', '#type_view_sel', function()
	{
		type_view = $(this).val();

		getProductsTable();
	});

	$(document).on('click', '.setType', function()
	{
		type_view = $('#type_view_sel').val();
		currency_id = $('#currency_sel').val();

		getProductsTable();

		return false;
	});

	$(document).on('change', '#currency_sel', function()
	{
		currency_id = $(this).val();

		getProductsTable();
	});

	$(document).on('change', '#availability', function()
	{
		getProductsTable();
	});

	$(document).on('change', '#vendor_sel', function()
	{
		page = 1;
		vendor_id = $(this).val();

		getProductsTable();
	});

	function getProductsTable()
	{
		if (search == '')
		{
			url = 'products.php?category_id=' + category_id;
		}
		else
		{
			url = 'search.php?search=' + search;
		}

		var price_start = $('#price_start').val();
		var price_end = $('#price_end').val();
		var availability = $('#availability').val();

		url += '&ajax=1&page=' + page + '&type_view=' + type_view + '&currency=' + currency_id + '&price_start=' + price_start + '&price_end=' + price_end + '&vendor=' + vendor_id + '&availability=' + availability + '&order=' + order_name + '_' + order_type;

		$.get(url, function(data)
		{
			$('#mainBlock').html(data);
		});
	}

	$(document).on('click', '.order', function()
	{
		var name = $(this).data('order');

		if (order_name == name)
		{
			if (order_type == 'asc') order_type = 'desc';
			else order_type = 'asc';
		}
		else
		{
			order_name = name;
			order_type = 'asc';
		}

		getProductsTable();

		return false;
	});

	$(document).on('click', '#orderSend', function()
	{
		var surname = $('#surname').val();
		var name = $('#name').val();
		var phone = $('#phone').val();
		var email = $('#email').val();

		if (surname == '' || name == '' || phone == '' || email == '')
		{
			alert('Поля: Фамилия, Имя, Телефон, E-Mail обязательны к заполнению!');
		}
		else
		{
			var str = $('#formOrder').serialize();

			$.post('basket.php?orderSend', str, function(data)
			{
				$('#mainBlock').html(data);
			});

			$('#basket_count').html(0);

			delivery_price = 0;
		}

		return false;
	});

	$('#searchBut').click(function()
	{
		removeSelCategory();

		search = $('#input_search').val();

		$('#input_search').val('');

		getProductsTable();

		return false;
	});

	$(document).on('click', 'a.page', function()
	{
		page = $(this).html();

		getProductsTable();

		return false;
	});

	$(document).on('click', 'a.viewProduct', function()
	{
		var id = $(this).data('id');

		win = window.open('products.php?getProduct='+id+'&ajax=1', 'name', 'width=1000,height=700,scrollbars=1');

		if (window.focus) {win.focus();}

		return false;
	});

	/** **/

	$('.miniPic').click(function()
	{
		$('.miniPic img').removeClass('selected');

		var id = $(this).data('id');
		var path = $(this).data('path');

		$('#bigPhoto').attr('src', '/images/products/' + path + id + '_350.jpg');
		$('#bigPhoto').parent().attr('href', '/images/products/' + path + id + '.jpg');
		$(this).children('img').addClass('selected');

		return false;
	});

	/** **/

	$(document).on('click', '.addBasket', function()
	{
		var id = $(this).data('id');

		$.get('basket.php?add=' + id, function(data)
		{
			$('#basket_count').html(data);
		});	

		return false;
	});

	$('#basket').click(function()
	{
		removeSelCategory();

		$.get('basket.php?basket', function(data)
		{
			$('#mainBlock').html(data);
		});

		return false;
	});

	$(document).on('change', '#pay_type', function()
	{
		if ($(this).val() == 2)
		{
			$('#bz5').show();
		}
		else
		{
			$('#bz5').hide();
		}
	});

	$(document).on('click', '#calculate', function()
	{
		var str = $('#formBasket').serialize();

		$.post('basket.php?calculate', str, function(data)
		{
			$('#mainBlock').html(data);
		});

		return false;
	});

	$(document).on('click', '#order', function()
	{
		var str = $('#formBasket').serialize();

		$.post('basket.php?order', str, function(data)
		{
			$('#mainBlock').html(data);
		});

		return false;
	});

	$(document).on('click', '#sendOrder', function()
	{
		var str = $('#formOrder').serialize();

		$.post('products.php?sendOrder', str, function(data)
		{
			$('#mainBlock').html(data);
		});

		$('#basket_count').html(0);

		return false;
	});

	$(document).on('click', '#delivery_sel', function()
	{
		setDelivery();
	});

	$(document).on('click', '#setDelivery', function()
	{
		setDelivery();

		return false;
	});

	function setDelivery()
	{
		var price_total = mathFloat($('#price_total').html(), delivery_price, 'minus');

		delivery_price = $('#delivery_sel option:selected').attr('price');
		price_total = mathFloat(price_total, delivery_price, 'plus');

		$('.delivery_pr').html(delivery_price);

		$('#price_total').html(price_total);
	}

	/** Compare **/

	$(document).on('click', '.compCheck', function()
	{
		var id = $(this).attr('id').substr(4);
		var tmpArray = new Array();

		if ($(this).attr('checked'))
		{
			compareSel.push(id);
		}
		else
		{
			for (var key in compareSel)
			{
				if (compareSel[key] != id) tmpArray.push(compareSel[key]);
			}

			compareSel = tmpArray;
		}
	});

	$(document).on('click', '#compBut', function()
	{
		var str = '';

		for (var key in compareSel)
		{
			str += '&comp[]=' + compareSel[key];
		}

		var window_compare = window.open('products.php?compare' + str, 'name', 'width=1000,height=700,scrollbars=1');

		if (window.focus) {window_compare.focus();}

		return false;
	});

	function mathFloat(val1, val2, act)
	{
		var result;

		result = (+val1);
		if (act == 'plus') result += Number(val2);
		else result -= Number(val2);

		result = Math.round(result*Math.pow(10, 2))/Math.pow(10, 2);

		return result;
	}
});
