//Global var for Namespace to hold vars, functions
var EvtCal;
if(!EvtCal) EvtCal = {};

EvtCal.reload = false;

EvtCal.setup_form = function ()
{
     $("#id_date_0").datePicker();
     $("#id_end_date").datePicker();
     $("#calendar_form").bind("submit", function (){return false;});
     $.mask.addPlaceholder('?',"[ 01]");
     $("#id_date_1").mask("?9:99");
     $("#id_date_1").after("<select id='ampm' name='ampm'><option>AM</option><option>PM</option></select>");
     $("#btn_cancel").bind("click", function(){$("#calendar_form_container").hide();});
     $("#btn_save").bind("click", EvtCal.save_entry);
     $("#btn_cancel_adv").bind("click", function(){$("#evtcal_show_advanced").attr('checked', false);
             EvtCal.showMainForm();});
     $("#btn_save_adv").bind("click", EvtCal.showMainForm);     
     $("textarea#id_details").addClass("editor_active");
     $("#id_categories").after('<a id="add_category" href="#notfound"><img src="/media/editing_utils/add.png"></a>');
     $("#add_category").click(EvtCal.addOption)
     $("#id_repeat").change(function(){
             $(".evt_cal_advanced").hide();
             $("#evt_cal_advanced_" + this.value).show();             
         });
     $("#evtcal_show_advanced").click(function(){ if(this.checked) EvtCal.showAdvancedForm(); });
     $("#calendar_form_container").jqm();
     $("#evtcal_advanced").jqm();
}

EvtCal.showMainForm = function(){
    $(".swf_container").hide();
    $('#evtcal_advanced').jqmHide();
    EvtCal.center('#calendar_form_container');
    $('#calendar_form_container').jqmShow();
}
EvtCal.showAdvancedForm = function(){
    $('#calendar_form_container').jqmHide();
    EvtCal.center('#evtcal_advanced');
    $('#evtcal_advanced').jqmShow();
}

EvtCal.update_tags_ui = function (){
          $("input.filter").each(function (){ if(EvtCal.active_tags[this.id])
                                                  this.checked = true;
                                              else this.checked = false;});
          if(document.location.search)
              $("a.calnav").each(function(){this.href = this.href + document.location.search;});
}

EvtCal.save_entry = function ()
{
  data = {}
  data.title = $("#id_title").val();
  if (!data.title)
  { 
     $("input#id_title").after("<span>Required!</span>");
     return false;
  }
  //ed.setProgressState(1);
  if(ed = tinyMCE.get('id_details'))
  {
      $("#id_details").val(ed.getContent());
  }
  data = $("form#calendar_form").serializeArray();
  //if no JS, details already filled by textarea

  id = $("input#id_pk").val();
  url = "/calendar/ajax_save/";
  if(id)          
    url = url + id + "/"
  jQuery.post(url, data, 
              function(d,s){
               $("#calendar_form_container").jqmHide();
               //location.reload(true);
              }, "text");
}

EvtCal.add_new_calendar_event =  function ()
{
    
   var month = $("#frm_month").val();
   var year = $("#frm_year").val();
   
   var day = this.id.replace("calendar_","")
   $("input#id_date_0").attr("value",day + "/" + month + "/" + year); 
   $("input#id_title").attr("value","");
   $("input#id_date_1").attr("value","");
   $("input#id_pk").attr("value","");
   $("input#evtcal_show_advanced").attr("checked",false);
   $("select#id_categories")[0].selectedIndex = 0;
   
   ed = tinyMCE.get("id_details");
   if(!ed)
   {
     $("textarea#id_details").attr("value","");
     tinyMCE.init({
         mode : "textareas",
         theme : "simple"
     });
     ed = tinyMCE.get("id_details");          
   }  
   else
       ed.setContent('');
   EvtCal.showMainForm();
   $("#evtcal_new_only").show();
   $("input#id_title").focus();
   
}

EvtCal.edit_calendar_event = function ()
{
   $("#calendar_form_container").show();
   var id = this.id.replace("event_","");
   r = Math.random()
   jQuery.getJSON("/calendar/json/" +  id + "/?rand=" + r, function (d){
           fields = d[0].fields;
           $("input#id_pk").attr("value",d[0].pk); 
           $("input#id_title").attr("value",fields.title); 
           date = fields.dt.split(" ");
           time = date[1].split(":");
           date = date[0].split("-");     
           year = date[0];month=date[1];day=date[2];
           hour = time[0];minute=time[1];
           if(hour >= 12)
           {
               if(hour > 12)
                   hour = hour - 12;
               $("#ampm")[0].selectedIndex = 1;
           }
           else
           {
               $("#ampm")[0].selectedIndex = 0;
           }
           sel = $("select#id_categories")[0];
           ndx = 0;
           for(i=0;i<sel.options.length;i++)
               if(sel.options[i].value == fields.categories)
                   ndx = i;
           sel.selectedIndex = ndx;
           $("input#id_date_0").attr("value",day + "/" + month + "/" + year); 
           $("input#id_date_1").attr("value",hour + ":" + minute);
           $("textarea#id_details").attr("value",fields.details);
           ed = tinyMCE.get('id_details');
           if(ed)
               ed.setContent(fields.details);

       });
   tinyMCE.init({
         mode : "textareas",
         theme : "simple"
     });
   $("#evtcal_new_only").hide();
   $("input#id_title").focus();
}

EvtCal.delete_calendar_event =  function ()
{
    v = this;
    id = this.id.replace("event_","");
    repl = confirm("This will delete the calendar event titled \"" + $("a#title_" + id).text() + '"');
    if(repl){
        url = "/calendar/ajax_save/";
        if(id)          
            url = url + id + "/";
        jQuery.post(url, {"delete":1}, 
              function(d,s){
                  location.reload(true);
              }, "text");
    }
}
EvtCal.delete_calendar_group =  function ()
{
    v = this;
    id = this.id.replace("event_","");
    repl = confirm("This will delete all the repeating events in the series \"" + $("a#title_" + id).text() + '"');
    if(repl){
        url = "/calendar/ajax_save/";
        if(id)          
            url = url + id + "/";
        jQuery.post(url, {"delete":1,"delete_group":1}, 
              function(d,s){
                  location.reload(true);
              }, "text");
    }
}

EvtCal.addOption = function()
    {
        var sel = $("#id_categories")[0];
        opt = prompt("What should the new category be called?",'');
        if(opt)
        {
            sel.options[sel.options.length] = new Option(opt,opt);
            sel.options[sel.options.length-1].selected = true;
        }
    }
EvtCal.center = function(obj)
{
    obj = $(obj);
    var w = $('body').width();
    var h = $('body').height();
    var w2 = obj.width();
    var h2 = obj.height();
    var top = parseInt((h/2) - (h2/2)) + "px";
    var left = parseInt((w/2) - (w2/2)) + "px";
    obj.css('top',top);
    obj.css('left', left);
}