function showDiv (e) {
	var id = this.href.match(/(#[\w-]+)$/).pop();
	
	$(id).slideToggle(600, function(){
		$(id).css("overflow", "hidden");
	});
	
	e.preventDefault();
}

function initScroller() {
  if ($(".scroll").length) {
    setWidth("div.train", "div.train>div");
  
    $(".scroll").localScroll({
      hash: true,
      axis: "x",
      duration: 600,
      easing: "easeOutQuad",
      target: "#scrollpane",
      event: "click",
      queue: true,
      onAfter: scrollAfter
    });
  
    if (window.location.href.indexOf("#") > -1) {
      $.localScroll.hash({
        hash: true,
        axis: "x",
        duration: 0,
        target: "#scrollpane",
        onBefore: function(e, target, el) {
          // $(el).height($(target).height());
          setInitialHeight(e);
        },
        onAfter: scrollAfter
      });
    } else {
      var pane = $("#scrollpane");
      setInitialHeight(pane);
      // pane.height(pane.children("div:first").children("div:first").height());
      // setHere(pane.children("div:first").children("div:first").attr("id"));
    }
  }
  
  if ($(".wizard").length) {
    setWidth("div.train", "div.train>div");

    $(".wizard").localScroll({
      hash: false,
      axis: "x",
      duration: 200,
      easing: "easeOutQuad",
      target: "#scrollpane",
      event: "click",
      queue: true,
      onAfter: scrollAfter
    });
    
    var pane = $("#scrollpane");
    setInitialHeight(pane);
  }
}

function scrollAfter(target) {
  // setHeight(target);
  setHere(target.id);
}

function setHere(id) {
  var regex = new RegExp("#"+id);
  
  $(".scroll a").removeClass("here").each(function() {
    if (this.href.match(regex)) {
      $(this).addClass("here");
    }
  });
  
}

function setHeight(target) {
  // $(this).height($(target).height());
  $("#scrollpane").animate({height: $(target).height()}, 200, "easeInQuad");
}

function setInitialHeight(scrollpane) {
  var h=0;
  
  $(scrollpane).children("div:first").children("div").each(function() {
    if ($(this).height() > h) h = $(this).height();
  });
  
  $(scrollpane).height(h);
}

function setWidth (target, els) {
  var w = 0;
  $(els).each(function() {
    w += $(this).outerWidth();
  });

  $(target).width(w);
}

function showMenu(e) {
	e.preventDefault();
	$(this).children("ul").slideDown(400, "easeInQuad");
}

function hideMenu(e) {
	e.preventDefault();
	$(this).children("ul").slideUp(200, "easeOutQuad");
	
  // $(this).animate(
  //  { width: "22px", height: "22px" },
  //  { queue: false, duration: 200,
  //  easing: "easeOutQuart" }
  // );
}

function initMailer() {
  $("#mailer-input").bind("focus", function() {
    if ($(this).val() == "input your email") {
      // clear input
      $(this).val("");
    }
  });
  
  $("#mailer-submit").bind("click", submitMailer);
}

function submitMailer(e, ui) {
  e.preventDefault();
  
  $("#message").fadeOut(200, "easeInQuad").remove();
  
  if (validateEmail($("#mailer-input").val())) {
    var data = "data="+encodeURIComponent("0192934");
    data += "&email="+encodeURIComponent($("#mailer-input").val());
    var url = "/mailinglist.php";

    // loading message
    var loading = '<p class="loading" id="message" style="display:none">Sending details to server.</p>';
    
    $("#mailer")
      .children().fadeOut(200, "easeInQuad")
      .parent("div:first")
      .append(loading)
      .fadeIn(200, "easeOutQuad");
      
    $.ajax({
      url:url,
      data:data,
      type:"POST",
      dataType:"json",
      success:mailerSuccess,
      error:mailerError
    });
    
  } else {
    var error = '<p id="message" class="error" style="display:none">The address you entered is invalid, please try again.</p>';
    $("#mailer").prepend(error);
    $("#message").slideDown(400, "easeOutQuad");
  }
}

function mailerSuccess(data, status) {
  var message, type;

  if (data.success == 0) {
    type = "error";
        
    switch (data.message) {
      case "exist":
        type = "success";
        message = "Thanks, your address is already in the database and we will contact you with news about ClearBubble in the near future.";
        break;
      case "invalid":
        message = "The address you entered is invalid, please try again.";
        $("#mailer").children().slideDown(200, "easeOutQuad");
        break;
      default:
        message = "Oops! We’re sorry that didn’t work. Would you like to try again?";
        $("#mailer").children().slideDown(200, "easeOutQuad");
    }
  } else if (data.success == 1) {
    message = "Thanks for giving us your details, we'll let you know what we're up to in the future";
    type = "success";
  }

  $("#message")
    .removeClass("loading")
    .fadeOut(200, "easeInQuad")
    .addClass(type)
    .html(message)
    .fadeIn(400, "easeOutQuad");
}

function mailerError(xml, status, error) {
  // body...
  $("#message")
    .removeClass("loading")
    .fadeOut(200, "easeInQuad")
    .addClass("error")
    .html("Oops! We’re sorry that didn’t work. Would you like to try again?")
    .slideDown(400, "easeOutQuad");
    
  $("#mailer").children().slideDown(200, "easeOutQuad");
}

function validateEmail(val) {
  return (val.match(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/) != null);
}


$(document).ready(function() {
  initScroller();
  initMailer();
  $("#upper-nav>li").hover(showMenu,hideMenu);
  // Show / hide divs
	$("a.show").bind("click", showDiv);
});
