// Run when the page has loaded
$('document').ready(function() {

	// Ensure the left column is never longer that the main content
	if ($('#left_col').height() > $('#page_content').height()) {
		$('#page_content').css('min-height', $('#left_col').height() + 'px');
	}
	
	var model_base = {
		'titleShow' : false,
		'speedIn'		: 500,
		'speedOut'		:	200,
		'overlayShow' : true,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'type' : 'iframe',
		'autoDimensions' : false,
		'width' : 480,
		'height' : 250,
		'showCloseButton' : true
	}
	
	
	// Video
	var modal_video = $.extend({}, model_base);
	modal_video.width = 580;
	modal_video.height = 352;
	
	$("a.modal_video").fancybox(modal_video);
	
	
	$('ul#menu > li > a').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	})
	
	// Allow click to work for menu items that don't have dropdowns
	// Otherwise treat as a hover
	/*$('ul#menu > li > ul').parent().find('>a').click(function() {
		$(this).mouseover();
		return false;
	});*/
	
	$('ul#menu li').hover(function() {	
	
	$(this).addClass('hover');
	$(this).find('>a').addClass('hover');
		
	$(this).find('ul').fadeIn(100);
		
	}, function() {
		
			$(this).removeClass('hover');
			$(this).find('>a').removeClass('hover');
			$(this).find('ul').fadeOut(100);

	});
	
	$('ul#menu li ul').hover(function() {
		$(this).parent().find('a').addClass('hover');
	}, function() {
		$(this).parent().find('a').removeClass('hover');
	});
	
	
	$('input[type=submit].replace').each(function() {
		$(this).after('<a class="button submit ' + $(this).attr('class')  + '" href="#submit"><span>' + $(this).val() + '</span></a>');
		$(this).addClass('hide');
	});
	
	$('a.submit').live('click', function() {
			$(this).prev('input').click();
			return false;
	});
	
	$('div.side_panel_plain form input.text').focus(function() {
		if ($(this).val() == 'Enter your e-mail address') $(this).val('');
	});
	
	$('div.side_panel_plain form input.text').blur(function() {
		if ($(this).val() == '') $(this).val('Enter your e-mail address');
	});

		
	fade_to_next_photo();

		// Only run if there's more than 1 news item
		if ($('#image_fader img').length > 1) {
			setInterval(function() {
				fade_to_next_photo()
			}, 6000);
		}
	

});

function fade_to_next_photo() {
	
	mark_current_fader_photo();

	$('#image_fader img').not('.current').fadeOut(1000);

	$('#image_fader img.current').fadeIn(1000);
	
}

function mark_current_fader_photo() {
	$('#image_fader img').not(get_next_fader_photo().addClass('current')).removeClass('current');
}

function get_next_fader_photo() {
	
	// Determine next item
	var next_photo = $('#image_fader img.current:last').next();
	
	// Defatult to first item if no items are selected
	if (next_photo.length < 1) next_photo = $('#image_fader img:first');
	
	return next_photo;
}



