// JavaScript Document
$(document).ready(function(){
	rotateImage();
	
	var limit = $('#sections ul li').length;
	
	var $sections = $('#sections');
	$sections.serialScroll({
		items: 'li', //selector to the items ( relative to the matched elements, '#sections' in this case )
		prev: '#leftArrow',//selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next: '#rightArrow',//selector to the 'next' button (absolute too)
		axis: 'x',//the default is 'y'
		event: 'click',//on which event to react (click is the default, you probably won't need to specify it)
		stop: false,//each click will stop any previous animations of the target. (false by default)
		lock: true, //ignore events if already animating (true by default)
		duration: 700,//length of the animation
		start: 0, //on which element (index) to begin ( 0 is the default, redundant in this case )
		force: true, //force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		cycle: true,//cycle endlessly ( constant velocity, true is the default )
		step: 1, //how many items to scroll each time ( 1 is the default, no need to specify )
		jump: false, //if true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
		lazy: false,//(default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
		interval: false,
		onBefore: function( e, elem, $pane, $items, pos ){
			$('#top li.selected').removeClass('selected');
			rotateImage();
			hideNews();
			hideResponse();
		},
		onAfter: function( elem ){
			var index = $('#sections li').index(elem);
			$('#top li:eq(' + index + ')').addClass('selected');
		}
	});
	
	$('#top ul li:not(.christmas)').each(function() {
		$(this).click(function() {
			var index = $('#top li').index(this);
			
			$sections.trigger('goto', [index]);
		});
	});
	
	$('#about a.more').click(function() {
		$sections.trigger('goto', [1]);
	});

	$('#aboutUs a.more, #bottom a').click(function() {
		$sections.trigger('goto', [3]);
	});
	
	$('#news a.more').each(function() {
		var button = $(this);
		var newsItem = button.parent('div.newsItem');
		var headline = newsItem.children('div.headline').text();
		var body = newsItem.children('div.body').html();
		
		button.click(function() {
			showNews(headline, body);	
		});
	});
	
	$('#contactForm a').click(function() {
		submitForm();
	});
});

function submitForm() {
	var recipient = $('#formRecipient');
	var nameObj = $('#formName');
	var emailObj = $('#formEmail');
	var subjectObj = $('#formSubject');
	var bodyObj = $('#formBody');
	
	var name = nameObj.attr('value') ? nameObj.attr('value') : '';
	var email = emailObj.attr('value') ? emailObj.attr('value') : '';
	var subject = subjectObj.attr('value') ? subjectObj.attr('value') : '';
	var body = bodyObj.attr('value') ? bodyObj.attr('value') : '';
	
	var send = true;
	
	if (name.length == 0) {
		send = false;
		nameObj.addClass('hasError');
	}
	else {
		nameObj.removeClass('hasError');
	}

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (email.length == 0 || !filter.test(email)) {
		send = false;
		emailObj.addClass('hasError');
	}
	else {
		emailObj.removeClass('hasError');	
	}

	if (subject.length == 0) {
		send = false;
		subjectObj.addClass('hasError');
	}
	else {
		subjectObj.removeClass('hasError');
	}

	if (body.length == 0) {
		send = false;
		bodyObj.addClass('hasError');
	}
	else {
		bodyObj.removeClass('hasError');
	}
	
	if (send) {
		showResponse(name, subject, body);	

		$.ajax({
			type: "POST",
			url: "sendmail.asp",
			data: "recipient=" + recipient.attr('value') + "&email=" + email + "&subject=" + subject + "&body=" + body,
			success: function() {
				nameObj.attr('value', '');
				emailObj.attr('value', '');
				subjectObj.attr('value', '');
				bodyObj.attr('value', '');
			}
		 });
	}
}

function showResponse(name, subject, body) {
	var container = $('#contactUs');
	
	$('#contactForm').fadeOut('normal', function() {
		container.append('<div class="response"><p>Takk fyrir sendinguna, ' + name.substring(0, name.indexOf(' ')) + '.</p><p>Við munum svara erindi þínu eins fljótt og auðið er.  Einnig bendum við á að hægt er að hringja í síma 557 6600 á skrifstofu Fitt.</p><a class="back" href="javascript:void(0);">Til baka</a></div>');
		$('#contactUs .response').fadeIn('normal');
		
		$('#contactUs .response a.back').click(function() {
			hideResponse();
		});
	});
}

function hideResponse() {
	$('#contactUs .response').fadeOut('normal', function() {
		$(this).remove();
		$('#contactForm').fadeIn('normal');
	});
}

function showNews(headline, body) {
	var container = $('#welcome');
	
	container.children('div.threeColumns').fadeOut('normal', function() {
		container.prepend('<div class="newsDetails"><h1>' + headline + '</h1><p>' + body + '</p><a class="back" href="javascript:void(0);">Til baka</a></div>');
		$('#welcome .newsDetails').fadeIn('normal');
		
		$('#welcome .newsDetails a.back').click(function() {
			hideNews();
		});
	});
}

function hideNews() {
	$('#welcome .newsDetails').fadeOut('normal', function() {
		$(this).remove();
		$('#welcome div.threeColumns').fadeIn('normal');
	});
}

var currentImage = 0;

function rotateImage() {
	var index = currentImage;
	while (index == currentImage) {
		index = getRandomNumber(3);
	}
	
	if (currentImage > 0) {
		if (!$.browser.msie && $.browser.version != '6.0') {
			$('#banner-' + currentImage).fadeOut('slow', function() {
				$('#banner-' + index).fadeIn('slow');
			});
		}
	}
	else {
		$('#banner-' + index).fadeIn('slow');
	}
	currentImage = index;
}

function getRandomNumber(count) {
	return Math.floor(Math.random() * count) + 1;
}
