$(document).ready(function() {
	var previousPage = '';
	var currentPage = '';
	
	//Navigation hover
	$('#navigation li').mouseenter(function() {
		var width = $(this).width();
		$(this).addClass('hover');
		$(this).children('ul').show();
		$(this).css('width',width+'px');
	});
	$('#navigation li').mouseleave(function() {
		$(this).removeClass('hover');
		$(this).children('ul').hide();
	});
	
	//Navigation handling
	$('#navigation li').click(function() {
		if ($(this).attr('alt') != currentPage) {
			previousPage = currentPage;
			currentPage = $(this).attr('alt');
			$('.active').removeClass('active');
			$(this).addClass('active');
			var content = $('#pages>div#'+currentPage).html();
			var container = $('#body');
			container.fadeOut(200,function() {
				container.html(content);
				container.fadeIn(800);
			});

			var logo = '';
			switch (currentPage) {
				case 'home': logo = 'finance_group_logo'; break;
				case 'news': logo = 'finance_group_logo'; break;
				case 'contact': logo = 'finance_group_logo'; break;
				case 'team': logo = 'finance_group_logo'; break;
				case 'utilities': logo = 'finance_group_logo'; break;
				case 'management': logo = 'wealth_management_logo'; break;
				case 'protection': logo = 'asset_protection_logo'; break;
				default: logo = 'center';
			}
			$('#header>div').attr('class',logo);
		}
	});
	
	$('#foot-menu>li').live('click',function() {
		$('#'+$(this).attr('alt')).trigger('click');
	});
	
	//Link handling
	$('.link').live('click',function() {
		$('#'+$(this).attr('alt')).trigger('click');
	});
	
	//Contact email send
	$('.submit').live('click',function(event) {
		event.preventDefault();
		
		var n = $('#contact-name').val();
		var e = $('#contact-email').val();
		var t = $('#contact-telephone').val();
		var c = $('#contact-calltime').val();
		var m = $('#contact-message').val();
		
		if (n && e && t && c && m) {
			var data = 'data[Contact][name]='+n+'&data[Contact][email]='+e+'&data[Contact][telephone]='+t;
			data += '&data[Contact][calltime]='+c+'&data[Contact][message]='+m;
			
			$.ajax({
				type: 'POST',
				url: 'contact.php',
				dataType: 'json',
				data: data,
				success: function(data,status,object) {
				},
				complete: function(object,status) {
					$('.textfield').add('.textarea').val('');
					alert('Thank you, your message was sent successfully');
				}
			});
		}
		else alert('Please fill all fields');
	});
	
	function clear(){
		$('#contact-name').val('');
		$('#contact-email').val('');
		$('#contact-telephone').val('');
		$('#contact-calltime').val('');
		$('#contact-message').val('');
	}
	
	//Donate hover
	$('#donate>li').mouseenter(function() {
		$(this).addClass('hover');
	});
	$('#donate>li').mouseleave(function() {
		$(this).removeClass('hover');
	});
	
	//Donation progress
	function evaluate() {
		var target = 45000;
		var raised = 9000;
		var width = raised / target * 630;
		$('#raised>h2>span').html(raised.toFixed(2));
		$('#target>h2>span').html(target.toFixed(2));
		$('#progress').width(width);
	} evaluate();
	
	//Startup delay
	$('#pages').delay(100).fadeOut(100,function() {
		$('.active').trigger('click');
	});

});
