var layout;
var container;

(function($){

	$.setLayout = function() {
		var layoutH = layout.height();
		var windowH = $(window).height();
		if (layoutH < windowH) {
			var newH = container.height() - 352;
			if (newH > 590) {
				layout.height(newH);
			} else {
				layout.height(590);
			}
		}
		
	};

})(jQuery);

$(document).ready(function(){
	
	layout = $('#layout');
	container = $('#container');
	
	// Set layout to max height
	$.setLayout();
	$(window).bind('resize', function() {
		$.setLayout();
	});
	
	// Submenu
	$('#nav li').hover(
		function(){
			$(this).find('.subnav').slideDown('fast');
		},
		function() {
			$(this).find('.subnav').slideUp('fast');
		}
	);
	
	// Post-it note toggle
	$('#postit a.toggle').live('click',
		function(){
			var postIt = $(this).parent();
			var currentState = postIt.attr('class');
			var title = postIt.find('h3');
			var contents = postIt.find('ul');
			if (currentState == 'hidden') {
				$(this).html('Hide');
				$(this).removeClass('show');
				$(this).addClass('hide');
				postIt.removeClass();
				title.show();
				contents.show();
			} else {
				title.hide();
				contents.hide();
				$(this).html('Show');
				$(this).removeClass('hide');
				$(this).addClass('show');
				postIt.addClass('hidden');
			}
			return false;
		}
	);
	
	// Students page
	$('.student').hover(
		function(){
			var studentInfo = $(this).find('.info-screen');
			studentInfo.css('filter', 'alpha(opacity=60)');
			studentInfo.fadeIn(300);
			$(".tooltip-readmore").show();
		},
		function() {
			var studentInfo = $(this).find('.info-screen');
			studentInfo.fadeOut(300);
			$(".tooltip-readmore").hide();
		}
	);

	// Add readmore tooltip on student page
	if ($('.student').length != 0 ) {
		$('body').append('<div class="tooltip-readmore"></div>');
		$(".tooltip-readmore").hide();
	};
	
	// Gallery page
	$('.photo').hover(
		function(){
			var photoSreen = $(this).find('.photo-screen');
			photoSreen.fadeIn(300);
			$(".tooltip-expand").show();
		},
		function() {
			var photoSreen = $(this).find('.photo-screen');
			photoSreen.fadeOut(300);
			$(".tooltip-expand").hide();
		}
	);

	// Add expand tooltip on gallery page
	if ($('.photo').length != 0 ) {
		$('body').append('<div class="tooltip-expand"></div>');
		$(".tooltip-expand").hide();
	};
	

	// Tooltip position
    $(document).mousemove( function(e){
    	$(".tooltip-readmore").css({
            left: (e.pageX + 9) + "px",
            top: (e.pageY + 19) + "px"
        });
    	$(".tooltip-expand").css({
            left: (e.pageX + 9) + "px",
            top: (e.pageY + 19) + "px"
        });
    });
	
	// Videos page
	$('.video').hover(
		function(){
			var videoPlay = $(this).find('.play-screen');
			videoPlay.fadeIn(300);
		},
		function() {
			var videoPlay = $(this).find('.play-screen');
			videoPlay.fadeOut(300);
		}
	);
	
	// Video selection listener
	$('.video').live('click',
		function(){
			var videoLink = getUrlVars($(this).attr('href'));
			var videoSrc = 'http://www.youtube.com/v/' + videoLink['v'];
			var videoTitle = $(this).attr('title');
			Shadowbox.open({
				'content':	'<div id="video-container"><div id="videoContent">' +
							'</div><div id="videoInfo">' + videoTitle + '<a id="sb-nav-close" onclick="Shadowbox.close()"> &nbsp; </a></div></div>',
		        'player':	"html",
		        'width':	710,
		        'height':	510,
		        'viewportPadding':	0,
		        'options': {onFinish: function (){
							$('#videoContent').flash({
								swf: videoSrc,
								width: 640,
								height: 385
							});
						}}
		    });
			return false;
		}
	);
	
	function getUrlVars(url) {
		var map = {};
		var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			map[key] = value;
		});
		return map;
	}
	
	// Quiz selection listener
	$('#launchQuiz').live('click',
		function(){
			var quizSrc = $(this).attr('href');
			Shadowbox.open({
				'content':	'<div id="quiz-container"><div id="quizContent">' +
							'</div></div>',
		        'player':	"html",
		        'width':	710,
		        'height':	540,
		        'viewportPadding':	0,
		        'options': {onFinish: function (){
							$('#quizContent').flash({
								swf: quizSrc,
								width: 640,
								height: 480
							});
						}}
		    });
			return false;
		}
	);
	
	// Office Tour selection listener
	$('#launchTour').live('click',
		function(){
			var tourSrc = $(this).attr('href');
			Shadowbox.open({
				'content':	'<div id="tour-container"><div id="tourContent">' +
							'</div></div>',
		        'player':	"html",
		        'width':	710,
		        'height':	540,
		        'viewportPadding':	0,
		        'options': {onFinish: function (){
							$('#tourContent').flash({
								swf: tourSrc,
								width: 640,
								height: 480
							});
						}}
		    });
			return false;
		}
	);

	// Tooltip
	$(".eventDay").tooltip({
		position: "top right",
		layout: '<div><span class="tooltip-arrow"></span></div>'
	});
	
	// Current time display
	$('#time').jclock({
		timeNotation: '12h',
		am_pm: true
	});
	
	// Cycle banner if exists
	if ($('#slideshow #photos').length != 0 ) {
		$('#photos').cycle();
	};

	// Footer folder flyout
	$('#footer a.work').click(function() {
		if ($.browser.msie && $.browser.version=="6.0") {
			$('#note-container').show();
		} else {
			$('#note-container').animate({
				bottom: '+=543'
			}, 500);
		}
		return false;
	});

	// Footer flyout close button listener
	$('#footer a.close').click(function() {
		if ($.browser.msie && $.browser.version=="6.0") {
			$('#note-container').hide();
		} else {
			$('#note-container').animate({
				bottom: '-=543'
			}, 300);
		}
		return false;
	});
	
	// Start shadowbox for inline pop-ups
	Shadowbox.init({
    	enableKeys: false,
    	players: ['html','iframe','swf','img'],
    	animate: false,
    	animateFade: true,
    	viewportPadding: 0,
        onOpen: function (){
			if ($('.photo-details').length != 0 ) {
				$('#sb-info').css('top', '200px');
				$('#sb-info').css('margin-right', '-100px');
			}
			if ($('#students').length != 0 ) {
				$('#sb-info').css('top', '280px');
				$('#sb-info').css('padding-right', '0');
			}
		},
        onFinish: function (){
			if ($('.photo-details').length != 0 ) {
				$('#sb-info').css({
					'width': '805px',
					'height': '105px',
					'z-index': '0'
				});
				$('#sb-nav-next').addClass('photo');
				$('#sb-nav-previous').addClass('photo');
			}
		}
	});
	
});
