// JavaScript Document
function setGalleryMenu (category,image) {
	//alert("category = "+category+", image = "+image);
	//loop through the gallery li's and make them inactive
	var galleryItems = getElementsByClass('c_item');
	var imageItems = getElementsByClass('i_item');
	
	for (i = 0; i < galleryItems.length; i++) {
		galleryItems[i].className = 'c_item';
	}
	
	//make the selected li active
	document.getElementById("c"+category).className = 'c_item active';
	
	for (i = 0; i < imageItems.length; i++) {
		imageItems[i].className = 'i_item';
	}
	
	//make the selected li active
	document.getElementById("i"+image).className = 'i_item active';
	//alert("Doing just fine");
	//add to the history
	var queryString = 'category_id='+category;
	//alert(queryString);
	if (!isNaN(image) && image >= 0) {
		queryString += '&image_id='+image;
	}
	//alert(queryString);
	//alert("About to add to history");
	addHistoryEvent(queryString,queryString);
}

function focusField (element,defaultValue) {
	//alert("Focusing "+this.name);
	if (element.value == defaultValue) {
		element.value = '';
	}
}

function blurField (element,defaultValue) {
	//alert("Blurring "+this.name);
	if (element.value == '') {
		element.value = defaultValue;
	}
}

function clearContactForm () {
	document.getElementById('name').value = 'Name';
	document.getElementById('company').value = 'Company';
	document.getElementById('email').value = 'Email';
	document.getElementById('phone').value = 'Phone';
	document.getElementById('subject').value = 'Subject';
	document.getElementById('message').value = 'Message';
}

function getQueryVariables() {
	if (String(window.location).split('#')[1]) {
		var queryString = new String(window.location).split('#')[1];
		var queryVars = queryString.split('&');
		var resultArray = new Array();
		//loop through query string
		for (i=0;i<queryVars.length;i++) {
			resultArray[i] = new Array();
			//alert(queryVars[i].split('=')[0]+" = "+queryVars[i].split('=')[1]);
			resultArray[i]["name"] = queryVars[i].split('=')[0];
			resultArray[i]["value"] = queryVars[i].split('=')[1];
			//alert(resultArray[i]+" = "+resultArray[i]);
		}
		return resultArray;
	} else {
		return false;
	}
}

//Initialise dhtmlHistory
window.dhtmlHistory.create();

window.onload = function() {
		externalLinks();
		footnoteLinks('content','container');
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(historyChange);
};

/* Our event handler to add history change events */
addHistoryEvent = function(newLocation,historyData) {
	/*
	dhtmlHistory.add(newLocation,historyData);
	*/
}

historyChange = function (newLocation,historyData) {
	var categoryId = false;
	var imageId = false;
	var queryVars = getQueryVariables();
	
	for (i=0;i<queryVars.length;i++) {
		if (queryVars[i]["name"] == 'category_id') {
			categoryId = queryVars[i]["value"];
		}
		if (queryVars[i]["name"] == 'image_id') {
			imageId = queryVars[i]["value"];
		}
	}
	
	if (!categoryId && !imageId) return false;
	gotoCategoryImage(categoryId,imageId);
}