var global = {
	view: {
		embed_swf: function(options, flashvars) {
			var flashvars = flashvars || {};
			var attributes = { bgcolor: "#ffffff" };
			var params = {
				wmode: "transparent",
				allowScriptAccess: "always"
			};
			
			if (global.model.has_flash) {
				swfobject.embedSWF(
					'/swfs/' + options.swf,
					options.target,
					options.width || flashvars.stageWidth,
					options.height || flashvars.stageHeight,
					"8.0.0", "",
					flashvars, params, attributes
				);
			}
			else {
				global.controller.show_get_flash_msg(options.target);
			}
		},
		
		//
		// bgMoviePlay & bgMoviePause are stubs to prevent errors caused by the movie player which assumes these to exist.
		//
		bgMoviePlay: function() {},
		bgMoviePause: function() {}
	},
	
	model: {
		open_course_description: null,
		movie_width: "630",
		movie_height: "380",
		has_flash: null,
		tracker: null,
		tracking_acct: (window.location.host.indexOf('ccsmfa.com') == -1) ? 'UA-6980626-2' : 'UA-6979936-2'
	},
	
	controller: {
		init: function() {
			//
			// Initialize analytics tracker
			//
			global.model.tracker = _gat._getTracker(global.model.tracking_acct);
			global.model.tracker._trackPageview();
			
			//
			// Detect installed flash
			//
			try {
				global.model.has_flash = swfobject.hasFlashPlayerVersion("8");
			} catch (e) {}
			
			//
			// Setup global menu system
			//
			$('#mainNav .navMenu').hover(function() {
				$('ul', this).slideDown('fast');
			}, function() {
				$('ul', this).slideUp('fast');
			});
			
			//
			// Highlight active nav item
			//
			$('#'+page_id+'_nav').addClass('current');
			var menu = $('#'+page_id+'_nav').closest('#program_details_nav');
			if (menu.length) {
				menu.addClass('current');
			}

			//
			// Setup trackable links on the page
			//
			$('a.trackable').click(function(event) {
				var link_text = $(this).attr('name') || $.trim($(this).text().replace(/ /g, ''));
				global.controller.linkTrack('clicked'+link_text);
			});
			
			//
			// Setup all Read More links on this page
			//
			$('a.read_more').click(function(event) {
				event.preventDefault();
				
				$(this).closest('#content_container > div > div').children('.read_more_target').slideDown('fast');
				$(this).fadeOut('fast');
			});
			
			$('a.close_read_more').click(function(event) {
				event.preventDefault();

				$(this).closest('.read_more_target').slideUp('fast');
				$(this).closest('#content_container > div > div').find('.read_more').fadeIn('fast');
			});

			//
			// Handle page specific initialization
			//
			switch (page_id) {
				case 'homepage':
					global.view.embed_swf({
						swf: "homepage.swf",
						target: 'homepage_animation',
						width: "960",
						height: "666"
					});

					break;

				case 'mfa_program':
				case 'mfa_design':
				case 'mfa_trans_design':
					global.controller.build_movies();

					break;
					
				case 'curriculum':
					//
					// Setup Curriculum Guide overlays
					//
					$('.curriculum_guide td a:not(.course_close_button)').click(function(event) {
						event.preventDefault();
						event.stopPropagation();

						$('#content_container').prepend("<div id='curriculum_overlay'>&nbsp;</div>");
						$('#curriculum_overlay').show().fadeTo('fast', 0.7);

						var course_description = $(this).next('div');
						course_description.slideDown('fast');
						global.model.open_course_description = course_description;
					});

					//
					// Setup Curriculum Guide Dismissal clicks/escape key
					//
					$('#curriculum_overlay').live('click', global.controller.close_curriculum_overlay);
					$('.course_close_button').click(global.controller.close_curriculum_overlay);

					$(window).keydown(function(event) {
						if (event.keyCode == 27) {
							global.controller.close_curriculum_overlay(event);
						}
					});
				
					break;
			}
		},
		
		close_curriculum_overlay: function(event) {
			event.preventDefault();
			
			if (global.model.open_course_description) {
				$('#curriculum_overlay').fadeOut('fast', function() { $(this).remove(); });
				global.model.open_course_description.slideUp('fast');
				global.model.open_course_description = false;
				global.controller.linkTrack('closedCourseDescription');
			}
		},
		
		build_movies: function() {
			var movie_data = global.model.videos;
			
			global.view.embed_swf({
				swf: "stand_alone_player.swf",
				target: 'movie_player'
			},
			{
				title: movie_data[0].title,
				poster: movie_data[0].poster,
				flv: movie_data[0].flv,
				stageWidth: global.model.movie_width,
				stageHeight: global.model.movie_height
			});

			$('.thumbnail_list li').click(function(event) {
				if (!$(this).hasClass('inactive')) {
					var index = $('.thumbnail_list li').index(this);

					$('#movie_title').html(movie_data[index].title + ' | ' + '<span>' + movie_data[index].speaker + '</span>');
					global.view.embed_swf({
						swf: "stand_alone_player.swf",
						target: 'movie_player'
					},
					{
						title: movie_data[index].title,
						poster: movie_data[index].poster,
						flv: movie_data[index].flv,
						stageWidth: global.model.movie_width,
						stageHeight: global.model.movie_height
					});
				
					$('.thumbnail_list li.inactive').removeClass('inactive');
					$(this).addClass('inactive');
					
					global.controller.linkTrack('clickedMovie' + movie_data[index].title.replace(/ /g, ''));
				}
			});
		},

		// Tracking individual clicks
		// action, opt_label, opt_value are optional parameters to describe link
		linkTrack: function(action, opt_label, opt_value, event) {
			for (var i = 0, j = arguments.length; i < j; i++) {
				arguments[i] = arguments[i] || null;
			}
			
			try { global.model.tracker._trackEvent(window.location.pathname, action, opt_label, opt_value); }
			catch(e) {}
		},
		
		// Track page loads
		// opt_url is anything that should be appended AFTER the trailing '/' of the current location of the displayed page
		pageTrack: function(opt_url) {
			if (typeof(opt_url) == 'undefined') { opt_url = ''; }

			try { global.model.tracker._trackPageview(window.location.pathname + opt_url); }
			catch(e) {}
		},
		
		show_get_flash_msg: function(target) {
			$('#'+target).replaceWith(
				"<div class='flash_error_msg'>"+
					"This site requires Adobe Flash Player&reg;.<br />"+
					"<a href='http://get.adobe.com/flashplayer/' target='_blank'>Click here to install Flash Player&reg;</a>"+
				"</div>"
			);
		}
	}
};

var app = global; // Alias this object so that the movie player's assumption that the global option is named 'app' remains true.

$(window).ready(global.controller.init);
