/*

bool VIL_add_event_listener() -- Attach an event listener to an object
object element -- The object to attach the listener to
string event_type -- The name of the event type (e.g. click, mouseover)
string listener -- The event listener function
bool useCapture -- useCapture parameter to addEventListener

*/

function VIL_add_event_listener( element, event_type, listener, useCapture ) {
  var return_value = false;

  // The UA supports it, so use the DOM method
  if ( element.addEventListener ) {
    element.addEventListener( event_type, listener, useCapture );
    return_value = true;
  }
  // end if

  // Use the traditional method
  else {
    element[ "on" + event_type.toLowerCase() ] = listener;
    return_value = true;
  }
  // end else

  return return_value;
}
// end VIL_add_event_listener


function VIL_loading_complete( event ) {
  // Execute specific onload logic for the current page if defined
  if ( window.loading_complete ) {
    window.loading_complete();
  }
  // end if

  return;
}
// end VIL_loading_complete


VIL_add_event_listener( window, "load", VIL_loading_complete, false );


/*****************************************************************************
**
**		Begin homepage behaviors
**      Author: Ben Belcourt - 2008.03.12
**
*****************************************************************************/

$(document).ready(function() {
   width.init();
  // introImg.init();
   quotes.init();
   viTab.init();
   dropDown.init();
 });


/* Begin HP image rotation behavior */

var introImg = {
	introID :		'intro',
	altImg :		'alt',
	imgTimeout :	5000,
	
	init : function () {
		setTimeout('introImg.rotate()', introImg.imgTimeout);
	},
	
	rotate : function () {
		$('#'+introImg.introID).toggleClass(introImg.altImg).fadeIn('slow');
		//$('#'+introImg.introID).css({ backgroundImage: 'url(/images/homepage/intro-image-2.jpg)' }).fadeIn('slow');
	}
};

/* Begin Quote "show/hide" behavior */

var quotes = {
	containerDiv :		'quotes',
	controlDiv :		'quote_controls',
	hideClass :			'hide',
	quoteID :			'quote',
	quoteIndex :		0,
    scrollSpeed :       10000,
	showClass :			'show',
	selectedClass :		'selected',
	
	
	init : function () {
		
		if ($('#'+quotes.containerDiv)) {
			$('#'+quotes.containerDiv).css({
					height: 122,
					width: 231
			});
			quotes.slideShow();
			$('#'+quotes.controlDiv+' > li').children('a').click(function(){
					quotes.toggle(this);
					return false;
			});
		}
	},
	
	onFadeIn : function () {
		var quoteClass = this.className;
		$('#'+quotes.controlDiv+' > li').removeClass(quotes.selectedClass);
		$('#'+quotes.controlDiv).children('.'+quoteClass).addClass(quotes.selectedClass);
	},
	
	slideShow : function () {
		var quoteDiv = $('#'+quotes.containerDiv +' > div');
		var currentDiv = quoteDiv[quotes.quoteIndex];
		quotes.quoteLast = quoteDiv.length;
		
		if( quotes.quoteLast > 0 ) {
			$('#'+quotes.containerDiv).cycle({
					fx: 'scrollDown',
					before: quotes.onFadeIn,
					timeout: quotes.scrollSpeed
			});
		}
	},
	
	toggle : function (link) {
		var showDiv = $(link).parent('li').attr('class');
		
		$('#'+quotes.containerDiv).cycle('stop');
		$('#'+quotes.controlDiv+' > li').removeClass(quotes.selectedClass);
		$(link).parent('li').addClass(quotes.selectedClass);
		
		
		$('#'+quotes.containerDiv +' > div').css({
				display: 'none',
				opacity: 0,
				position: 'absolute',
				top: 0
		});
		$('#'+quotes.containerDiv).children('.'+showDiv).css({
				display: 'block',
				opacity: 1
		});
	}
};


/* Begin homepage tabs behavior */

var viTab = {
	activeTabIndex :	0,
	contentClass : 		'tab_container', //Class of tab content container
	hideClass :			'hide',
	selectedTab :		'selected',
	tabList : 			'tabs_list', //ID of tab ul
	
	attachEvents : function () {
		$('#'+viTab.tabList+' > li').children('a').click( function(e) {
			var $target = $(e.target);
			while (!$target.is('li')) {
				$target = $target.parent();
				if ($target.is('body')) {
					return;
				}
			}
			viTab.toggleTabs($target);
			e.preventDefault();
		});
	},
	
	hideContent : function () {
		var activeTab = $('#'+viTab.tabList).children('.'+viTab.selectedTab).attr('id');
		$('.'+viTab.contentClass).not('#'+activeTab+'_content').addClass(viTab.hideClass);
	},
	
	init : function () {
		if( $('#'+viTab.tabList) && $('#'+viTab.tabList).size() > 0 ) {
			viTab.hideContent();
			viTab.attachEvents();
		}
	},
	
	toggleTabs : function ($target) {
		var activeTab = $target.attr('id');
		$target.siblings().removeClass(viTab.selectedTab);
		$target.addClass(viTab.selectedTab);
		$('.'+viTab.contentClass).removeClass(viTab.hideClass);
		$('.'+viTab.contentClass).not('#'+activeTab+'_content').addClass(viTab.hideClass);
	}
};


/* Fixes for dropdown behavior */

var dropDown = {
	firstClass :    'first',
    hoverClass :	'sfhover',
	lastClass :		'last',
	navID :			'VIL_primary_nav',
	version :		7.0,
	
	hover : function () { /* IE hover fix */
		if ($.browser.version <= dropDown.version && $('#'+dropDown.navID)) {
			$('#'+dropDown.navID+' > li').mouseover(function(){$(this).addClass(dropDown.hoverClass);});
			$('#'+dropDown.navID+' > li').mouseout(function(){$(this).removeClass(dropDown.hoverClass);});
		}
	},
	
	init : function () {
		dropDown.hover();
		dropDown.listStyle();
	},
	
	listStyle : function () {
		var submenus = $('#'+dropDown.navID+' > li').children('div').children('ul');
		for (var x=0; x<submenus.length; x++) {
			$(submenus[x]).children('li:last').addClass(dropDown.lastClass).end().children('li:first').addClass(dropDown.firstClass);
		}
	}
};

/* Set page width */

var width = {
	container :			'VIL_primary_content',
	focusID :			'VIL_focus_area_content',
	widthClass :		'widePage',
	
	init : function () {
		if(!$('#'+width.focusID)[0]) {
			width.setWidth();
		}
	},
	
	setWidth : function () {
		$('#'+width.container).addClass(width.widthClass);
	}
};
