/* 
 * 
 * File: thondo.common.js 
 * Notes: Common JS functions and global namespace
 * 
 *  Copyright Thondo Communications
*/

thondo = window.thondo || {};

thondo.common = (function() {
	// private

    function init() {
        //initialize common code
		heroSlider();
		infoBox();
		easingDefault();
		initCarousel();
    }

	function heroSlider() {
		$('#hero-slider').each(function(){
			var heroSlider = $(this), 
				next = $('a.next', heroSlider),
				list = $('ul:first',heroSlider),
				_width = heroSlider.width(), 
				_max = list.children('li').size() * (-_width);
				
			_curPos = list.position();
			next.click(function(e){
				e.preventDefault();
				// close any open detail areas
				$('.details').hide();
				var _move = _curPos.left + (-_width);
				list.animate({left:_move + 'px'}, 800, function(){
					_curPos = list.position();
					// **infinite functionality**
					// keep popping off the first LI with content
					// and placing it at the end of the carousel, leaving
					// an empty LI so as to keep the list constantly moving
					// to the left.
					var nextNode = null;
					if($('.next-in-line').size() == 0) {
						nextNode = list.children().first();
					} else {
						nextNode = list.find('li.next-in-line');
					}
					// clone LI, place it at end and then perform cleanup
					nextNode.removeClass('next-in-line').clone(true).appendTo(list);
					nextNode.next().addClass('next-in-line');
					list.css('width',list.width() + _width + 'px'); // need to increase the UL width					
				});
			});
		});
	}
	
	function infoBox(context){
		$('.info-box').each(function(){
			var infoBox = $(this),
				details = $('.details',infoBox),
				open = $('<a class="open" href="#">Open</a>'),
				close = $('<a class="close" href="#">Close</a>');			
			
			details.hide();
			
			// add elements
			open.appendTo(infoBox);
			close.appendTo(details);
			
			// events
			open.click(function(e){
				e.preventDefault();
				$('.details',$(this).parent()).show('medium','linear');
			});
			close.click(function(e){
				e.preventDefault();
				$('.details',$(this).parent().parent()).hide('medium','linear');
			});
		});		
	}
	
	function easingDefault() {
		// add easeOutQuad to the easing options and make it the default option.
		jQuery.extend(jQuery.easing, {
			def: 'easeOutQuad',
			easeOutQuad: function (x, t, b, c, d) {
				return -c *(t/=d)*(t-2) + b;
			}
		});
	}
	
	function initCarousel(){
		$('.carousel').carousel();
	}
	
    return {
        // public
        init: init
    };
})();

jQuery(document).ready(thondo.common.init);
