var waiter_interval;
function onLoad(finished) {
	if (finished == true) {
		clearInterval(wait_interval);
		document.getElementById('cover').style.display = 'none';
	} else {
		//setTimeout('onLoad(true)', 1500);
		onLoad(true);
	}
}
//var lastPos = 0;
function setPage(page) {
	new Effect.ScrollTo(page+ '_page', {duration:1.0});
	pageTracker._trackPageview(page);
	return false;
}

function showSample(sample) {
	for (var i=0; i <= 7; i++) {
		if (i == sample) {
			$('sample'+ i).style.display = 'block';
		} else {
			$('sample'+ i).style.display = 'none';
		}
	}
	return false;
}

//Load the work once
var workXML = null;
new Ajax.Request('data.xml', {
	method: 'get',
	onSuccess: function(transport) {
		var data = transport.responseXML;
		if (data == null) {
			alert('Unable to retrieve information');
		} else {
			data = data.firstChild;
			if (data.nodeName == 'xml') {
				data = data.nextSibling;
			}
			workXML = data;
			setTimeout("loadWork('advertising', 'DOLE', null)", 1000);
		}
	},
	onFailure: function() {
		alert('Unable to load work samples');
	}
});

function loadWork(category, label, e) {
	if (workXML == null) {
		//alert('Unable to load data');
		return;
	}
	var arrowY = -1;
	if (e != null) {
		arrowY = e.clientY+ getScrollPos() - 1619 - 7;
	}
	if (arrowY > -1) {
		$('work_arrow').style.top = arrowY+ 'px';
	}
	
	var categories = workXML.getElementsByTagName(category)[0];
	var samples = categories.getElementsByTagName('sample');
	var sample = null;
	for (var i=0; i < samples.length; i++) {
		if (samples[i].getAttribute('label') == label) {
			sample = samples[i];
			break;
		}
	}
	if (sample == null) {
		alert('Unable to locate requested samples');
	} else {
		var imgs = sample.getElementsByTagName('img');
		for (var i=0; i < 7; i++) {
			if (i >= imgs.length) {
				$('thumb'+  (i)).innerHTML = '';
				$('sample'+ (i)).innerHTML = '';
			} else {
				$('thumb'+  (i)).innerHTML = '<img src="'+ imgs[i].getAttribute('thumb')+ '" />';
				if (imgs[i].getAttribute('link') == null) {
					$('sample'+ (i)).innerHTML = '<img src="'+ imgs[i].getAttribute('image')+ '" />';
				} else {
					$('sample'+ (i)).innerHTML = '<a href="'+ imgs[i].getAttribute('link')+ '" target="_blank">'+
													'<img src="'+ imgs[i].getAttribute('image')+ '" /></a>';
				}
			}
		}
		showSample(0);
	}	
	pageTracker._trackPageview('work_category/'+ label);
	return false;
}
function getScrollPos() {
	var scrollTop = document.body.scrollTop;
	if (scrollTop == 0) {
		if (window.pageYOffset)
			scrollTop = window.pageYOffset;
		else
			scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return scrollTop;
}
