/**
 * update basket - Version 1.0
 * (c) 2009 Nguyen Hoang
 * Update : 6/03/2009
 *
 * All basket functions 
 */
	msgAdd_basket_success = "This product's been added into your basket";
	var msgError = "Error! Please try again!";
	order_page = ROOT_URL + 'modules.php?module=order&file=order';
	var flyingDiv;
	
function Add_good(id, quantity) 
{
	//ajaxRead("classes/shopping_basket.class.php?shopping_basket_aj=1&good_id=" + id + "&total=" + quantity + "&basket_name=current_basket");
	var aj = new AJAX ("modules.php");
	aj.execute = true;
	aj.onCompletion = function() {
							Display_thumb_basket();
						};
	aj.run("module=shopping_basket&file=basket_actions&op=basket_add_good_action&good=" + id);
}

function Update_good(str, display) 
{
	var aj = new AJAX ("modules.php");
	aj.execute = true;
	aj.onCompletion = function() {
							if (display) {
								Display_thumb_basket();
								Display_main_basket();
							}
						};
	
	//document.write("module=shopping_basket&file=basket_actions&op=basket_update_quantity_action&" + str);					
	aj.run("module=shopping_basket&file=basket_actions&op=basket_update_quantity_action&" + str);
	
	//ajaxRead("actions.php?module=basket&op=update_quantity" + str + "&display=" + display);
	
}

function Remove_good(id) 
{	
	
	var aj = new AJAX ("modules.php");
	aj.element = "result";
	aj.execute = true;
	aj.onCompletion = function() {
							Display_thumb_basket();
						};
	aj.run("module=shopping_basket&file=basket_actions&op=remove_good_action&good=" + id);
}

function Update_basket(id, quantity) {
	
	Add_good(id, quantity);
		
	writeCookie("current_basket_changed", 1, 8760);
	
	sid = 'good_' + id;
	n = sid.lastIndexOf('-'); // Extend product
	if (n != -1) sid = sid.substring(0, n);
	
	try {
		if(!flyingDiv){
			flyingDiv = document.createElement('DIV');			
			flyingDiv.style.position = 'absolute';			
			flyingDiv.style.zIndex = 1000;			
			document.body.appendChild(flyingDiv);
		}
				
		flyingDiv.innerHTML = $ele(sid).innerHTML;
		
		var divX = getOffsetLeft($ele(sid));
		var divY = getOffsetTop($ele(sid));
		
		var posX = getOffsetLeft($ele("thumb_basket")) ;
		var posY = getOffsetTop($ele("thumb_basket"));
		
		var dX = posX - divX;
		var dY = posY - divY;
		
		var signX = "+";
		var signY = "+";
		if (dX < 0) signX = "-";
		if (dY < 0) signY = "-";
		
		$(flyingDiv).css({'top': divY + "px", 'left': divX + "px"});
		
		$(flyingDiv).show();
			
		$(flyingDiv).animate({ 'left': signX + "=" + Math.abs(dX) + "px",'top': signY + "=" + Math.abs(dY)+ "px" }, 1500, "swing", function () { $(this).hide(); });
	}catch(ex) {
	//	alert(ex.message);
	}
		
	return true;
}

// Add a good to thumb_basket

function Add_thump_basket(detail_page, id, name, value, quantity) {
	var table = document.getElementById("thumb_basket");	
	var price_label = document.getElementById("thumb_total_price");	
	
	if (!table || !price_label) return false;
		
	var total_price = price_label.innerHTML;				
	
	var strCheck = '.0123456789';
	pos1=0; pos2=0;
	for(var i=0; i < total_price.length; i++)  {
		if (strCheck.indexOf(total_price.charAt(i)) != -1) {
			if (!pos1) pos1=i;
			else pos2=i;
		}
	}
	if (pos2 < pos1) pos2 = pos1;
	
	dv = total_price.substr(0, pos1);
	
	total_price =  dv + (parseFloat(total_price.substr(pos1, pos2-pos1+1)) + value) + total_price.substr(pos2+1);
	
	var tbody = table.getElementsByTagName("tbody")[0];   
	var row = document.createElement("TR"); 
	var cell1 = document.createElement("TD"); 
	var cell2 = document.createElement("TD"); 
 			
	cell1.innerHTML = '<a class=good href="' + detail_page + '?good=' + id + '">' + name + '</a>';
	cell2.innerHTML = dv + (value * quantity);		
	cell2.setAttribute('align','right');	
	
	row.appendChild(cell1); 
	row.appendChild(cell2); 
	
	tbody.appendChild(row); 

	pos1 = total_price.indexOf(".");
	if (pos1 != -1) {
		total_price = total_price.substr(0, pos1) + total_price.substr(pos1, 3);
	}
	
	price_label.innerHTML = total_price;
	
	table.style.display='block';	
	document.getElementById("thumb_basket_result").style.display='block';
	document.getElementById("empty_basket").style.display='none';
	
	tbody = null;
	
	
	return true;
}

function Display_thumb_basket(contentID) {
	if (!contentID) contentID = "thumb_basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=thumb_basket_action", contentID , "innerHTML");
}
function Display_order_basket(contentID) {
	if (!contentID) contentID = "basket_info";
	
	ajaxRead("modules.php?module=shopping_basket&op=order_basket_action", contentID , "innerHTML");
}

function Display_main_basket(contentID) {
	if (!contentID) contentID = "basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=main_basket_action", contentID, "innerHTML");
}


function Display_tracking_basket(contentID) {
	if (!contentID) contentID = "basket";
		ajaxRead("modules.php?module=shopping_basket&op=main_basket_action&basket_type=tracking_basket", contentID, "innerHTML");
}

function Display_basket_index(contentID) {
	if (!contentID) contentID = "sliderContent";
		ajaxRead("modules.php?module=shopping_basket&op=thumb_basket_index_action", contentID, "innerHTML");	
}
	
function Display_thumb_item_basket(contentID) {
	if (!contentID) contentID = "thumb_item_basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=thumb_item_basket_action", contentID , "innerHTML");
}
function Display_thumb_price_basket(contentID) {
	if (!contentID) contentID = "thumb_price_basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=thumb_price_basket_action", contentID , "innerHTML");
}

function Display_basket(id) {				
	/*if(document.getElementById(id).style.display == "block") {
			document.getElementById(id).style.display = "none";
	}else 
			document.getElementById(id).style.display = "block";
	*/
	if ($("#" + id).is(":hidden"))
	 jQuery("#" + id).slideDown("slow");
	else  jQuery("#" + id).hide("slow");
}

function Add_to_basket(id,order_now) {					
		
	try{
		//alert(id);
		if (!order_now) order_now = false;
		if (!Update_basket(id,1))
			alert(msgError);
		else 
			alert(msgAdd_basket_success);
			
		if (order_now) window.location = order_page;			
						
	}catch(er) {
		alert(er.message);
	}
					
}

function Load_fly_to_basket() {
	if (document.body )	{
				
		ele_fly_to_basket = document.createElement("SCRIPT");
		ele_fly_to_basket.type = 'text/javascript';
		ele_fly_to_basket.src = ROOT_URL + "js/fly-to-basket.js";		
							
		document.body.appendChild(ele_fly_to_basket);
	}else {
		document.write("<script language=\"JavaScript\" src=\"" + ROOT_URL + "js/fly-to-basket.js\" type='text/javascript'></script>");
	}
}


