$(document).ready(function () {
	$("header .headerWrapper .menus .mainNav ul li").not(".subNavLinks li").bind("mouseenter", openNav).bind("mouseleave", closeNav);
	//$("header .headerWrapper .menus .mainNav ul").not(".subNavLinks").bind("mouseleave", closeNav);
	
	$("header .headerWrapper .socialWrapper .siteSocial .taf, header .headerWrapper .socialWrapper .siteSocial .share").bind("click", showHidePopout);
	
	highlightHeaderSection();
	
	checkMainNavLinks();
});

function checkMainNavLinks(){
	$("header .headerWrapper .subMenu .col2 ul").each(function(index){
		var globalItem = $(this);
		var globalPosition = globalItem.position().top;
		
		if (globalPosition + globalItem.height() > 330){
			$(this).find("li").each(function(){
				var curItem = $(this);
				var indivPosition = curItem.position().top;
				
				if(indivPosition + curItem.height() > 330){
					if(globalItem.parent().find("ul").length == 1){
						globalItem.parent().append("<ul class=\"subNavLinks\"></ul>");
					}
					
					globalItem.parent().find("ul:last").append(curItem);
				}
			});
		}
	});
}

function openNav(){
	if($(this).children(".subMenu").children(".col2").children(".subNavLinks").children("li").size() > 0){
		/*
		if($(this).children(".subMenu:animated").size() == 0){
			$(this).css("backgroundColor", "#ffffff");
			$(this).children(".subMenu").not(":animated").css("display", "block");
			$("header .headerWrapper .menus .mainNav ul li").children(".subMenu").removeClass("open");
			$(this).children(".subMenu").not(":animated").addClass("open");
			$(this).children(".subMenu").not(":animated").stop().animate({opacity:"1"}, 200, function(){$("header .headerWrapper .menus .mainNav ul li").children(".subMenu").not(".open").css("display", "none");});
		}
		*/

		$(this).css("backgroundColor", "#ffffff");
		$(this).children(".subMenu").css("opacity", "1").css("display", "block");
	}else{
		$(this).css("backgroundColor", "#ffffff");
		closeNav();
	}
}

function closeNav(){
	//$("header .headerWrapper .menus .mainNav ul li").children(".subMenu").stop().animate({opacity:"0"}, 200, function(){$("header .headerWrapper .menus .mainNav ul li").children(".subMenu").css("display", "none");});
	$(this).css("backgroundColor", "transparent");
	$("header .headerWrapper .menus .mainNav ul li").children(".subMenu").css("display", "none");
}

function showHidePopout(e){
	e.preventDefault();
	
	var socialBtn = false;
	if($(this).hasClass('taf')){
		$("#socialPopout").css("left", "545px");
		$("#socialPopout .tafPopout").css("display", "block");
		$("#socialPopout .sharePopout").css("display", "none");
		socialBtn = true;
	}else if($(this).hasClass('share')){
		$("#socialPopout").css("left", "455px");
		$("#socialPopout .tafPopout").css("display", "none");
		$("#socialPopout .sharePopout").css("display", "block");
		socialBtn = true;
	}

	if(!socialBtn){
		$("body").unbind();
		$("#socialPopout").stop().animate({opacity:"0"}, 250, function(){$("#socialPopout").css("display", "none");});
	}else{
		$("#socialPopout").css("display", "block");
		$("#socialPopout").stop().animate({opacity:"1"}, 250, function(){$("body").bind("click", function(e){if($(e.target).parent().parent().attr("id") != "socialPopout" && $(e.target).parent().parent().parent().attr("id") != "socialPopout" && $(e.target).parent().parent().parent().attr("class") != "taf" && $(e.target).parent().parent().parent().attr("class") != "share"){showHidePopout(e);}});});
	}
}

function clearInputText(input) {
	if(input.value == $(input).attr("alt")){
		$(input).attr("alt", input.value);
		input.value = "";
	}
}

function returnInputText(input) {
	if(input.value == ""){
		input.value = $(input).attr("alt");
	}
}

function shareLinksSubmit(site) {
    var link = "";
    switch (site) {
        case "reddit":
            link = "http://www.reddit.com/r/all/submit?url=" + escape(window.location) + "&title=" + escape(document.title);
            break;
        case "linkedin":
            link = "http://www.linkedin.com/shareArticle?mini=true&url=" + escape(window.location) + "&title=" + escape(document.title);
            break;
        case "delicious":
            link = "http://del.icio.us/post?url=" + escape(window.location) + "&title=" + escape(document.title);
            break;
        case "facebook":
            link = "http://www.facebook.com/sharer.php?u=" + escape(window.location);
            break;
        case "stumbleupon":
            link = "http://www.stumbleupon.com/submit?url=" + escape(window.location) + "&title=" + escape(document.title);
            break;
    }

    openFullWindow(link);
}

function tellAFriendSubmit() {
    var yourEmail = document.getElementById('yourEmail').value;
    var friendsEmail = document.getElementById('friendEmail').value;
    var location = window.location;

    var message = "";

    if (!isValidEmail(yourEmail)) {
        message = "Invalid Sender Email";
    }

    if (!isValidEmail(friendsEmail)) {
        message = "Invalid Friend Email";
    }

    if (message == "") {
        try {
            $.post("http://application.newairplane.com/Data.aspx", { "strFunction": "TellAFriend", "strYourEmail": escape(yourEmail), "strFriendsEmail": escape(friendsEmail), "strEmailType": "default", "strLocation": escape(location), "strApplicationURL": escape(applicationURL) });
			message = "Send Complete!";
        } catch (err) {
            message = "Submit error.";
            alert(err);
        }
    }

    $("#socialPopout .tafPopout .error").html(message);
    $("#socialPopout .tafPopout .error").css("display","block");
}


function findHeaderSection() {
    var baseUrl = window.location.href;
    var tempURL = baseUrl.split(applicationURL)[1];
    var finalURL = tempURL.split("/")[0];
    return finalURL;
}

function highlightHeaderSection() {
    var finalURL = findHeaderSection();
	var splitURL = finalURL.split("/");
	
	if(splitURL.length > 1){
		finalURL = splitURL[0] + "/";
	}

	if(finalURL != "" && finalURL != null && splitURL.length > 0){
		$('.navLink').each(function (ind, obj) {
			if ($(this).attr("href").search(finalURL) != -1) {
				$(this).addClass("on");
				Cufon.replace(".navLink", { fontFamily: 'Helvetica', hover: true });
			}
		});
	}

    //$('.otherLink').each(function (ind, obj) {
    //    var tempOtherArray = new Array(obj.href, obj.id);
    //    otherArray[ind] = tempOtherArray;
    //});

	/*for (var j = 0; j < otherArray.length; j++) {
		if (otherArray[j][0].search(finalURL) != -1) {
			var link = document.getElementById(otherArray[j][1]);
			$(link).parent().get(0).className = "headerOtherLinkOn";
			link.className = "otherLinkOn";
			Cufon.replace("#" + link.id, { fontFamily: 'Helvetica', hover: true });
		}
	}*/
}

