// Open rel='external' links in new windows.
$(document).ready(function(){
	$("a[rel='external']").click(function(){
		window.open(this.href);
		return false;
	});
});


/* Mimic Flash nav rollover fade-in effect 
$(document).ready(function(){
	// Make nav items 50% opaque
	$("#nav li a").css("opacity","0.5");

	$("#nav li a").hover(
		// Fade in on rollover
		function(){
			$(this).animate({
				opacity: "1"
			}, 500);
		},
		// Fade back to 50% on rollout.
		function(){
			$(this).animate({
				opacity: "0.5"
			}, 10);
		}
	);
});*/



/* Fade HTML Elements in for when Flash has finished animating. */
$(document).ready(function(){
	$("#container").find("#logo, #nav, #content").hide();
});
function startAni() {
	setTimeout(function(){ $("#logo").show(); }, 1000);
	setTimeout(function(){ $("#nav").fadeIn("slow"); }, 2300);
}


$(document).ready(function(){

	// Click a navigation item.
	$("#nav a").click(function(){
		if ( $(this).hasClass("active") )
		{ return false;	}
		else {
			// Hide Content
			$("#content").hide();

			// Set Flash
			var myFlashMovie = document.getElementById("animation");
		
			// Load AJAX content from location in navigation into #content div.
			var ajaxurl = $(this).attr("href");
			$("#content").load(ajaxurl + " div#ajax", function(){ 
				makeThickbox();
				tb_init('a.thickbox, area.thickbox, input.thickbox');	
				makeCloseClickable();
			});

			// Hello and Contact have the same animation background, so do a check.
			var noanimation = false;
			if ($(this).parent().hasClass("contact") && $("#content").hasClass("s-hello") || $(this).parent().hasClass("hello") && $("#content").hasClass("s-contact"))
			{ noanimation = true; }

			// Set #content div to the correct class.
			var contentclass = $(this).parent().attr("class");
			$("#content").removeClass().addClass("s-" + contentclass);
			$("#nav a").removeClass();
			$(this).addClass("active");

			// Swap flash section
			var flashsection = $(this).attr("rel");
			myFlashMovie.checkSec(flashsection,0);

			// Show content
			if (noanimation == false) {
				setTimeout(function(){ $("#content").fadeIn("normal", formControls()); }, 2000);
			} else {
				$("#content").fadeIn("normal", formControls());
			}
		
			// Ignore the real click event
			return false;
		}
	});
});

$(document).ready(function(){
	makeCloseClickable();
	$("h1#logo a").click(function(){ $("#nav a").removeClass(); });
});
function makeCloseClickable(){
	$("p.close a, #logo a").click(function(){

		var myFlashMovie = document.getElementById("animation");
		setTimeout(function(){ 
			// Send flash home
			myFlashMovie.goHome();
			// Fade out content
			$("#content").fadeOut();
		}, 200);
		return false;
	});
}

function makeThickbox(){
	$(".casestudies a").addClass("thickbox");
}


function triggerNavClick(arg) {
	switch (arg) {
		case "0": 
			$("#nav li.hello a").click();
			break;
		case "1": 
			$("#nav li.incentives a").click();
			break;
		case "2": 
			$("#nav li.conferences a").click();
			break;
		case "3": 
			$("#nav li.events a").click();
			break;
		case "4": 
			$("#nav li.groups a").click();
			break;
	}
	
}

function formControls() {
	$('p.contact-success').hide();
	$("#cmdSubmit").click(function() {
		var name = $("#txtName");
		var email = $("#txtEmail");
		var phone = $("#txtPhone");
		var message = $("#txtComments");
		var incompleteFields = 0;
		if (name.val() == '') {
			name.addClass("errorInput");
			incompleteFields++;					
		} else {
			name.removeClass("errorInput");
		}
		if (email.val() == '') {
			email.addClass("errorInput");
			incompleteFields++;
		} else {
			email.removeClass("errorInput");
		}
		var feedback = $("ul.small");
		if (incompleteFields > 0) {
			feedback.html('<li><span style="font-size: normal;"><b>Please ensure all <span class="errorInput" style="color:black;">highlighted fields</span> are filled in.</b></span></li>');
			feedback.fadeOut();
			feedback.fadeIn();
			feedback.fadeOut();
			feedback.fadeIn();
		} else {
			feedback.html(''); // clear contents of ul
			$.ajax({
				 type: 'POST',
				 url: 'contact.aspx/SendEmail',
				 data: "{'email':'" + email.val() + "', 'name':'" + name.val() + "','phone':'" + phone.val() + "', 'message':'" + message.val() + "'}",
				 contentType: 'application/json; charset=utf-8',
				 dataType: 'json',
				 success: function(msg) {
					var response = msg;
					var html_array = msg.split('|');
					for (var msg = 0; msg < html_array.length; msg++) {
						if (html_array[msg] != '') {
							feedback.append("<li>" + html_array[msg] + "</li>");
							var affectedField = html_array[msg].replace(/.+<[Bb]>([A-Za-z ]+)<\/[Bb]>.+/, "$1").toLowerCase();
							if (html_array[msg] == 'Email sent successfully.') {
								$('div#pnlFormFields').hide();
								$('p.contact-success').show(100);
							}
						}
					}				
				 }, error: function(msg) {
					alert('Eep! Error: ' + msg.responseText);
					/*$.ajax({
					 type: 'POST',
					 url: 'index.aspx/LogAjaxError',
					 data: "{'errorMessage':'" + msg.responseText + "'}",
					 contentType: 'application/json; charset=utf-8',
					 dataType: 'json'		 
					});*/
				}
			  });				
		}				
	} );
}


/*$(document).ready(function(){
	$("#TB_closeWindowButton a").click(function(){
		setTimeout(function(){$("#content").show();},400);
	});
});*/