
var quickview_cart = new Array();

quickview_cart['scroll_speed'] = 15;
quickview_cart['bottom'] = 0;
quickview_cart['top'] = 0;
quickview_cart['scroll_direction'] = null;
quickview_cart['break_loop'] = false;
quickview_cart['scrool_loop'] = null;

quickview_cart['move_active'] = false;
quickview_cart['top_position'] = 0;

function startCartScroll(direction) {
	quickview_cart['inner_height'] = document.getElementById('quickview_cart_inside').offsetHeight;
	quickview_cart['outer_height'] = document.getElementById('quickview_cart').offsetHeight;
	
	quickview_cart['bottom'] = quickview_cart['outer_height']-quickview_cart['inner_height'];
	quickview_cart['top'] = 0;
	if (direction != 0 && quickview_cart['inner_height'] > quickview_cart['outer_height']) {
		quickview_cart['scroll_direction'] = direction;		
		quickview_cart['scroll_loop'] = setInterval("scrollCartContent();", 50);
	} else if (quickview_cart['inner_height'] > quickview_cart['outer_height']) {
		if (quickview_cart['scroll_loop']) {
			clearInterval(quickview_cart['scroll_loop']);
		}
	}
}

function scrollCartContent() {
	if (quickview_cart['scroll_direction'] == "down") {
		next_pos = document.getElementById('quickview_cart_inside').style.top.replace("px", "")-quickview_cart['scroll_speed'];
		if (next_pos < quickview_cart['bottom']) {
			next_pos = quickview_cart['bottom'];
			quickview_cart['break_loop'] = true;
		}
		document.getElementById('quickview_cart_inside').style.top = next_pos+"px";
	} else if (quickview_cart['scroll_direction'] == "up") {
		next_pos = parseInt(document.getElementById('quickview_cart_inside').style.top.replace("px", ""))+quickview_cart['scroll_speed'];
		if (next_pos > quickview_cart['top'] || !next_pos) {
			next_pos = quickview_cart['top'];
			quickview_cart['break_loop'] = true;
		}

		document.getElementById('quickview_cart_inside').style.top = next_pos+"px";
	}
	if (quickview_cart['break_loop'] == true) {
		clearInterval(quickview_cart['scroll_loop']);
		quickview_cart['break_loop'] = false;
	}
}

function moveCart() {
	if (quickview_cart['move_active']) {
		if (document.compatMode && document.compatMode != "BackCompat") {
			if (document.documentElement.scrollTop > quickview_cart['top_position']) {
				document.getElementById("quickview_cart_container").style.top = (document.documentElement.scrollTop-quickview_cart['top_position'])+"px";
			}
		} else {
			if (document.body.scrollTop > quickview_cart['top_position']) {
				document.getElementById("quickview_cart_container").style.top = (document.body.scrollTop-quickview_cart['top_position'])+"px";
			}
		}
		setCorrectCartPosition();
	}
}

function setCorrectCartPosition() {
	if (document.compatMode && document.compatMode != "BackCompat") {
		if (document.documentElement.scrollTop == 0 && parseInt(document.getElementById("quickview_cart_container").style.top) > 0) {
			document.getElementById("quickview_cart_container").style.top = "0px";
		}
	} else {
		if (document.body.scrollTop == 0 && parseInt(document.getElementById("quickview_cart_container").style.top) > 0) {
			document.getElementById("quickview_cart_container").style.top = "0px";
		}
	}
}



function initCart() {
	if (document.getElementById('quickview_cart_container') != undefined) {
		setCartPosition();
		$.get('/functions/cart.php', function(data) {
			$('#quickview_cart_inside').html(data);
		});
		
		window.onscroll = moveCart;
	}
}

function setCartPosition() {
	quickview_cart['top_position'] = findTopPos(document.getElementById("quickview_cart_container"));
	quickview_cart['move_active'] = true;
}

function findTopPos(obj) {
	var top = 0;
	if (obj.offsetParent) {
		if (obj["xmlns"] != 'http://www.w3.org/1999/xhtml') { // BUGFIX FOR BROWSERRAMME
			top = obj.offsetTop;
		}
		while (obj = obj.offsetParent) {
			if (obj["xmlns"] != 'http://www.w3.org/1999/xhtml') { // BUGFIX FOR BROWSERRAMME
				top += obj.offsetTop;
			}
		}
	}
	return top;
}


window.onload = initCart;