/**
	* @depends jquery.colorbox.js
	* @depends jquery.preload.js
	* @depends jquery.validate.js
	* @depends states.js
**/

var clickSound, airhornSound, videoPlaying; // I know, I know, globals

function closeVideoPlayer () {
	$("#flash").empty();
	$.fn.colorbox.close();
	videoPlaying = false;
}

function centerLayout () {
	var window_width = $(window).width();
	if (window_width > 1000) {
		var wrapper_width = 787; //$("#wrapper").width();		
		var left_pos = (window_width - wrapper_width)/2 - 70;
		$("#wrapper").css("left", left_pos);	
	}
	else {
		$("#wrapper").css("left", 0);	
	}
}

function pageload(hash) {
	if (videoPlaying === true) {
		closeVideoPlayer();
	}
	else {
		if(hash) {
			$("#content").load(hash + ".php", function(){				
				$("#content div").fadeIn(500);
				eventHandlers();
			});
		} else {
			$("#content div").fadeOut(500);
		}
	};
	window.scrollTo(0,0);
}

// Highlight active tab
function activeTab (hash) {
	$("ul#nav li").each(function(){
		$(this).removeClass("active"); 	// Remove "active" class from current nav item
	});
	$("ul#nav li#" + hash).addClass("active"); 	// Add "active" class to current tab
}

function eventHandlers(){	
	$("#flash").hide();	
	$("div.faq_item").bind('click', function(e){
		
		clickSound.play();
		var child = $(this).children();
		$(this).toggleClass("open");
		if ($(this).hasClass("open")) {
			$(child).each(function(){
				if (!$(this).hasClass("faq_header")) {
					$(this).slideDown("fast");
				};				
			});
			return;		
		}
		else {
			$(child).each(function(){
				if (!$(this).hasClass("faq_header")) {
					$(this).slideUp("fast");
				};
			});	
			return;
		}
	});

	$("#videos").delegate("a", "click", function(){
		// Lightbox behavior for opening videos
		videoPlaying = true;
		$(this).colorbox({
			innerWidth: 505,
			innerHeight: 380,
			inline: true,
			href: "#flash",
		 	html: function(){
				var video = "../videos/" + $(this).attr("id") + ".flv";
				var flashVars = {flvPath:video};
				var flashParams = {};
				var attributes = {};
				if(swfobject.hasFlashPlayerVersion("9.0.45")){
					swfobject.embedSWF("swfs/FSPVideoPlayer.swf", "flash", "500", "375", "9.0.45", "false", flashVars);
				}
			},
			onClosed: function(){
				$("#flash").hide();
				videoPlaying = false;
			}
		});	
	});
	
	var select = $("select#state"); // stash this element in a var to avoid unnecessary DOM traversal
	$(states).each(function(){ // states is held in states.js
		var state = this.toString();
		var option = "<option label=' "  + state + "'>" + state + "</option>";
		select.append(option);			
	});
	
	$("#privacy_link").click(function(){
			$.history.load("privacy");
			// alert(this)
			return false;
	})
	
	// Contact form validation
	$("form#contact_form").validate({
		rules: {
			mso_name: {
				required: true
			},
			first_name: {
					required: true
				},
				last_name: {
					required: true
				},
				email_address: {
					required: true,
					email: true
				},
				street_address: {
					required: true
				},
				city: {
					required: true
				},
				state: {
					required: true
				},
				zip: {
					required: true
				}, 
				privacy_policy: {
					required: true
				}
			}
		});
	// End of validation rules
};


$(function(){	
	$.history.init(pageload);
	centerLayout();
	$.preloadCssImages(); // self-explanatory
	soundManager.url = 'swfs/';
	soundManager.onload = function() {
		clickSound = soundManager.createSound({
			id: 'clicksound',
			url: 'audio/click.mp3'
		});
		airhornSound = soundManager.createSound({
			id: 'airhornsound',
			url: 'audio/airhorn.mp3'
		});		
	};
	$(window).bind("resize", function(){
		centerLayout();
	});

	// Navigation Links
	$("ul#nav li").bind('click', function(e){ 
		if ($(this).attr("id") === "nav-home") {
			airhornSound.play();
		}
		else {
			clickSound.play();			
		};
		var hash = this.id;
		
		hash = hash.replace(/^.*nav-/, '');
		
		$.history.load(hash);
		activeTab(hash);
		return false;
	});

	$("div#fsp_logo").bind('click', function(e){
		airhornSound.play();
		setTimeout("window.location.href = 'http://msn.foxsports.com/foxsoccer/plus'", 700);
	});

	$.history.load("home"); // Load home content on initial load
	setTimeout("eventHandlers()", 100);	// Add event handlers
});
