var timeout    = 500;
var closetimer = 0;
var menuitem = 0;

  function menu_open() {
    menu_canceltimer();
    menu_close();
    menuitem = $(this).find('ul').css('visibility', 'visible');
  }

  function menu_close() {
    if(menuitem) menuitem.css('visibility', 'hidden');
  }

  function menu_timer() {
    closetimer = window.setTimeout(menu_close, timeout);
  }

  function menu_canceltimer()
  {
    if(closetimer) {
      window.clearTimeout(closetimer);
      closetimer = null;
    }
  }

$(document).ready(function() {

  // Admin Menu
  $('.admin_actions > li').bind('mouseover', menu_open);
  $('.admin_actions > li').bind('mouseout',  menu_timer);

  if($('#event_date').length > 0){
    if($('#event_date').val() == ""){
      $('#event_date_widget').datepicker({ altField: '#event_date', dateFormat: 'yy-mm-dd'});
    } else {
      var d_raw = $('#event_date').val();
      default_date = new Date(d_raw.substring(0,4), (d_raw.substring(5,7) - 1), d_raw.substring(8,10));
      $('#event_date_widget').datepicker({ altField: '#event_date', dateFormat: 'yy-mm-dd', defaultDate: default_date });
    }
  }

  			var options = {
				height: 100,
				width: 180,
				navHeight: 0,
				labelHeight: 20,
				onMonthChanging: function(dateIn) {
					return true;
				},
				onEventLinkClick: function(event) {
					alert("event link click");
					return true;
				},
				onEventBlockClick: function(event) {
					alert("block clicked");
					return true;
				},
				onEventBlockOver: function(event) {
					//alert(event.Title + " - " + event.Description);
					return true;
				},
				onEventBlockOut: function(event) {
					return true;
				},
				onDayLinkClick: function(date) {
          var d = new Date(date);
          var curr_date = d.getDate();
          var curr_month = d.getMonth();
          var curr_year = d.getFullYear();
          window.location("http://label/event/day/" + curr_date + "-" + curr_month + "-" + curr_year);
					return true;
				},
				onDayCellClick: function(date) {
					return true;
				},
				locale: {
            days: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
            months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
            monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
            weekMin: 'wk'
        }

			};


			var events = [];

			$.jMonthCalendar.Initialize(options, events);
			
		  $("#chart_edit_form").validate();
		  
		  $('#new_album').click(function(){
		    add_album_click();
		    
		    return false;
		  });
		  
		  $('.new_single').click(function(){
        add_single_click();
        
        return false;
      });
});

function add_album_click(){
  $('<div class="new_album_form"></div>').load('/album/ajax',null,function(){
    ajaxAlbumFormInit();
  }).dialog({
    modal: true,
    height: 500,
    width:  800,
    title: "Add a new Album",
  });
}

function ajaxAlbumFormInit(){
  $('.album_ajax_form').ajaxForm({
    success: function(response){
      if(response == 1){
        $('.new_album_form').dialog('close');
      } else {
        $('.new_album_form').html(response);
        ajaxAlbumFormInit();
      }
    },
  });
}
  
function add_single_click(){
  $('<div class="new_single_form"></div>').load('/single/ajax',null,function(){
    ajaxSingleFormInit();
  }).dialog({
    modal: true,
    height: 500,
    width:  800,
    title: "Add a new Single",
  });
}
  
function ajaxSingleFormInit(){
  $('.single_ajax_form').ajaxForm({
    success: function(response){
      if(response == 1){
        $('.new_single_form').dialog('close');
      } else {
        $('.new_single_form').html(response);
        ajaxSingleFormInit();
      }
    },
  });
}