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);
		viewID = parseInt(id-1);
		
		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 });
		$("#project-photo a").attr({ href: "javascript:void(viewer.show("+viewID+"))" });
		$("#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);
		}
		viewNextID = parseInt(next-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-photo a").attr({ href: "javascript:void(viewer.show("+viewNextID+"))" });
		$("#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);
		}
		viewPrevID = parseInt(prev-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-photo a").attr({ href: "javascript:void(viewer.show("+viewPrevID+"))" });
		$("#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;
	});
});

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;
}


//Open Image
function popImage(imageURL){
	var pw = 200;
	var ph = 200;
	var pLeftPosition = (screen.width) ? (screen.width-pw)/2 : 100;
	var pTopPosition = (screen.height) ? (screen.height-ph)/2 : 100;
	var psettings = 'width='+pw+',height='+ph+',top='+pTopPosition+',left='+pLeftPosition+',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	ppage = 'popImage.htm?'+imageURL+'';
window.open(ppage,name,psettings);
}
//end open image
