//ə

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 gettotal(info) {
	$.get('AddBasket.php', info,
		function(data){
			data = data.split(':');
			$('.total_count').html(data[0]);
			$('.total_price').html(data[1] + ' €');
			$('.my_basket div').html('(' + data[0] + ')');
		}
	);
}

function clearBasket() {
	$.get('ClearBasket.php', '',
		function(data){
			data = data.split(':');
			$('.my_basket div').html('(' + data[0] + ')');
		}
	);
}

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]);
	}
};

$(document).ready(function () {
	function getbasket(info) {
		$.get('AddBasket.php', info,
			function(data){
				data = data.split(':');
				$('.my_basket div').html('(' + data[0] + ')');
			}
		);
	}

	getbasket('');
	
	textReplacement($('input[name=search]'));

	$(".AddToBasket").click( function(){
		var info = 'id=' + $(this).attr('rel');
		getbasket(info);
		x = $(this).children('div').offset();
		$("<div/>", {
		  "class": "basket_anim"
		}).appendTo("body");
		$('.basket_anim').css('left', x.left);
		$('.basket_anim').css('top', x.top);
		$('.basket_anim').css('display', 'block');
		y = $('.my_basket div').offset();
		$('.basket_anim')
			.animate( { left: y.left - 1 }, 1500 )
			.animate( { top: y.top + 5, opacity: 0.4 }, 1500, function() {
			$(this).remove();
		});
		return false;
	});
	
	$(".quantity a.minus").click( function(){
		var info = 'id=' + $(this).attr('rel') + '&type=minus';
		gettotal(info);
		if (parseInt($(this).parents('.quantity').children('div').html()) > 1)
		{
			$(this).parents('.quantity').children('div').html(parseInt($(this).parents('.quantity').children('div').html()) - 1);
			$(this).parents('tr').animate({ opacity: "hide" }, 400).animate({ opacity: "show" }, 400);
		}
		else
		{
			$(this).parents('.basket').slideToggle(400).slideToggle(400);
			$(this).parents('tr.prod_i').animate({ opacity: "hide" }, 4);
		}
	});
	
	$("#clear_basket").click( function(){
		clearBasket();
		$('.basket').animate({ opacity: "hide" }, 400);
		$('.make_order_wrapper').animate({ opacity: "hide" }, 400);
		$('.content_static').append('<ul class="make_order" style="margin: 10px 10px 20px 10px;"><li class="error">' + empty + '</li></ul>');
	});
	
	$(".quantity a.plus").click( function(){
		var info = 'id=' + $(this).attr('rel');
		if (parseInt($(this).parents('.quantity').children('div').html()) > 0)
		{
			$(this).parents('.quantity').children('div').html(parseInt($(this).parents('.quantity').children('div').html()) + 1);
			$(this).parents('tr').animate({ opacity: "hide" }, 400).animate({ opacity: "show" }, 400);
			gettotal(info);
		}
	});
});
