﻿(function() {

    $(document).ready(
		function() {

		    var cal_control_id = $("table[id^='ctl00_BodyIncludes1_SPWebPartManager1_']:eq(1)").attr('id');
		    var cal_table = $('table.#' + cal_control_id);

		    $("td[onmouseover*='OverMouseOnMonthView'] div").removeAttr("style"); 	// eliminates height restriction on multi-line items in the bamboo calendar

		    var audience_dropdown = $('#_invisibleIfEmpty select');
		    $('option:first', audience_dropdown).remove();
		    $("#audiences").append(audience_dropdown); 					// moves the audience dropdown to the right column

		    var gheader = $('tr.ms-WPHeader:eq(1)');

		    var title = $("div b", gheader);
		    title.attr("class", "month");

		    var header = $('tr.ms-WPHeader:first');
		    header.addClass('header');

		    $("div p", header).css("margin-top", "20px");
		    $("div p", header).parent().append(title);
	
		    $("table.ms-toolbar:eq(1)").css("display", "none");

		    $("#_invisibleIfEmpty span:contains('Select View:')").parent().css("display", "none");

		    cal_table.attr('bordercolor', '#666666')

		    var tr_days = $('tr:first', cal_table);
		    tr_days.addClass('month-day-header');
		    $('td', tr_days).css({ 'border-bottom': '1px solid #666666', 'border-right': '1px solid #666666' });
		    $('td span', tr_days).wrap('<div></div>');


		    var txt_next="Next";
		    var txt_prev="Previous";
		    $('span[class^="CalendarPlus-Text-Day"]', cal_table).addClass('month-day-link');
		    $('a', header).addClass('header-link');
		    $('a:contains(">")', header).text(txt_next);
		    $('a:contains("<")', header).text(txt_prev);

		    var setcontent = function() {
		        var filters = $('#filtereventtypes input:checkbox:gt(0)'); 					// get all filter checkboxes excluding the All option
		        var events = $('a[onmouseover*="BambooShowRollTip"]'); 						// get the anchors that have the Roll tip mouseover event

		        //console.log(filters);
		        //console.log(events);

		        events.each(																	// iterate each matching event
					function() {
					    var tooltip = this.attributes.getNamedItem("onmouseover").value; 	// current value of the tooltip
					    var cats = 0; 														// counter to keep track of how many event types are assigned to an event

					    filters.each(															// iterate the filters for each event
							function() {
							    if (tooltip.indexOf($(this).attr('name')) != -1)				// if we find the filter as part of the onmouseover attribute, alter the cats appropriatly
							    {
							        cats += ($(this).attr('checked')) ? 1 : 0; 				// if the filter is checked, add one, otherwise, do nothing ( if at least one checked then display)
							    }
							}
						);

					    //console.log(tooltip);
					    //console.log(cats);

					    var contain = $(this).closest("table");

					    if (cats <= 0)				// if the net number of types is less than 0
					        contain.hide(); 		// the event has all of its event types unchecked, hide it
					    else
					        contain.show(); 		// the event has some events that are still checked, show it

					}
				);
		    }

		    $('.events-filters #allfilters').click(
				function() {
				    var allChecked = this.checked;
				    $('.events-filters input:checkbox').not('#allfilters').each(function() { this.checked = allChecked; });

				    setcontent();
				    return true;
				}
			);

		    $('.events-filters input:checkbox').not('#allfilters').click(
				function() {
				    if (!this.checked)
				        $('#allfilters:checked').removeAttr("checked");
				    setcontent();
				    return true;
				}
			);

		    $('input.event-filter').click(setcontent);


		    var isdayview = ($('nobr:contains("8:00")').length > 0);
		    var vday = $.getQueryString({ ID: 'day', DefaultValue: '' })

		    //alert(isdayview);

		    //alert($('div#ctl00_BodyIncludes1_SPWebPartManager1_g_5a1bc402_889f_41de_be95_a771afd54650').length);
		    //alert(x.length);
		    //alert(x.attr('id'));

		    if (!isdayview
					&& vday != ''									// make sure there is a day parameter
					&& window.location.href != document.referrer	// make sure this is not a postback
				) {

		        var gex = /g\_[0-9a-fA-F]{8}\_[0-9a-fA-F]{4}\_[0-9a-fA-F]{4}\_[0-9a-fA-F]{4}\_[0-9a-fA-F]{12}/
		        var g = gex.exec(cal_control_id);
		        var cal_postback_id = cal_control_id.replace(gex, '*').replace(/\_/g, '$').replace(/\*/g, g);
		        //alert(cid);
		        //ctl00$BodyIncludes1$SPWebPartManager1$g_5a1bc402$889f$41de$be95$a771afd54650$BambooCalendarControl
		        //ctl00$BodyIncludes1$SPWebPartManager1$g_5a1bc402_889f_41de_be95_a771afd54650$BambooCalendarControl
		        //console.log(cid);
		        __doPostBack(cal_postback_id, "OnSelectedViewChange," + unescape(vday) + ",6")
		    }

		    // filter event types based on querystring
		    var eventTypes = $.getQueryString({ ID: 'eventtype', DefaultValue: '' });

		    // if eventtype is specified
		    if (eventTypes.length > 0) {
		        // get all filters
		        var filters = $('#filtereventtypes input:checkbox:gt(0)');

		        // uncheck the all filter
		        $('.events-filters #allfilters').attr('checked', 0);

		        // for each filter, check whether event type was specified	        
		        filters.each(
		            function() {
		                if (eventTypes.indexOf($(this).attr('name')) != -1)				// if we find the filter as part of the onmouseover attribute, alter the cats appropriatly
		                {
		                    $(this).attr('checked', 1);
		                }
		                else {
		                    $(this).attr('checked', 0);
		                }
		            }
		        );

		        // update events
		        setcontent();
		    }

		    // add ControlMode=Display to all event links to resolve a bug on production environment
		    var events = $('a[onmouseover*="BambooShowRollTip"]'); 						// get the anchors that have the Roll tip mouseover event

		    events.each(
                function() {
                    var href = this.attributes.getNamedItem("href").value; 	// current value of the hyperlink
                    href += "&ControlMode=Display";
                    $(this).attr("href", href);
                }
            );

		}
	);
})();