function getElement(id) {
	return document.getElementById(id);
}

function getCoordinates(id) {
	var id = getElement(id);

	var
		left = id.offsetLeft,
		top = id.offsetTop;
		
	for (var parent = id.offsetParent; parent; parent = parent.offsetParent) {
		left += parent.offsetLeft - parent.scrollLeft;
		top += parent.offsetTop - parent.scrollTop;
	}
	
	return {
		width: id.offsetWidth,
		height: id.offsetHeight,
		left: left,
		top: top
	};
}

function productAdded() 
{
}

function basketMessage() {
	getElement("basketMessage").style.display = "none";
	window.clearInterval(basketMessage_timer);
}

if (basketMessage_set == true) {
	getElement("basketMessage").style.display = "block";
	var basketMessage_timer = window.setInterval('basketMessage()', 5000);
}

var
	setMove_key = false,
	setMove_timer,
	setMove_step = 1,
	setMove_steps = 60;

function setMove(elementID, sourceID, targetID) {
	setMove_key = true;
	
	var
		coordinatesElement = getCoordinates(elementID),
		coordinatesSource = getCoordinates(sourceID),
		coordinatesTarget = getCoordinates(targetID);
	
	var
		element = getElement(elementID),
		source = getElement(sourceID),
		target = getElement(targetID);
	
	element.style.display = "block";
	
	var
		x1 = coordinatesSource.left,
		y1 = coordinatesSource.top,
		x2 = coordinatesTarget.left,
		y2 = coordinatesTarget.top,
		xDelta,
		yDelta,
		x,
		y;
	
	if (x1 < x2) {
		xDelta = Math.abs(x2 - x1);
	} else {
		xDelta = Math.abs(x1 - x2);
	}
	
	if (y1 < y2) {
		yDelta = Math.abs(y2 - y1);
	} else {
		yDelta = Math.abs(y1 - y2);
	}

	step = setMove_step / setMove_steps;
	
	if (setMove_step <= setMove_steps) {
		if (x1 < x2) {
			x = x1 + Math.round(xDelta * step);
		} else {
			x = x1 - Math.round(xDelta * step);
		}
		
		if (y1 < y2) {
			y = y1 + Math.round(yDelta * step);
		} else {
			y = y1 - Math.round(yDelta * step);
		}
		
		width = coordinatesSource.width - Math.round((coordinatesSource.width - coordinatesTarget.width) * step);
		height = coordinatesSource.height - Math.round((coordinatesSource.height - coordinatesTarget.height) * step);
		
		element.setAttribute("width", width);
		element.setAttribute("height", height);
		
		element.style.left = x;
		element.style.top = y;
		
		setMove_step++;
	} else {
		setMove_key = false;
		setMove_step = 1;
		
		element.style.display = "none";
		if (getElement("submitBasket")) {
			getElement("submitBasket").disabled = false;
		}

		if (getElement("productForm")) {
			getElement("productForm").submit();
		}
		
		if (getElement("productForm_" + sourceID)) {
			getElement("productForm_" + sourceID).submit();
		}
		
		productAdded();
		
		window.clearInterval(setMove_timer);
	}
}

var
	scrollSmooth_key = false,
	scrollSmooth_timer,
	scrollSmooth_step = scrollSmooth_steps = 50,
	scrollDistance = false;

function scrollSmooth(sourceID) 
{
	if (getElement("submitBasket")) 
	{
		getElement("submitBasket").disabled = true;
	}
	scrollSmooth_step--;
	
	var
		scrollDeterminant = getCoordinates("scroll");
		
	if (scrollDistance == false) {
		scrollDistance = Math.abs(scrollDeterminant.top);
	}

	if (scrollSmooth_step == 0 || scrollDistance === 0) {
		window.scroll(0, 0);
		scrollSmooth_step = 50;
		scrollDistance = false;
		window.clearInterval(scrollSmooth_timer);
		scrollSmooth_key = true;
		setBasket(sourceID);
	} else {
		window.scroll(0, Math.round(scrollDistance * (scrollSmooth_step / scrollSmooth_steps)));
	}
}

function setBasket(sourceID) {
	if (setMove_key) {
		return false;
	}
	
	if (scrollSmooth_key == true) {
		scrollSmooth_key = false;
		
		var
			productMoving = getElement("productMoving"),
			productSource = getElement(sourceID);

		productMoving.setAttribute("src", productSource.getAttribute("src"));
		productMoving.setAttribute("width", productSource.getAttribute("width"));
		productMoving.setAttribute("height", productSource.getAttribute("height"));

		var
			coordinatesImg = getCoordinates(sourceID);

		productMoving.style.left = coordinatesImg.left;
		productMoving.style.top = coordinatesImg.top;
		
		var
			basket = getCoordinates("basketTarget");
		var
			basketHeight = Math.round((productSource.getAttribute("height") * basket.width) / productSource.getAttribute("width"));
		if (basketHeight > basket.height) {
			getElement("basketTarget").style.height = basketHeight + "px";
		}
		
		setMove_timer = window.setInterval('setMove("productMoving", "' + sourceID + '", "basketTarget")', 10);
	} else {
		scrollSmooth_timer = window.setInterval("scrollSmooth('" + sourceID + "')", 10);
	}
	
	return false;
}