var copyDate=1;
$(document).ready(function () {
  $('.ec_calendar td').live('click',function () {
    $('.ec_calfloat').remove();
    $("<div/>",{'class':'ec_calfloat'}).html($(this).closest('.ec_calendar').html()).dialog({title:'Calendar',width:900,close:function(){$('.ec_calfloat').remove();;}});
  });
  $('.ec_bkgrndcolour').click(function () {
    $(this).find('img').slideDown().click(function (e) {
        var colour = getColour(e,this);
        $(this).parent().prevAll('.ec_colour').css('background-color',colour);
        $(this).unbind().slideUp();
        $(this).parent().find('input').val(colour)
        e.stopPropagation();
     }).mouseout(function (e){$(this).unbind().slideUp();});
  });
  $('.ec_fontcolour').click(function () {
    $(this).find('img').slideDown(function () {
      $(this).click(function (e) {
        var colour = getColour(e,this);
        $(this).parent().prevAll('.ec_colour').css('color',colour);
        $(this).unbind().slideUp();
        $(this).parent().find('input').val(colour)
        e.stopPropagation();
     }).mouseout(function (e){$(this).unbind().slideUp();});
    });
  });
  $('.ec_calfloat .calendar P[id]').live('click',function () {    /* click on event item */
    var id = $(this).attr('id').substr(1);
    $("<div id='ep" + id + "'  class='ec_ep'></div>").dialog({title:$(this).text(),close:function(){$(this).remove();}}).load('p0.html?sc=showevent&ajax=1&id='+id+globalParams);
  });
  $('.ec_calselect input').live('click',function () {
      var selected = $(this).closest('.ec_calfloat').find('.'+$(this).attr('name'));  /* the entries for this calendar */
      if ($(this).is(':checked')) {$(selected).show();} else {$(selected).hide();}
  });
});
var vals=['00','33','66','99','CC','FF'];
function getColour (e,el) {  // calculates the colour from the x and y co-ordinates
  var o = $(el).offset();
  var x = e.pageX - o.left;
  var y = e.pageY - o.top;
  var col = ((x-1)/11) | 0; // |0 casts the division into an integer
  var row =  ((y-1)/11) | 0;
  return '#'+vals[((col / 6) | 0) + 3 * ((row / 6) | 0)]
    + vals[ col % 6]
    + vals[ row % 6];  
}
function newEvent() {
  $("<div id='ep0' class='ec_ep'></div>").dialog({title:'New Event',close:function(){$(this).remove();}});
  editEvent(0,0);
}
function editEvent(id,calid) {
  $('#ep'+id).load('p0.html?sc=eventedit&ajax=1&ec_id='+id+'&calid='+calid+globalParams,
        function(){
          $(this).dialog('option','width',750);
           WYSIWYG.attach('ec_description');
          $('#ev_startdate, #ev_enddate').datepicker({dateFormat: 'd/m/yy'});
          copyDate=1;
        });
}
function delEvent(id,calid) {
  if(confirm("Are your sure you want to delete '" + $('#ep'+id+' p').text()+"'?")) {
    $('#ep'+id).load('p0.html?sc=eventdel&ajax=1&ec_id='+id+'&calid='+calid+globalParams,function () {$(this).remove();$('#e'+id).remove();});
  }
}
function saveEvent(el) {
  $.ajax({url:'p0.html','data':$(el).serialize(),success:function(ht,t,q){
            if (ht.length) {
              $(el).parent().html(ht);
            } else {
              $('.ec_calfloat .calendar').load('?sc=calendar&caldate='+$(el).find('table').attr('class')+'&ajax=1'+globalParams+' .calendar table',
                    function () {
                      $('.ec_calselect input').not(':checked').each(function(inx,e) { $(el).find('.'+$(e).attr('name')).hide();});
                    } );
              $(el).parent().remove();
            }
          }
        });
}

function viewCalendar(id) {
    $('.ec_calfloat').remove();
    $("<div/>",{'class':'ec_calfloat'})
      .load('?sc=basecalendar&ajax=1'+globalParams)
      .dialog({title:'Calendar',
               width:900,
               close:function(){
                    $(this).remove();
                  }
              });
}
