var img;
var title;
var id;
var total;


$(document).ready(function(){
	
	if ($("#project").length > 0) {
		var title = $("#thumb-1 img").attr("title");
		if (title.length == "0") {
			$("#photo-details").css({display: "none"});
		}
		else {
			$("#photo-details").fadeIn("slow");
		}
		$("#photo-1").fadeIn("slow");
	}
	
	$("#project-thumbs li img").click(function() {
					

		img = $(this).parent().attr("href"); //Get image selected
		title = $(this).attr("title"); //Get title of selected image
		id = $(this).parent().attr("id").split("-")[1];
		total = $("#project-thumbs li").length;
		
		id = parseInt(id);
		
		if (title.length == "0") {
			$("#photo-details").css({display: "none"});
		}
		else {
			$("#photo-details span").text("Photo " + id + " of " + total + ":");
			$("#photo-details p").text(title);
			$("#photo-details").css({display: "none"});
			$("#photo-details").fadeIn("slow");
		}
	
		$("#project-thumbs li img").removeClass("current");
		$("#thumb-" + id + " img").addClass("current");
		$("#project-photo img").attr({ src: img, id: "photo-" + id, title: title, alt: title });
		$("#photo-" + id).css({display: "none"});
		$("#photo-" + id).fadeIn("slow");
		
		return false;
		
	});
	
	$("#project-photo #photo-next a").click(function() {
		
		var id = $("#project-photo img").attr("id").split("-")[1];
		var total = $("#project-thumbs li").length;
		var next;
		
		id = parseInt(id);
		
		if ((id + 1) > total) {
			next = 1;
		}
		else {
			next = (id + 1);
		}
		
		var img = $("#thumb-" + next).attr("href");
		var title = $("#thumb-" + next + " img").attr("title");
		
		if (title.length == "0") {
			$("#photo-details").css({display: "none"});
		}
		else {
			$("#photo-details span").text("Photo " + next + " of " + total + ":");
			$("#photo-details p").text(title);
			$("#photo-details").css({display: "none"});
			$("#photo-details").fadeIn("slow");
		}
		
		$("#project-photo img").fadeIn("slow");
		$("#project-photo img").attr({ src: img, id: "photo-" + next });
		$("#project-thumbs li img").removeClass("current");
		$("#thumb-" + next + " img").addClass("current");
		$("#photo-" + next).css({display: "none"});
		$("#photo-" + next).fadeIn("slow");

		return false;
		
	});
	
	$("#project-photo #photo-prev a").click(function() {
		
		var id = $("#project-photo img").attr("id").split("-")[1];
		var total = $("#project-thumbs li").length;
		var prev;
		
		id = parseInt(id);
		
		if ((id - 1) == 0) {
			prev = total;
		}
		else {
			prev = (id - 1);
		}
		
		var img = $("#thumb-" + prev).attr("href");
		var title = $("#thumb-" + prev + " img").attr("title");
		
		if (title.length == "0") {
			$("#photo-details").css({display: "none"});
		}
		else {
			$("#photo-details span").text("Photo " + prev + " of " + total + ":");
			$("#photo-details p").text(title);
			$("#photo-details").css({display: "none"});
			$("#photo-details").fadeIn("slow");
		}
		
		$("#project-photo img").attr({ src: img, id: "photo-" + prev });
		$("#project-thumbs li img").removeClass("current");
		$("#thumb-" + prev + " img").addClass("current");
		$("#photo-" + prev).css({display: "none"});
		$("#photo-" + prev).fadeIn("slow");

		return false;
		
	});
	
	$("#searchbox").click(function() {
		$("#searchbox").val("");
		return false;
	});
	
	$("#searchbox-submit").click(function() {
		var url = $("#search-url").attr("value");
		return false;
	});
	
	$(".careers-search").click(function() {
	
		careers_window($(this).attr('href'),'800','600');
		
		return false;
	});
	
	/*
	$("#markets a").append("<div></div>");
	
    $("#markets a").hover(function() {
        var hoverText = $(this).children().attr("alt");
		$(this).find("div").css({display: "inline", bottom: "-55"});
	    $(this).find("div").text(hoverText);
	}, function() {
		$(this).find("div").css({display: "none", bottom: "-55"});
	});
	*/
	
	var subnav = $("#subnav h6").text();
	
	if (subnav == "Careers") {
	
	    //Add Careers Survey
	    $("#subnav").append("<div id='careers-feedback'><a href='/careers/careers-survey.aspx' target='_blank'>Give us your feedback about our recruiting!</a></div>");
	    
	    //Careers Downtime
	    //$("body").prepend('<div class="alert"><div class="container"><p><strong>Notice: </strong>On Saturday, October 24, 2009 from 6 PM to 10 PM CST, the Career Search will be offline for routine system maintenance.</p></div></div>');
	}
	
});

function careers_window(page,w,h) {
	var LeftPosition = (screen.width) ? (screen.width-w)/2 : 100;
	var TopPosition = (screen.height) ? (screen.height-h)/2 : 100;
	var settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	
	window.open(page,name,settings);
}

function siteSearch(form) {
	var q = $("#searchbox").attr("value");
	var url = $("#search-url").attr("value");
	if (q == "Search..." || q.length == "0" ) {
	    alert("Please enter search criteria.");
	    return false;
	}
    form.action = url + "?q=" + q;
    return true;
}