// Load Shadowbox components
Shadowbox.loadSkin('classic', '/scripts/shadowbox/skin');
Shadowbox.loadLanguage('en', '/scripts/shadowbox/lang');
Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'swf'], '/scripts/shadowbox/player');

function removeShadowboxScrolling() {
		$('#shadowbox_content').attr('scrolling', 'no');
	}

function timedRefresh() {
		setTimeout('location.reload(true);',0);
	}

function prepareSlider() {
	
	$('#nav_sub li:first').addClass('current');
	
	var course_nav = $('#nav_sub a');
	var course = $('#course_strip div');
	var course_height = course.closest('#course').height();

	course_nav.click(function(){
		var offset_multiplier = course_nav.index(this);
		var offset = "-" + offset_multiplier * course_height + "px";
		$('#course_strip').animate({"marginTop": offset }, "slow");
		$('#nav_sub li').removeClass('current');
		$(this).closest('li').addClass('current');
		return false;
	});
}

function openPopup(url) {	
		window.open(
			url,
			'coursewindow',		'status=0,location=0,scrollbars=0,menubar=0,toolbar=0,width=700,height=525,resizable=0,'
		);
		return false;
	}

function swapClass() {
	$('.sb-web')
		.removeClass()
		.addClass('popup');
	}

$(document).ready(function(){

	// Add Course Evaluation navigation item
	/*
	if ($('.status_complete').length > 0) {
		$('#nav_main').append('<li><a href="/evaluation/">Evaluation</a></li>');
	}
	*/

	if ($('#course_strip div').length > 0) {
		prepareSlider();
	}

	// Fork for smaller screen resolutions
	if ((screen.width>=801) && (screen.height<=800) && (screen.height>=601)) {
		// Insert Shadowbox override stylesheet
//		$('title').before('<link rel="stylesheet" href="/css/sb-override.css" type="text/css" media="all">');
		swapClass();
		// Create event handlers to trigger openPopup() for swapped class
		$('.popup').click(function () {
			var url = $(this).attr('href');
			openPopup(url);
			return false;
		});
	} else if ((screen.width<=800) && (screen.height<=600)) {
		swapClass();
		// Create event handlers to trigger openPopup() for swapped class
		$('.popup').click(function () {
			var url = $(this).attr('href');
			openPopup(url);
			return false;
		});
	}

	// Initialize Shadowbox		
    var sb_options = {
		animSequence:		'sync',
		counterType:		'skip',
		continuous:			true
    };
	Shadowbox.init(sb_options);

	// Add Shadowbox listeners
	if ($('.sb-img').length > 0) {
		Shadowbox.setup($('.sb-img'));
	}
	if ($('.sb-imgs').length > 0) {
		Shadowbox.setup($('.sb-imgs'), {
			gallery:	'Gallery'
		});
	}
	if ($('.sb-web').length > 0) {
		Shadowbox.setup($('.sb-web'), {
			player: 	'iframe',
			onFinish: 	removeShadowboxScrolling,
			onClose:	timedRefresh,
			width:		'700',
			height:		'525'
		});
	}
	if ($('.sb-vid').length > 0) {
		Shadowbox.setup($('.sb-vid'), {
			width:		'400',
			height:		'300'
		});
	}
	
});
