// get position of child node
function getPosition(main,spec) {
	var items = main.getElementsByTagName(spec.tagName);
	var found = 0;
	for (p = 0; p < items.length; p++) {
		if (items[p] == spec) {
			found = 1;
			break;
		}
	}
	if (found) {
		return p;
	}
	else {
		return -1;
	}
}

$(document).ready(function(){
	
	var currentLabel = $("a.inlineCallOut").html();

	$("a.inlineCallOut").toggle(
		function(){
			$(this).html("back &nbsp;&nbsp;&nbsp;");
			$("ul#inlineNav").fadeIn("slow");
			$("div.slideOutContent").show().animate({left: "-50px"}, 400).animate({left: "30px"}, 250).animate({left: "-10px"}, 250).animate({left: "0px"}, 250);
			return false;
		},
		function(){
			$(this).html(currentLabel);
			$("ul#inlineNav").hide();
			$("div.slideOutContent").animate({left: "-20px"}, 250).animate({left: "416px"}, 300);
			return false;
		}
	);
	
	// client logo click thru
	var currentLogo = 0;
	var logoCount = $("ul#clientLogos li").length - 1;
	
	$("a#nextLogoSet").click(function(){
		if(currentLogo == logoCount){
			$("ul#clientLogos li").hide();
			if (navigator.userAgent.indexOf("Mac")!=-1 && navigator.userAgent.indexOf("Safari")!= -1) {
				$("ul#clientLogos li.logoSet:eq(0)").show();
			} else {
				$("ul#clientLogos li.logoSet:eq(0)").fadeIn(650);
			}
			currentLogo = 0;
		} else {
			currentLogo++;
			$("ul#clientLogos li").hide();
			if (navigator.userAgent.indexOf("Mac")!=-1 && navigator.userAgent.indexOf("Safari")!= -1) {
				$("ul#clientLogos li.logoSet:eq(" + currentLogo + ")").show();
			} else {
				$("ul#clientLogos li.logoSet:eq(" + currentLogo + ")").fadeIn(650);
			}
		}
		return false;
	});
	
	$("a#previousLogoSet").click(function(){
		if(currentLogo == 0){
			$("ul#clientLogos li").hide();
			if (navigator.userAgent.indexOf("Mac")!=-1 && navigator.userAgent.indexOf("Safari")!= -1) {
				$("ul#clientLogos li.logoSet:eq(" + logoCount + ")").show();
			} else {
				$("ul#clientLogos li.logoSet:eq(" + logoCount + ")").fadeIn(650);
			}
			currentLogo = logoCount;
		} else {
			currentLogo--;
			$("ul#clientLogos li").hide();
			if (navigator.userAgent.indexOf("Mac")!=-1 && navigator.userAgent.indexOf("Safari")!= -1) {
				$("ul#clientLogos li.logoSet:eq(" + currentLogo + ")").show();
			} else {
				$("ul#clientLogos li.logoSet:eq(" + currentLogo + ")").fadeIn(650);
			}
		}
		return false;
	});
	
	// client testimonials
	$("ul#inlineNav li").click(function(){
	
		var inlineNavPosition = getPosition(this.parentNode,this) + 1;
		
		$("ul#inlineNav li a").removeClass("selected");
		$(this).find("a").addClass("selected");
		$("div.slideOutContent").find("div.testimonials").hide();
		$("div.slideOutContent").find("div#testimonial" + inlineNavPosition).fadeIn("slow");
		return false;
		
	});
	
	
	
	// news arvhices
	currentSub = 1;
	$("div#currentNews div.lightfinNews:last-child").addClass("lastNews");
	$("div.subCategory div.lightfinNews:last-child").addClass("lastNews");
	$("div.archives div.lightfinNews:last-child").addClass("lastNews");
	
	$("h1.newsHdr ul#inlineNav li").click(function(){
	
		var inlineNavPosition = getPosition(this.parentNode,this) + 1;
		
		$("ul#inlineNav li a").removeClass("selected");
		$(this).find("a").addClass("selected");
		$("div.slideOutContent").find("div.archives").hide();
		$("div.slideOutContent").find("div#archive" + inlineNavPosition).fadeIn("slow");
		currentSub = 1;
		$("div.subCategory").hide();
		$("div#sub" + archiveYear + currentSub).fadeIn("slow");
		$("div.archives a").removeClass("inactiveNews");
		$("div.archives a.previousNews").addClass("inactiveNews");
			
		return false;
	});
	
	$("div.archives a.previousNews").click(function(){
		
		if(currentSub == 1){
			$("div.archives a").removeClass("inactiveNews");
			$(this).addClass("inactiveNews");
			return false;
		}
		else if(currentSub == 2){
			$("div.archives a").removeClass("inactiveNews");
			$(this).addClass("inactiveNews");
			currentSub--;
			$("div.subCategory").hide();
			$("div#sub" + archiveYear + currentSub).fadeIn("slow");
			currentSub = 1;
			return false;
		}
		else {
			$("div.archives a").removeClass("inactiveNews");
			currentSub--;
			$("div.subCategory").hide();
			$("div#sub" + archiveYear + currentSub).fadeIn("slow");
			return false;
		}
	});
	
	$("div.archives a.nextNews").click(function(){
		
		var numberOfSubs = $(this).parent().find("div.subCategory").length;
		var secondToLastSub = numberOfSubs - 1;
		
		if( currentSub == (numberOfSubs - 1)){
			$("div.archives a").removeClass("inactiveNews");
			$(this).addClass("inactiveNews");
			currentSub++;
			$("div.subCategory").hide();
			$("div#sub" + archiveYear + numberOfSubs).fadeIn("slow");
			return false;
		}
		else if(currentSub < numberOfSubs){
			currentSub++;
			$("div.archives a").removeClass("inactiveNews");
			$("div.subCategory").hide();
			$("div#sub" + archiveYear + currentSub).fadeIn("slow");
			return false;
		}
		else if(currentSub == numberOfSubs){
			$("div.archives a").removeClass("inactiveNews");
			$(this).addClass("inactiveNews");
			return false;
		}
		
	});

	$("div.archives a.inactiveNews").click(function(){return false;});
	
	
	// principals
	$("a.showBio img").css({opacity: 0.5});
	$("a.showBio").click(function(){
		$("a.showBio img").animate({bottom: "-90px"}, 300);
		$("a.showBio img").animate({opacity: 0.5}, 200);
		$(this).find("img").animate({bottom: "0px"}, 350);
		$(this).find("img").animate({opacity: 1.0}, 200);
		$("div.principals").fadeOut("fast");
		return false;
	});
		
		$("a#dj").click(function(){ $("#principals2").fadeIn("slow"); return false; });
		$("a#sean").click(function(){ $("#principals3").fadeIn("slow"); return false; });
		$("a#sam").click(function(){ $("#principals4").fadeIn("slow"); return false; });
	
	
	// directions
	$("div#topBar a").click(function(){
		$("div#topBar a").removeClass("selected");
		$(this).addClass("selected");
		return false;
	});
	$("div#topBar a#south").click(function(){
		$("div#content").find("div.directions").hide();
		$("div#content").find("div#bucket0").fadeIn("slow");
		return false;
	});
	$("div#topBar a#north").click(function(){
		$("div#content").find("div.directions").hide();
		$("div#content").find("div#bucket1").fadeIn("slow");
		return false;
	});
	$("div#topBar a#map").click(function(){
		$("div#content").find("div.directions").hide();
		$("div#content").find("div#bucket2").fadeIn("slow");
		return false;
	});
	

// form
$("div.formRow input").focus(function(){ $(this).css({background: "#fefefe", border: "1px solid #a8b37f"}); });
$("div.formRow textarea").focus(function(){ $(this).css({background: "#fefefe", border: "1px solid #a8b37f"}); });
$("div.formRow input").blur(function(){ $(this).css({background: "#e8eedb", border: "1px solid #ccd1bf"	}); });
$("div.formRow textarea").blur(function(){ $(this).css({background: "#e8eedb", border: "1px solid #ccd1bf"	}); });

// form helpers

	$("div.formRow input").focus( function(){ $("div.formHelper").hide(); });
	$("div.formRow textarea").focus( function(){ $("div.formHelper").hide(); });
 
	$("div.withHelper input").focus(
		function(){
			$("div.formHelper").hide();
			$(this).prev("div.formHelper").fadeIn(500);
		}
	);
	
	$("div.formRow input").blur( function(){ $("div.formHelper").fadeOut(500); });
	
	// links with rel="external" will open in a new window
	$("a[rel=external]").click(function(){
		 window.open(this.href);
		 return false;
	});

});

function centeredPopUpWindow(mypage, myname, w, h, scroll){
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
