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
	};
}

var
	basketMessage_timer;

function basketMessage(set) {
	if (!set) {
		getElement("basketMessage").style.display = "none";
		window.clearInterval(basketMessage_timer);
	} else {
		getElement("basketMessage").style.display = "block";
		basketMessage_timer = window.setInterval('basketMessage(false)', 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 + "px";
		element.style.top = y + "px";
		
		setMove_step++;
	} else {
		setMove_key = false;
		setMove_step = 1;
		
		element.style.display = "none";
		
		if (getElement("submitBasket") && getElement("submitBasket").getAttribute("disabled")) {
			getElement("submitBasket").removeAttribute("disabled");
		}
		
		if (getElement("productForm")) {
			getElement("productForm").submit();
		} else if (getElement("productForm_" + sourceID)) {
			getElement("productForm_" + sourceID).submit();
		}

		window.clearInterval(setMove_timer);
		window.setTimeout("basketMessage(true)", 100);
		getElement("basketTarget").style.display = "none";
	}
}

var
	scrollSmooth_key = false,
	scrollSmooth_timer,
	scrollSmooth_step = scrollSmooth_steps = 50,
	scrollDistance = false;

function scrollSmooth(sourceID) {
	if (getElement("submitBasket")) {
		getElement("submitBasket").setAttribute("disabled", "disabled");
	}
	
	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 (
		getElement("scroll")
		&&
		getElement("productMoving")
		&&
		getElement("basketTarget")
		&&
		getElement("basketMessage")
	) {
		if (setMove_key) {
			return false;
		}
		
		window.clearInterval(basketMessage_timer);
		basketMessage(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 + "px";
			productMoving.style.top = coordinatesImg.top + "px";
			
			getElement("basketTarget").style.display = "block";
			
			var
				basket = getCoordinates("basketTarget");
			var
				basketHeight = Math.round((productSource.getAttribute("height") * basket.width) / productSource.getAttribute("width"));
			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;
	}
}

