/*Adding jquery easing functions for the nav menu
*/

jQuery.extend( jQuery.easing,
{
	
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	}
	
});


$(document).ready(function() {
	
	// rand = randomXToY(0, 2);
	// eq('+rand+'))'
	
	// hide all news divs except the first one on startup
	$('#news div:not(:first)').hide();
	
	
	
	
	// start by hiding all divs in #news
	$('#news').css({
		'overflow' : 'hidden'
		}) 
	
	$('#news div').css({
		'position':'absolute',
		'top': 0,
		'padding': '12px',
		'left' : 0
		
		});
		

		
	setTimeout(newsSlider, 16000);
	
	$('#rightarrow').click(function(){
		
		var current = $('#news div.current');
		var next = current.next('div').length ? current.next() : $('#news div:first');
		current.hide().removeClass('current');
		next.fadeIn( 'medium').addClass('current');
		changeNum();
		clearTimeout(timer);
		
	});
	
	$('#leftarrow').click(function() {
		
		var current = $('#news div.current');
		var back = current.prev('div').length ? current.prev() : $('#news div:eq(2)');
		current.hide().removeClass('current');
		back.fadeIn( 'medium').addClass('current');
		changeNumBack();
		clearTimeout(timer);
		
	});
	
});

var num = 1;
function changeNum() {
	if (num != 3) {
num = num + 1;
$('#num').text(num);
} else {
	num = 1;
$('#num').text(num);	
}
}
function changeNumBack() {
		if (num != 1) {
num = num - 1;
$('#num').text(num);
} else {
	num = 3;
$('#num').text(num);	
}
	
}

//function to get random number from 1 to n
function randomXToY(minVal,maxVal,floatVal)
{
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}


function newsSlider() {
	
	
		var current = $('#news div.current');
		var next = current.next('div').length ? current.next() : $('#news div:first');
		current.hide().removeClass('current');
		next.fadeIn( 'medium').addClass('current');
		changeNum();
	var timer =	setTimeout(newsSlider, 16000);
		} 
	

$(function(){
	
	$('#nav .dropdown').hide(); // hide the nav links on load so non javascript users can still access the drop down menu
	
	
	$('#navbar li.sub').each(function(){
		$(this).hover(function(){
			if (!$(this).children('.dropdown').hasClass('animated')) {
				$(this).children('.dropdown').dequeue().stop().slideDown(200, 'easeInOutSine');
			}
		},function(){
			$(this).children('.dropdown').addClass('animated').slideUp(200, 'easeInOutSine', function() {
				$(this).removeClass('animated').dequeue();
			});
		});
	});
	$('#contact_frm').submit(function(){
		var valid = true;
		$('#contact_frm input.req').each(function(){
			if($(this).val() == ''){
				$(this).css({'border':'2px solid #e00001', 'padding': '2px 1px'});
				valid = false;
			}else{
				$(this).removeAttr('style');
			}
		});
		$('#contact_frm textarea.req').each(function(){
			if($(this).val() == ''){
				$(this).css({'border':'2px solid #e00001'});
				valid = false;
			}else{
				$(this).removeAttr('style');
			}
		});
		var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if(!filter.test($('#contact_frm #Email').val())){
			$('#contact_frm #Email').css({'border':'2px solid #e00001', 'padding': '2px 1px'});
			valid = false;
		}else{
			$(this).removeAttr('style');
		}
		if(valid){
			$('#error').html('');
		}else{
			$('#error').html('<strong>There appears to be one or more errors in the highlighted fields.<br>Please check the form and try again.</strong>');
			return false;
		}
	});
	$('#quote_frm').submit(function(){
		var valid = true;
		$('#quote_frm input.req').each(function(){
			if($(this).val() == ''){
				$(this).css({'border':'2px solid #e00001', 'padding': '2px 1px'});
				valid = false;
			}else{
				$(this).removeAttr('style');
			}
		});
		$('#quote_frm textarea.req').each(function(){
			if($(this).val() == ''){
				$(this).css({'border':'2px solid #e00001'});
				valid = false;
			}else{
				$(this).removeAttr('style');
			}
		});
		var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if(!filter.test($('#quote_frm #Email').val())){
			$('#quote_frm #Email').css({'border':'2px solid #e00001', 'padding': '2px 1px'});
			valid = false;
		}else{
			$(this).removeAttr('style');
		}
		if(valid){
			$('#error').html('');
		}else{
			alert('There appears to be one or more errors in your form. Please check the form and try again.');
			return false;
		}
	});
	
	// toggle for homepage
	
	$('h1.toggle').click(function() {
	$(this).toggleClass('opened').next('div').fadeToggle('fast','easeInOutSine');
		
	});
	
});
