<!-- AlpineCalendar.ca Bootstrap Calendar - Rev2 version 1.01   -->
<div class="containerForCalendar" style="background-color:#fff;">
<?php

	require_once "updatePlugin.php"; // script allows for updating of the plugin if required.

	$alpcal_options['cache_folder'] = str_replace("ALPCAL/Storage/", "", plugin_dir_path( __FILE__ )) . 'acCache/';

//	echo "<pre>";
//	print_r($alpcal_options);
//	echo "</pre>";

	
// initialize Draw\Calendar object
/*
$alpcal_options (Array)
(
    [which_cal] => BCA
    [first_day] => MON
    [show_eventTypes] => N
    [cache_length_months] => 300
    [cache_length_events] => 300
    [calendars_additional] => Aoa_xml_aoaevents
    [alpcal_event_template] => something will go here later
)
*/
	try {
		$cal = new ALPCAL\Draw\Calendar($alpcal_options);
		$cal->setYearMonth($_GET['Y'],$_GET['M']); // needs to run to set the variables to retrieve
		$cal->getData(); // retrieves data and puts in in object
		}
	catch (Exception $exc)
		{
		echo "Exception caught: " . $exc->getMessage();
		die("<hr />Please report or correct this error and reload the page once fixed.");
		}

// put the JavaScript and CSS on the page
// -> this is contrary to suggested methods for Wordpress but it solves the problem of the CSS/JS showing up on all other pages
$plugins_url = plugins_url();
echo "
\n	<link rel='stylesheet' id='stylesheet-css'  href='{$plugins_url}/alpineCalendar-bootstrap-cal-cached/css/style.css' type='text/css' media='all' />
\n	<link rel='stylesheet' id='stylesheet-css'  href='{$plugins_url}/alpineCalendar-bootstrap-cal-cached/css/bootstrap-multiselect.css' type='text/css' media='all' />
\n	<script type='text/javascript' src='{$plugins_url}/alpineCalendar-bootstrap-cal-cached/js/main.js'></script>
\n	<script type='text/javascript' src='{$plugins_url}/alpineCalendar-bootstrap-cal-cached/js/bootstrap-multiselect.js'></script>
\n	<script type='text/javascript' src='{$plugins_url}/alpineCalendar-bootstrap-cal-cached/js/jquery.cookie.js'></script>
	 ";


if($_GET['showArray']=="Y")
	{
	echo "<hr/><hr/>START DUMP:<hr/><hr/><pre>"; print_r($cal->drawEvents); print_r($rsTypes); echo "<hr/>:END DUMP<hr/>";
	}
if($_GET['showCal']=="Y")
	{
	echo "<hr/><hr/>START DUMP:<hr/><hr/><pre>"; print_r($cal); echo "<hr/>:END DUMP<hr/>";
	}
if ($_GET['testC'])
	{ 
	echo '<button class="btn btn-circle" id="testC">testC</button><script>$(document).ready(function(e) {    $("#testC").on("click",function()		{		alert($.cookie(\'cal_eTypes\'));		}) }); </script> ';
	} // if ($_GET['testC']) 

$thisM = $cal->workingMonth->format('m');
$thisY = $cal->workingMonth->format('Y');
?>
	
<div class="row">
	<div class="col-md-1">
    </div>
	<div class="col-md-2">
        <label for="selectEventMonth">Quick Change Date:</label>
    </div>
	<div class="col-md-3 form-group">
    	<select class="form-control form-inline quickDateChanger" id="selChangeM">
<?	for($x=1; $x<=12; $x++)
		{
		echo '<option value="'.$x.'"';
		if($x==$thisM)	{	echo " selected";	}
		echo '>'.date('F',strtotime('2015-'.$x.'-1')).'</option>'.PHP_EOL;
		}	?>
        </select>
    </div>
	<div class="col-md-3 form-group">
    	<select class="form-control form-inline quickDateChanger" id="selChangeY">
<?	for($x=2001; $x<=date("Y")+1; $x++)
		{
		echo '<option value="'.$x.'"';
		if($x==$thisY)	{	echo " selected";	}
		echo '>'.$x.'</option>'.PHP_EOL;
		}	?>
        </select>
    </div>
</div>

<?php
if ($alpcal_options['show_eventTypes']=='Y')
	{
?>
<div class="row">
	<div class="col-md-1">
    </div>
	<div class="col-md-2">
        <label for="selectEventTypes">Show Event Types:</label>
    </div>
	<div class="col-md-8">
        <select id="selectEventTypes" multiple="multiple">
    <?	foreach($cal->EventTypes as $eType)
            {
			 $eType = json_decode(json_encode( $eType ), true);
    ?>        <option <?	$showSel = "selected";
							if($_COOKIE['cal_eTypes'])
								{
								$cookExplode = explode(",",$_COOKIE['cal_eTypes']);
								if( !in_array( str_replace(" ","_",$eType['EventTypeName']) , $cookExplode) )
									{
									$showSel = "";
									}
								}
							echo $showSel;	?>
    		class="setSelectItem evtype-<?=str_replace(" ","_",$eType['EventTypeName'])?>"
    		value="<?=str_replace(" ","_",$eType['EventTypeName'])?>"
            ><?=$eType['EventTypeName']?></option>
    <?
            }	?>
        </select>

<style>
<?	foreach($rsTypes as $eType)
		{
		echo '.evtype-'.str_replace(" ","_",$eType['EventTypeName']).' { background-color:#'.$eType['EventTypeCLR'].' }';
		echo "\n";
		}
?>
</style>
		<script type="text/javascript">

			jQuery( document ).ready(function( $ )
				{

				if($.cookie('cal_eTypes'))
					{	filterEventsMS();	}

                $("#selectEventTypes").multiselect(
                    {
                    onChange: function(option, checked, select)
                        {
                        filterEventsMS()
                        }
                    , onSelectAll: function(option, checked, select)
                        {
                        filterEventsMS()
                        }
                    , numberDisplayed: 12
                    , includeSelectAllOption: true
                    });

                });

            function filterEventsMS()
                {
                jQuery(".BCAC-event").fadeOut("fast");
                var theseVals = [];
                jQuery("#selectEventTypes option:selected").each(function()
                    {
                    theseVals.push(jQuery(this).val());
                    });
                jQuery(theseVals).each(function(i, e)
                    {
                    jQuery(".BCAC-Type-"+e).fadeIn("fast");
                    });
				var countPotentials = jQuery("#selectEventTypes option").size();
				var countPotentialsSelected = jQuery("#selectEventTypes option:selected").length;
				if(countPotentials==countPotentialsSelected)
					{
//					alert(countPotentials+ " is " +countPotentialsSelected);
					jQuery.cookie('cal_eTypes', '', {path: '/'});
//					alert('393');
					}
				else{
//					alert(countPotentials+ " else " +countPotentialsSelected);
					jQuery.cookie('cal_eTypes', theseVals, {path: '/'});
//					alert('397: '+theseVals);
					}

                }
        </script>

    </div>
	<div class="col-md-1">
    </div>
</div>
<?
	} //if ($alpcal_options['show_eventTypes']=='Y')
?>

<div class="container-calendar">
	<div class="grid-calendar">
		<div class="row" style="vertical-align:bottom">
        	<div class="col-xs-3 text-left"><h3><small><a href="?M=<?=$cal->workingMonth_Prev->format('n')?>&Y=<?=$cal->workingMonth_Prev->format('Y')?>"><?=$cal->workingMonth_Prev->format('M')?></a></small></h3></div>
        	<div class="col-xs-6 text-center"><h1><?=$cal->workingMonth->format('F Y')?></h1></div>
        	<div class="col-xs-3 text-right"><h3><small><a href="?M=<?=$cal->workingMonth_Next->format('n')?>&Y=<?=$cal->workingMonth_Next->format('Y')?>"><?=$cal->workingMonth_Next->format('M')?></a></small></h3></div>
        </div>
		<div class="row calendar-week-header">
<?
$dowMap = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
foreach ($cal->daysOfWeekSequence as $k => $v)
{
	echo '			<div class="col-xs-1 grid-cell"><div><div><span>'.$dowMap[$v].'</span></div></div></div>';
}
?>
		</div>

<?








$arrayID = 1;
while($cal->drawRangeStart<$cal->workingMonth_Next)
	{
	//  obtain maximum height required for this week
	$thisWeekHeight = 2;
	for($x=1; $x<=7; $x++)
		{
		for($pos = 1; $pos<20; $pos++)
			{
			if($cal->drawEvents[$arrayID+($x-1)][$pos]["len"] || $cal->drawEvents[$arrayID+($x-1)][$pos]["lenLeft"])
				{
				if($pos>$thisWeekHeight)
					{
					$thisWeekHeight=$pos;
					}
				}
			}
		}
	// /obtain maximum height required for this week

	echo '		<div class="row calendar-week BCAC-weekH'.$thisWeekHeight.'">'.PHP_EOL;
	for($x=1; $x<=7; $x++)
		{
		echo '			<div class="col-xs-1 grid-cell';
		if($cal->drawRangeStart->format('m')<$thisM)	{	echo " previous-month";	}
		if($cal->drawRangeStart->format('m')>$thisM)	{	echo " next-month";	}
		if(($cal->todayLiteral->format('m')==$cal->drawRangeStart->format('m'))&&($cal->todayLiteral->format('d')==$cal->drawRangeStart->format('d')))	{	echo " BCAC-today";	}
		echo ' calendar-day"><div><div>'.PHP_EOL;
		echo '            	<div class="grid-calendar-date">';
		echo '                    ';
		echo $cal->drawRangeStart->format('j');
		echo '                </div>'.PHP_EOL;
		echo '                <div class="grid-calendar-events">'.PHP_EOL;

		for($pos = 1; $pos<20; $pos++)
			{

			if	(
				$cal->drawEvents[$arrayID][$pos]["len"]
				||
				($x==1 && $cal->drawEvents[$arrayID][$pos])
				)
				{
				$thisLength = $cal->drawEvents[$arrayID][$pos]["len"];
				if(!$thisLength)	{	$thisLength = $cal->drawEvents[$arrayID][$pos]["lenLeft"];	}
				if($thisLength>(8-$x))
					{
					$thisLength = 8-$x;
					}

				
				if ($cal->drawEvents[$arrayID][$pos]['calPlugin']['whichCal']=='AlpineCalendar_ca') {
					drawEv_AlpineCalendar_ca($cal->drawEvents[$arrayID][$pos],$pos,$thisLength);
				}else{
					drawEv_Other($cal->drawEvents[$arrayID][$pos],$pos,$thisLength);	
				}
				
				

			
				}
			}

		echo '                </div>'.PHP_EOL;
		echo '            </div></div></div>'.PHP_EOL;
		date_add($cal->drawRangeStart,date_interval_create_from_date_string("1 days"));
		$arrayID++;
		}
	echo "		</div>".PHP_EOL;
	}
?>
	</div>
</div>

<!-- Modal for AlpineCalendar.ca Events -->
<div
	class="modal eventModalLiteral fade"
    style="z-index:9999 !important;"
    id="eventModal"
    tabindex="-1"
    role="dialog"
    aria-labelledby="EventInfo"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" id="eventmodal-content">
            <div id="eventModal-header" class="modal-header">
            	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                <h3 id="eventModal-headerH3">Modal header</h3>
            </div>
            <div id="eventModal-body" class="modal-body">
                <p>My modal content here…</p>
            </div>
            <div id="eventModal-footer" class="modal-footer">
                <button class="btn" data-dismiss="modal">Close</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal for AlpineCalendar.ca Events -->
	

<?
foreach ($cal->allEvents as $k=>$ev)
{
	if (!is_object($ev)) { $ev = (object)(array) $ev; } // convert to object 
	if ($ev->calPlugin['whichCal'] != 'AlpineCalendar_ca' ) {
	// OUTPUT MODALS FOR NON-AlpineCalendar.ca Events
	
	// create object to connect to this calendar's draw methods
	$newClass = 'ALPCAL\\Remote\\' . $ev->calPlugin['whichCal'];
	$outputClass = new $newClass();
		
		?>
<!-- Modal for NON-AlpineCalendar.ca Event -->
<div 
	class="modal eventModalLiteral fade"
    style="z-index:9999 !important;"
    tabindex="-1"
    role="dialog"
    aria-labelledby="EventInfo"
	id="evModal-<?=$ev->EvID?>" 
	aria-labelledby="myModalLabel"
    aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel"><?=$ev->EvTi?></h4>
			</div>
			<div class="modal-body">
				<?
				if (method_exists($outputClass,modelBody)) {	$outputClass->modelBody($ev);	}
				?>
			</div>
			<div class="modal-footer">
				<?
				if (method_exists($outputClass,modelFooter)) {	$outputClass->modelFooter($ev);	}
				?>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<!-- /.modal for NON-AlpineCalendar.ca Event -->
	<?
	}
	unset($outputClass);
}
	?>

	
</div><!-- /.container -->

<?php
function drawEv_AlpineCalendar_ca($ev,$pos,$len)
{
	echo '                	<a href="#'.$ev['EvID'].'">';
	$eTypes = explode(",",$ev['eventTypes']);
//				$eTypePrimary = explode("~",$eTypes[0]);
	$eTypePrimary = explode("~",$ev['EventType_Primary']);

	if($ev['Location'])
		{	$titleLocation = $ev['Location']; }
	else{	$titleLocation = $ev['LocationAlt']; }

	echo '<div class="BCAC-event BCAC-AC-event BCAC-eventDepth'.($pos-1).' BCAC-eventd'.$len.'';
	foreach($eTypes as $thisType)
		{
		$type = explode("~",$thisType);
		echo ' BCAC-Type-'.str_replace(" ","_",$type[0]).'';
		}
	echo '"';
	echo ' style="background-color:#'.$eTypePrimary[1].' !important;"';
	echo ' data-title="<strong>'.$titleLocation.'</strong>: '.$ev['Title'].'"';
	echo ' data-eid="'.$ev['EvID'].'"';
	echo ' id="eventClickID'.$ev['EvID'].'"';
	echo ' title="'.$ev['Location'].': '.$ev['Title'].' - '.$ev['Overview'].'"';
	echo ' >';
	echo $ev['EvTi'];
	echo '</div></a>'.PHP_EOL;

}

function drawEv_Other($ev,$pos,$len)
{ 
	echo '                	<a href="#'.$ev['EvID'].'">';
	$eTypes = explode(",",$ev['eventTypes']);
//				$eTypePrimary = explode("~",$eTypes[0]);
	$eTypePrimary = explode("~",$ev['EventType_Primary']);

	if($ev['Location'])
		{	$titleLocation = $ev['Location']; }
	else{	$titleLocation = $ev['LocationAlt']; }

	echo '<div class="BCAC-event BCAC-eventDepth'.($pos-1).' BCAC-eventd'.$len.'';
	foreach($eTypes as $thisType)
		{
		$type = explode("~",$thisType);
		echo ' BCAC-Type-'.str_replace(" ","_",$type[0]).'';
		}
	echo '"';
	echo ' style="background-color:#'.$eTypePrimary[1].' !important;"';
	echo ' data-title="'.$ev['EvTi'].'"';
	echo ' data-eid="'.$ev['EvID'].'"';
	echo ' id="eventClickID'.$ev['EvID'].'"';
	echo ' title="'.$ev['Location'].': '.$ev['Title'].' - '.$ev['Overview'].'"';
	echo ' data-toggle="modal" data-target="#evModal-'.$ev['EvID'].'"';
	echo ' >';
	echo $ev['EvTi'];
	echo '</div></a>'.PHP_EOL;
	
}

if ($_GET['showCalFoot']) {
	echo "<pre>";
	print_r($cal);
	echo "</pre>";
	}