var current_photo_num = 1;
var intervalID;
var autoPlay = true;
var okToTransition = true;
var quantityTransitioningIn = 0;
var quantityTransitioningOut = 0;
var current_quote_num = 1;

function nextPhoto() {
	// default to the first edition
	var next_num = 1;
	// get child count
	var children_count = $("#gallery").children().size(); 
	// if current_photo_num is less than to the number of children
	if (current_photo_num < children_count){
		next_num = current_photo_num+1;//next_num = $("#gallery_nav_button" + current_photo_num).next().attr("id").replace(/gallery_nav_button/,"");		
	}
	clickAction(next_num);
}

function previousPhoto() {
	// get child count
	var children_count = $("#gallery").children().size(); 
	// default to the first edition
	var previous_num = children_count;
	// if current_photo_num is less than to the number of children
	if (current_photo_num > 1){
		previous_num = current_photo_num-1;//$("#gallery_nav_button" + current_photo_num).prev().attr("id").replace(/gallery_nav_button/,"");		
	}
	clickAction(previous_num);
}

function clickAction(num) {
	$("#gallery_nav_button" + current_photo_num + " img").removeClass("current");
	$("#photo" + current_photo_num).fadeOut("slow");
	var previous_photo_num = current_photo_num;
	current_photo_num = num;
	$("#gallery_nav_button" + current_photo_num + " img").addClass("current");
	$("#photo" + current_photo_num).fadeIn("slow");
	// if homepage gallery...
	//var parent_class = $("#photo"+current_photo_num).parent().get(0).className;
	if ($("#gallery").hasClass("home")) { 
		var new_title = $("#photo"+current_photo_num).attr("title");
		var new_link = $("#photo"+current_photo_num).attr("alt");
		$("#frame_bottom h2").html('<a href="'+new_link+'">'+new_title+'</a>');
	} else if ($("#gallery").hasClass("project")) {
		//alert('current:' + previous_photo_num + ' previous:' + current_photo_num);
		$("#gallery_nav_button"+previous_photo_num).removeClass("active");
		$("#gallery_nav_button"+current_photo_num).addClass("active");
	}
	/*
	var parent_class = $("#photo"+current_photo_num).parent().get(0).className;
	if (parent_class=="home") {
		var new_title = $("#photo"+current_photo_num).attr("title");
		var new_link = $("#photo"+current_photo_num).attr("alt");
		$("#frame_bottom h2").html('<a href="'+new_link+'">'+new_title+'</a>');
	} else if (parent_class=="project") {
		//alert('current:' + previous_photo_num + ' previous:' + current_photo_num);
		$("#gallery_nav_button"+previous_photo_num).removeClass("active");
		$("#gallery_nav_button"+current_photo_num).addClass("active");
	}
	*/
}


$(document).ready(function(){
	// style hr's consistently
	$("hr").each(function(){
    	var hr_class = $(this).attr("class");
    	$(this).wrap('<div class="'+hr_class+'"></div>');
    });

	// Open external links in new window
   	$("a.external").click(function() {
		var link = $(this).attr("href");
		window.open(link); return false;
	});

	// Hide photos initially
	//$('.photo').hide();

	// Prepare gallery navigation functionality
	$("#controls a.gallery_nav").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		var num = $(this).attr("id").replace(/gallery_nav_button/,"");
		clickAction(num);
	});
	// Click on "Next Photo"
	$("#gallery_b_next").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		nextPhoto();
	});
	// Click on "Previous Photo"
	$("#gallery_b_previous").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		previousPhoto();
	});
	
	if ($("#frame").hasClass("gallery")) {
		clickAction(current_photo_num);
		var count_photos = 0;
		// check to see if there's only 1 photo
		$("#gallery .photo").each(function(){
			count_photos++;
		});
		if (count_photos < 2) {
			autoPlay = false;
		} 
	}

	//start transitions
	if (autoPlay == true) {
		slide_time = 5000;
		intervalID = window.setInterval('nextPhoto()', slide_time);	
	}
	
	// Animate Project Meta Data
	$("#info_button").click(function(){
		var transition_time = 500; // .5 seconds
		var block_position = $("#project_meta_data").css("right");
		if (block_position == "-330px") {
			$("#project_meta_data").animate({ 
				right:0
			}, transition_time );
		} else {
			$("#project_meta_data").animate({ 
				right:-330
			}, transition_time );
		}
    });
	// Hover Project Meta Data
	$("#project_meta_data").hover(function(){
		$(this).css("cursor","pointer");
	});
	// Animate Project Meta Data
	$("#project_meta_data").click(function(){
		var transition_time = 500; // .5 seconds
		var block_position = $("#project_meta_data").css("right");
		if (block_position == "-330px") {
			$("#project_meta_data").animate({ 
				right:0
			}, transition_time );
		} else {
			$("#project_meta_data").animate({ 
				right:-330
			}, transition_time );
		}
    });
    
	// Animate Featured Project TNs
	jQuery(function($){
		var $ul = $("#featured_tns ul");
		$ul.hover(
			function() {
				$("li", $ul).stop().fadeTo("fast", 0.3);
				$("li", $ul).hover(function(){
				$(this).stop().fadeTo("fast", 1.0);
			},
			function() {
			    $(this).stop().fadeTo("fast", 0.3);
				});
			},
			function() {
				$("li", $ul).stop().css("opacity", 1.0);
			}
		);
	});

	// Animate Featured Employee TNs
	/*
	jQuery(function($){
		var $ul = $("#employee_list");
		$ul.hover(
			function() {
				$("li", $ul).stop().fadeTo("fast", 0.3);
				$("li", $ul).hover(function(){
					$(this).stop().fadeTo("fast", 1.0);
				},
				function() {
				    $(this).stop().fadeTo("fast", 0.3);
				});
			},
			function() {
				$("li", $ul).stop().css("opacity", 1.0);
			}
		);
	});
	*/
	
	// Animate Featured Employees TNs
	
	
	$("#employee_list").hover(
		function() {}, 
		function() {        
			$('#employee_list>li').fadeTo(300, 1.0); 
			$('#employee_list>li span.name').fadeOut(100); 
		}
	);
	$("#employee_list>li").hover(
		function() {
	    	$("#employee_list li span.name").fadeOut(300);
	    	$(this).siblings().fadeTo(300, 0.2); // Fade other items to 20%
	    	$(this).fadeTo(100,1.0);
	    	var employee_id = $(this).attr("id").replace(/employee_li_/,"");
	    	$("#employee_name_"+employee_id).fadeIn(100);
	    	//$("#employee_overlay").fadeIn(500);
			//$(this).addClass("hovered");
			//$(this).fadeTo(1000, 1.0); // Fade current to 100%
		},
		function() {
	    	//$(this).removeClass("hovered");
			//$(this).siblings().fadeTo(1000, 1.0); // This should set the other's opacity back to 100% on mouseout   
		}
	);
	
	
	// Animate Staff Quotes
	$("#quoted_staff_tns li a").click(function(){
		// Get order number
		var order_id = parseInt($(this).attr("id").replace(/staff_tn_/,''));
		
		// move arrow
		var new_arrow_position = (78*order_id)-56;
		new_arrow_position = new_arrow_position+"px";
		$("#quote_arrow").animate({ 
			left: new_arrow_position
		}, 500 );
		
		// fade out current quote
		$("#staff_quote_"+current_quote_num).fadeOut(500);
		$("#staff_quote_"+order_id).fadeIn(500);
		current_quote_num = order_id;
		
	});
		
	// Suckerfish  Drop-down Menus
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );
    if (document.all) {
        $("#nav-one li").hoverClass ("sfHover");
    }

});

function synchronizeTransition(direction) {
	if (direction == "in") {
		// (in = reappearing)
		// catch transition if new hover occurs
		quantityTransitioningIn++;
		//$(this).fadeTo(500,1,transitionComplete);
	} else if (direction == "out") {
		// (out = going dark)
		quanityTransitioningOut++;
		//$(this).fadeTo(500,0.2,transitionComplete);
	}
}

function transitionComplete(direction) {
	$(this).removeClass("in-transition");
	if (direction == "in") {
		quantityTransitioningIn--;
		$("#transitioning_in").html(quantityTransitioningIn);
	} else if (direction == "out") {
		quantityTransitioningOut--;
		$("#transitioning_out").html(quantityTransitioningOut);
	}
	okToTransition=true;
}

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};    
