// JQUERY INTERFACE RULES

$(window).load(function(){  
});

$(document).ready(function() {
	
	
	
	$("div.body_text_content p:first").addClass("first_paragraph");
	
	$("div.testimonial_container_large:first").css({padding: '20px 0px 0px 0px'});
	
	// TABBED INTERFACE

	$(".tabbed_div").hide();  // HIDE content divs based on class
	$("ul.tabbed_links li:first").addClass("active_tab").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_tab"); // REMOVE active class
		$(this).addClass("active_tab"); // 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;
	});


/*
	if(!$("body").hasClass("home")){
		
		$target = $('.body_text_content *');
		
		function span_wrap( str ){
		  return '<span class="word_analyzer">' + str + '<\/span>';
		};
		
		$target.replaceText( /\b(\S+?)\b/g, span_wrap );
		$target.replaceText( /(,|\.|\!|#|"|\:|;|-)/g, span_wrap );
		
		$target.find('.word_analyzer').each(function() {
			if(!$target.parent('div').hasClass("dealbroken")){
				offset = $(this).offset();
				$(this).addClass("body_text_highlight"); 
				if(offset.top > 360){
					$(this).removeClass("word_analyzer body_text_highlight").addClass("dealbreaker");
					$target.parent('div').addClass("dealbroken");
				}
			}
		});
*/		
		// IE FIX 		
/*		
		dealbreaker_offset = $(".dealbreaker").offset();
//		console.log(dealbreaker_offset.left);
		
		if(dealbreaker_offset.left <= 645 ){
			console.log("erra");
			console.log(dealbreaker_offset.left);			
			$(".dealbreaker").prev().addClass("new_dealbreaker");
			$(".new_dealbreaker").removeClass("body_text_highlight");
		}
*/
/*
	// REMOVE UNNDEED SPANS 
		
		$('.body_text_highlight').removeClass("word_analyzer");
		$('.word_analyzer').contents().unwrap();
	}
*/

	
	// DROPDOWN MENUS
	$("div.dropdown ul li:last-child").addClass('last');

	$('#navigation li a').hover(
		function() {
			$('#navigation li a').css({backgroundPosition: '0px 0px'});
			$(this).css({backgroundPosition: '0px -105px'});	
			$('.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'});			
	});
	
	//NESTED
	$('a#post_production_link').hover(
		function() {
			$('.nested_dd').stop(true, true).fadeOut(200);
			$('div#post_production_dd').stop().fadeIn(400);
			$('a').removeClass('active');
			$('a#post_production_link').addClass('active');
		}, function(){}
	);	
	
	$('a#live_captioning_link').hover(
		function() {
			$('.nested_dd').stop(true, true).fadeOut(200);
			$('div#live_captioning_dd').stop().fadeIn(400);
			$('a').removeClass('active');
			$('a#live_captioning_link').addClass('active');
		}, function(){}
	);	
	
	$("#navigation_container").hover(
	function (){
		$('.nested_dd').stop(true, true).fadeOut(200);
			$('a').removeClass('active');
		}, function(){}
	);
	
	$("a#services_overview_link, a#faq_link").hover(
	function (){
		$('.nested_dd').stop(true, true).fadeOut(200);
			$('a').removeClass('active');
		}, function(){}
	);
	
	$("div#body_text").hover(
	function (){
		$('.nested_dd').stop(true, true).fadeOut(200);
			$('a').removeClass('active');
		}, function(){}
	);
	
	
	// SEARCH FORM RULES
	
	$("form#site_search input").focus(
		function (){
			$(this).val('');	
		}
	);
	
	
	// GENERAL FORM RULES							 	
	
	if ($("#request_quote_form").hasClass("body_form")){
		$.getScript('/scripts/validate_rules.js');
		
		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);
		
		
		$('select[name="referred_by"]').change(function(){
			referred_by_val = $(this).val();
			if (referred_by_val == "Other"){
				$('.referred_by_other').show();
				$('.referred_by_referral').hide();
			}
			else if (referred_by_val == "Referral"){
				$('.referred_by_referral').show();
				$('.referred_by_other').hide();				
			}
			else {
				$('.referred_by_other').hide();				
				$('.referred_by_referral').hide();
			}
		});
		
		$('select[name="programming_type"]').change(function(){
			referred_by_val = $(this).val();
			if (referred_by_val == "Other"){
				$('.programming_type_other').show();
			}
			else {
				$('.programming_type_other').hide();				
			}
		});
		
		$("a.submit_button").live('click', function() {
			$(this).parents("form").submit();
			return false;
		});
	}
});
