var timer = null;
var headerImages = [];
var disableImageLinks;

function showHeader (elm, elmIdx) {

	if (disableImageLinks == true) {
	
	} else {
	
		if (elm.hasClass('active') || elm.attr('rel') == '') {
		
			// DO NOTHING
		
		} else {
		
			disableImageLinks = true;

			var preloadElm = headerImages[elmIdx][0];
			var targetElm = $('#HeaderImage img').not('#LogoElement img');

			$(preloadElm).load(function(){
				headerImages[elmIdx][2] = true;
				elm.addClass('active').siblings().removeClass('active').blur();
				targetElm.replaceWith($(this)).animate({'opacity' : 1},{ duration: 500, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');
			});
			
			targetElm.animate(
				{'opacity' : 0},
				{ duration: 250, queue: false, easing: 'easeOutSine', complete: function(){
					if (headerImages[elmIdx][2] != true) {
						$(preloadElm).attr({'src' : headerImages[elmIdx][1]});
					} else {
						elm.addClass('active').siblings().removeClass('active').blur();
						$(this).replaceWith($(preloadElm)).animate({'opacity' : 1},{ duration: 500, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');					
					};
				}}
			);
		}
	}
};

function nextHeaderImage()
{
	var total = $('#ImageNav a').size();
	var current = -1;
	$('#ImageNav a').each(function(i) {
		if ($(this).hasClass("active")) current = i;
	});
	next_image = current + 1;
	if (next_image > total - 1) {
		next_image = 0;
	}
	showHeader($("#ImageNav a:eq(" + next_image + ")"), next_image);
	//clearInterval(timer);
}

function prepHeader () {
	
	$('#ImageNav a').each(function(i) {
		var imgObj = $('<img>');
		var imgSrc = $(this).attr('rel');
		headerImages[i] = [imgObj, imgSrc, false];
		$(this).click(function(){
			showHeader($(this), i);
			return false;
		});
	});
	
	$('#LogoElement').css('opacity',0.75);
	$('#HeaderShadow').css('opacity',0.25);
	
	$("li:last", "#MainNav > ul").addClass("nomargin");
	$('#ImageNav a:first').addClass('active');
	timer = setInterval("nextHeaderImage()", 10000);
}

function quickGallery () {
	
	$('.quickGallery').each( function(){
									  
		var	viewWrapper = $('div.wrapper', this); 						  
		var viewTarget = $('img', viewWrapper);
		var galleryThumbs = $('.thumbs', this);
		var activeThumb = $('a:first', galleryThumbs).addClass('active');
		var currentWidth = viewWrapper.width();
		var currentHeight = viewWrapper.height();
		
		viewTarget.load( function(){
								  
			var newWidth = $(this).width();
			var newHeight = $(this).height();
			
			viewWrapper.removeClass('load').animate({'width' : newWidth + 4, 'height' : newHeight + 4},{queue: true, complete: function(){
				
				viewTarget.animate({'opacity' : 1},{ queue: true, complete: function(){
					currentWidth = viewWrapper.width();
					currentHeight = viewWrapper.height();
					$('a.active', galleryThumbs).removeClass('active');
					activeThumb.addClass('active');
					}});
				
			}});
			
		});
		
		$('a', galleryThumbs).click( function(){
			if (! $(this).hasClass('active')) {
				var viewSrc = $(this).attr('href');
				activeThumb = $(this);
				viewWrapper.width(currentWidth).height(currentHeight);
				viewTarget.animate(
					{'opacity' : 0},
					{queue: false, complete: function(){ $(this).attr({'src' : viewSrc}).parent().addClass('load'); }}
				);
			}
			return false; 
		});
		
	});
	
};

function ieFix () {

	$('#MainNav li, #SideNav li').hover(function(){
		$(this).addClass('hover').find('ul:first, a:first').addClass('show');
	},function(){
		$(this).removeClass('hover').find('ul:first, a:first').removeClass('show');
	});
	
	$('p + h1, p + p, p + table').addClass('ieTop');
	$('.content table + table').addClass('ieDblTop');
	
}

function prepNav(activeElm) {
	if(activeElm != '') {
		var targetElm = $('li.' + activeElm);
		targetElm.each( function(i) {
			var currentItem = $(targetElm[i]);
			currentItem.addClass('active').find('a:first').addClass('active').end().parents('li').addClass('active').find('a:first').addClass('active');
		})
	}
	
	$('#SideNav li > ul').parent().find('a:first').wrapInner('<span style="float: left"></span>').append('<span style="float: right">&raquo;</span>');
	
}

function toggleQuicknav () {
	if ( $("#FooterNav td.trigger a").html () == "close" ) { $("#FooterNav td.trigger a").html ( "open" ); }
	else { $("#FooterNav td.trigger a").html ( "close" ); }
	$('#FooterNav div.linkWrapper').slideToggle(250);
	return false;
}

function emptyBlankParagraphs() {
	$('p').each(function(){
		if ($(this).html() == '&nbsp;') {
		$(this).remove();
		}
	})
}

$(document).ready(function() {
	$("#SubscribeToNewsletter form").submit(function() {
		$("input.genInput").each(function() {
			if ($(this).attr("name") == "name" && $(this).val() == "Name") $(this).val("");
			if ($(this).attr("name") == "surname" && $(this).val() == "Surname") $(this).val("");
			if ($(this).attr("name") == "email" && $(this).val() == "Email") $(this).val("");
		});
	});
});