/**
 * Namespace "Common"
 * @class Common
 * @desc Common of Base
 */
Base.Common = function() {
	/**
	 * Applies styling to tables
	 */
	function applyTableStyling()
	{
		
		$('table').each(function(){
			var table = $(this);
			
			// first row - ie6 only
			if( Base.ieVersion == 6 )
				table.find('tr:first-child').addClass('first-child');
			
			// alternate row bg color
			table.find('tr:even').not('tr:first-child').addClass('alt-row');
		});
	}
	
	/**
	 * Apply mouse-over to elements other then a
	 */
	function ie6MouseOver()
	{
		var elements = [
			'ul#nav li.careers',
			'#showcase-controls button',
			'#people-slider',
			'#people-slider-controls button',
			'#people-slider-controls span'
		];
		
		$(elements.toString()).hover(
			function(){ $(this).addClass('hover'); },
			function(){ $(this).removeClass('hover'); }
		);
	}
	
	/**
	 * On pageload init the carousel
	 */
	function initShowcase()
	{
		var start = 0; // set which picture to show first
		var first = $($('#showcase ul li')[start]);
		
		Cufon.replace('#showcase-controls p.text span, #showcase-controls p.text a');
		
		loadShowcaseText(first.attr('class')); // copy text for first image
		
		$('#showcase').jCarouselLite({
      btnNext : '#showcase-controls .next',
      btnPrev : '#showcase-controls .prev',
      visible : 1,
			start   : start,
			auto    : 5000,
			speed   : -1,
			beforeStart: function(){
				$('#tickertape').trigger('pauseTicker');
			},
			afterEnd: function(li){ // copy text after animation
				$('#tickertape').trigger('unpauseTicker');
				loadShowcaseText($(li).attr('class'));
			}
    });
		
		$('#showcase-controls').show(); // show the controls
	}
	
	/**
	 * Shows the text belonging to image
	 */
	function loadShowcaseText(slide_class)
	{
		$('#showcase-controls span:not(.link), #showcase-controls a').hide();
		$('#showcase-controls .'+ slide_class).show();
		
		// check link
		var text = $('#showcase-controls p.text');
		text.addClass('nolink'); // hide span.link
		
		if( text.children('a')[0] ){
			text.removeClass('nolink');
		}
	}
	
	/**
	 * Shows the peopleslider
	 */
	function initPeopleSlider()
	{
		PeopleSlider = $('#people-slider');
		PeopleSlider.people = {};
		PeopleSlider.allItems = PeopleSlider.find('ul li');
		PeopleSlider.selectedItem = -1;
		
		var peopleList = $('#people-slider li');
		peopleList.each(function(i){
			var item = $(this);
			var name_elem = item.children('.name');
			var link = name_elem.children('a');
			var name = link.text();
			
			// remove link from li, but save href and text
			item.person_id = link.attr('href'); // store href in li object
			PeopleSlider.allItems[i].person_id = link.attr('href');
			name_elem.text(name);
			link.remove();
			
			// if selected on pageload, get it
			if( item.hasClass('selected') ){
				getPersonInfo(item.person_id);
			}
			
			// simulate link with click event
			item.bind('click.getPerson', function(){
				PeopleSlider.find('li.selected').removeClass('selected');
				item.addClass('selected');
				getPersonInfo(item.person_id);
			});
		});
		
		var visible  = 5, start = 0;
		var selected = PeopleSlider.allItems.index(PeopleSlider.find('li.selected'));
		
		if( selected >= visible )
			start = selected - (visible-1);
		
		PeopleSlider.jCarouselLite({
			btnNext : '#people-slider-controls .next',
      btnPrev : '#people-slider-controls .prev',
			visible : visible,
			start   : start,
			circular: false,
			beforeStart: function(){
				PeopleSlider.selectedItem = PeopleSlider.allItems.index(PeopleSlider.find('li.selected'));
			},
			afterEnd: function(items){
				peopleSliderClick(items);
			}
		});
		
		// Disable next button if theres not enough persons to slide
		if( peopleList.length <= visible )
			$('#people-slider-controls .next').addClass('disabled');
	}
	
	/**
	 * Handle click on next or prev buttons
	 * @param {array} items
	 */
	function peopleSliderClick(items)
	{
		var first = PeopleSlider.allItems.index(items[0]);
		var last  = first + 4;
		var old   = PeopleSlider.selectedItem;
		
		if( old < first ){
			$(PeopleSlider.allItems[old]).removeClass('selected');
			$(PeopleSlider.allItems[first]).addClass('selected');
			getPersonInfo(PeopleSlider.allItems[first].person_id);
		}
		else if( old > last ){
			$(PeopleSlider.allItems[old]).removeClass('selected');
			$(PeopleSlider.allItems[last]).addClass('selected');
			getPersonInfo(PeopleSlider.allItems[last].person_id);
		}
	}
	
	/**
	 * Ajax load the person html
	 * @param {string} name
	 */
	function getPersonInfo(link)
	{
		// Base.Common.ajaxPerson set @ bottom in master.jsp
		$('#person-details').load(link, {}, function(){ applyTableStyling(); });
	}
	
	
	return {
		/**
		 * Initialize this Class
		 */
		init: function() {
			// Add RTE table styling
			applyTableStyling();
			
			if( Base.ieVersion == 6 )
				ie6MouseOver();
			
			// Determine actions based on body class
			var body = $('body');
			
			if( body.hasClass('home') ){
				//$('#tickertape').tickertape({speedDistance: 1.6, speedTime: 1}); // homepage ticker tape
				$('#tickertape').tickertape(12); // homepage ticker tape
				
				if(! body.hasClass('admin') )
					initShowcase(); // homepage showcase
			}
			
			// Department people slider
			if( body.hasClass('department') ){
				initPeopleSlider();
			}
			
			// window popups
			$('a.newwindow').bind('click.newWindow', function(){
				var link = $(this);
				var left = $(window).width()/2 - 300;
				
				window.open(link.attr('href'), link.attr('title'), 'width=600, height=600, resizable=1, scrollbars=1, top=100, left='+ left);
				return false;
			});
		}
	};
}();
Base.register(Base.Common.init);
