EOMS = {};
EOMS.UI = {};

EOMS.UI.Global = function() {
	
	var scroller; 
	var products;
	var scroll_position = 0;
	var scroll_width;
	
	var init = function() {
		check_position();
		hijack_links();
		init_nav();
		init_products();
		init_contact_form();
	};
	
	var check_position = function() {
		if (window.location.hash) {
			navigate_to(window.location.hash);
			$('#nav a[href=/'+window.location.hash+']').parent('li').addClass('selected');
		}
	};
	
	var hijack_links = function() {
		$('a').click(function(e){
			var self = $(this);

			if (window.location.pathname=='/' && self.attr('href').indexOf('#')==1) {
				e.preventDefault();
				navigate_to(self.attr('href').replace('/', ''));
			}
		});
	};
		
	var navigate_to = function(selector) {
		//window.location.hash = selector.replace('#', '');
		$('body').scrollTo(selector, {
				duration: 1000,
				offset: -180
			});
		
	};
	
	var init_nav = function() {
		$('#nav li a').click(function(e){
			$(this).parent('li').siblings('li').removeClass('selected').end().addClass('selected');
		});
	};
	
	var init_products = function() {
		scroller = $('#productscroller');
		if (scroller.length && $('#wrapper').width()==980) {
			$('body').addClass('js');
			var deck = $('<div id="deck"></div>');
			var porthole = $('<div id="porthole"></div>');
			scroller.find('ul:first').wrap(porthole).wrap(deck);
			products = scroller.find('li');
			
			
			// create nav
			var nav = $('<div id="scrollernav"></div>').insertAfter(porthole);
			var prevnext = ($('<ul class="prevnext"></ul>')).appendTo(nav);
			var prev = $('<a href="#" rel="previous"><img src="/img/scroller-prev.gif" alt="previous slide" /></a>').wrap($('<li></li>')).appendTo(prevnext);
			var next = $('<a href="#" rel="next"><img src="/img/scroller-next.gif" alt="next slide" /></a>').wrap($('<li></li>')).appendTo(prevnext);
			
			var itemslist = $('<ul class="items"></ul>');
			var i, l;
			for(i=0, l=products.length; i<l; i++) {
				itemslist.append($('<li><a href="#"><img src="/img/scroller-item-off.gif" alt="'+i+'" /></a></li>'));
			}
			var firstitem = itemslist.find('li:first img:first');
			firstitem.attr('src', firstitem.attr('src').replace('-off', '-on'));
			nav.append(itemslist);
			
			prev.click(function(e){
				e.preventDefault();
				products_move_prev();
			});
			
			next.click(function(e){
				e.preventDefault();
				products_move_next();
			});
			
			scroller.append(nav);
			
			scroll_width = scroller.outerWidth();
		}
	};
	
	var products_move_prev = function() {
		scroll_position--;
		if (scroll_position>=0) {
			var new_left = 0-(scroll_position*scroll_width);
		}else{
			scroll_position = products.length-1;
			var new_left = 0-(scroll_position*scroll_width);
		}
		$('#deck').animate({left: new_left});
		set_scroll_indicator();
	};
	
	var products_move_next = function() {
		scroll_position++;
		if (scroll_position <= products.length-1) {
			var new_left = 0-(scroll_position*scroll_width);
		}else{
			scroll_position = 0;
			var new_left = 0;
		}
		$('#deck').animate({left: new_left});
		set_scroll_indicator();
	};
	
	var set_scroll_indicator = function() {
		var imgs = $('#scrollernav ul.items img');
		imgs.each(function(i, o) {
			var self = $(o);
			if (i==scroll_position) {
				self.attr('src', self.attr('src').replace('-off', '-on'));
			}else{
				self.attr('src', self.attr('src').replace('-on', '-off'));
			}
		});
	};
	
	var init_contact_form = function() {
		var form = $('form.contact-form');
		if (form.length) {
			form.submit(function(e){
				e.preventDefault();
				if ($('#contactEmail').val() && $('#contactComments').val()) {
					form.ajaxSubmit(function(r){
						form.empty();
						form.append('<p>Thank you for your message!</p>');
					});
				}else{
					form.find('span.error').remove();
					if (!$('#contactEmail').val()) $('#contactEmail').after('<span class="error">Required</span>');
					if (!$('#contactComments').val()) $('#contactComments').after('<span class="error">Required</span>');
					
				}
			});
		}
	};
	
	return {
		init: init
	};
}();

jQuery(function($){
	EOMS.UI.Global.init();
});
