function item_box(id) {
	document.getElementById('quickopen').style.display = 'block';
	document.getElementById('quickopen').style.top = yMousePos-200;
	document.getElementById('quickopen').style.left = xMousePos;
	ajax.Server('index.php?a=itemshort&id='+id+'&embed=quickopen');
}

var cancel_hide=0;
function hide_item_box() {
	cancel_hide=0;
	setTimeout('do_hide_item_box()', 500);
}

function do_hide_item_box() {
	if(!cancel_hide) {
		document.getElementById('quickopen').style.display = 'none';
	}
}

function close_item_box() {
	document.getElementById('quickopen').style.display = 'none';
}

function clear_cart() {
	ajax.Server('?a=cart/clear');
}

function remove_prd_tr(num) {
	var tbody = $("prd_body");
	for(a=0;a<tbody.rows.length;a++) {
		if(tbody.rows[a].id=="prd_tr_"+num)
			tbody.removeChild(tbody.rows[a]);
	}
}

function view_input(obj,id) {
	var source = obj.innerText;
	var evnt = "update_prd_details(this,'"+source+"','"+id+"')";
	obj.outerHTML='<input style="width:38px;height:20px;" type="text" value="'+source+'" onblur="'+evnt+'">';
}

function update_prd_details(obj,source,id) {
	var val = parseFloat(obj.value);
	if(isNaN(val)) {
		alert("Please enter number!");
		val = source;
	}
	var evnt = "view_input(this,'"+id+"')";
	obj.outerHTML = '<span onclick="'+evnt+'">'+val+'</span>';
	ajax.Server('?a=cart/update_prd_details&id='+id+'&val='+val);
}

function craete_order() {
	ajax.Server('?a=cart/craete_order');
}

function order_list(orderby,id,desc){
	var url='?a=navigator/main&id='+id+'&orderby='+orderby+'&j='+nowadays();
	if(desc=='1'){
		url+='&orderdesc=1';
	}
	document.location=url;
}

function move_to_page(page,id,order_by,is_desc){
	var url='?a=navigator/main&id='+id+'&page='+page+'&j='+nowadays()+'&orderby='+order_by;
	if(is_desc=='1'){
		url+='&orderdesc=1';
	}
	document.location=url;
}

function order_result(orderby,s,desc,set) {
	if(!set) {
		var url='?a=navigator/find&search_prd';
	} else {
		var url='?a=navigator/pset&set';
	}
	url += '='+s+'&orderby='+orderby+'&j='+nowadays();
	if(desc=='1'){
		url+='&orderdesc=1';
	}
	document.location=url;
}

function move_to_found_page(page,s,order_by,desc,set){
	if(!set) {
		var url='?a=navigator/find&search_prd';
	} else {
		var url='?a=navigator/pset&set';
	}
	url += '='+s+'&page='+page+'&j='+nowadays()+'&orderby='+order_by;
	if(desc=='1'){
		url+='&orderdesc=1';
	}
	document.location=url;
}

function add_product_to_basket(product_id,is_unknown_user){
	//check if unknown user
	if (is_unknown_user){
		document.location="?a=cart/show";
		return;
	}
	
	//fly img to basket
	var img_product_id='imgProduct_'+product_id;
	var button_id='productBtn_'+product_id;
	productFly(img_product_id,button_id);
	
	//sent data to server
	ajax.Server('?a=cart/add&id='+product_id);
}

function remove_product_from_basket(id){
	ajax.Server('?a=cart/remove&id='+id);
	document.location="?a=cart/show";
}

function clear_container(){
	ajax.Server('?a=cart/clear');
	document.location="?a=cart/show";
}

/*********************
Flying img functions
*********************/

//initialize global variables
var imgProductFlyStep;
var imgProductFlyStartDim = {x:0,y:0};
var imgProductFlyEndDim   = {x:0,y:0};

function productFly (imgProductId,butId)
{
    // initialize objects
    var divShopCart          = document.getElementById ('cartTab');
    var imgShopCartProduct   = document.getElementById ('imgShopCartProduct');
    var imgProduct           = document.getElementById (imgProductId);
    var butObject            = document.getElementById (butId);
    
    if (! (divShopCart && imgShopCartProduct && imgProduct) ){
        return;
    }

    // start flying product thumbnail
    var butDim = getDim (butObject);
    var divDim = getDim (divShopCart);

    imgProductFlyStartDim.x = butDim.x+butDim.w/2-50;
    imgProductFlyStartDim.y = butDim.y+butDim.h/2-50;
    imgProductFlyEndDim.x   = divDim.x+divDim.w/2-50;
    imgProductFlyEndDim.y   = divDim.y+divDim.h/2-50;
    imgProductFlyStep = 0;
    
    imgShopCartProduct.src = imgProduct.src;
    imgShopCartProduct.style.width   = '100px';
    imgShopCartProduct.style.height  = '100px';
    imgShopCartProduct.style.left    = imgProductFlyStartDim.x.toString()+'px';
    imgShopCartProduct.style.top     = imgProductFlyStartDim.y.toString()+'px';
    imgShopCartProduct.style.display = 'block';
   
    productFlyToCart();
}

function productFlyToCart()
{
    // initialize object
    var imgShopCartProduct = document.getElementById ('imgShopCartProduct');
    if (!imgShopCartProduct)
        return;
        
    // advance fly step and check for finish
    imgProductFlyStep +=0.1;
    if (imgProductFlyStep >= 1)
    {
        imgShopCartProduct.style.display = 'none';
        return;
    }
    
    // calculate currents coords
    var x = imgProductFlyStartDim.x * (1-imgProductFlyStep) + 
            imgProductFlyEndDim.x   *    imgProductFlyStep;
    var y = imgProductFlyStartDim.y * (1-imgProductFlyStep) + 
            imgProductFlyEndDim.y   *    imgProductFlyStep;

    // move flying product thumbnail
    imgShopCartProduct.style.left = x.toString()+'px';
    imgShopCartProduct.style.top  = y.toString()+'px';
    
    // wait for next step
    setTimeout ('productFlyToCart()',40);
}

function getDim(el)
{
    var lw=el.offsetWidth;
    var lh=el.offsetHeight;

    for (var lx=0,ly=0;
         el!=null;
         lx+=el.offsetLeft, ly+=el.offsetTop, el=el.offsetParent);
    return {x:lx,y:ly,w:lw,h:lh}
}

function check_search_text(search){
	var len = search.length;
	if(!search || len<2){
		alert('נא חפש מילה עם שני תווים לפחות');
		return false;
	}
	return true;
}