var $hostname = "http://"+location.hostname+"/";
var active_item = '';
var sel_item = '';

$(document).ready( function() {

  /* listener for clicking the newsletter signup button */
  $('#send').click(function(){
                sendValue($(this).val());  
                return false;
               
            }); 

  //get the id of the selected nav item and make the active sub menu stay
  $("#navigation .main li").each(function() {
    if (this.className == 'page_selected') {
        active_item = this.id;
        sel_item = 'sub_' + this.id;
        document.getElementById(sel_item).style.display = "block";
    }
  });

  /* main navigation listeners and functionality */
  $("#navigation .main li a").hover(function(e){
    $(".sub").each(function() {
      this.style.display = "none";
    });

    $("#navigation .main li").each(function() {
      if (this.className != 'page_selected')
          this.className = "";
    });

    if (this.parentNode.className != 'page_selected')
        this.parentNode.className = "nav_selected";
    else
        this.parentNode.className = "nav_selected2";
    
    if (active_item && this.parentNode.className != 'nav_selected2') 
        document.getElementById(active_item).className = "page_selected";

    if($("#sub_" + e.target.parentNode.id)[0]) {
      $("#sub_" + e.target.parentNode.id)[0].style.display = "block";
    }
  });
 
  var menu_interval = null;
  $("#navigation").hover(function(){
    if (menu_interval) {
      if (this.className != 'page_selected')
          clearInterval(menu_interval);
    }
  },
  function(e){
    menu_interval = setInterval(function(){
      $("#navigation .main li").each(function() {
        if (this.className != 'page_selected')
            this.className = "";
      });
      $(".sub").each(function() {
        this.style.display = "none";
        if (sel_item) {
            document.getElementById(sel_item).style.display = "block";
            document.getElementById(active_item).className = "page_selected";
        }
      });
    }, 500);
  });
});

//function for signing up for newsletter
function sendValue(str){

    //$(".error").hide();
    var hasError = false;
    $("div.error").remove();
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    var emailVal = $("#email").val();
    if(emailVal == '') {
	$("#email").before('<div class="error"><strong>Please enter your email address</strong></div>');
	hasError = true;
    } else if(!emailReg.test(emailVal)) {
	$("#email").before('<div class="error"><strong>Please enter a valid email address</strong></div>');
	hasError = true;
    }
    if (hasError)
        return false;
        
    var path = $("#path").val();
    //alert('hi ' + path+"newsletter-su/");

    $.post(path+"newsletter-su/",{ email:emailVal },
    function(data){ 
        $("#newsletter").hide();
        $("#signup_msg").show();
	
    });

}


function checkInput() {
    if ($("#query").val() == '') {
        alert('You must enter a valid query.');
        return false;
    }
}

function checkInput2() {
    if ($("#query2").val() == '') {
        alert('You must enter a valid query.');
        return false;
    }
}

