/*
 * Lemon Tree Grocer JS
 * ====================
 */ 

LTG = {
	common: {
		init: function() {
		  	// lemon video
			$(".lemon-video").colorbox({width:"600px", height:"400px", iframe:true});		  
		}	
	},
	
	grocery: {
		
		init: function() {
			
			
			$(function(){
			  $(".product-detail:first").show();
			  $("#product_section nav a").click(function(){
			    var selectedProduct = $(this).attr("href").replace(/#/,"");
			    $(".selected").removeClass("selected");
			    $(this).parent("li").addClass("selected");
			    $(".product-detail").hide();
			    $("#"+selectedProduct+"").show();
			    return false;
			  });
			});
			
			
		}
		
	},
	
	people: {
		init: function() {
			var $links = $("section nav ul li a")
			.bind('click', this.person_Click);
			if (!document.location.hash) {	// if no hash, load first person bio
				$links.eq(0).trigger('click');
			} else { // load the bio for hash supplied.
				$links.each(function() {
					if (this.getAttribute('href') == document.location.hash) {
						$(this).trigger('click');
						$(window).load(function() {
							window.scrollTo(0,0);
						});
					}
				});
			}
			

			
			// still working on a better algorithm for this
			$(document).scroll(function(e){
			  var currentYPosition = window.scrollY;
			  var currentBioHeight = $(".bio-detail:visible").height();
			  if(currentYPosition >= 1050){
			    $(".bios").css({"position":"fixed","top":"-"+(currentYPosition-1050)+"px"});
			  } else if(currentYPosition >= 550){
			    $(".bios").css({"position":"fixed","top":"0"});
			  } else {
			    $(".bios").css("position","static");
			  }
			});
						
			
			
		},
		person_Click: function(evt) {
			var $this = $(this);		
			var $all = $(".bio-detail").hide();
			var $go = $($this.attr('href')).show();

			$("section nav ul li").removeClass('selected');			
			$this.parent().addClass('selected');	
			return false;
		}
	}
};

LTG.contact = (function() {
	var $subject,
		$name,
		$email,
		$message,
		$form;
	
	var $fields;

	function init() {
		$subject = $('input#subject');
		$name = $('input#name');
		$email = $('input#email');
		$message = $('textarea#message');
		$form = $('form#form1');
		
		$fields = [$subject, $name, $email, $message];
		
		setFieldText();
	}
	
	function setFieldText() {
		$.each($fields, function(index, $field) {
			// set default value
			if (!$field.val()) {
				$field.val($field.attr('title'));
			}
			// clear on focus if default value
			$field.bind('focus', function() {
				$(this).addClass('focus');
				
				if ($(this).val() == $(this).attr('title'))
					$(this).val('');
			});
			
			// reset to default value if empty
			$field.bind('blur', function() {
				$(this).removeClass('focus');
			
				if (!$(this).val())
					$(this).val($(this).attr('title'));
			});

		});
		
		// clear default values on form submit
		$form.submit(function() {
			var $fields = [$subject, $name, $email, $message];
			
			$.each($fields, function(index, $field) {
				if ($field.val() == $field.attr('title'))
					$field.val('');
			});
		});
	}

	return {
		init: init
	}
})();

UTIL = {
  exec: function( controller, action ) {
    var ns = LTG,
        action = ( action === undefined ) ? "init" : action;

    if ( controller !== "" && ns[controller] && typeof ns[controller][action] == "function" ) {
      ns[controller][action]();
    }
  },

  init: function() {
    var body = document.body,

        controller = body.getAttribute( "id" ),
        action = body.getAttribute( "class" );

    UTIL.exec( "common" );
    UTIL.exec( controller );
    UTIL.exec( controller, action );
  }
};

$(UTIL.init)

