/*
*******************************************************************************
*	Copyright © Neoco; www.neoco.com
*	$Id: general.js 2670 2011-02-15 10:53:44Z alex $
*	purpose:  site-wide JavaScript
*
*******************************************************************************
*/

$(function() {

	// twitter
	$("div#main div.tweet p").css("display","none");
	$("div#main div.tweet").tweet({
		join_text: "auto",
		username: "nicholsonintluk",
		count: 1,
		loading_text: "Loading tweets..."
	});

	// homepage slider
	$("#slider").nivoSlider({
		directionNavHide:false,
		controlNav:false,
		captionOpacity:1.0,
		effect:"fade",
		slices:1, // if not using slices then this needs to be 1
		animSpeed:1000,
		pauseOnHover:false,
        pauseTime:12000
	});

	// home page vacancy rotation
	if($("#homeVacancyRotate").size() > 0) {
		setInterval(function() {
			homeJobCurrent++;
			if(homeJobCurrent > homeJobCount)
				homeJobCurrent = 1;
			$("#homeVacancyRotate div").hide();
			$("#homeVacancyRot" + homeJobCurrent).fadeIn();
		}, 5000);
	}

	// search form clear
	var frmclearonfocus = $(".frmclearonfocus");
	frmclearonfocus.focus(function() {
		var el = $(this);
		if(el.val() == el.attr("defaultValue"))
			el.val("");
	});
	frmclearonfocus.blur(function() {
		var el = $(this);
		if(el.val().length < 1)
			el.val(el.attr("defaultValue"));
	});

	// popovers
	$(".team a.modal").click(function(e) {
		e.preventDefault();
		var permalink = $(this).attr("rel");
		if(permalink) {
			$("#basic-modal-content").load("/the-team/"+permalink+"/?nowrapper", function() {
				$("#basic-modal-content").modal({
					onShow: function(dlg) {
						$(dlg.container).css("height", "auto");
					}
				});
			});
		}
	});
	$(".job .apply a").click(function(e) {
		e.preventDefault();
		var permalink = $(this).attr("href");
		if(permalink) {
			$("#basic-modal-content").load(permalink+"?nowrapper", function() {
				$("#basic-modal-content").modal({
					onShow: function(dlg) {
						$(dlg.container).css("height", "auto");
					}
				});
			});
		}
	});

	// sitemap hover
	$("table td.link").css({
		"cursor":"pointer"
	});
	$("table td.link").hover(function() {
		$("a",this).css({
			"color":"#6E6E4C"
		});
	}, function() {
		$("a",this).css({
			"color":"#BABA0E"
		});
	});
	$("table td.link").click(function() {
		$url = $("a",this).attr("href");
		window.open($url, "_self");
		return false;
	});

	// force footer height
	function forceFooter(bodyHeight, footerHeight) {
		var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
		if(viewportHeight > bodyHeight){
			var footerSize = viewportHeight - bodyHeight;
			footerSize = footerSize + footerHeight;
			$("div#footer").css({"height":footerSize+"px"});
		}
	};
	bh = $("body").height();
	fh = $("div#footer").height();
	forceFooter(bh, fh);
	$(window).resize(function(){
        forceFooter(bh, fh);
    });


	// Search Functionality
	// -----------------------------------------

	// Level Two goes directly underneath level One
	// Levels Three & Four go to the right of the previous level
	$("div#searchbox ul.levelTwo").css({"position":"absolute", "top":"40px", "left":"0px"});
	$("div#searchbox ul.levelThree, div#searchbox ul.levelFour").css({"position":"absolute", "top":"0", "left":"100%"});

	// when hovering over an item, make the container relative
	// so the ul inside can be positioned absolutely
	$("div#searchbox ul li").hover(function() {
		$(this).css({"position":"relative"});
	}, function() {
		$(this).removeAttr("style");
	});

	// when hovering over Level Two, add a class of hover to the containing li
	// this can then be styled to keep the rollover state
	$("div#searchbox ul.levelTwo").hover(function() {
		$(this).closest("li").addClass("hover");
	}, function() {
		$(this).closest("li").removeAttr("class");
	});

	// when hovering over Levels Three & Four, add a class of hover to anchor tag
	// of the containing li, but remover from anchor tags of current Level.
	// This can then be styled to keep the rollover state
	$("div#searchbox ul.levelThree, div#searchbox ul.levelFour").hover(function() {
		var closest = $(this).closest("li");
		$("a", closest).addClass("hover");
		$("li a", closest).removeAttr("class");
	}, function() {
		var closest = $(this).closest("li");
		$("a", closest).removeAttr("class");
	});

	// when hovering over a level anchor link, if the link has no more levels to
	// go to then add a class so that the background image can be hidden
	$("div#searchbox ul.levelTwo li, div#searchbox ul.levelThree li, div#searchbox ul.levelFour li").hover(function() {
		var kids = $(this).children("ul").length;
		if(kids != 0) {
			$("a", this).addClass("kids");
		}
	}, function() {
		var kids = $(this).children("ul").length;
		if(kids != 0) {
			$("a", this).removeAttr("class");
		}
	});

	//
	$("div#searchbox li.selected a").hover(function() {
		$(this).closest("li").css({"background-position" : "0 -120px"});
	}, function() {
		$(this).closest("li").removeAttr("style");
	});

});

