/* 
	eBay Careers - Global Functionality
	Author:  Michael "Spell" Spellacy, Lead UI Developer <michael.spellacy@tmp.com>, TMP Worldwide LLC
*/

/* ====== Default functionality ====== */

$("html").attr("class", "js"); // Make document JS active

window.scroll(0, 0);  // Scroll all documents to top of page

// Supress Script Errors

function errorsuppressor(){

	return true;

}

if(location.href.indexOf("localhost") == -1){

	window.onerror = errorsuppressor;

}

// Multiple Onload Utility

function addLoadEvent(func) {

	var oldonload = window.onload;

	if (typeof window.onload != 'function') {

		window.onload = func;

	} else {

		window.onload = function() {
		oldonload();
		func();

		}

	}

}

// Insert-after Function

function insertAfter(newElement, targetElement) {

	var parent = targetElement.parentNode;

	if (parent.lastChild == targetElement) {

		parent.appendChild(newElement);

	} else {

		parent.insertBefore(newElement, targetElement.nextSibling);

	}

}

//To handle Site Search text box Keyboard Enter 
function clickSiteSearch(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        if (evt.keyCode == 13) {
            bt.click();
            return false;
        }
    }
}

/* Get class and id from body tag, set default state name */

var currentSection = $("body").attr("class");
var currentPage = $("body").attr("id");
var stateName = "active";

$("#sec-" + currentSection + " > a").addClass(stateName); 
$("#dir-" + currentPage + " > a").addClass(stateName); 
$("#hdr-" + currentSection + " > a").addClass(stateName); 
$("#nav-" + currentPage + " > a").addClass(stateName);
$("#panel-" + currentSection + " ul.node").show(); // Show correct panel

// Hide input/textarea placeholder text on focus, return on blur 

function populateElement(selector, defvalue) {

	$(selector).each(function() {

		if($.trim(this.value) == "") {

			this.value = defvalue;

		}

	});

	$(selector).focus(function() {

		if(this.value == defvalue) {

			this.value = "";

		}

	});

	$(selector).blur(function() {

		if($.trim(this.value) == "") {

			this.value = defvalue;

		}

	});

}

var searchLabel = $("#dir-search input.text").val(); // Global Search Label (Header)
var searchQuestion = $("#dir-search input.text"); // Global Search Input (Header)

searchQuestion.val(searchLabel);
populateElement(searchQuestion, searchLabel);

// Open links in new window 

$("a.external").click(function(){

	this.target = "_blank";

});

/* ====== The Directory ====== */

$("#dir-directory > a").toggle(function() {

		$(this).addClass(stateName);
		$(".primary").show().animate({opacity: 1}, "medium");
		closeNavigation(); // Close Home Navigation if open
		 return false;

}, function() {

		 $(this).removeClass(stateName);
		 $(".primary").animate({opacity: 0}, "medium").hide();
		 return false;

});

// Tabs

 $("#student-content, #project-content").tabs(); // Tabs

if($("#nav-home").length) {

	// Close Panel Function

	closePanel = function(id) {

		$(id).removeClass("active"); // Remove "active" class from container (.nav > li)
		$(id + " .close").removeClass("active"); // Remove "active" class from close button
		$(id + " .node").animate({opacity: "0"}, 100); //  Slowly fade out elements within panel
		$(id + " .panel").animate({right: "-503px"}, 600); // Slide in panel
		$(id + " .panel").css("z-index", "1"); // Reset z-index to 1
		$(id + " h2 a").animate({opacity: "1"}, 600); // Fade in h2 link which slow hides background image of h2

	};

	closeNavigation = function() {

		$(".nav > li").each(function() { // Check to see if other panels are open, close if they are

			var node = "#" + $(this).attr("id");

			if ($(this).hasClass ("active")) {

				closePanel(node); // Close (Open) Panels

			} 

		});

	}

	// Prep the panels

	$(".nav > li").each(function() {

		var listHeight = (($(this).height() + 10) * $(this).index()) + "px";

		$(this).css("top", listHeight); // Since containers (.nav > li) are absolutely positioned, we need to set the position between each via script

		$(".nav").css("height", ($(this).height() + 10) * ($(this).index() + 1) + "px"); // Apply height so we don't have to add it to CSS

		var nodeName = "#" + $(this).attr("id"); // Get List ID

		$(nodeName + " .panel").append("<a class=\"close\" href=\"#\" title=\"Close this tab\">Close</a>"); // Append Close Button to Nav
		$(nodeName + " .panel").prepend("<h3 class=\"node\"><a href=" + $(nodeName + " h2 a").attr("href") + ">" + $(nodeName + " h2").text() + "</a> <span>" + $(nodeName + " p.node").text() + "</span></h3>"); // Create h3 tag and append appropriate text
		$(nodeName + " p.node").remove(); // Remove p tag in .panel (not needed)

	});

	// Open Panel

	$(".nav h2 a").click(function() { 
	
		$(".article").css("z-index", "1" );

		var nodeName = "#" + $(this).parent().parent().attr("id"); // Parent id of select list item

		closeNavigation();

		$(nodeName + " .close").addClass("active"); // Add "active" class to close button

		$(this).animate({opacity: "0"}, 600); // Fade out h2 link (which slowly reveals background image of h2)

		$(nodeName + " .panel").animate({right: "0"}, 600, function() { // Slide out panel

				$(this).css("z-index", "3"); // Place panel in front of h2 so that links are clickable

				$(nodeName + " .node").animate({opacity: "1"}, 600); // Slowly fade in elements within panel

		});

		$(this).parent().parent().addClass("active"); // Set calss of "active" to selected parent id

		return false;

	});

	// Close Panel

	$(".nav .close").click(function() { 

		var panelID = "#" + $(this).parent().parent().attr("id"); // Get List ID

		closePanel(panelID); // Close Panel

		return false;

	});

}

// Begin: Nav hack just so we can hover (Thanks for the extra garbage, Creative)

function removeStuff() {

	$("#change-preferences").hide();
	$("#un-preferences > a").removeClass("nav-active"); // Remove class to retain hover state

}
	
function showStuff() {

	$("#change-preferences").show();
	$("#un-preferences > a").addClass("nav-active"); // Remove class to retain hover state

}

$("#un-preferences").click(function(event) { // Stop Event Propagation

	event.stopPropagation();

});

$("#un-preferences > a").click(function() { // Disable Click

	return false;

});

$("#un-preferences").mouseover(function() {

	showStuff();

});

$("#un-employees, #un-faqs, #un-contact, #un-facebook, #un-twitter, #linkedin, #un-youtube").mouseover(function() {

	removeStuff();

});

$("body").click(function() {

	removeStuff();

});

// End: Nav Hack
