function Calculate(oldprice, price, pcount, writeto)
{
	//alert(price);
	count = (isNaN(pcount.value)) ? 0 : parseFloat(pcount.value);
	price = (isNaN(price)) ? 0: parseFloat(price);
	oldprice = (isNaN(document.getElementById(writeto).innerHTML)) ? 0 : parseFloat(document.getElementById(writeto).innerHTML);
	totalprice = (isNaN(document.getElementById('basket_all_price').innerHTML)) ? 0 : parseFloat(document.getElementById('basket_all_price').innerHTML);

	var result = count * price;
	//alert(result + "\n" + totalprice + "\n" + oldprice);
	totalprice = totalprice + result - oldprice;

	totalprice = totalprice.toFixed(2);
	result = result.toFixed(2);

	document.getElementById('basket_all_price').innerHTML = totalprice;
	document.getElementById(writeto).innerHTML = result;
	pcount.value = count;
}

function getbasket() {
		$.ajax({
		type: "GET",
		url: "basket.php",
		data: 'lang=' + $(".lang_x_active").html(),
		cache: false,
		success: function(html){
			$(".basket_products").html(html);
		}
	});
}

function getorder() {
		$.ajax({
		type: "GET",
		url: "getorder.php",
		data: 'lang=' + $(".lang_x_active").html(),
		cache: false,
		success: function(html){
			data = html.split(':');
			$('input[name=AMOUNT]').val(data[0]);
			$('input[name=OTHER]').val(data[1]);
			$('input[name=DESC]').val(data[2]);
		}
	});
}

function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function setFilter() {
	var str = $("#psearch").serialize();
	$.ajax({
		type: "GET",
		url: "psearch.php",
		data: str,
		cache: false,
		success: function(html){
			$("#cat_prod_sum").html(html);
		}
	});
}

function textReplacement(input){
	var originalvalue = input.val();
	input.focus( function(){
		if( $.trim(input.val()) == originalvalue ){ input.val(''); }
		});
		input.blur( function(){
		if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
	});
}

function textReplacement2(textarea){
	var originalvalue = textarea.html();
	textarea.focus( function(){
		if( $.trim(textarea.html()) == originalvalue ){ textarea.html(''); }
		});
		textarea.blur( function(){
		if( $.trim(textarea.html()) == '' ){ textarea.html(originalvalue); }
	});
}

$.preloadImages = function() {
	for(var i = 0; i < arguments.length; i++)
	{
		$("<img>").attr("src", arguments[i]);
	}
};

function detectBrowser()
{
	var browser = navigator.appName;
	var b_version = navigator.appVersion;
	//alert(b_version);
	var re = new RegExp('MSIE 6.0');
	if (browser == "Microsoft Internet Explorer" && b_version.match(re))
	{
		if (confirm(alert1)) window.open("http://www.microsoft.com/rus/windows/internet-explorer/", "Test", "width=1024, height=768, resizable=yes,scrollbars=yes,status=yes");
	}
}

$(document).ready(function () {
	getbasket();
	detectBrowser();

	textReplacement($('input[name=search]'));
	textReplacement($('input[name=person_login]'));
	textReplacement($('input[name=person_pass]'));

	textReplacement($('input[name=product_code]'));
	textReplacement2($('textarea[name=product_comment]'));
	
	textReplacement($('input[name=profile_password]'));
	textReplacement($('input[name=profile_password_new]'));
	textReplacement($('input[name=profile_password_new2]'));
	
	$(".AddToBasket").bind("click", function(){
		var info = $(this).attr("id").split(':');
		info = 'lang=' + info[1] + '&prod_id=' + info[0];
		$.ajax({
			type: "GET",
			url: "basket.php",
			data: info,
			cache: false,
			success: function(html){
				$(".basket_products").html(html);
				if (html != '')
				{
					alert(alert2);
				}
			}
		});
	});
	
	$(".basket_minus").bind("click", function(){
		var info = $(this).attr("id").split(':');
		var info = 'prod_id=' + info[1] + '&lang=' + info[0];
		$.ajax({
			type: "GET",
			url: "delete.php",
			data: info,
			cache: false,
			success: function(html){
				getbasket();
				getorder();
				$(".basket_all_price").html(html);
			}
		});		
		if (parseInt($(this).parents().children('.basket_product_elem').html()) > 1)
		{
			$(this).parents().children('.basket_product_elem').html(parseInt($(this).parents().children('.basket_product_elem').html()) - 1);
			$(this).parents(".basket").animate({ opacity: "hide" }, 400).animate({ opacity: "show" }, 400);
		}
		else
		{
			$(this).parents('.basket').slideToggle(400).slideToggle(400);
			$(this).parents('.basket_product').animate({ opacity: "hide" }, 4);
		}
	});
	
	$(".basket_plus").bind("click", function(){
		var info = $(this).attr("id").split(':');
		var info = 'prod_id=' + info[1] + '&lang=' + info[0];
		//alert(info);
		$.ajax({
			type: "GET",
			url: "basketplus.php",
			data: info,
			cache: false,
			success: function(html){
				getbasket();
				getorder();
				$(".basket_all_price").html(html);
			}
		});
		$(this).parents(".basket").animate({ opacity: "hide" }, 400).animate({ opacity: "show" }, 400);
		$(this).parents().children('.basket_product_elem').html(parseInt($(this).parents().children('.basket_product_elem').html()) + 1);
	});
	
	$('#example-links a').click(
		function(){
			var index = $("#example-links a").index(this);
			$('#example-links a').removeClass();
			$(this).addClass('active');
			$('#example-content').animate({"marginTop" : -index*320 + "px"});
			return false;
		}
	);
	
	$('ul#top_right_banners').innerfade({
		speed: 1000,
		timeout: 3000,
		type: 'sequence',
		containerheight: '220px'
	});
	
	$("#slider").easySlider({
		prevText: 'Previous',
		nextText: 'Next',
		firstShow: true,
		lastShow: true,
		vertical: true,
		continuous: true
	});
	
	$('#order_form_button').click( function (){
		$.post('buy.php', $('#order_form_basket').serialize(),
			function(data) {
				if (data == 'success') {
					$('#order_form_basket').submit();
				}
			}
		)
		return false;
	});
});
