$(document).ready(function() {
  //var base_url = 'http://localhost/rnsa_redesign/index.php/';
  var base_url = 'http://www.secureaustralia.org.au/index.php/';
  
  /*Initialisation*/
  $("#accordion").accordion({ header: "h3", collapsible: true, active : false,autoHeight: false});

  
  /*Register Form*/
  $("#registerForm").validate({
	  messages:{
		  email: {
			  remote: "Your email address has already been used"
		  }
	  },
	  rules: {
		email: {
		  required: true,
		  email: true,
		  remote: {
			  type: "post",
			  url: base_url + "ajax/check_email/"
		  }
		}
	  },
        errorPlacement: function(error, element) {
            offset = element.offset();
            error.insertBefore(element);
            error.addClass('error_validation');  // add a class to the wrapper
			error.addClass('small-text');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
        }
	});
	
	$("#checkme").click(function(){
		showExtra();
	});
 	showExtra();
 
 	function showExtra(){
		if($("#checkme").is(":checked")){
    	$("#extra").fadeIn("fast");
    	}else $("#extra").fadeOut("fast");
	}
 
 
	$("#recoverForm").validate({
        errorPlacement: function(error, element) {
            offset = element.offset();
            error.insertBefore(element);
            error.addClass('error_validation');  // add a class to the wrapper
			error.addClass('small-text');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
        }
	});
	$("#changepasswordForm").validate({
		errorPlacement: function(error, element) {
            offset = element.offset();
            error.insertBefore(element);
            error.addClass('error_validation');  // add a class to the wrapper
			error.addClass('small-text');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
        }
	});
	
  	var validator = $("#editForm").validate({
	   	invalidHandler: function() {
    		alert(validator.numberOfInvalids() + " field(s) are invalid, please go back and check");
  		},
		errorPlacement: function(error, element) {
            offset = element.offset();
            error.insertBefore(element);
            error.addClass('error_validation');  // add a class to the wrapper
			error.addClass('small-text');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
        }
   });

   /*Register Form*/
   	$('#register_disclaimer').hide();

	/*Search Form*/
	$('#search_button').click(function(){
		var name = $('#name').val();
		var area = $('#area').val();
		var state = $('#state').val();
		$('#search_result').html('Loading...');
		$.ajax({
		   type: "POST",
		   url: base_url+"ajax/search_result/",
		   data: ({name:name,state:state,area:area}),
		   success: function(msg){
			 var targetOffset = $('#search_button').offset().top;
	         $('html,body').animate({scrollTop: targetOffset}, 1000);
			 $('#search_result').html(msg).addClass('search_result');	 
		   }
		});
	});
   
   /*Open and Show Researcher's Details*/
   $(".details").hide();
   $(".show_details").click(function(){
        $(this).next('div').toggle();
   });
   
   /**
   	Open and Show Login Box
   **/
  	$('#login-box').hide();
	$('#login-button').click(function(){
		$('#login-box').slideToggle();
	});
	
	
	
	/** Quick Search Filter**/
   $("#filter").keyup(function () {
		var filter = $(this).val();
		$("#list:first li").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});
	$("#area_quick_filter").keyup(function () {
		var filter = $(this).val();
		$("#area_filter:first option").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});
	
	$("#filter_accordion").keyup(function () {
		var filter = $(this).val();
		$("#accordion:first div").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});
	
	
	/*Step System*/
	$('.step').hide();
	$('.step:first').show();
	$('.form_next').click(function(){	
		$('.step').hide();
		$(this).parent().parent().hide().next().fadeIn();
	});
	$('.form_prev').click(function(){
		$('.step').hide();
		$(this).parent().parent().hide().prev().fadeIn();
	});
	
	
	/*Next System*/
	$('.next').click(function(){
		$(this).hide();
		$(this).parent().parent().next().fadeIn();
	});
	
	/*Edit Form*/
	$('#add_research_area_button').click(function(){
		var e_id = $('#area_filter').val();
		var e_name = $('#area_filter :selected').text();
		var m_id = $('#id').val();
		//alert(e_name + e_id);
		$.ajax({
		   type: "POST",
		   url: base_url+"ajax/add_research_area/",
		   data: ({e_id : e_id,m_id:m_id}),
		   success: function(msg){
			 reload_area();
			 //alert(msg);
		   }
		});
	});
	
	$('#add_other_research_area').click(function(){
		var other = $('#other').val();
		var m_id = $('#id').val();
		//alert(other+m_id);
		$.ajax({
		   type: "POST",
		   url: base_url+"ajax/add_other_research_area/",
		   data: ({other : other,m_id:m_id}),
		   success: function(msg){
			 reload_area();
			 $('#other_updated').html(msg);
			 //alert(msg);
		   },
		   error: function(msg){
			   console.log(msg);
		   }
		});
	});
	
	$('#phd_student_exclusive_fields').hide();
	$('#phd_student_button').click(function(){
		$('#position').val('PhD Student');
		$('#phd_student_exclusive_fields').toggle();
	});
	$('#position').keyup(function(){
		if (this.val()=='PhD Student') $('#phd_student_button').click();
	});
	
	$('#other').keypress(function(e){
		if(e.keyCode == 13) {
			$('#add_other_research_area').click();
			return false;
	    }
	});
	
	$('#name').keypress(function(e){
		if(e.keyCode == 13) {
			return false;
	    }
	});

	$('.remove_research_area_button').live('click', function() {
		var e_id = $(this).attr('id');
		var m_id = $('#id').val();
		$.ajax({
		   type: "POST",
		   url: base_url+"ajax/remove_research_area/",
		   data: ({e_id : e_id,m_id:m_id}),
		   success: function(msg){
			 reload_area();
			 //alert(msg);
		   }
		});
	});


	/*Helper Function*/
	function remove_research_area(e_id){
		var e_id = e_id;
		var m_id = $('#id').val();
		$.ajax({
		   type: "POST",
		   url: base_url+"ajax/remove_research_area/",
		   data: ({e_id : e_id,m_id:m_id}),
		   success: function(msg){
			 reload_area();
			 //alert(msg);
		   }
		});
	}
});




