var imgSwitchTarget='#switch';
var imgSwitchIdx=0;
var imgSwitchTimer;

function imgSwitch() {
	var cur=imgSwitchList[imgSwitchIdx];
	var img=$(imgSwitchTarget);
	var desc=$('#desc');
	desc.css('width', desc.width()+'px').html('').fadeTo("normal",0);
	img.fadeTo("slow",0);
	preIm=new Image();
	preIm.onload=function() {
		img.stop().clearQueue().attr('src', cur[0]).fadeTo("slow",1);
		desc.stop().css('width', 'auto').html(cur[1]).fadeTo("slow", 1);
	}
	preIm.src=cur[0];
}

function imgSwitchNext(auto) {
	imgSwitchIdx++;
	if (imgSwitchIdx > (imgSwitchList.length-1)) imgSwitchIdx=0;
	imgSwitch();
	if (auto && auto == true) imgSwitchTimer=setTimeout('imgSwitchNext(true);', imgSwitchList[imgSwitchIdx][2]);
	else clearTimeout(imgSwitchTimer);
}

function imgSwitchPrev() {
	imgSwitchIdx--;
	if (imgSwitchIdx < 0) imgSwitchIdx=imgSwitchList.length-1;
	imgSwitch();
	clearTimeout(imgSwitchTimer);
}

function imgSwitchInit() {
	if (typeof(imgSwitchList) == "undefined" || imgSwitchList.length == 0) return false;
	$(imgSwitchTarget).parent().css('background', 'url(res/img/throbber.gif) center center no-repeat #FFFFFF');
	imgSwitch();
	if (imgSwitchList.length > 1) imgSwitchTimer=setTimeout('imgSwitchNext(true)', imgSwitchList[0][2]);
	else $("#hdr_nav").hide();
	return true;
}

$(function() {
	$("#hdr_n").attr('href', 'javascript:void(0);').click(imgSwitchNext);
	$("#hdr_p").attr('href', 'javascript:void(0);').click(imgSwitchPrev);
});
