var cart_connection = 0;
var fadeout = 0;
var last_shown_dialog = null;

function addItem(item_id, variation_id, from_search) {
	if (from_search == true) {
		var from_search = "true";
	} else {
		var from_search = "false";
	}
	ran = Math.round(Math.random()*100000); 
	url = "/functions/cart/add_item.php?ItemId="+item_id+"&VariationId="+variation_id+"&FromSearch="+from_search+"&ran="+ran.toString();

	if (window.XMLHttpRequest) {
		cart_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		cart_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (cart_connection) {
		cart_connection.open("GET",url,true);
		cart_connection.send(null);
		cart_connection.onreadystatechange=addItemEval
	}
}

function addItemEval() {
	if (cart_connection.readyState==4) {
		if (cart_connection.status==200){
			eval(cart_connection.responseText);
		}
	}
}

function addItemDialog(object) {
	if (last_shown_dialog) {
		closeCartDialog(last_shown_dialog);
		last_shown_dialog = null;
	}
	if (fadeout) {
		clearTimeout(fadeout);
	}
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	// Scroll position
	var scrollPos = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        scrollPos = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        scrollPos = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        scrollPos = document.documentElement.scrollTop;
    }
	
	var w = document.getElementById(object).offsetWidth;
	var h = document.getElementById(object).offsetHeight;

	leftpos = (windowWidth-w)/2;
	toppos = (windowHeight-h)/2+scrollPos;
	
	document.getElementById(object).style.top = Math.round(toppos)+'px';
	document.getElementById(object).style.left = Math.round(leftpos)+'px';
	
	document.getElementById(object).style.visibility = 'visible';
	
	fadeout = setTimeout("closeCartDialog('"+object+"')", 5000);
	
	last_shown_dialog = object;
}

function closeCartDialog(object) {
	if (object == 'cart_dialog' && document.getElementById('cart_dialog_hide').checked) {
		hideDialog();
	}
	document.getElementById(object).style.visibility = 'hidden';
}

function hideDialog() {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/cart/hide_dialog.php?ran="+ran.toString();
	hide_connection = 0;

	if (window.XMLHttpRequest) {
		hide_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		hide_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (hide_connection) {
		hide_connection.open("GET",url,true);
		hide_connection.send(null);
	}
}

function changeQuantity(array_key, direction) {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/cart/change_quantity.php?ArrayKey="+array_key+"&Do="+direction+"&ran="+ran.toString();

	if (window.XMLHttpRequest) {
		cart_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		cart_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (cart_connection) {
		cart_connection.open("GET",url,true);
		cart_connection.send(null);
		cart_connection.onreadystatechange=cartEval
	}
}

function cartEval() {
	if (cart_connection.readyState==4) {
		if (cart_connection.status==200){
			eval(cart_connection.responseText);
		}
	}
}

function removeItem(array_key) {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/cart/remove_item.php?ArrayKey="+array_key+"&ran="+ran.toString();

	if (window.XMLHttpRequest) {
		cart_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		cart_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (cart_connection) {
		cart_connection.open("GET",url,true);
		cart_connection.send(null);
		cart_connection.onreadystatechange=cartEval
	}
}
