$(document).ready(function() {
    var tabNavigator = $("#tabnav li");
    var topNavigator = $("ul#nav li a");
    var middleNav = $(".din");
    
    var sidenotes = Array({title:"ENTERPRISE AGILE AND LEAN TRANSFORMATION",subtitle:"for energy companies"},{title:"LEAN IT AND CLOUD SERVICES",subtitle:"for the largest telecommunications companies"},{title:"AGILE TRAINING AND TEAM COACHING",subtitle:"for Lonely Planet"},{title:"INNOVATING WITH AGILE AND KANBAN",subtitle:"for video game development"},{title:"ENTERPRISE SCRUM ADOPTION ",subtitle:"for financial services"},{title:"TRANSITIONING TO AGILE",subtitle:"for health services"},{title:"INNOVATING WITH FOCUSED DISCOVERY SESSIONS",subtitle:"for responsive user experience"});
    var questions = Array("How can agile help drive my<br /> company forward?","How can I prioritise <br />client needs?","How can agile make my life <br /> better?", "Agile teams go fast, are they <br />heading in the right direction?")
    
    $("#questionbubble").html(questions[Math.floor(Math.random()*questions.length)]);
    
    
    tabNavigator.click(function() {
        switchTab($(this));
    });
    middleNav.hover(function() {
        if ($(this).hasClass("activeFade")) {
            $(this).removeClass("activeFade");
        }
        $(this).addClass("onIt")
    },
    function() {
        $(this).removeClass("onIt")
    });
    if ($("#fade").is(":visible")) {
        $("#fade").headerFader({
            delay: 3000,
            navElem: ".din",
            random: true,
            sidenote: sidenotes
        });
    }
    topNavigator.dropdownMenu();
    
    //since the sidebar items are now minified the height of the sidebar need to
    //be adjusted to the content height
    if($("#sidebar").height()<$(".content").height()){
        $("#sidebar").css("min-height",$(".content").height()+50);
    }
    
    //Ok time to attach click event on li with team info
    $("#team li").click(function(){
       //slide up any opened descriptions
       var element = $(this);
       //check if the item you clicked is already open
       if(element.find(".team-description").is(":visible")){
        //do nothing
        element.find(".team-description").slideUp(500)
       }else{
            //if any description is visible
            if($(".team-description:visible")){
                $(".team-description:visible").slideUp(300);//hide it
                element.find(".team-description").delay(600).stop().slideDown(500);//wait and then show
            }else{
                element.find(".team-description").stop().slideDown(500); //just show the desc  
            }
       }
    })
    
    $("form").submit(function(e){
        e.preventDefault();
        //form is going to be sent by ajax
        var submit = $(this).find(".submit");
        
       if(formValid($(this))){
        submit.val("SENDING..."); 
        $.post($(this).attr("action"), $(this).serialize(),function(data){
            if(data == "false"){
               $("input[name=email]").css("border","1px solid #000");
               submit.val("SEND MESSAGE");
            }else{
                submit.val("MESSAGE SENT")
                setTimeout(function(){submit.val("SEND MESSAGE");},1000);
                $("form").children("input:not('submit'),textarea").val("");
            }
       });
        }else{
            $("input[name=email]").css("border","1px solid #000");
        }
    });
    
    $("input:not(':submit'),textarea").focus(function(){
        $(this).val("");    
    });
    
});
function switchTab(element) {
    if (!element.hasClass("active")) {
        var currID = element.attr("id");
        var prev = element.siblings();
        var prevID = prev.attr("id");
        $("#" + prevID + "-content").stop(false, true).fadeOut("fast",
        function() {
            $("#" + currID + "-content").show();
            prev.removeClass("active");
            element.addClass("active");
        });
    }
}

function formValid(form){
    
    var inputs = form.find("input:not(':submit'),textarea");
    inputs.css("border","1px solid #cfdede");
    var result = true;
    inputs.each(function(){
       if($(this).val() == ""){
        result = false;
        $(this).css("border","1px solid #000")
       }
    });
    //not empty
    if(result){
       inputs.each(function(){
       if($(this).attr("name") == "email"){
        result = checkEmail($(this).val());
       }
    });
    }
    return result;
}
function checkEmail(email){
    var regex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
    return regex.test(email);	
}
