var ls = function() {
	function hashManager(hash) {
		if (hash) {
			hashParams = decodeURIComponent(hash).split(':');
			switch (hashParams[0]) {
				case 'top':
					$('body').scrollTo('0px', 500);
					break;

				default:
					$('body').scrollTo('#section-' + hashParams[0], 500);
					$(document).attr('title', $('a[rel="section-' + hashParams[0] +'"]').attr('title'));
					break;
			}
		}
	}

	function scrollTop() {
		$('a.top').click(function(event) {
			event.preventDefault();
			$('body').scrollTo('0px', 500);
		});
	}

	function navigationInit() {
		$('#main-nav a, #footer-nav a').each(function(i) {
			var tmpID = $(this).attr('rel').replace('section-','');
			$(this).click(function(event) {
				event.preventDefault();
				$('body').scrollTo('#'+$(this).attr('rel'), 500);
				window.location.hash = tmpID;
			});
		});
	}
	
	function carouselContentInit() {
		$('.carousel-nav li').click(function(event) {
			event.preventDefault();
			$('.carousel-nav li').removeClass('selected');
			$(this).addClass('selected');
			el = $('#' + $(this).attr('rel')).clone();
			$('#carousel-container').html($(el).css('display','block'));
			$(el).jcarousel({scroll:1});
		});

		$('.carousel-nav li').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);

	}
	
	
	function formElements() {
		$('#contact-form input, #contact-form textarea').focus(function() {
			if ($(this).attr('rel') == $(this).val()) $(this).val('');
		}).blur(function() {
			if ($(this).val() == '') $(this).val($(this).attr('rel'));
		});
		
		
		$('#postContactForm').click(function(event) {
			event.preventDefault();

			var required = 0;
			$('#contact-form input, #contact-form textarea').each(function(i) {
				if ($(this).val() == '' || $(this).val() == $(this).attr('rel')) {
					required += 1;
					$(this).addClass('required-field');
				} else {
					$(this).removeClass('required-field');
				}
			});
			
			if (required > 0) {
				$('#errorMessage').show();
				$('#successMessage').hide();
			} else {
				postContactForm();
				$('#errorMessage').hide();
				$('#successMessage').show();
			}
		});

		$('#postContactForm').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
		
		
	}


	function postContactForm() {
		$.post($('#contact-form').attr('action'),
			{ name: $('#name').val(), phone: $('#phone').val(), email: $('#email').val(), content: $('#content').val() },
			function(response) { }
		);
	}




	return {
		attachEvents: function() {
			$.address.change(function(event) { hashManager(event.value); });
			scrollTop();
			if ($('.custompage').length == 0) {
				navigationInit();
			}
			carouselContentInit();
			formElements();
		}
	}
} ();

$(document).ready(function() {
	ls.attachEvents();
	
	$('#products').jcarousel({scroll:1});
});