//<![CDATA[
jQuery.ajaxSetup({ cache: true });
$(document).ready(function() {
  $('#login-link').click(function(){
    $('#login').dialog({modal:true});
    return false;
  });

  $('#login-form').ajaxForm({
    dataType:  'json',
    success: processJson,
    beforeSubmit: validate
  });

  $('a#logout').click(function() {
    var url = this.href;
    $.getJSON(url, function(data) {
      console.log(data);
      if(data.status==1){
        // Do sth
        alert('You have been signed out.');
        window.location.reload();
      }
    });
    return false;
  });
  
  $('.menu-item-activate').click(function (){
		var parent = $(this).parent();

		var toggler_status;
		$(parent).children('.menu-content').slideToggle('slow');
		$(parent).toggleClass('selected');
		
  });
  
});

// Validate
function validate(formData, jqForm, options) {
    // formData is an array of objects representing the name and value of each field
    // that will be sent to the server;  it takes the following form:
    //
    // [
    //     { name:  username, value: valueOfUsernameInput },
    //     { name:  password, value: valueOfPasswordInput }
    // ]
    //
    // To validate, we can examine the contents of this array to see if the
    // username and password fields have values.  If either value evaluates
    // to false then we return false from this method.

    for (var i=0; i < formData.length; i++) {
        if (!formData[i].value) {
            alert('Please enter a value for both Username and Password');
            return false;
        }
    }
}

// Process user data
function processJson(data) {
  console.log('processJson');
  console.log(data);
  console.log(data.data);
  console.log(data.status);
  console.log(data['status']);
  alert("You are logged in.");
  if(data.status) {
    location.reload(true);
  }
}

//]]>
