var THEKIDS = THEKIDS || {};

THEKIDS.global = {
	navigation: {
		init: function(){
			$('#header ul#mainNavigation').delegate('li.topLevel','hover', function( event ) {
			    if( event.type === 'mouseenter' ) {
					$(this).children('a.topLevelLink').addClass('over');
					$(this).children('ul.subLevel').show();
				} else {
					$(this).children('a.topLevelLink').removeClass('over');
					$(this).children('ul.subLevel').hide();
				}
			});
			
			$('div#header ul#mainNavigation li#info.topLevel a.topLevelLink').live('click', function(event) {
			  	event.preventDefault();
			});
		}
		
	}, 
	
	videosController: {
		init: function(){
			$('#videosSelector').delegate('li','hover', function( event ) {
			    if( event.type === 'mouseenter' ) {
					$(this).addClass('over');
				} else {
					$(this).removeClass('over');
				}
			});
			
			// show first video
			$('#videosWrapper #videos li:first-child').show();
			
			$('#videosSelector').delegate('li','click', function( event ) {
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
			});
			
			$('#videosSelector').delegate('li','click', function( event ) 
			{
				$('#videosWrapper #videos li.videoItem').html('');
				
				if($(this).hasClass('youtube')) {
					// load youtube player
					$('#videosWrapper #videos li.videoItem').html('<iframe src="http://www.youtube.com/embed/' + $(this).attr('rel') + '?wmode=transparent' + '" width="600" height="338" frameborder="0"></iframe>');
				} else {
					// load vimeo player
					$('#videosWrapper #videos li.videoItem').html('<iframe src="http://player.vimeo.com/video/' + $(this).attr('rel') + '" width="600" height="338" frameborder="0"></iframe>');
				}
				
				//var index = $(this).index();
				//$('#videosWrapper #videos li.videoItem').hide();
				//$('#videosWrapper #videos li:eq(' + index + ')').show();
			});
			
			$('#videosSelector li:first-child').trigger('click');
		}
	}, 
	
	homepageEvents: {
		init: function(){
			// init slideshow
			$('#theKidsFeaturedWorkSlideshow').cycle({
				fx: 'fade',
				speed: 1400,
				//speedIn:
				//speedOut:
				timeout: 3000,
				sync: 0
			});
			
			// listen for window resize
			$(window).resize(function() {
				var yOffset = 100;
				var yPosition = ($(window).height() / 2) - 275;
				yPosition += yOffset/2;
				if (yPosition <= yOffset) yPosition = yOffset;

				$('#theKidsFeaturedWork').css({
					'top' : yPosition
				});
			});
			
			$(window).resize();
		}			
	}
	
};

$(window).load(function(){
	THEKIDS.global.navigation.init();
	
	THEKIDS.global.videosController.init();
	
	if ($('body.homePage').length > 0){
		THEKIDS.global.homepageEvents.init();
	}
	
});

