//
// COMMON JS FUNCTIONS FOR: PUBLICINC.COM
//
// 1. Global navigation rollovers
//    main nav, subnav
// 2. Feedback:
//    rollover, animate open/close
// 3. Contest:
//    mouseover, close banner, start contest
// 4. Image maps:
//    mouseover, mouseout
//


//
// GLOBAL NAVIGATION
//

// track active subnav
var navActive = "";

// mouseover events
jQuery(document).ready(function(){

	// mouse events: main nav
	$('#topnav').mouseleave(function() {
		navRestore();
	});

	// mouse events: subnav
	$('#wwa').mouseenter(function() {
		navOver($(this).attr('id'));
	}).mouseleave(function() {
		navOut($(this).attr('id'));
	});

	$('#wwd').mouseenter(function() {
		navOver($(this).attr('id'));
	}).mouseleave(function() {
		navOut($(this).attr('id'));
	});

	$('#wycd').mouseenter(function() {
		navOver($(this).attr('id'));
	}).mouseleave(function() {
		navOut($(this).attr('id'));
	});

	$('#contact').mouseenter(function() {
		navOver($(this).attr('id'));
	}).mouseleave(function() {
		navOut($(this).attr('id'));
	});

	// mouse events: utility functions
	function navOver(id) {
		navOut(navActive);
		$('#' + id + '-subnav').css("display", "block");
	}
	function navOut(id) {
		$('#' + id + '-subnav').css("display", "none");
	}
	function navRestore() {
		if (navActive != '') {
			$('#' + navActive + '-subnav').css("display", "block");
		}
	}

	// show active subnav
	navRestore();

});

// animation effects
jQuery(function(){
	jQuery("#topnav").children().hover(function () {
		jQuery(this).siblings().stop().fadeTo(400, 0.5);
	},
	function () {
		jQuery(this).siblings().stop().fadeTo(300, 1);
	});
})



//
// FEEDBACK
//

var submitted = false;

jQuery(document).ready(function(){

	// declare object
	var feed;
	var img;
	var formElems;

	// hide from old browsers (including IE6)
	if (typeof document.body.style.maxHeight != "undefined") {

		feed = $("#feedback"),
		    img  = feed.children("img"),
			formElems = feed.children("form, h3");
		feed.css("display", "block").data("showing", false);
		formElems.hide();
		$('#message').slideUp('slow');

		// click event
		img.click(function() {
			if (submitted == true) {
				// hide feedback
				$('#feedback').slideUp('slow');
			} else if (feed.data("showing") == true) {
				// collapse feedback
				feed.data("showing", false)
					.animate({
						marginLeft: "-320px",
						height: "116px",
						padding: "0"
					});
				$(this).attr("src", "/images/publicinc/feedback.png").css("top", "0");
			} else {
				// hide message
				$('#message').hide();
				// show feedback
				feed.data("showing", true)
					.animate({
						marginLeft: "0",
						height: "285px",
						padding: "15px"
					});
				formElems.fadeIn("normal");
				$(this).attr("src", "/images/publicinc/feedback-on.png").css("top", "0");
			}
		});

		// feedback button: rollover
		$("#feedback_button").mouseover(function() {
			$(this).attr("src", "/images/publicinc/feedback-on.png").css("top", "0");
		}).mouseout(function(){
			if (feed.data("showing") == true) {
				$(this).attr("src", "/images/publicinc/feedback-on.png").css("top", "0");
			} else {
				$(this).attr("src", "/images/publicinc/feedback.png").css("top", "0");
			}
		});

	}

});

function closeFeedback() {
	$('#feedback').slideUp('slow');
}


//
// CONTEST
//

// contest: mouseover
function uploadOver() {
	$("#home-top-logo").fadeOut(500);
	setTimeout( function() {
		$("#home-top-contest").fadeIn(500);
	}, 550);
}

// contest: close
function uploadClose() {
	$("#home-top-contest").fadeOut(500);
	setTimeout( function() {
		$("#home-top-logo").fadeIn(500);
	}, 550);
}

// contest: start
function uploadStart() {
	alert("start!");
}


//
// IMAGE MAPS: POPUPS
//

jQuery(document).ready(function(){

	// image maps: mouseover
	$('area').mouseover(function() {
		var id = $(this).attr('id');
		var idClass = $(this).attr('class');
		if (id != '') {
			id = id + '-popup';
			if (idClass.indexOf('right') >= 0) {
				$('#' + id).removeClass("hidden");
				$('#' + id).addClass("active-right");
			} else {
				$('#' + id).removeClass("hidden");
				$('#' + id).addClass("active");
			}
		}
	});

	// image maps: mouseout
	$('area').mouseout(function() {
		var id = $(this).attr('id');
		var idClass = $(this).attr('class');
		if (id != '') {
			id = id + '-popup';
			if (idClass.indexOf('right') >= 0) {
				$('#' + id).removeClass("active-right");
				$('#' + id).addClass("hidden");
			} else {
				$('#' + id).removeClass("active");
				$('#' + id).addClass("hidden");
			}
		}
	});

});



function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

