$(document).ready(init_page);

var hoverbox_pos = {
  mm_16:{left:420,width:49},
  mm_2:{left:514,width:79},
  mm_12:{left:642,width:136},
  mm_20:{left:828,width:84},
  mm_30:{left:965,width:37}
}

function init_page(){
  current_hoverbox();
  parent_hoverbox();
  init_hoverbox();
  init_form();
}

function parent_hoverbox(){
 mid = $(".current_page_parent").attr('id');
 $("#hoverbox").addClass(mid);

  if(hoverbox_pos[mid]!=undefined){
	$('#hoverbox').animate({
	  left: hoverbox_pos[mid].left,
	  width: hoverbox_pos[mid].width
	  },{queue:false,duration:2000,easing:'easeOutQuint'});

  }
};

function current_hoverbox(){
 mid = $(".current_page_item").attr('id');
 $("#hoverbox").addClass(mid);

  if(hoverbox_pos[mid]!=undefined){
	$('#hoverbox').animate({
	  left: hoverbox_pos[mid].left,
	  width: hoverbox_pos[mid].width
	  },{queue:false,duration:2000,easing:'easeOutQuint'});

  }
};

function init_hoverbox(){
  $('#main_menu li a').bind('mouseover',
    function(){
      mid = $(this).parent().attr('id');
      
      if(hoverbox_pos[mid]!=undefined){
        $('#hoverbox').animate({
          left: hoverbox_pos[mid].left,
          width: hoverbox_pos[mid].width
          },{queue:false,duration:1000,easing:'easeOutQuint'});
      
      }
    }
  );  
}

function init_form(){
  $('#submit_send').css('display','none');
  $('#bt_send').css('display','').bind('click',submit_form);
  $('#post_ajax').value = 't';
  $('#form_contact').bind('submit',function(){
    submit_form();
    return false;  
  });
  
}


function submit_form(){
  // validation
  var o_name = $('#contact_name');
  var o_email = $('#contact_email');
  
  var o_question = $('#contact_question');

  var o_phone = $('#contact_phone');
  var o_message = $('#contact_message');
  


  var contact_name  = o_name.val();
  var contact_email = o_email.val();
  
  $('#form_message').html( '');

  

  if(contact_email==''){
    $('#form_message').html( 'No email address entered' );
    return false;  
  }else if(!is_valid_email(contact_email)){

    $('#form_message').html( 'Please enter a valid email' );
    return false;
  }else if(contact_name==''){
    $('#form_message').html( 'All Fields must be filled' );
    return false;
  }else if (contact_name.length < 6 ){
    $('#form_message').html( 'Name must be at least 6' );
    return false;      
  }
  

  var arr_label = new Array();
  var arr_data   = new Array();

  arr_label.push('Name'); arr_data.push(contact_name);
  arr_label.push('Email'); arr_data.push(contact_email);

  
  if(o_question.get()!=''){
    if(o_question.val() == ''){
      $('#form_message').html( 'Question must be filled' );
      return false;  
    }else{
      arr_label.push('Question'); arr_data.push(o_question.val());
    }   
  }
  if(o_phone.get()!=''){
    if(o_phone.val() == ''){
      $('#form_message').html( 'Phone must be filled' );
      return false;  
    }else{
      arr_label.push('Phone'); arr_data.push(o_phone.val());
    }   
  }
  if(o_message.get()!=''){
    if(o_message.val() == ''){
      $('#form_message').html( 'Message must be filled' );
      return false;  
    }else{
      arr_label.push('Message'); arr_data.push(o_message.val());
    }   
  }

  
  
  post_data = {labelsArr: arr_label.join(',') , dataArray:  arr_data.join(',') ,post_ajax:'t' };
  $('#form_message').html( 'Please wait' );
  /*$.post( www_root + 'mailingList.php', post_data, function(data){
    if(data=='returnString=success'){
      $('#form_message').html( 'Thank You!' );
    }else{
      $('#form_message').html( 'Try again' );
      alert(data);
      
    }
    
    o_name.val('');
    o_email.val('');
    o_question.val('');
    o_phone.val('');
    o_message.val('');
    
    o_name.focus();
  });
  
  */
   
  
  
  
  return false;
}

function is_valid_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}
