// Helper function to get all the elements by Classname
function getElementsByClassName( strClassName, obj ) {
	var ar = arguments[2] || new Array();
	var re = new RegExp("\\b" + strClassName + "\\b", "g");

	if ( re.test(obj.className) ) {
		ar.push( obj );
	}
	for ( var i = 0; i < obj.childNodes.length; i++ )
		getElementsByClassName( strClassName, obj.childNodes[i], ar );

	return ar;
}

// Function to check that the text field (called filename) in the "basket-download"
// form does contain something other than spaces, and submit the form if it does
// (this form is in the header)
function checkBasket(basket){
	var value = basket.replace(" ", "");
	if (value.length > 0)
		document.forms['basket-download'].submit();
}

// Function to clear the display of basket states - display all the "Add to Basket"
// and hide all the "Remove from Basket"
function clearAllStates(){
	rootNode = document.getElementById("library");
	
	if (rootNode){
		var elemList = getElementsByClassName("basket-add", rootNode);
		for (var i=0; i<elemList.length; i++)
			elemList[i].style.display="block";
		
		elemList = getElementsByClassName("basket-remove", rootNode);
		for (var i=0; i<elemList.length; i++)
			elemList[i].style.display="none";
	}
}

// Function to set one or all baskets to show the correct state ("add to basket" if
// picture has not yet been added, or "remove from basket" if it has)
function setAllStates(selection){
	var filenameNoExt;
	var elem;
	var selectionArray = selection.split("|");
	
	// Filter out the images already added to the basket
	for(var i=0; i<selectionArray.length; i++) {
		// To access the document ids, which use "add-" and "remove-", plus
		// the filename (without the extension), first remove the extension
		filenameNoExt = (selectionArray[i].split("."))[0];
		
		// Set "add to basket" to hidden
		elem = document.getElementById('add-' + filenameNoExt);
		if (elem != null) elem.style.display="none";
		
		// Set "remove from basket" to visible
		elem = document.getElementById('remove-' + filenameNoExt);
		if (elem != null) elem.style.display="block";
	}
}

// This function is used to correctly set the state of the activated item.
function setCurrentState(action, filename){
	var filenameNoExt = (filename.split("."))[0];
	var formattedAction = (action.split("-"))[1];
	var id = formattedAction + "-" + filenameNoExt;
	var otherId;
	
	if (formattedAction == "add")
		otherId = "remove-" + filenameNoExt;
	else
		otherId = "add-" + filenameNoExt;
	
	document.getElementById(id).style.display="none";
	document.getElementById(otherId).style.display="block";	
}

// Main Ajax function
function ajaxFunction(action, filename){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			// Update the hidden field in the header that contains the basket files
			// so that the full path is stored
			var selection = ajaxRequest.responseText;
			// Test for whitespace
			var testValue = selection.replace(" ", "");
			if (testValue.length > 0){
				var selectionArray = selection.split("|");
				for(var i=0; i<selectionArray.length; i++) {
					selectionArray[i] = "http://" + location.hostname + "/media/image-library/" + selectionArray[i];
				}
				document.forms['basket-download'].filename.value = selectionArray.join("|");
			}
			else
				document.forms['basket-download'].filename.value = "";
			
			// Set the basket div states
			if ((action == "basket-add") || (action == "basket-remove"))
			{
				setCurrentState(action, filename);
				setAllStates(selection);
			}
			else if (action == "basket-clear")
				clearAllStates();
		}
	}
	
	// Set up the action and the filename

	ajaxRequest.open("GET", "/cms/scripts/minicart/minicart.php?"+action+"="+filename, true);
	ajaxRequest.send(null); 
}
