var canExecute = true;  // Global for ajax newsletter subscription

$(document).ready(function(){	
	/* add hover and links to our work page */
	if ($("#body_161")) { 
		$(".list_blocks li").hover(
			function() {
				$(this).addClass("list_blocks_hover");
				$(this).click(function () {
					if ($(this).children("div").hasClass("blog_title")) {
						document.location = $(this).children("div").children("h2").children("a").attr("href");
					} else {
						document.location = $(this).children("h2").children("a").attr("href");
					}
					
				});
			},
			function(){
				$(this).removeClass("list_blocks_hover");
			}
		)
		$("#recent_work li").hover(
			function() {
				$(this).children("h2").append(" <span class='find_out_more'>find out more</span>");
			},
			function () {
				$(this).children("h2").children("span").remove();
			}	  
		)
	}
	
	
	/* who we are side nav functionality */
	if ($(".who_we_are")) { 
		$("#side_nav_who div.expand").hide();
		
		$("#side_nav_who div.expand").each(function (i) {
			if ($(this).children("ul").children("li").hasClass("current")) {
				$(this).show().addClass("active").prev("h4").addClass("active");
			}
			
			$("#side_nav_who h4").click(function () {
				if (!$(this).hasClass("active")) {
					$("#side_nav_who div.expand:visible").hide("slow");
					$("div.expand:visible").queue(function () {
						$(this).removeClass("active").prev("h4").removeClass("active");
						$(this).dequeue();
				  	});
				}
				$(this).addClass("active").next("div").show("slow").addClass("active");
			});
			
			$(this).siblings("h4").hover(
				function () {
					if (!$(this).hasClass("active")){
						$(this).addClass("header_hover");
					}
				}, 
				function () {
					$(this).removeClass("header_hover");
				}
			)
		});
	}
	
	/* adds link to feet on homepage */
	if ($("#body_1")) { 
		$(".feets").hover(
			function() {
				$(this).addClass("feet_hover");
				
				$(this).click(function () {
					document.location = $(this).children("a").attr("href");
				});
			},
			function() {
				$(this).removeClass("feet_hover");
			}
		);
	}
	
	/* adds hover and link to who we are page */
	if ($("#body_223")) { 
		$(".department li").hover(
			function() {
				$(this).addClass("person_hover");
				
				$(this).click(function () {
					document.location = $(this).children("h3").children("a").attr("href");
				});
			},
			function() {
				$(this).removeClass("person_hover");
			}
		);
	}
	
	/* Ajax newsletter subscription signup */
    $('form#contact').submit(function() {       
        if(canExecute) {
            canExecute = false;
            $('.success').hide();
            $('.signup').hide();
            $('#resultMessage').hide();
            $('.processing').show();        
            var post = $(this).serialize();	    
            $.post("/newsletter-ajax/", post, function(xml) {
                var message = $("message", xml).text();  
                if($("status", xml).text() == 1) {
                    setTimeout(function() { runSignupSuccess(message, xml) }, 1000);               
                } else {              
                    setTimeout(function() { runSignupError(message, xml) }, 1000);                             
                }
            });
        }
        return false; 	        
    });	
});

// Helper functions for newsletter signup ajax
function runSignupSuccess(message, xml) {
    
    // show the success message
    $('.processing').hide();               
    $('.success').show();   
         
    $('#resultMessage').hide();
    
    setTimeout(function() { restoreSignupButton() }, 2000);  

    // add some custom tracking
    if($("live", xml).text() == "TRUE") {
        pageTracker._trackPageview("/about-us/newsletter-subscription-success");
    } 

}

function runSignupError(message, xml) {
    
    //Get the error code
    var errorCode = $("statusNumber", xml).text();
    var errorClass = "";
    
    // Set the class for the error message
    if(errorCode == 21) {
        errorClass = "email_error2";
    } else if(errorCode == 22) {
        errorClass = "email_error1";
    }
    
    // show the error message
    $('.processing').hide(); 
    $('.signup').show();   
    $('#resultMessage').removeClass().addClass(errorClass).html('<strong>' + message + '</strong>').show(); 
    canExecute = true;
}

function restoreSignupButton() {
    $('.success').fadeOut(1000, function() { 
        $('.signup').fadeIn(1000); 
        canExecute = true; 
    });
}
