if (Drupal.jsEnabled) {
	$(document).ready(function () {
		// Hide input form for date picker
		$('#edit-calendar-goto-datepicker-popup-0').focus(function(){
			$(this).animate( {height:"1px"}, 500)
				   .css('color', '#fff')
				   .next().hide();

		});
		$('#edit-calendar-goto-datepicker-popup-0').blur(function(){
			$(this).animate( {height:"13px"}, 500)
				   .css('color', '#666')
				   .next().show();		
		});
		
		// Hover tooltips for calendar
    // $(".calendar-calendar .view-data-node-title a").easyTooltip();
		
		// Change table text to download
		$('td.views-field-field-document-fid a').text('Download');
		
		// Hide events in calendar when there are more than 2 events
		$('div.month-view td div.inner').each(function(){
			if($(this).children().hasClass('view-item')){
				var c= $(this).children('.view-item').size();
				if(c > 2){
					$(this).children('.view-item').filter(function(index){
						return index > 1;
					}).hide();
					$(this).after("<a href='#' class='expand-events'>...more</a>");
				}
			}
		});
		
		$('a.expand-events').click(function(e){
			if($(this).prev('.inner').children('.view-item').is(':hidden')){
				$(this).prev('.inner').children('.view-item:hidden').fadeIn();
				$(this).text("...hide");
			} else {
				$(this).prev('.inner').children('.view-item').filter(function(index){
					return index > 1;
				}).fadeOut();
				$(this).text("...more");
			}
			
			return false;
		});
		
		
		// Create colour legend for calendar based on term
		var $stripes = new Array();
		$('div.stripe').each(function(){
			var $color = $(this).css("background-color");
			if($color != "rgb(255, 255, 255)" && $color != "#ffffff"){
				var $key = $(this).attr("title").replace(/Key: /, "") + "|" + $color;
				if(!in_array($key, $stripes)){
					$stripes.push($key);
				}
			//	alert($key + $color)
			}
		});
		if($stripes.length > 0){
			$out = "<div class='cal-legend'>";
			for(var i=0;i<$stripes.length;i++){
				var s = $stripes[i].split('|');
				$out = $out.concat("<span style='background-color:"+s[1]+"'>&nbsp;</span> "+s[0]+"&nbsp;&nbsp;");
			}
			$out = $out.concat("</div>");
			$("div.calendar-calendar:last").append($out);
		}
	
		//add exposed filter to meetings date browser
		var prev = $('.view-meeting-browser .date-prev a').attr('href');
		var next = $('.view-meeting-browser .date-next a').attr('href');
		if(prev != "" && next != ""){
			var q = location.search;
			if(q != ""){
				$('.view-meeting-browser .date-prev a').attr('href', prev + q);
				$('.view-meeting-browser .date-next a').attr('href', next + q);
			}
		}
	
	});
	
	


	function in_array( what, where ){
    	var a=false;
		for(var i=0;i<where.length;i++){
      		if(what == where[i]){
      			a=true;
      			break;
      		}
      	}
      	return a;
	}


}