
function EventPeek(obj,config){
  try{
	  
	this.over = function(){
		//console.log("Menu over");
		var obj = $(this);
		$(".menu",obj).alignTo(obj,'TLtoBL',{offsets:[-4,0],animation:false});
		obj.addClass("over");
		$(".menu:visible").css({'display':'none'});
		$(".menu",obj).css({'display':'block'});
		$(".menu ul",obj).animate({opacity:1.0},400,"swing");
	};
		
	this.out = function(){
		//console.log("Menu out");
		obj = $(this);
		obj.removeClass("over");
		$(".menu ul",obj).css({opacity:0});
		$(".menu",obj).css({'display':'none'});
		/*,0,"swing",function(){
			$(".menu",obj).css({'display':'none'});
		});*/
	};
	
	this.obj = ((obj == null)? $('.menu_base'):obj);
	this.config = ((config == null)?{    
			sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
			interval: 100, // number = milliseconds for onMouseOver polling interval    
			over: this.over, // function = onMouseOver callback (REQUIRED)    
			timeout: 200, // number = milliseconds delay before onMouseOut    
			out: this.out // function = onMouseOut callback (REQUIRED)    
		}:config
	);
	//obj.mouseenter(function(){$(this).addClass("over");});
	//obj.mouseleave(function(){$(this).removeClass("over");});
	obj.hoverIntent(this.config);
	
	return this;
	  
  }
  catch(e){
	  return false;
  }
};


Calendar = {
  init : function(){
      //console.log("Init Calendar");
      //this.interact();
  },
  month_interact : function(){
    $(".week_day").each(function(){
      _this = $(this);
      _this.click(function(e){
        _this = $(this);
        $(".week_day.selected").each(function(){
          $(this).removeClass("selected");
        });
        _this.addClass("selected");
        date = _this.attr('date').split('-');
        date_text = _this.attr('date_text');
        //date = date.split('-');
        $params = {
          'range':'day',
          'year':date[0],
          'month':date[1],
          'day':date[2]
        };
        flag_content = $("#event_focus .flag .title");
        flag_content.animate({"width":"toggle","opacity": "toggle"}, 300, "swing");
        $.getJSON("/calendar/events",$params,function(data,textStatus){
          var code = "";
          //console.log((data.unique_events == null));
          if(data.events != null){
            
            $.each(data.events,function(date,events){
              var length = events.length;
              var i = 0;
              $.each(events,function(index,event){
                code += '<div class="event">';
                code += '<div class="event_title">'+event.title+'</div>';
                code += '<div class="event_location">'+event.location+'</div>';
                code += '<div class="event_brief">'+event.brief_description+'</div>';
                code += '</div>';
                if(i < length-1)
                  code += '<div class="event_divider"><div></div></div>';
                i++;
              });
            });            
          }
          else{
            code += '<div class="event">';
            code += '<div class="event_location">No scheduled events</div>';
            code += '</div>';
          }
          flag_content.html(date_text+" Events");
          flag_content.animate({"width":"toggle","opacity": "toggle"}, 300, "swing");
          $("#event_focus .content").fadeOut("normal",function(){
            $(this).html(code);
            $(this).fadeIn("normal");
          });
        });
        
         
      });
    });
    
    $("#month_view .month_event").each(function(){
      _this = $(this);
      config = {    
        sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: Calendar.eventOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 200, // number = milliseconds delay before onMouseOut    
        out: Calendar.eventOut // function = onMouseOut callback (REQUIRED)    
      };
      _this.hoverIntent(config);
      _this.click(function(e){
        _this = $(this);
        var type;
        code = '';
        $('.details',_this).each(function(){
          type = $(".event_type",this).html()
          code += '<div class="selected_event">';
          //console.log($(".event_image",this).html());
          if($(".event_image",this).html() != "/site/images/")
            code += '<div class="event_image"><img src="'+$(".event_image",this).html()+'" /></div>';
          code += '<div class="event_title">'+$(".event_title",this).html()+'</div>';
          code += '<div class="event_location">'+$(".event_location",this).html()+'</div>';
          code += '<div class="event_description">'+$(".event_description",this).html()+'</div>';
          code += '</div>';
        });
        $("#event_focus .flag .title").each(function(){
          $(this).animate({"width":"toggle","opacity": "toggle"}, 300, "swing",function(){
            $(this).html(type);
            $(this).animate({"width":"toggle","opacity": "toggle"}, 300, "swing");
          });
        });
        $("#event_focus .content").fadeOut("normal",function(){
          $(this).html(code);
          $(this).fadeIn("normal");
        });
        //$("#event_focus .content")
        return false;
      });
    });
    //$(".week_day.selected").trigger('click')
    if($("#month_view .month_event.selected").size() > 0){
      $("#month_view .month_event.selected:eq(0)").trigger('click');
    }else if($(".week_day.selected").size() > 0){
      $(".week_day.selected").trigger('click');
      
    }else{
      $(".week_day[the_first]").trigger('click');
    }
    
    $("#legend .item").each(function(){
      _this = $(this);
      _this.click(function(e){
        $(".icon",this).each(function(){
          $("#month_view ."+this.className.split(" ")[1]).each(function(){
            $(this).animate({"height":"toggle"}, 300);//toggle("normal");//animate({"opacity":"toggle"}, 300, "swing");
          });
        });
        if($(this).css("opacity")<1){
          $(this).animate({"opacity":"1"}, 300);
        }
        else{
          $(this).animate({"opacity":"0.5"}, 300);
        }
      });
    });
    
  },
  eventOver : function(){
    var obj = $(this);
    if($.browser.msie)
      $(".details",obj).alignTo(obj,"BCtoTC",{offsets:[0,2]}).removeClass('hidden');
    else
      $(".details",obj).alignTo(obj,"BCtoTC",{offsets:[0,-18]}).animate({"top": "+=20px", "opacity": "toggle"}, 300);//.removeClass("hidden");
  },
  eventOut : function(){
    var obj = $(this);
    if($.browser.msie)
      $(".details",obj).addClass('hidden');
    else
      $(".details",obj).animate({"top": "-=20px", "opacity": "toggle"}, 300);
  }
};

$(document).ready(function(){Calendar.init.apply(Calendar,[]);});
//$(document).ready(Calendar.init);
