var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	$('#navigation > li').bind('mouseover', jsddm_open);
	$('#navigation > li').bind('mouseout',  jsddm_timer);});

document.onclick = jsddm_close;

function fadeHeroImg()
{
	if( current_id != '' )
	{
		// INITIALISATION
		// Number of images
		var img_count = $('#'+current_id).children('.hero_img').length;
		img_count = img_count - 1;
		
		// What is the current image
		var current_img = $('#'+current_id).attr('currentimg');
		current_img = current_img;
		if( current_img < 0 || current_img > img_count )
		{
			current_img = img_count;
			$('#'+current_id).children('.hero_img').fadeIn('fast');
		}
		$('#'+current_id).attr('currentimg',current_img);
		

		if( current_img == 0 )
		{
			$('#'+current_id).children('.hero_img').eq(img_count).fadeIn(650);
			$('#'+current_id).attr('currentimg',img_count);
		}
		else if( current_img == img_count )
		{
			$('#'+current_id).children('.hero_img').show();
			$('#'+current_id).children('.hero_img').eq(current_img).fadeOut(650);
			$('#'+current_id).attr('currentimg',(current_img-1));
		}
		else
		{
			$('#'+current_id).children('.hero_img').eq(current_img).fadeOut(650);
			$('#'+current_id).attr('currentimg',(current_img-1));
		}

	}
	
	setTimeout("fadeHeroImg()", 2000);
}

function setBoxesToSameHeight()
{
	var max_height = 0;
	$('#quicklinks .content').each(function(){
		var this_height = $(this).height();
		if( this_height > max_height )
		{
			max_height = this_height;
		}
	})
	.css('height', max_height+'px');
}


$(document).ready(function(){
	
	// ROLLOVER SLIDER MAIN IMAGES
	
	$('.hero_item').attr('currentimg',-1).attr('dofade',0).each(function(i){
		$(this).attr('id','hero_item_fade'+(i+1));
	});
	
	
	
	current_id = '';
	fadeHeroImg();
	$('.hero_item').hover(
		function()
		{
			current_id = $(this).attr('id');
		},
		function()
		{
			current_id = '';
		}
	);
	
	
	// SET ALL CONTENT ELEMENTS TO SAME HEIGHT
	// var max_height = 0;
	// $('#quicklinks .content').each(function(){
	// 	var this_height = $(this).height();
	// 	if( this_height > max_height )
	// 	{
	// 		max_height = this_height;
	// 	}
	// })
	// .css('height', max_height+'px');
	setTimeout("setBoxesToSameHeight()", 300);
	
	
	// STRIP ALL TITLE-ATTRIBUTES FROM LINKS
	$('a').attr('title','');
	
});
