	
/*////////////////////////////////////////////////////////////////////////////////////////////////
//                                         ///////////////////////////////////////////////////////
//    isaacw                               ///////////////////////////////////////////////////////
//                                         ///////////////////////////////////////////////////////
//    Website: www.isaacw.com              ///////////////////////////////////////////////////////
//    Date:    07/30/08                    ///////////////////////////////////////////////////////
//                                         ///////////////////////////////////////////////////////
//    Coded by Isaac Weinhausen            ///////////////////////////////////////////////////////
//    http://isaacw.com                    ///////////////////////////////////////////////////////
//                                         ///////////////////////////////////////////////////////
//    Feel free to use these scripts.      ///////////////////////////////////////////////////////
//    Please give credit where due. You    ///////////////////////////////////////////////////////
//    are welcome to contact me with any   ///////////////////////////////////////////////////////
//    questions and/or thoughts. Thanks!   ///////////////////////////////////////////////////////
//                                         ///////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////*/


	// XHTML Requirements
	// 	- div#containter
	// 	- ul.nav
	// 	- 'href's must relate to target 'id's

	// Create debug vars
	if (!console) {
		var console = new Object();
		console.log = function() {
			// leave empty
		}
	};

	// Psuedo Classes
	function Link(){
		
		// Constructor code
		
		// Private variables and functions

		// Public variables
	
		// Privileged functions
		
		// Prototyped Functions
		
		// Prototyped Variables
		
	}
	
	Link.prototype = {
	    url: 'link name undefined',
		xTarget: -1,
		$: null
	}	
	
	
	// Objects
	var Nav = (function(){
	
		function obj(){};
		
		obj.$ = null;
		obj.links = new Array();
		obj.currentLink = null;
	
		obj.initialize = function(){
			
			obj.$ = $('#branding,#nav');	
			
			obj.$
				
				// Create Link objects
				.find('a')
					.each(function(i){
						
						// Define link
						obj.links[i] = new Link();
						obj.links[i].url = $(this).attr('href');
						obj.links[i].$ = $(this);
						
						// Check for "on" state
						if ($(this).parent().hasClass("on")) {
							obj.currentLink = obj.links[i];
						};
						
					})
					.end()
				.end()
				
		};
		
		// Checks to see if an anchor name exists in the url, so that we can jump to that location
		obj.checkUrl = function(){	
			
			var url = new String(window.location);
			var linkName = "";
			if (url.search('#') != -1){
				linkName = url.substring(url.indexOf('#')+1);
				// console.log('exits! Location: '+url.indexOf('#')+'  linkName: '+linkName);
				for (i=0;i<Nav.links.length;i++) {
					if (Nav.links[i].name == linkName) {
						Nav.links[i].el.click();
						break;
					}
				}
			}
			else {
			 	// console.log('doesn\'t exit');
			 	// FUTURE: Jump to default page!
			}
			
		};
		
		// FUTURE: Support browser's back and foward naviation
		obj.back = function(){};
		obj.forward = function(){};
	
		return obj;

	})();
	
	var Scroller = (function(){
		
		// 
		// Coded by Travis Beckham
		// http://www.squidfingers.com | http://www.podlob.com
		//
		// Stripped down and manipulated by Olof Lönnroth
		// http://lonnroth.info
		//
		// Modified by Isaac Weinhausen
		// http://isaacw.com
		// 
		
		function obj(){};
	
		obj.scrollLoop = false;
		obj.scrollInterval = null;
		
		// X position (from center) of specified element
		obj.getXTarget = function(linkName){
			var el = $('#'+linkName+'Page');
			var radialWidth = el.width() * 0.5;
			var xPos = el.offset().left;
			return xPos + radialWidth;
		};
		
		// Inner width of browser window
		obj.__getViewportWidth = function(){
			
			// jQuery 1.2.1 BUG ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			// In IE6, $(window).width() returns the document's width, not the viewport width.
			// Revert to document.documentElement.clientWidth instead.
			if ($.browser.msie && $.browser.version == "6.0") return document.documentElement.clientWidth;
			else return $(window).width();
			
		};
		
		// Width of entire page
		obj.__getDocumentWidth = function(){
			return $('#container').width();
		};
		
		// Distance scrolled from the left
		obj.__getScrollLeft = function(){
			if($.browser.msie) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
			else return window.pageXOffset;
		};
		
		// Distance scrolled from the top
		obj.__getScrollTop = function(){
			if($.browser.msie) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			else return window.pageYOffset;
		};
		
		// Maximum distance that can be scrolled from the left
		obj.__getScrollLeftMax = function(){
			return obj.__getDocumentWidth() - obj.__getViewportWidth();
		};
		
		// Make sure the requested scrollLeft is attainable
		obj.__safeScrollLeft = function(x){
			var scrollLeftMax = obj.__getScrollLeftMax();
			if(x >= 0) return (x > scrollLeftMax) ? scrollLeftMax : x;
			else return 0;
		};
		
		// Middle-align the requested scrollLeft
		obj.__middleAligned = function(x){
			return obj.__safeScrollLeft(x - obj.__getViewportWidth() * 0.5);
		}
		
		// Creates and manages the interval responsible for animating the window.scrollTo command
		obj.scrollTo = function(x,y){
			if(obj.scrollLoop){
				var left = obj.__getScrollLeft();
				var top = obj.__getScrollTop();
				console.log('left: '+left+' top: '+top);
				if(Math.abs(left-x) <= 2 && Math.abs(top-y) <= 2){
					window.scrollTo(x,y);
					clearInterval(obj.scrollInterval);
					obj.scrollLoop = false;
					obj.scrollInterval = null;
					
				}else{
					window.scrollTo(left+(x-left)/2, top+(y-top)/2);
				}
			}else{
				console.log('middleAligned(x): '+obj.__middleAligned(x)+'  y: '+y+' --- Viewport Width: '+obj.__getViewportWidth()+'  ScrollLeft Max: '+obj.__getScrollLeftMax());
				obj.scrollInterval = setInterval("Scroller.scrollTo("+obj.__middleAligned(x)+","+y+")",50);
				obj.scrollLoop = true;
			}
		};
		
		return obj;

	})();
	
	// Functions
	function validateForm(f) {
		
		pass = true;
		
		// Prep attention messages
		$(f).css("position", "relative");
		
		$([f.name, f.email, f.message]).removeClass("attn");
		$(f).find(".attn-msg").remove();
				
		if (!f.message.value || f.message.value == "") {
			
			$(f.message).addClass("attn");
			addNewAttnMsg(f, f.message, "message", "<b>&#x2190;&nbsp;</b><span>Don't forget the message!</span>");
			pass = false;
			
		}
	
		if (!f.email.value || f.email.value == "") {
			
			$(f.email).addClass("attn");
			addNewAttnMsg(f, f.email, "email", "<b>&#x2190;&nbsp;</b><span>Email address please.</span><br/><span>Don't fret, I won't spam you.</span>");
			pass = false;
			
		}
		
		if ( (f.email.value || f.email.value != "") && !isEmail(f.email.value) ) {
			
			$(f.email).addClass("attn");
			addNewAttnMsg(f, f.email, "email", "<b>&#x2190;&nbsp;</b><span>That's not an email address, silly! Follow this format: joe@website.com</span>");
			pass = false;
			
		}
		
				// 
				// else if ((f.emailAddress.value || f.emailAddress.value != "") && !isEmail(f.emailAddress.value)) {
				// 	alert('Please make sure your email address looks something like this: user@domain.com');
				// 	f.emailAddress.focus();
				// 	return false;
				// }
			
		if (!f.name.value || f.name.value == "") {
			
			$(f.name).addClass("attn");
			addNewAttnMsg(f, f.name, "name", "<b>&#x2190;&nbsp;</b><span>Who are you?</span>");
			pass = false;
			
		}
		
		return pass;
		
	}	
		
	function addNewAttnMsg(f, field, idText, msgText) {
		
		htmlText = '<div class="attn-msg" style="position:absolute;"></div>';
		idPrefix = "attn-msg-";
		
		var fieldPos = $(field).parent().position();
		var formWidth = $(f).width();
		$(f)
			.append(htmlText)
			.find("div:last-child")
				.attr("id", idPrefix + idText)
				.css({
					top:	fieldPos.top + 14,
					left:	formWidth + 7
				})
				.html(msgText);
			
		return false;
	}
	
	function isEmail(em) {
		if (em.indexOf('@') !== -1) {
			host = em.substring(em.indexOf('@') + 1, em.length);
			if (host.indexOf('.') !== -1) return true;
		}
		return false;
	}
	
	function setMouseEvents(){
	
		for (var i=0; i < Nav.links.length; i++) {
			Nav.links[i].$
			
				.hover(function(){
					$(this).parent()
						.addClass("hover")
						.removeClass("on");
					/*
					$("#status")
						.children()
							.andSelf()
								.addClass("highlight");
					*/
				},
				function(){
					$(this).parent()
						.removeClass("hover");
					Nav.currentLink.$
						.parent()
							.addClass("on");
					/*
					$("#status")
						.children()
							.andSelf()
								.removeClass("highlight");
					*/
				});
				
		}

		
	}
	
	function setupJqirEls(){
	
		$(".jqir").jQIR("gif");
		
	}
	
	function setFormEvents(){
	
		$("form.simple")
			.find(":text, textarea")
				.focus(function(){
					$(this).val("");
				})
				.blur(function(){
					$(this).val("llalaal");
				});
		
	}
	
	// Instantiation
	$(function(){
  		// Document is ready...
  		
		//setupJqirEls();

		Nav.initialize();
		for (var i=0; i < Nav.links.length; i++) {
			console.log("URL " + i + ": " + Nav.links[i].url);
		}
		
		setMouseEvents();
		//setFormEvents();
		
	});
	
	$(window).load(function() {
		$("label.overlabel").overlabel( { wrapper_text:  { 'prefix': '(', 'suffix': ')' } } );
	});
	