djConfig = {
	parseOnLoad: true
};

var waitobj={}

function genericwait(flag,obj) {
	if (flag) {
		document.body.style.cursor='wait';
		var pos1=dojo.position(document.body,true);
		if ((typeof obj)==='string') {
			var pos=dojo.position(dojo.byId(obj),true);
		} else {
			var pos=dojo.position(obj,true);
		}
		if (!waitobj[obj]) {
			waitobj[obj]=document.createElement('div');
		}
		waitobj[obj].style.zIndex=99;
		waitobj[obj].style.width=pos.w+'px';
		waitobj[obj].style.height=pos.h+'px';
		waitobj[obj].style.position='absolute';
		waitobj[obj].style.top=pos.y+'px';
		waitobj[obj].style.left=(pos.x-pos1.x)+'px';
		waitobj[obj].style.background='white url(http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojox/image/resources/images/loading.gif) center center no-repeat';
		waitobj[obj].style.opacity='0.4';
		/*@cc_on
			/*@if (@_jscript)
				waitobj[obj].style.filter='alpha(opacity=60)';
			@else @*/
				waitobj[obj].style.opacity='0.6';
			/*@end
		@*/
		document.body.appendChild(waitobj[obj]);
	} else {
		document.body.style.cursor='default';
		if (waitobj[obj]) {
			document.body.removeChild(waitobj[obj]);
		}
	}
}

function add2compare(e) {
	dojo.stopEvent(e);
	var btn=this;
	var havesidebar=true;
	var compare=dojo.byId('compare');
	var productid=dojo.byId('productid');
	var item;
	if (!compare) {
		havesidebar=false;
	}
	if (!productid) {
		compare=this;
		while (compare.nodeName.toLowerCase() != 'td') compare=compare.parentNode;
	}

	if ((compare) && (productid)) {
		item=productid.value;
	} else {
		var q=dojo.query('input[name=id]',compare);
		item=q[0].value;
	}

	if (genericwait) {
		genericwait(true,compare);
	}
	btn.style.cursor='wait';
	btn.disabled=true;

	dojo.xhrPost({
		url:"/templates/compare-sidebar.cfm",
		content: {
			'action':'Add Item',
			'item':item
		},
		handleAs:"text",
		load:function(data) {
			if (genericwait) {
				genericwait(false,compare);
			}
			btn.style.cursor='default';
			btn.disabled=false;
			if (havesidebar) {
				dojo.byId('compare').innerHTML=data;
				if (onMinicartLoad) {
					onMinicartLoad();
				}
			}
		},
		error:function(error) {
			if (genericwait) {
				genericwait(false,compare);
			}
			btn.style.cursor='default';
			btn.disabled=false;
			alert(error);
		}
	});
}

function deletecartitem(e) {
	dojo.stopEvent(e);
	genericwait(true,'minicart');
	var id=this.alt;
	dojo.xhrPost({
		url:"/templates/minicart.cfm",
			content: {
				'action':'Delete Item',
				'item':id
			},
			handleAs:"text",
			load:function(data) {
				genericwait(false,'minicart');
				dojo.byId('minicart').innerHTML=data;
				onMinicartLoad();
			},
			error:function(error) {
				genericwait(false,'minicart');
				alert(error);
			}
		});
}

function deletecompareitem(e) {
	dojo.stopEvent(e);
	genericwait(true,'compare');
	var id=this.alt;
	dojo.xhrPost({
		url:"/templates/compare-sidebar.cfm",
			content: {
				'action':'Delete Item',
				'item':id
			},
			handleAs:"text",
			load:function(data) {
				genericwait(false,'compare');
				dojo.byId('compare').innerHTML=data;
				onMinicartLoad();
			},
			error:function(error) {
				genericwait(false,'compare');
				alert(error);
			}
		});
}

function onMinicartLoad() {
	var minicart=dojo.byId('minicart');
	if (minicart) {
		dojo.query('img.mc_delete_item',minicart).forEach(function(item){
			dojo.connect(item,'onclick',deletecartitem);
		});
	}
	var minicart=dojo.byId('compare');
	if (minicart) {
		dojo.query('img.sc_delete_item',minicart).forEach(function(item){
			dojo.connect(item,'onclick',deletecompareitem);
		});
	}

	dojo.query('button').forEach(function(item) {
		switch (item.className) {
			case 'compare':
				dojo.connect(item,'onclick',item,add2compare);
				break;
			default:
				break;
		}
	});
}

google.setOnLoadCallback(onMinicartLoad);
google.load("dojo", "1.4");
