var openClass = 'open'
var overSuffix = '-over'
var moving = false


$(document).ready(function() {

	// setup the accordion and apply open / close functions
	//$("dd:not(:first)").hide();
	//$("dt:first").toggleClass(openClass);

	$("dd").hide();

	$("dt a").click(function(){
		if (!$(this).parent().parent().hasClass(openClass))	{		// Don't allow click if already open
			$(this).parent().parent().addClass(openClass)
			$(this).parent().parent().next().slideDown("slow");
		}	else	{
			$(this).parent().parent().next().slideUp("slow");
			$(this).parent().parent().removeClass(openClass)
		}
		return false;
	});

	// Controls the rollovers on the main menu
	$('#main-nav ul li a').hover(
		function ()	{	$(this).parent().addClass('on')	},	function ()	{	$(this).parent().removeClass('on')	}
	)

	// All the button rollovers - the rollover image must have an _over suffix and site in the same folder as the normal state. eg   
	// normal    :   button.gif
	// over		 :	 button_over.gif
	$('.rollOver').hover(
		function ()	{	
			$(this).attr('src',doOverImg($(this).attr('src')))
		},	
		function ()	{	
			$(this).attr('src',doDefaultImg($(this).attr('src')))
		}
	)

});

function changeRecentProjects()	{
	//alert($('#recentProject-shrink').attr('src'));

	if ($('#recentProject-shrink').attr('src').indexOf('close') > 0)	{
		// Open so close
		$('#recentProject-shrink').attr('src', $('#recentProject-shrink').attr('src').replace('close','open'))
		$('#recentFundedProjects .item').css('display', 'none');
		$('#recentFundedProjects').css('background','url(img/fundedToDateHead-bg-closed.gif) no-repeat')
		$('#recentFundedProjects').css('height','34px')
	}	else	{
		// Closed so open
		$('#recentProject-shrink').attr('src', $('#recentProject-shrink').attr('src').replace('open','close'))
		$('#recentFundedProjects .item').css('display', 'block');
		$('#recentFundedProjects').css('background','url(img/fundedToDateHead-bg.gif) no-repeat')
		$('#recentFundedProjects').css('height','322px')
	}
}

function openAcc(bladeNo)	{
	$('#acc_' + bladeNo).prev().addClass(openClass)
	$('#acc_' + bladeNo).show();
}

// Form field defaults - Removes the caption on focus and puts it back on blur if nothing has been typed
function formDefault(me, initialContent)	{
	$("#" + me).focus(
		function ()	{	
			this.ic = initialContent;
			if (($(this).attr('value') == '') || ($(this).attr('value') == this.ic)) {	$(this).attr('value','');		}
		}
	);

	$("#" + me).blur(
		function ()	{	
			this.ic = initialContent;
			if ($(this).attr('value') == '') {		$(this).attr('value',this.ic);		}
		}
	);
}


// Roll over helpers
function doOverImg(path)	{
	start = path.substring(0, path.indexOf('.'))
	end = path.substring(path.indexOf('.'))
	overpath = (start + overSuffix + end);
	return overpath
}
function doDefaultImg(path)	{
	return path.replace(overSuffix,'');
}



// makes the slider for projects work
function slideKeyProjectsPanel(dir)	{
	currentPos = parseInt($('#slidingPanel').css('left'));
	// get the number of items in the list
	var itemCount = $('#slidingPanel .keyProject').length;
	scrollLimit = calculateScrollLimit(itemCount, 250, 1);

	if (dir == 'next')	{
		if ((currentPos > scrollLimit))		{
			if (moving != true)		{
				moving = true;
				$('#slidingPanel').animate( {left: (currentPos -250) + 'px'}, 700, 'linear', function (){	finishedMoving()	});
			}
		}
	}	else	{
		if (currentPos != 0)		{
			if (moving != true)		{
				moving = true;
				$('#slidingPanel').animate( {left: (currentPos +250) + 'px'}, 700, 'linear', function (){	finishedMoving()	});
			}
		}
	}
}


// makes the slider for news work
function slideNewsHeadlinePanel(dir)	{

	var amountOfItemsOnScreen = 3;
	var itemWidth = 160;
	var animationDuration = 700;

	currentPos = parseInt($('#newsSlider').css('left'));
	// get the number of items in the list
	var itemCount = $('#newsSlider .slide_newsHeadline').length;
	// hide the buttons if not needed
	if (itemCount <= 3)	{
		$('#news_backButton').css('display', 'block');
		$('#news_nextButton').css('display', 'block');
	}
	scrollLimit = calculateScrollLimit(itemCount, itemWidth, amountOfItemsOnScreen);

	if (dir == 'next')	{
		if ((currentPos > scrollLimit))		{
			if (moving != true)		{
				moving = true;
				$('#newsSlider').animate( {left: (currentPos -itemWidth) + 'px'}, animationDuration, 'linear', function (){	finishedMoving()	});
			}
		}
	}	else	{
		if (currentPos != 0)		{
			if (moving != true)		{
				moving = true;
				$('#newsSlider').animate( {left: (currentPos +itemWidth) + 'px'}, animationDuration, 'linear', function (){	finishedMoving()	});
			}
		}
	}
}

// takes in the amount of items, item width  and amount of items onm screen and returns a scroll limit
function calculateScrollLimit(itemCount, itemWidth, itemsOnScreen)	{
	itemsToScroll = (itemCount - itemsOnScreen);
	if (itemsToScroll > 0)	{
		return (itemsToScroll * itemWidth) * -1;		// Scrolls to the left so limit is negative
	}	else	{
		return 0;
	}
}

// Callback - sets the move status of the moving panels
function finishedMoving()	{
	moving = false
	$('#nPos').attr('value', 'finished');
}
