$('document').ready(function() {
	
	$('#main_nav').superfish({
		hoverClass:    'here', 
		autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: true
	});
	
	
	// $("ul.structure > li").addClass("dropdown");
	// // $("ul.structure ul").hide();
	// 
	// $('#main_nav li.dropdown').hover(function() {
	// 	$(this).parent('a:first-child').addClass('hovering');
	// 	$(this).children('ul').show();
	// }, function() {
	// 	$(this).children('a:first-child').removeClass('hovering');
	// 	$(this).children('ul').hide();			
	// });
	
	
  /**
   * Animate the home page top image with tabs.
   **/
   
   // $("a[rel='popup']").colorbox();
   
  $('#animated-image').cycle({
    pager: '#img-nav',
    pagerEvent: 'mouseover',
    activePagerClass: 'selected',
    pauseOnPagerHover: 1,
    pagerAnchorBuilder: function(idx, slide) {
        return '#img-nav li:eq(' + idx + ') a';
    }
  });

  $('#img-nav li a').click(function () {
    window.location = $(this).attr('href');
  });

  /**
   * JS to make elements level by row.
   **/
  $('.eql-wrapper').each(function() {
    $(this).children('.eql').each(function(){
      var currentTallest = 0;
      if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
      if ($.browser.msie && $.browser.version == 6.0) { $(this).siblings('.eql').css({'height': currentTallest}); }
      $(this).siblings('.eql').css({'min-height': currentTallest});
    });
  });

  /*
   * Newsletter AJAX sign up script.
   */
  $("form#newsletter-signup input#oyhijt-oyhijt").focus(function() {
   if (this.value == 'Enter your email') { 
     this.value = '';
   }
   $(this).css('color', '#333');
  });

  $("form#newsletter-signup input#oyhijt-oyhijt").blur(function() {
    if (this.value == '') {
      $(this).css('color', '#CCC');
      this.value = 'Enter your email';
    }
   });

  $("#btn-signup").click(function() {ajaxNewsletterSubmit()});

  $('#utilities-newsletter a.trigger').click(function() {
    $('#utilities-newsletter .newsletter-signup-box').slideToggle('slow');
  });

  /**
   * Search box script
   */
  $("#utilities-search form input").focus(function() {
   if (this.value == 'Enter your search term') {
     this.value = '';
   }
   $(this).css('color', '#333');
  });

  $("#utilities-search form input").blur(function() {
    if (this.value == '') {
      $(this).css('color', '#CCC');
      this.value = 'Enter your search term';
    }
   });

  $('#utilities-search a.trigger').click(function() {
    $('#utilities-search .top-search-box').slideToggle('slow');
  });


	
});

function checkEmail(emailFieldId)
{
  var emailVal = $("#" + emailFieldId).val();
  if (emailVal == 'Enter your email') {return false;}

  var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return pattern.test(emailVal);
}

function ajaxNewsletterSubmit() {

  // First, disable the form from submitting
  $('form#newsletter-signup').submit(function() {return false;});

  // Grab form action
  formAction = $("form#newsletter-signup").attr("action");

  // Hacking together id for email field
  // Replace the xxxxx below:
  // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
  emailId = "oyhijt";
  emailId = emailId.replace("/", "");
  emailId = emailId + "-" + emailId;

  // Validate email address with regex
  if (!checkEmail(emailId))
  {
    alert("Please enter a valid email address.");
    return;
  }

  // Serialize form values to be submitted with POST
  var str = $("form#newsletter-signup").serialize();

  // Add form action to end of serialized data
  final = str;

  // Submit the form via ajax
  $.ajax({
    url: "/addSubscriber.php",
    type: "POST",
    data: final,
    success: function(html){
      if (html == '0') {
        $("form#newsletter-signup").hide(); // If successfully submitted hides the form
        $("div.newsletter-signup-box").removeClass("error");
        $("#error").hide();  // Hides error div
        $("div.newsletter-signup-box").addClass("success");
        $("p.info").text("You have succesfully signed up to our newsletter we'll be in touch soon!");
        $("div.newsletter-signup-box").delay(5000).slideUp();
      } else {
        $("form#newsletter-signup").show();
        $("div.newsletter-signup-box").addClass("error");
        var message = "";
        if (html == '1') {
          message = "Oooops, that email address was invalid please check and try again....";
        } else if (html == '2') {
          message = "Oooops, there is a problem with our newsletter system, please try again later.";
        } else {
          message = "Oooops, there was an error please try again....";
        }
        $("#error p").text(message);

        $("#error").show();
      }
    }
  });
}
