// For IE hover flicker bug
$j(document).ready(function() {
    $j("#calendarLinks").calendar();
    //$j("#calendarMonth li h2, #calendarMonth li .more").calendarGridToList();

    $j("#calendarMonth").calendarGridToList();

    //	Calendar Switcher Links
    $j("#calSwitch").html('<p id="btnSwitchCal" class="active"><a>Calendar&nbsp;&rarr;</a></p><p id="btnSwitchList"><a>Event Listing&nbsp;&rarr;</a></p>');
    $j("#calSwitch a").click(function() {
        if (!$j(this).parent().hasClass("active")) {
            calSwitch();
        }
    });

    //	Num Hover
    $j("#content.calGrid h2").hover(
		function() {
		    $j(this).addClass("over");
		},
		function() {
		    $j(this).removeClass("over");
		}
	)

    //	Calendar Popup
    $j("#chooseDate").hover(
		function() {
		    $j(this).addClass("over");
		},
		function() {
		    $j(this).removeClass("over");
		}
	);
    $j("#chooseDate a").click(function() {
        $j("#chooseDate").removeClass("over");
    });


    //	Events Chooser
    $j("#chooseType .dropList a").click(function() {
        var category = $j(this).attr("class");
        var categoryName = $j(this).html();

        $j("#chooseType .dropList li").show();
        $j(this).parent().hide();
        $j(this).parent().parent().prev().html('<strong class="active">' + categoryName + '</strong>');

        $j(".vevent").each(function() {
            var thisCategory = $j(this).attr("class");
            var stringSearch = thisCategory.indexOf(category);

            if (categoryName == "All Events") {
                $j(this).show();
                $j("#calendarMonth").removeClass("filtered");
                $j(this).removeClass("evActive")
            } else if (stringSearch != -1) {
                $j("#calendarMonth").addClass("filtered");
                $j(this).show().addClass("evActive");
            } else {
                $j("#calendarMonth").addClass("filtered");
                $j(this).hide().removeClass("evActive");
            }

        });

        ///////////////////////////////////////////////////////////////
        // PAC 10/13/10 - add in "No Events Listed" tags
        addNoEventsTags();
        ///////////////////////////////////////////////////////////////

        $j(".dropList").removeClass("dlOver");
        return false;

    });

    var dt = new Date();
    if ($j("#calendarLinks a:eq(" + (dt.getDate() - 1) + ")") != null) {
        $j("#calendarLinks a:eq(" + (dt.getDate() - 1) + ")").click();
    } else {
        $j("#calendarLinks a:eq(0)").click();
    }

    ///////////////////////////////////////////////////////////////
    // PAC 10/13/10 - add in "No Events Listed" tags
    addNoEventsTags();
    ///////////////////////////////////////////////////////////////

});

function addNoEventsTags(){
	///////////////////////////////////////////////////////////////
	// PAC 10/13/10 - add in "No Events Listed" tags (run again)
	$j(".day").each(function(){
	    var found = false;
	    for(var i = 0; i < $j(this).find(".vevent").length; i++){
	        if($j(this).find(".vevent").eq(i).css("display") == "block") {found = true;}
	    }
	    if(!found){
	        if($j(this).find(".noEv").length < 1){
	            $j(this).append('<p class="noEv">No events listed.</p>');
	        }
	    }else{
	        $j(this).find(".noEv").remove();
	    }
	});
	///////////////////////////////////////////////////////////////
}

function calSwitch(){
	$j("#calSwitch p").toggleClass("active");
	$j("#content").toggleClass("calGrid").toggleClass("calList");
};

(function($) {
	$.fn.calendarGridToList = function(options) {
		var opts = $.extend({
			nonTrigger:		'.prevMonth, .nextMonth',		// element(s) that will not trigger function
			targetId:		'#calendarMonth',	// id of the calendar to target
			currentClass:	'current',			// the class to apply to the selected day
			nextClass:		'active',			// the class to apply to any subsequent days
			nextAmount:		6					// number of subsequent days
		}, options);
		
		return this.each(function() {
			var obj = $(this);
			var targetId = $(opts.targetId);
			var nextAmount = opts.nextAmount;
			var currentClass = opts.currentClass;
			var nextClass = opts.nextClass;			
			var calendarDays = targetId.children(); // All the days in the target calendar
					
			obj.find("h2, .more").each(function() {
				var container = obj.parent().parent();
				var targetDay = calendarDays.eq($(this).parent().prevAll().length); // The day in the target calendar that corresponds to the clicked day

				// Get all following days up to the limit defined in nextAmount
				var subsequentDays = [];
				targetDay.nextAll().each(function(i) {
					if(i < nextAmount) {
						subsequentDays[i] = $(this);
					}
				});

				// Bind click event to each item in the small calendar
				$(this).bind(
					"click",
					{
						targetDay:	targetDay,
						subsequentDays:	subsequentDays,
						container: container
					},
					calendarClick
				);
			});

			// Add and remove appropriate classes
			function calendarClick(e) {
				if(e.data.container.hasClass("calGrid")) {
					calSwitch();
				
					var subsequentDays = e.data.subsequentDays;
					var totalDays = subsequentDays.length;

					// Reset classes
					calendarDays.removeClass(currentClass + " " + nextClass);

					// Add Classes
					e.data.targetDay.addClass(currentClass);

					for(var i = 0; i < totalDays; i++) {
						subsequentDays[i].addClass(nextClass);
					}
					
					
					// jump the user back to the top of the page
					// so they can see the events list for their selected day
					document.location = "#";
				}
			}
		});
	};
	
	$.fn.calendar = function(options) {
		var opts = $.extend({
			nonTrigger:		'.prevMonth, .nextMonth',		// element(s) that will not trigger function
			targetId:		'#calendarMonth',	// id of the calendar to target
			currentClass:	'current',			// the class to apply to the selected day
			nextClass:		'active',			// the class to apply to any subsequent days
			nextAmount:		6					// number of subsequent days
		}, options);

		return this.each(function() {
			var obj = $(this);
			var targetId = $(opts.targetId);
			var nextAmount = opts.nextAmount;
			var currentClass = opts.currentClass;
			var nextClass = opts.nextClass;			
			var calendarDays = targetId.children(); // All the days in the target calendar
							
			obj.children().each(function() {
				if(!$(this).hasClass(opts.nonTrigger)) {
					var targetDay = calendarDays.eq($(this).prevAll().length); // The day in the target calendar that corresponds to the clicked day

					// Get all following days up to the limit defined in nextAmount
					var subsequentDays = [];
					targetDay.nextAll().each(function(i) {
						if(i < nextAmount) {
							subsequentDays[i] = $(this);
						}
					});

					// Bind click event to each item in the small calendar
					$(this).bind(
						"click",
						{
							targetDay:	targetDay,
							subsequentDays:	subsequentDays
						},
						calendarClick
					);
				}
			});

			// Add and remove appropriate classes
			function calendarClick(e) {

				var subsequentDays = e.data.subsequentDays;
				var totalDays = subsequentDays.length;

				// Reset classes
				calendarDays.removeClass(currentClass + " " + nextClass);

				// Add Classes
				e.data.targetDay.addClass(currentClass);

				for(var i = 0; i < totalDays; i++) {
					subsequentDays[i].addClass(nextClass);
				}

				return false;
			}		

		});
	};

})(jQuery);