var timer;
var ready = true;
var reset = false;
var running = true;
var locked = false;
var lockedby;

var currentImage = 0;
var currentImage2 = 0;
var totalImages;
var totalImages2;



function nextImage() {
	
	$("#slideshow a.next").click();
		
	timer = setTimeout("nextImage()", 6000);

}

function nextClient() {

	if($("#clientscoller").find("ul").css("marginTop")=="-30px") {
	
		$("#clientscoller ul li:first").appendTo("#clientscoller ul");
		$("#clientscoller ul li:first").appendTo("#clientscoller ul");
		$("#clientscoller").find("ul").css({marginTop : 0}, "fast");
	}

	$("#clientscoller").find("ul").animate({
		marginTop : -30
	}, 800);
	
	setTimeout("nextClient()", 1400);

}

function nextOfficeImage() {
	totalImages = $("#officeinfo img.office").length-1;
	
	if (currentImage!=totalImages) {
		$("#officeinfo img.office").eq(currentImage+1).fadeIn("slow");
		$("#officeinfo img.office").eq(currentImage).fadeOut("slow");
		currentImage += 1;
	}
	else {
		$("#officeinfo img.office").eq(0).fadeIn("fast");
		$("#officeinfo img.office").eq(totalImages).fadeOut("slow");
		currentImage = 0;
	}
	
	setTimeout("nextOfficeImage()", 4000);
}

function nextOfficeImage2() {
	totalImages2 = $("#officeinfo2 img.office").length-1;
	
	if (currentImage2!=totalImages2) {
		$("#officeinfo2 img.office").eq(currentImage2+1).fadeIn("slow");
		$("#officeinfo2 img.office").eq(currentImage2).fadeOut("slow");
		currentImage2 += 1;
	}
	else {
		$("#officeinfo2 img.office").eq(0).fadeIn("fast");
		$("#officeinfo2 img.office").eq(totalImages2).fadeOut("slow");
		currentImage2 = 0;
	}
	
	setTimeout("nextOfficeImage2()", 4000);
}

$(document).ready(function() {

	/* IE6 fix for dropdowns  */

	if (document.all) {
		 $("#globalnav li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
				
       $("#mainnav li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		
		$("#pagetools").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		
		$("div.dropdown").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
    }
	
	
		
		/* Projects module  */
	
		$("div.imgcontainer img.thumb").fadeTo("fast", 0.25);
		
		$("#grid_nav ul a").hover(function(){
			if (ready && !locked) {
				$("div."+$(this).attr("class")+" img.thumb").fadeTo(200, 1.0);
			}
		},function(){
			if (!locked)
				$("div.imgcontainer img.thumb").fadeTo(100, 0.25);
		});
		
		$("#grid_nav ul a").click(function(){
			if (ready) {
				if (locked) {
					

					$("div.imgcontainer img.thumb").fadeTo("fast", 0.25);
					
					$("#grid_nav ul li").removeClass("selected");
					
					
					if ($(this).attr("class") == lockedby) {
						locked = false;
					}
					else {
						$("div."+$(this).attr("class")+" img.thumb").fadeTo("fast", 1.0);
						lockedby = $(this).attr("class");
						var gridID = document.getElementById("Grid");
						gridID.addClass("selected");
					}
				}
				else {
					locked = true;
					lockedby = $(this).attr("class");
					var gridID = document.getElementById("Grid");
					gridID.addClass("selected");
				}
			}
		});
	
		
		$("div.project").hover(function(){
			if (ready) {
				if (!locked)
					$(this).find("img.thumb").fadeTo("fast", 1.0);
					
					$(this).find("p.tooltip").show();
				$(this).css({ zIndex: 10000 });
			}
		},function(){
			if (ready) {
				if (!locked)
					$(this).find("img.thumb").fadeTo("fast", 0.25);
					

				$(this).find("p.tooltip").hide();
				
				$(this).css({ zIndex: 1 });
			}
		});
		
		$("div.project").click(function() {
			if (ready) {
				ready = false;
				
				$(this).find("p.tooltip").hide();
				
				$("div.project").css({ position: 'static' });
				
				if($.browser.msie)
					$(this).find("div.popup").show();
				else
					$(this).find("div.popup").fadeIn("fast");
					//alert("click event");
					//reset = true;
			}
			
			if (reset) {
				ready = true;
				reset = false;
			}
			
		});
		
		
		$("div.imgcontainer p.close").click(function() {
			$(this).parent().parent().hide();
			$("div.project").css({ position: 'relative' });
			reset = true;
		});
		
		
	/* Projecs Overview */
	
	$("#projectinfo span.total").html($("#projectinfo img.project").length);
	
	$("#projectinfo p.controls").fadeIn("fast");
	
	$("#projectinfo img.project").hide();
	
	$("#projectinfo img.project").eq(0).show();
		
	$("#projectinfo a.next").click(function(){
		
		if ($("#projectinfo span.current").html() != $("#projectinfo span.total").html()) {
			$("#projectinfo img.project").eq($("#projectinfo span.current").html()).fadeIn("fast");
			$("#projectinfo img.project").eq($("#projectinfo span.current").html()-1).fadeOut("fast");
			$("#projectinfo span.current").html(parseInt($("#projectinfo span.current").html())+1);
		}
		else {
			$("#projectinfo span.current").html("1");
			$("#projectinfo img.project").eq(0).fadeIn("fast");
			$("#projectinfo img.project").eq($("#projectinfo img.project").length-1).fadeOut("fast");
		}
		
		return false;
	});
	
	$("#projectinfo a.previous").click(function(){
		
		if ($("#projectinfo span.current").html() != "1") {
			$("#projectinfo img.project").eq($("#projectinfo span.current").html()-2).fadeIn("fast");
			$("#projectinfo img.project").eq($("#projectinfo span.current").html()-1).fadeOut("fast");
			$("#projectinfo span.current").html(parseInt($("#projectinfo span.current").html())-1);
		}
		else {
			$("#projectinfo span.current").html($("#projectinfo img.project").length);
			$("#projectinfo img.project").eq($("#projectinfo img.project").length-1).fadeIn("fast");
			$("#projectinfo img.project").eq(0).fadeOut("fast");
		}
		
		return false;
	});
	

	


	
});