$(document).ready(function(){
	$("input#MyBucknellUsername").focus();

	$("ul.tabs a").click(function(){
		$("ul.tabs a.active").removeClass('active');
		$(this).addClass('active');
		var tab = $(this).attr('href');
		$("div.tab").hide();
		$(tab).show();
		return false;
	});
	
	$("#nav li").hover(function() {
		$(this).children("a").slice(0,1).addClass('hover');
		$(this).children('ul.dropdown').show();
	}, function() {
		$(this).children("a").slice(0,1).removeClass('hover');
		$(this).children('ul.dropdown').hide();
	});
	
	$("dl.tree dt a").click(function(){
		$(this).toggleClass('active');
		$(this).parent().next('dd').toggle();
		return false;
	});
	
	// Expand/Collapse Tree structure
	$("a.collapse_all").click(function(){
		$("dl.tree dd").hide();
		$("dl.tree dt a").removeClass('active');
		return false;
	});
	
	$("a.expand_all").click(function(){
		$("dl.tree dd").show();
		$("dl.tree dt a").addClass('active');
		return false;
	});
	
	$('#calendar').datepicker(calendar_options);
	$('.carousel').carousel();
	$('.media_center').carousel({perpage:1, padding: 10});
	$('.fast_facts').carousel({perpage:1});
	
	$('input#SiteSearch').focus(function(){
		if($(this).attr('value') == $(this).attr('defaultValue'))
		{
			$(this).attr('value',''); 
		}
	});
	$('input#SiteSearch').blur(function(){
		if($(this).attr('value') == '')
		{
			$(this).attr('value', $(this).attr('defaultValue')); 
		}
	});
	
	// General playlist
	$("ul.video_thumbnails a").mouseover(function(){
		$("#video_description").html($(this).attr('title'));
		return false;
	});
	// Callout playlists
	$("ul.video_callout_list a").mouseover(function(){
		$("#video_description").html($(this).attr('title'));
		return false;
	});

	$("ul.video_thumbnails a").click(function(){
		index = $('ul.video_thumbnails a').index(this);
		//alert(index);
		playVideo(index, 'mini_flash_player');
		$(this).parents("ul").find('a.active').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
	// Multimedia center playlist
	$('ul.media_list a').click(function(){
		index = $('ul.media_list').find('li').index($(this).parent());
		playVideo(index, 'mini_flash_player');
		//$('ul.media_list').find('a.selected').removeClass('selected');
		//$(this).parent('li').find('a.bordered_img').addClass('selected');
		return false;
	});
	//general video callout playlist
	$('ul.video_callout_list a').click(function(){
		index = $('ul.video_callout_list').find('li').index($(this).parent());
		//alert(index);
		playVideo(index, 'mini_flash_player');
		$('ul.video_callout_list').find('a.selected').removeClass('selected');
		$(this).parent('li').find('a.bordered_img').addClass('selected');
		return false;
	});
	
	$("#wrapper #sidebar #randomimage").html("<div class=\"callout\"><h2>Bucknell in Pictures</h2>" + randomSideImage(genericimages) + "</div>");
	
	if(!$('#BodyCopy img').attr('class')){
		$('#BodyCopy img').addClass('photo');
	}

	$('.carousel').find('img').removeClass('photo');

	//display random admissions feature
	var $features = $('#admissions_feature').children();
	var featurecount = $features.length;
	var randomfeature = Math.floor(Math.random()*featurecount) + 1;
	$('#admissions_feature').find('div#feature' + randomfeature).show();
});

var genericimages = new Array();
for(var x=1; x<=51; x++){
	genericimages.push('Generic' + x + '.jpg');
}


function randomSideImage(imgArray){
	var arrayLength = imgArray.length;
	var randomnumber1 = Math.floor(Math.random()*arrayLength)
	var randomnumber2 = Math.floor(Math.random()*arrayLength)
	while(randomnumber2 == randomnumber1){
		randomnumber2 = Math.floor(Math.random()*arrayLength)
	}
	var randomImageHtml = "<img class=\"photo\" src=\"images/system/GenericPages/" + imgArray[randomnumber1] + "\"><img class=\"photo\" src=\"images/system/GenericPages/" + imgArray[randomnumber2] + "\">";
	return randomImageHtml;
}

var events = new Array();
/*events[5] = new Array(1, 4, 13, 24, 28);
events[6] = new Array(4, 7, 19, 21);*/
var calendar_options = {beforeShowDay: show_event, changeYear: false, changeMonth: false, changeFirstDay: false, dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']};

function calendar_select(date)
{
	// handle calendar click event
	document.location = '/script/admissions/visitationcalendar/details.asp?date=' + date;
}

function show_event(date)
{
	if(typeof(events[date.getMonth()]) != 'undefined')
	{
		for(i=0; i < events[date.getMonth()].length; i++)
		{
			if (events[date.getMonth()][i] == date.getDate())
			{
				return [true, 'ui-datepicker-event'];
			}
		}
	}
	return [true, ''];
}

// Flash integration
function getFlashMovieObject(movieName){
	if (window.document[movieName]){
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	}
	else // if (navigator.appName.indexOf(”Microsoft Internet”)!=-1)
	{
		return document.getElementById(movieName);
	}
}

// Pass in path to movie, and flash object name ( same name as passed into SWFObject)
function playVideo(index, flash_object)
{
	var flashmovie = getFlashMovieObject(flash_object);
	flashmovie.playVideo(index);
}

function changeVideo(index)
{
	if($(active_playlist)[0] == $('.media_list')[0])
	{
		$(active_playlist).find('a.selected').removeClass('selected');
		$(active_playlist).find('li').slice(index, index + 1).find('a.bordered_img').addClass('selected');
	}
	$(active_playlist).find('a.active').removeClass('active');
	$(active_playlist).find('a').slice(index, index + 1).addClass('active');
}

//samek site navigation
sfHover = function() {
	if(document.getElementById("sameknav")){
		var sfEls = document.getElementById("sameknav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function flashPutHref(href) { location.href = href; }