// JavaScript Document

/*
 *  CART
 */

function shopAddProduct(id) {
	jQuery.get(
		  "/api.php"
		, { request: "shopaddproduct", product: id }
		, function(data) { 
			refeshCart();
		}
		, "html"
	);
}

function shopRemoveProduct(id) {
	jQuery.get(
		  "/api.php"
		, { request: "shopremoveproduct", product: id }
		, function(data) { 
			refeshCart();
		}
		, "html"
	);
}

function shopTotalRemoveProduct(id) {
	jQuery.get(
		  "/api.php"
		, { request: "shoptotalremoveproduct", product: id }
		, function(data) { 
			refeshCart();
		}
		, "html"
	);
}

function refeshCart() {
	jQuery.get(
		  "/api.php"
		, { request: "shoprefreshcart" }
		, function(data) {
			jQuery("#shoppingcartcontent").html(data);
		}
		, "html"
	);
}

function emptyCart() {
	jQuery.get(
		  "/api.php"
		, { request: "shopresetcart" }
		, function(data) {
			refeshCart();
		}
		, "html"
	);
}



/*
 *  SEARCHBOX
 */

function blurSearchbox() {
	if(jQuery("#input_searchq").val() == '') {
		jQuery("#input_searchq").val("search");
	}
	else {
		execSearchbox();
	}
}

function focusSearchbox() {
	if(jQuery("#input_searchq").val() == 'search') jQuery("#input_searchq").val("");
}

function execSearchbox() {
	var sk = jQuery("#input_searchq").val();
	if(sk.length > 1) {
		jQuery.get(
			  "/api.php"
			, { request: "shopsearchproducts", key: sk }
			, function(data) { 
				jQuery("#middle").html(data);
			}
			, "html"
		);
	} 
	if(sk.length == 0) {
	   sk = '';
	   jQuery.get(
			  "/api.php"
			, { request: "shopsearchproducts", key: sk }
			, function(data) { 
				jQuery("#middle").html(data);
			}
			, "html"
		);
	}
}


jQuery(document).ready(function() {
	jQuery("#input_searchq").bind("focus", focusSearchbox);
	jQuery("#input_searchq").bind("blur",  blurSearchbox);
	jQuery("#input_searchq").bind("keyup", execSearchbox);
});
