/* setPosition
Moves an OBJECT to the coordinates X and Y;
*/
function setPosition(OBJECT,X,Y) {
	if (OBJECT.style) {
		if (X != "") { OBJECT.style.left = X + "px"; }
		if (Y != "") { OBJECT.style.top = Y + "px"; }
	}
}

/* removeNumbers
removes any number 0 to 9 from the STRING. Returns the STRING without the numbers;
*/
function removeNumbers(STRING) {

	var newSTRING = "";	// stores the new STRING to be returned to the caller.
	var thisCHAR;		// stores the current character in the loop.
	var thisCODE;		// stores the ASCII code of the current character in the loop.

	for (var i=0; i<STRING.length; i++) {
	
		thisCHAR = STRING.substr(i,1);
		thisCODE = STRING.charCodeAt(i);
		
		if (thisCODE < 48 || thisCODE > 57) {
			newSTRING += thisCHAR;
		}
	}

	return newSTRING;

}

/* removeLetters
removes every character except numbers from STRING and returns the result to the caller.
*/
function removeLetters(STRING) {

	var newSTRING = "";	// stores the new STRING to be returned to the caller.
	var thisCHAR;		// stores the current character in the loop.
	var thisCODE;		// stores the ASCII code of the current character in the loop.

	for (var i=0; i<STRING.length; i++) {
	
		thisCHAR = STRING.substr(i,1);
		thisCODE = STRING.charCodeAt(i);
		
		if (thisCODE >= 48 && thisCODE <= 57) {
			newSTRING += thisCHAR;
		}
	}

	return newSTRING;

}

function removeCharacter(STRING,CHARACTER) {
	var newSTRING = "";
	var thisCHAR;

	for (var i=0; i<STRING.length; i++) {
	
		thisCHAR = STRING.substr(i,1);
		
		if (thisCHAR != CHARACTER) {
			newSTRING += thisCHAR;
		}
	}
	return newSTRING;
}

/* checkForClass
This function checks whether the class CLASS is applied to the OBJECT or not. Returns the Boolean value of the test.
*/
function checkForClass(OBJECT,CLASS) {

	var classes = OBJECT.className.split(" ");		// Split the CLASS-string into the array "classes" using a space as the separator.
	var x;											// used as a counter in the For-In loop.
	var result = 0;									// Will store the Boolean outcome of the loop. Default is FALSE.
	
	for (x in classes) {
		if (classes[x] == CLASS) { result = 1; }	// If the CLASS value matches the value in the array the "result" will be set to TRUE.
	}

	return result;
}

/* findIndex
This function looks up the position number of the widget button from the "mainWidgets"-array;
*/
function findIndex(OBJECT) {
	var idNumber = mainWidgets.length;
	var idCheck = OBJECT.id.replace("drag_","");
	
	idCheck = removeNumbers(idCheck);

	for (var i=0; i < mainWidgets.length; i++) {
		if (mainWidgets[i] == idCheck) {
			idNumber = i;
		}
	}
	
	return idNumber;
}

/* removeWidget
The purpose of the function is to remove a window dropped in the Trashcan.

OBJECT is the dragged object. Referred to as "_object" in the main script.
BUTTON is the number of the button. Referred to as "factor" in the main script.
*/
var trashed;
function removeMainWidget(OBJECT,BUTTON,e) {
	if (e == null) { var e = window.event; }

	// Check if the OBJECT has the "main"-class, which indicates that it is a main widget and as such should be returned as a button.
	if (checkForClass(OBJECT,"main")) {
		var button = document.getElementById(OBJECT.id + BUTTON);	// associate the button-object to the button-variable.
	// set classes for the button
		button.className = "";							// remove any existing classes on the "button"-object;
		button.className = "main button shadow";		// add the default classes for a button positioned in the menu bar.
	// display the button and center it at the mouse position
		button.style.display = "block";
		button.style.left = e.clientX-button.offsetWidth/2 + "px";
		button.style.top = e.clientY-70-button.offsetHeight/2 + "px";
	// justify the z-position of the button and the Trashcan so that the button is on top of the Trashcan
		button.style.zIndex = _currentZIndex;	// move the button-object every other object.
		trashcan.style.zIndex = -1;				// Reset the Trashcan z-position to the far back.
	// hide the dragged object
		OBJECT.style.display = "none";

	// ANIMATION CODE STARTS HERE ...
		// store the start and goal positions of the animation;
		var startPosX = parseInt(button.style.left);
		var startPosY = parseInt(button.style.top);
		var stopPosX = 100*BUTTON;
		var stopPosY = 0;
		
		var distanceY = startPosY-stopPosY;
		var distanceX = startPosX-stopPosX;
		var frames = Math.floor(Math.max(distanceX,distanceY)/30); // The number of frames from the start to the goal.

		var rateX = Math.floor(distanceX/frames);
		var rateY = Math.floor(distanceY/frames);
		
		trashed = setInterval("resetButton(" + frames + ",'" + button.id + "','" + rateX + "','" + rateY + "','" + BUTTON + "')",1);
	}
}

var frameCounter = 0;
function resetButton(FRAMES,OBJECT,X,Y,FACTOR) {

		var button = document.getElementById(OBJECT);
		
		button.style.top = parseInt(button.style.top)-Y + "px"
		button.style.left = parseInt(button.style.left)-X + "px"

		frameCounter++;
		
		if (frameCounter == FRAMES) {
			clearInterval(trashed);
			button.style.top = 0 + "px";
			button.style.left = 10+FACTOR*105 + "px";
			frameCounter = 0;
		}
}

/* findTrashcan
This function test if the mouse pointer is over the trashcan and returns true if it is
Make use of the global variables "tcTop" and "tcLeft";
*/
function findTrashcan(e) {

	var X = e.clientX;
	var Y = e.clientY-70;

	if (X >= tcLeft && Y >= tcTop) { return true; } else { return false; }
}
function hoverTrashcan(SWITCH) {

	trashHover = SWITCH;
	trashcan.style.zIndex = _currentZIndex * SWITCH + 1;

	switch (SWITCH) {
		case 0 : trashcan.src = _imgTrashOut.src; break;
		case 1 : trashcan.src = _imgTrashOvr.src; break;
	}
}

/*findShoppingCart
This function test if the mouse pointer is over the shopping cart and returns true if it is
Make use of the global variables "scTop","scBottom" and "scLeft";
*/
function findShoppingCart(e) {

	var X = e.clientX;
	var Y = e.clientY-70;

	if (X >= scLeft && Y >= scTop && Y <= scBottom) { return true; } else { return false; }
}
function hoverShoppingCart(SWITCH) {

	var pThis = document.getElementById("priceThis");
	var pAfter = document.getElementById("priceAfter");
	
	var asin;

	if (infoID != "none") { asin = infoID.substring(5,15); }
	else if (dragItem != "nothing") { asin = dragItem.id.substring(5,15); }

	var thisPrice = eval("itemPrice." + asin)*1;
	var temp;

	if (SWITCH == 1) {
		pThis.innerHTML = "+ £" + thisPrice;
		pThis.style.display = "block";
		
		temp = eval(shoppingCartValue + thisPrice)
		temp = Math.round(temp*100)/100;
		
		pAfter.innerHTML = "= £" + temp;
		pAfter.style.display = "block";

		for (var i=0; i<shoppingCart.length; i++) {
			if (shoppingCart[i] == asin) {
				document.getElementById("attention").style.display = "block";
			}
		}
		
		if (infoID == "none") { pricetag.className = "priceHover"; } else { pricetag.className = "priceHoverAlt"; }
	} else if (SWITCH == 0) {
		pThis.style.display = "none";
		pAfter.style.display = "none";
		pricetag.className = "";
		document.getElementById("attention").style.display = "none";
	}

}

/* addContentShoppingCart
This function adds items to the shopping cart array as well as updates the visual appearence of the shopping cart as new items are added.
*/
var shoppingCart = new Array();
var shoppingCartValue = 0;
function addContentShoppingCart(ASIN) {

	// Insert the ASIN first in the shoppingCart-array.
	// This way the latest added item will always be on top of the list in the Shopping cart-widget.
	shoppingCart.unshift(ASIN);
	
	// Update the content of the Shopping Cart Widget, if it is open...
	var scW = document.getElementById("drag_shoppingcart");	// scW as in Shopping Cart Widget ...
	if (scW.style.display == "block") { updateWidget("shoppingcart","drag_shoppingcart"); }
	
	calculateValue();
	hoverShoppingCart(0);
}

function findMenu(Y) {
	if (Y < 30) { return true; } else { return false; }
}

/* setSize
This function resizes an object to match the passed in values.
*/
function setSize(OBJECT,X,Y) {
	OBJECT.style.width = X + "px";
	OBJECT.style.height = Y + "px";
}

/*calculateValue
This function grabs all objects in the Shopping Cart and compares their ASIN with the price data stores in the itemPrice-object.
It then calculates the Value of all items and prints it on the screen.
*/
function calculateValue() {

	shoppingCartValue = 0;
	var cart = document.getElementById("shoppingCartContent");	// shortcut to the Shopping Cart element.
	var temp;
	
	if (shoppingCart.toString() == "") {
		shoppingCart.length = 0;
		cart.style.backgroundImage = "url('gfx/blank.gif')";
		document.getElementById("priceBefore").innerHTML = "£0.00";
	} else {
		for (var i=0; i < shoppingCart.length; i++) {
			temp = shoppingCartValue + eval("itemPrice." + shoppingCart[i])*1
			temp = Math.round(temp*100)/100;
			shoppingCartValue = temp;
		}
		document.getElementById("priceBefore").innerHTML = "£" + shoppingCartValue;
	}


	// The following array stores the values when the content image of the shopping cart should change.
	var changeArray = new Array(1,2,3,5,10,15,20);

	// This loop runs through the changeArray-array and checks if any of the values stored there matches the amount of items in the shopping cart.
	// If it does the shopping cart content image will change accordingly.
	for (var i=0; i < changeArray.length; i++) {
		if (shoppingCart.length == changeArray[i]) {
			cart.style.backgroundImage = "url('gfx/cartContent" + shoppingCart.length + ".png')";
		}
	}
	/*

	*/
}






