$(document).ready(function(){
  //when any 'datetime' field gets clicked it will open the datatime dropdown
  $(".datetime").datepicker({
    duration: '',
    showTime: true,
    constrainInput: true
  });
  
  //when any 'date' field gets clicked it will open the date dropdown
  $(".date").datepicker({
    duration: '',
    showTime: false,
    constrainInput: true
  });
  
  //when any ajax form get submitted, handle the request
  $(".ajax").live('click',function(){
    var form = $(this).parents("form");
    var action = form.attr("action");
    //this will update CKEditor's content so the correct data is carried accross
    for(instance in CKEDITOR.instances){
      CKEDITOR.instances[instance].updateElement();
    }
    form_data = form.serialize();
    $.ajax({
      type: "post",
      url: action,
      data: form_data,
      success: function(returnText){
        if(returnText.indexOf("error") >= 0){
          $.fn.colorbox({html:returnText});
        }
        else{
          document.location = returnText;
        }
      },
      error: function(){
        $.fn.colorbox({html:"There was an error while trying to submit your form, please try again."});
      }
    });
    return false;
  });
  
  //when any 'colorbox' link gets clicked open a colorbox modal window
  $(".colorbox").colorbox();
  $(".colorbox_iframe").colorbox({maxWidth:'100%',maxHeight:'100%',iframe:'true',height:'650px',width:'650px'});
  
});
