$(document).ready(function() {
	
	var current_nav = 'tab_1',
	current_point,
	current_id,
	max_point,
	min_point = 0;
	
	function addToFeedback($message,$class) {
		$('#feedback-dd #feedback-container').html($message);
		$('#feedback-dd').addClass($class);
		$('#feedback-dd').slideDown();
		setTimeout(function() {
			$('#feedback-dd').slideUp(function() {
				$('#feedback-dd #feedback-container').html('');
				$('#feedback-dd').removeClass($class);
			});
		},5000);
	}
	
	function checkContact($id) {
		if($id == 5 || $id == 6) {
			// change to purple
			$('.arrow.right').addClass('purple');
			$('.arrow.left').addClass('purple');
		} else {
			// make white
			$('.arrow.right').removeClass('purple');
			$('.arrow.left').removeClass('purple');
		}
	}
	
	function moveNavigationLine($from,$to) {
		var o1 = $('#tab_' + $from).offset();
		var o2 = $('#tab_' + $to).offset();
		
		if($from < $to) {
			// moving right
			var dx = o1.left - o2.left;
			dx = dx * -1;
			$new_wid = $('#tab_' + $to + ' a').width();
			$('#underline').animate({
				left:'+=' + dx + 'px',
				width:$new_wid + 'px'				
			},1000,function() {
				checkContact($to);
			});
		}
		else if($from > $to) {
			// moving left
			var dx = o1.left - o2.left;
			$new_wid = $('#tab_' + $to + ' a').width();
			$('#underline').animate({
				left:'-=' + dx + 'px',
				width:$new_wid + 'px'				
			},1000,function() {
				checkContact($to);
			});
		}
	}
	
	$('.right').live('click',function() {
		if((current_point - $i1) >= (max_point * -1)) {
			if($('#content ul').is(':animated')) {
			}
			else {
				moveNavigationLine(current_id,(current_id + 1));
				$('#content ul').animate({
					left:'-='+$i1+'px'
				},1000,function() {
					current_point = parseFloat($(this).css('left'));
					current_id = (current_point * -1)/$i1 + 1;
					checkContact(current_id);
				});
			}
		}
	});
	$('.left').live('click',function() {
		if((current_point + $i1) <= (min_point)) {
			if($('#content ul').is(':animated')) {
			}
			else
			{
				moveNavigationLine(current_id,(current_id - 1));
				$('#content ul').animate({
					left:'+='+$i1+'px'
				},1000,function() {
					current_point = parseFloat($(this).css('left'));
					current_id = (current_point * -1)/$i1 + 1;
					checkContact(current_id);
				});
			}
		}
	});
	
	$('#navigation ul li a').live('click',function() {
		$id = $(this).parent('li').attr('id');
		$tmp = $id.split("_");
		$speed = 1000;
		if($('#underline').is(':animated')) {
		
		}
		else
		{
			if($tmp[1] < current_id) {
				$blocks = current_id - $tmp[1];
				$distance = ($blocks * $i1);
				$s = Math.round(($speed * $blocks / (1.5 * $blocks)));
				moveNavigationLine(current_id,$tmp[1]);
				$('#content ul').animate({
					left:'+='+$distance+'px'
				},$s,function() {
					current_point = parseFloat($(this).css('left'));
					current_id = (current_point * -1)/$i1 + 1;
				});
				// going left
			}
			else if($tmp[1] > current_id) {
				// going right
				$blocks = $tmp[1] - current_id;
				$distance = ($blocks * $i1);
				$s = Math.round(($speed * $blocks / (1.5 * $blocks)));
				moveNavigationLine(current_id,$tmp[1]);
				$('#content ul').animate({
					left:'-='+$distance+'px'
				},$s,function() {
					current_point = parseFloat($(this).css('left'));
					current_id = (current_point * -1)/$i1 + 1;
				});
			}
		}
	});
	
	$('input[name="submit"]').click(function(e) {
		e.preventDefault();
		var $form = $('#newsletter');
		$fname = $form.find('input[name="full_name"]').val();
		$email = $form.find('input[name="email"]').val();
		$.post('/includes/email.php',{email:$email,name:$fname},function(data) {
			$res = jQuery.parseJSON(data);
			if( $res.status == 'error') {
				$mess = $res.message;
				$str = '';
				$.each($mess,function(index,value) {
					$str = $str + '<p>'+value.message+"</p>";
				});
				addToFeedback($str,'error');
			}
			else if( $res.status == 'success') {
				$mess = $res.message;
				$str = '';
				if($mess.length > 1) {
					$.each($mess,function(index,value) {
						$str = $str + '<p>'+value.message+"</p>";
					});
				}
				else
				{
					$str = $str + '<p>'+$mess+"</p>";
				}
				addToFeedback($str,'success');
			}
		});
	});
	
	$('input[name="submit_contact"]').click(function(e) {
		e.preventDefault();
		var $form = $('#contact-details');
		$fname = $form.find('input[name="full_name"]').val();
		$email = $form.find('input[name="email_address"]').val();
		$phone = $form.find('input[name="phone_number"]').val();
		$comment = $form.find('textarea[name="commment"]').val();
		if($form.find('input[name="updates"]').is(':checked')) {
			$updates = 1;
		}
		else {
			$updates = 0;
		}
				
						
		$.post('/includes/contact.php',{email:$email,name:$fname,phone:$phone,comment:$comment,updates:$updates},function(data) {
			$res = jQuery.parseJSON(data);
			if( $res.status == 'error') {
				$mess = $res.message;
				$str = '';
				$.each($mess,function(index,value) {
					$str = $str + '<p>'+value.message+"</p>";
				});
				addToFeedback($str,'error');
			}
			else if( $res.status == 'success') {
				$mess = $res.message;
				$str = '';
				if($mess.length > 1) {
					$.each($mess,function(index,value) {
						$str = $str + '<p>'+value.message+"</p>";
					});
				}
				else
				{
					$str = $str + '<p>'+$mess+"</p>";
				}
				addToFeedback($str,'success');
			}
		});
	});
	
	$('a[rel="nofollow"]').live('click', function(e) {
		window.open($(this)[0].href);
	 	// Prevent the link from actually being followed
	    e.preventDefault();
	});
	
	//updated form toggle
	$('input.toggle, textarea.toggle').live('focus', function(){
		if(this.defaultValue == this.value){
			this.value = '';
		}
	}).live('blur', function(){
		if(this.value == ''){
			this.value = this.defaultValue;
		}
	});
	
	$('.next').click(function() {
		if($('#content ul').is(':animated')) {
		}
		else {
			moveNavigationLine(current_id,(current_id + 1));
			$('#content ul').animate({
				left:'-='+$i1+'px'
			},1000,function() {
				current_point = parseFloat($(this).css('left'));
				current_id = (current_point * -1)/$i1 + 1;
				checkContact(current_id);
			});
		}
	});
	
	$('#send-message').click(function() {
		if($('#content ul').is(':animated')) {
		}
		else {
			moveNavigationLine(current_id,(current_id + 1));
			$('#content ul').animate({
				left:'-='+$i1+'px'
			},1000,function() {
				current_point = parseFloat($(this).css('left'));
				current_id = (current_point * -1)/$i1 + 1;
				checkContact(current_id);
			});
		}
	});
	
	// CENTRE THE PURPLE BOXES
	
	function positionPurple() {
		
		$('#content ul li .banner_text').each(function(index,value) {
			$ph = $(value).outerHeight();
			$ch = $(value).parent('li').height();
			$top = (($ch / 2) - ($ph /2) - 8);
			$(value).css('top',$top);
			$(value).fadeIn();
		});
	
	}
	
	$len = $('#content ul li').length;
	$i1 = $('#content ul li').width();
	$width = $len * $i1;
	max_point = ($width - $i1);
	current_point = 0;
	current_id = 1;
	$('#content ul').css('width',$width);
	$('#content ul li.hidden').each(function(index,value) {
		$(this).removeClass('hidden');	
	})
	
	$(window).load(function() {
		positionPurple();
	});
});
