// NORMAL RULES

$(document).ready(function() {

	// DROPDOWNS
	
	$('.content_fader_link').click( function() {
		$('.content_fader_link_body').stop(true, true).fadeIn(200);
		dd_name = $(this).attr("id");
		dd_name = dd_name.replace("_link","_dd");
		dd_selector = "div#"+dd_name;
		$(dd_selector).stop().slideDown(400);
	});	
	
	
	$('#navigation li a').hover(
	function() {
		$('#navigation li a').css({backgroundPosition: '0px 0px'});
		$(this).css({backgroundPosition: '0px -50px'});	
		$('.dropdown').stop(true, true).slideUp(200);
		dd_name = $(this).attr("id");
		dd_name = dd_name.replace("_link","_dd");
		dd_selector = "div#"+dd_name;
		$(dd_selector).stop().slideDown(400);
	}, function() {});	
	
	$("#navigation_container").hover(
		function (){},
		function (){
			$('.dropdown').stop(true, true).slideUp(200);
			$('#navigation li a').css({backgroundPosition: '0px 0px'});			
		}
	);




	// TABBED INTERFACE

	$(".tabbed_div").hide();  // HIDE content divs based on class
	$("ul.tabbed_links li:first").addClass("active").show(); // Set first link in list active
	$(".tabbed_div:first").show(); // SHOW first content div

	$("ul.tabbed_links li").click(function() {

		$("ul.tabbed_links li").removeClass("active"); // REMOVE active class
		$(this).addClass("active"); // SET active on current link
		$(".tabbed_div").hide(); // HIDE content divs

		var activeTab = $(this).find("a").attr("href"); // Set correlation between link and content div
		$(activeTab).fadeIn(); // SHOW appropriate content div
		return false;
	});


	
	// IMAGE CAPTIONS
	
	   //for each description div...  
     $('div.description').each(function(){  
         $(this).css('opacity', 0);  
         $(this).css('width', $(this).siblings('img').width());    
         $(this).parent().css('width', $(this).siblings('img').width());  
         $(this).css('display', 'block');  
     });  
   
     $('div.caption_wrapper').hover(function(){  
         $(this).children('.description').stop().fadeTo(500, 0.6);  
     },function(){  
         $(this).children('.description').stop().fadeTo(500, 0);  
     });  
	
	
	// SLIDING GALLERY
	
	slides_shown = 4; 
	slide_speed = 300;  

	$(".thumbnail_gallery_list").each(
		function (){					 
			this_gallery_amount = $(this).children("li").length;
			this_gallery_item_width = $(this).children("li").first().outerWidth();
			$(this).css("width",(this_gallery_amount*this_gallery_item_width));
			$(this).data({"gallery_amount":this_gallery_amount, "gallery_item_width":this_gallery_item_width, "gallery_click_count": 1});
			$(this).prev(".previous_thumb_button").css('display',"none").addClass("null_button");
		 	if(this_gallery_amount <= slides_shown){
			$(this).next(".next_thumb_button").css('display',"none").addClass("null_button");
			}
		}
	);
	
	$(".previous_thumb_button").click(function(){
			this_gallery_amount = $(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data("gallery_amount");
			this_gallery_item_width = $(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data("gallery_item_width");
			click_count = $(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data("gallery_click_count");
			if (click_count - 1 != 0){
				click_count = click_count - 1;
				slide_amount = "-="+(this_gallery_item_width * -1);
				$(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").animate({"left": slide_amount}, slide_speed);
			}
			$(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data({"gallery_click_count":click_count});
			if($(this).parents(".thumbnail_gallery_container").find(".next_thumb_button").hasClass("null_button")){
				$(this).parents(".thumbnail_gallery_container").find(".next_thumb_button").css('display',"block").removeClass("null_button");
			}
			if (click_count == (click_count - 1 == 0)){
				$(this).css('display',"none").addClass("null_button");
			}			
			return false;
	});
	
	$(".next_thumb_button").click(function(){
			this_gallery_amount = $(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data("gallery_amount");
			this_gallery_item_width = $(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data("gallery_item_width");
			click_count = $(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data("gallery_click_count");
			if (click_count != (this_gallery_amount - slides_shown + 1)){
				click_count = click_count + 1;
				slide_amount = "+="+(this_gallery_item_width * -1);
				$(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").animate({"left": slide_amount}, slide_speed);
			}
			$(this).parents(".thumbnail_gallery_container").find(".thumbnail_gallery_list").data({"gallery_click_count":click_count});
			if($(this).parents(".thumbnail_gallery_container").find(".previous_thumb_button").hasClass("null_button")){
				$(this).parents(".thumbnail_gallery_container").find(".previous_thumb_button").css('display',"block").removeClass("null_button");
			}
			if (click_count == (this_gallery_amount - slides_shown + 1)){
				$(this).css('display',"none").addClass("null_button");
			}
			return false;
	});	
		

	// RESUME SUBMITAL FORM RULES				
	
	$.getScript('/scripts/validate_rules.js');

	// GENERAL FORM RULES							 	
	if ($("#request_info_form").hasClass("body_form") || $("#resume_submittal_form").hasClass("body_form")){
		var count = 10;
		countdown = setInterval(function(){
		$("#timer").html("Please wait " + count + " seconds before you submit!");
		if (count == 0) {
			$('#timer').remove();
			$("#submit_container").html('<a href="#" class="submit_button "><span>Submit</span></a>');
			$(".submit_button").fadeIn('slow');
		}
		count--;
		}, 1000);
		$("a.submit_button").live('click', function() {
			$(this).parents("form").submit();
			return false;
		});
	}
});
