/**
 * @author Christopher Frazier
 */

$(document).ready(function () {

	// Fixes hover for drop-down menus
	if (document.all) {
		$('#menu_site li').hoverClass('hover_fix');
		$('#locations_hover').hoverClass('hover_fix');
	}
	
	// Initialize pop-up functionality
	popup.init();
	
	// Initialize generic slide player
	slidePlayer.init();
	
	if($('#base.home')) { homePlayer.init(); }
	
	// Initialize program menu tabs if found
	if($('#base.program #requirements ul.menu')) { programMenu.init(); }
	
	// Set offsite links with an explanatory title
	$('.offsite').attr('title', 'This link will take you off the La Verne website.').attr('target','blank');
	
	// Give Data tables even / odd row styling
	i=0;
	$('table.data tr:nth-child(even)').addClass('even');


	// If the user has never visited the site, display the beta pop-up and set the beta cookie
	$('#alert_popup').each(function () {
		if (readCookie('alert_popup') == null) {
			createCookie('alert_popup', 'true', 1);		
			popup.show('alert_popup');
		}
	})
	
	// Set up basic show / hide for "Read more" style links.
	
	$('.read_more .more_link').click(function () {
		$(this).parent().addClass('expanded');
	})
		
});

// Fix for IE6 drop-down menus
$.fn.hoverClass = function (c) {
	return this.each(function () {
		$(this).hover(function () {
			$(this).addClass(c);
		}, function () {
			$(this).removeClass(c);
		});
	});
};

var programMenu = {
	current : null,
	current_tab: null,
	height : null,

	init : function () {
		// Set default tab and set
		programMenu.current = $('#requirements div.requirement_set.selected');
		programMenu.current_tab = $('#requirements ul.menu li.selected');
		programMenu.height = $('#requirements').height();
		
		// Add click action to tabs
		$('#requirements ul.menu a').click(function () {
			programMenu.showGroup(this);
			//return false;
		})
	},

	showGroup : function (element) {
		var newGroup = $('#' + element.href.split('#')[1]);
		var newTab = $(element).parent();
		if (programMenu.current !== newGroup) {
			// Hide previous requirements set and remove "selected" class from current tab
			programMenu.current.hide();
			programMenu.current_tab.removeClass('selected');

			// Change current tab and group to the new set
			programMenu.current = newGroup;
			programMenu.current_tab = newTab;
			// Add "selected" class to new tab
			$(programMenu.current_tab).addClass('selected');
			// Show new set
			$(programMenu.current).show();
			
			// Set display area to the largest needed height
			if ($('#requirements').height > programMenu.height) { 
				programMenu.height = $('#requirements').height;
			} else {
				$('#requirements').height(programMenu.height);
			}
			
		}
	}
}

var homePlayer = {
	
	interval :null,
	current_slide : 1,
	max_slides : null,
	interval_time : 8000,
	transition_time : 1,
	
	init : function () {
		
		homePlayer.max_slides = $('#slides .slide').length;
		$('#menu_slides li').click(function () {
			homePlayer.setSlide(this.id.split('_')[1]);
		});
		
		this.interval = window.setInterval( function () {
			var nextSlide = homePlayer.current_slide + 1;
			if (nextSlide > homePlayer.max_slides) { nextSlide = 1; }
			homePlayer.swapSlide(nextSlide);
		},this.interval_time);
	},
	
	swapSlide : function (newSlide) {
		if (newSlide !== homePlayer.current_slide) {
			$('div#slide_' + newSlide).fadeIn(1500);
			$('li#selector_' + newSlide).addClass('selected');
			$('div#slide_' + homePlayer.current_slide).fadeOut(1500);
			$('li#selector_' + homePlayer.current_slide).removeClass('selected');
			homePlayer.current_slide = newSlide;
		}
	},
	
	setSlide : function (slide) {
		clearInterval(homePlayer.interval);
		homePlayer.swapSlide(slide);
		return false;
	}
	
}

var popup = {
	current_content : null,
	
	init : function () {
		// Add the popup HTML to the page
		$('body').prepend('<div id="popup_base"><div id="popup_background">&nbsp;</div><div id="popup_content"></div></div>');

		// Connect click action to anchors with the popup_link class
		$('.popup_link').click(function () {
			popup.show(this.href.split('#')[1]);
		})
	},
	
	hide : function () {
		// Copy the latest popup HTML to the current content block
		popup.current_content.html($('#popup_content').html());

		// Hide the popup interface
		$('#popup_base').hide();
	},
	
	show : function (content_id) {
		// Set the current content object
		popup.current_content = $('#' + content_id);

		// Copy the HTML from the current content block into the popup
		$('#popup_content').html(popup.current_content.html());

		// Show the popup
		$('#popup_base').fadeIn();
		
		// Make the grayed out region the same height as the page
		$('#popup_background').height($('#base').height());
		
		$('#popup_content').css('top', $(window).scrollTop() + 50);

		// Add close functionality to all popup buttons in the content object
		$('.popup_close').click(function () {
			popup.hide();
			return false;
		})
	}
}

var slidePlayer = {
	players : new Array(),
	interval_time : 7000,
	transition_time : 1,
	init_player_id : null,
	
	init : function () {
		$('.slide_player').each(function () {
			player_id = this.id;
			slidePlayer.init_player_id = player_id;
			slidePlayer.players[player_id] = {
				current_slide : 0,
				interval : window.setInterval("slidePlayer.swapSlide('" + player_id + "')", slidePlayer.interval_time),
				slides : new Array()
			}
			$('#' + this.id + ' .slide').each(function () {
				slidePlayer.players[slidePlayer.init_player_id].slides[slidePlayer.players[slidePlayer.init_player_id].slides.length] = this;
			});
		})
	},
	
	swapSlide : function (player_id) {
		current_slide = slidePlayer.players[player_id].current_slide;

		slidePlayer.players[player_id].current_slide++;
		if (slidePlayer.players[player_id].current_slide >= slidePlayer.players[player_id].slides.length) { slidePlayer.players[player_id].current_slide = 0; }

		new_slide = slidePlayer.players[player_id].current_slide

		$(slidePlayer.players[player_id].slides[current_slide]).fadeOut();
		$(slidePlayer.players[player_id].slides[new_slide]).fadeIn();
	}
}
