// JavaScript Document
var Dxd = {};

Dxd.googleOnLoadCallback = new Array();

Dxd.showAddress = function(address, targetdiv) {

	var map = new GMap2(document.getElementById(targetdiv));
	var geocoder = new GClientGeocoder();
			
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " cannot be found in Google Maps");
			} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(address);
			}
		}
	);
	  
}

Dxd.getClosestRetailer = function(callurl, opt) {
	
	if(!opt) {
		opt = {};
	}
	
	if(!opt.callback) {
		opt.callback = function(response) {};	
	}
	
	var city = google.loader.ClientLocation.address.city;
	var region = google.loader.ClientLocation.address.region;
	var lat = google.loader.ClientLocation.latitude;
	var lon = google.loader.ClientLocation.longitude;
	var url = callurl + '?city=' + city + '&region=' + region + '&lat=' + lat + '&lon=' + lon;
	// alert(url);
	GabeUtil.ajaxRequest('get', url, {
		onLoad : function(response) {
			opt.callback(response);
		}
	}, 'json');
	
}

Dxd.submitCartCheckout = function() {
	var html = '';
	jQuery.each($(".quantityfld"), function(i, val) {
		var quantity = parseInt($(val).val(), 10);
		if(quantity <= 0) {
			return;
		}
		// <input type=checkbox name=item_rye value="a-8010^rye^loaf of Rye^2.22^1">
		var itemcode = $(val).metadata().itemcode;
		var name = $(val).metadata().name;
		var weight = $(val).metadata().weight;
		var price = $(val).metadata().price;
		html += '<input type="hidden" name="item_' + itemcode + '" value="a-8010^' + itemcode + '^' + name + '^' + price + '^' + quantity + '^^^^' + weight + '">' + "\n";
	});
	$("#cartcheckoutform .products").html(html);
	// alert(html);
	$("#cartcheckoutform").submit();
	$(".productinfowrapper").hide();
	$("#americart-wrapper").show();
	// alert(html);
}

Dxd.emptycart = function(url, opt) {
	
	if(!opt) {
		var opt = {};
	}
	
	if(!opt.callback) {
		opt.callback = function(response) {};	
	}
	
	GabeUtil.ajaxRequest('get', url, {
		onLoad : function(response) {
			opt.callback(response);
		}
	}, 'json');
	
}

Dxd.getcart = function(targetdiv, url, opt) {
	
	if(!opt) {
		var opt = {};
	}
	
	if(!opt.setUrl) {
		opt.setUrl = null;
	}
	
	GabeUtil.ajaxRequest('get', url, {
						 
		onLoad : function(response) {
			
			$(targetdiv).html(response);
			
			$("#cartcheckoutbtn").click(function(event) {
				Dxd.submitCartCheckout();
			});
			
			$("#cartemptybtn").click(function(event) {
				var conf = confirm("Are you sure you would like to empty your shopping cart?");
				if(!conf) {
					return;		
				}
				Dxd.emptycart("request/emptycart.php", {
					callback : function(response) {
						Dxd.getcart(targetdiv, url, opt);	
					}			  
				});
			});
			
			$(".savequantitybtn").click(function(event) {
				var id = $(this).metadata().id;
				var quantity = parseInt($(this).prev().val(), 10);
				Dxd.setcartitem(id, quantity, opt.setUrl, {
					success : function(response) {
						if(response.status == 'OK') {
							Dxd.getcart(targetdiv, url, opt);
							// window.location.href = 'cart.php';
						} else {
							alert(response.message);
						}
					}
				});
			});
			
			$(".removeproductbtn").click(function(event) {
				var conf = confirm("Would you like to remove this item from your shopping cart?");
				if(!conf) {
					return false;
				}
				var id = $(this).metadata().id;
				var quantity = 0;
				Dxd.setcartitem(id, quantity, opt.setUrl, {
					success : function(response) {
						if(response.status == 'OK') {
							Dxd.getcart(targetdiv, url, opt);
							// window.location.href = 'cart.php';
						} else {
							alert(response.message);
						}
					}
				});
			});
			
		}
		
	});
	
}

Dxd.addtocart = function(id, quantity, url, opt) {
	if(!opt) {
		var opt = {};	
	}
	if(!opt.success) {
		opt.success = function(response) {};
	}
	if(!url) {
		url = '/request/addtocart.php';
	}
	var newurl = url + '?id=' + id + '&quantity=' + quantity;
	// alert(newurl);
	GabeUtil.ajaxRequest('get', newurl, {
		onOK : function(response) {
			opt.success(response);
		},
		onERROR : function(response) {
		}
	}, 'json');
}

Dxd.setcartitem = function(id, quantity, url, opt) {
	if(!opt) {
		var opt = {};	
	}
	if(!opt.success) {
		opt.success = function(response) {};
	}
	if(!url) {
		url = '/request/setcartitem.php';
	}
	var newurl = url + '?id=' + id + '&quantity=' + quantity;
	// alert(newurl);
	GabeUtil.ajaxRequest('get', newurl, {
		onLoad : function(response) {
			// alert(response);	
		},
		onOK : function(response) {
			opt.success(response);
		},
		onERROR : function(response) {
		}
	}, 'json');
}

Dxd.registerDeleteContent = function(opt) {
	if(!opt.successCallback) {
		opt.successCallback = function(response) {
		}	
	}
	$(".deletecatbtn").click(function(event) {
		var conf = confirm("Are you sure you would like to delete this item?");
		if(!conf) {
			return;
		}
		var id = $(this).metadata().id;
		var url = 'request/deletepost.php?id=' + id + '&ts=' + Math.random();
		GabeUtil.ajaxRequest('get', url, {
			onOK : function(response) {
				opt.successCallback(response);		 
			}
		}, 'json');
	});
}

Dxd.registerAttributeDiv = function(parent) {
	$(".deleteattribute", $(parent)).click(function(event) {
		var conf = confirm("Are you sure you would like to delete this item?");
		if(!conf) {
			return;
		}
		$(this).closest("div").remove();
	});		
}
