<?php
if(!function_exists('get_my_event_calendar'))
{
function get_my_event_calendar()
{
	$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	
	if (!isset($_REQUEST["mnth"])) $_REQUEST["mnth"] = date("n");
	if (!isset($_REQUEST["yr"])) $_REQUEST["yr"] = date("Y");
	
	$cMonth = $_REQUEST["mnth"];
	$cYear = $_REQUEST["yr"];
	
	$prev_year = $cYear;
	$next_year = $cYear;
	$prev_month = $cMonth-1;
	$next_month = $cMonth+1;
	
	if ($prev_month == 0 ) {
		$prev_month = 12;
		$prev_year = $cYear - 1;
	}
	if ($next_month == 13 ) {
		$next_month = 1;
		$next_year = $cYear + 1;
	}
	$mainlink = $_SERVER['REQUEST_URI'];
	if(strstr($_SERVER['REQUEST_URI'],'?mnth') && strstr($_SERVER['REQUEST_URI'],'&yr'))
	{
		$replacestr = "?mnth=".$_REQUEST['mnth'].'&yr='.$_REQUEST['yr'];
		$mainlink = str_replace($replacestr,'',$mainlink);
	}elseif(strstr($_SERVER['REQUEST_URI'],'&mnth') && strstr($_SERVER['REQUEST_URI'],'&yr'))
	{
		$replacestr = "&mnth=".$_REQUEST['mnth'].'&yr='.$_REQUEST['yr'];
		$mainlink = str_replace($replacestr,'',$mainlink);
	}
	if(strstr($_SERVER['REQUEST_URI'],'?') && (!strstr($_SERVER['REQUEST_URI'],'?mnth')))
	{
		$pre_link = $mainlink."&mnth=". $prev_month . "&yr=" . $prev_year."#event_cal";
		$next_link = $mainlink."&mnth=". $next_month . "&yr=" . $next_year."#event_cal";
	}else
	{
		$pre_link = $mainlink."?mnth=". $prev_month . "&yr=" . $prev_year."#event_cal";	
		$next_link = $mainlink."?mnth=". $next_month . "&yr=" . $next_year."#event_cal";
	}
	?> 
    <div class="calendar_widget" id="event_cal">
    <table width="100%" class="calendar">
	<tr align="center">
	<td> 
    
    		<table width="100%">
            	<tr align="center" class="title">
    <td width="10%" class="title"> <a href="<?php echo $pre_link; ?>" > <img src="<?php bloginfo('template_directory'); ?>/library/calendar/previous.png" alt=""  /></a></td>
	<td   class="title"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></td>
    <td width="10%" class="title"><a href="<?php echo $next_link; ?>" >  <img src="<?php bloginfo('template_directory'); ?>/library/calendar/next.png" alt=""  /></a> </td>
	</tr>
            </table>
    
     </td>
	</tr>
	<tr>
	<td align="center">
	<table width="100%" border="0" cellpadding="2" cellspacing="2"  class="calendar_widget">
	
	<tr>
	<td  class="days" >S</td>
	<td class="days" >M</td>
	<td  class="days" >T</td>
	<td class="days" >W</td>
	<td class="days" >T</td>
	<td class="days" >F</td>
	<td class="days" >S</td>
	</tr> 
	<?php
	$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
	$maxday = date("t",$timestamp);
	$thismonth = getdate ($timestamp);
	$startday = $thismonth['wday'];
	
	if($_GET['m'])
	{
		$m = $_GET['m'];	
		$py=substr($m,0,4);
		$pm=substr($m,4,2);
		$pd=substr($m,6,2);
		$monthstdate = "$cYear-$cMonth-01";
		$monthenddate = "$cYear-$cMonth-$maxday";
	}
	global $wpdb;
	for ($i=0; $i<($maxday+$startday); $i++) {
		if(($i % 7) == 0 ) echo "<tr>\n";
		if($i < $startday){
			echo "<td class='date_n'></td>\n";
		}
		else 
		{
			$cal_date = $i - $startday + 1;
			$calday = $cal_date;
			if(strlen($cal_date)==1)
			{
				$calday="0".$cal_date;
			}
			$the_cal_date = $cal_date;
			$cMonth_date = $cMonth;
			if(strlen($the_cal_date)==1){$the_cal_date = '0'.$the_cal_date;}
			if(strlen($cMonth_date)==1){$cMonth_date = '0'.$cMonth_date;}
			
			$urlddate = "$cYear$cMonth_date$calday";
			$thelink = get_option('siteurl')."/?s=Calender-Event&amp;m=$urlddate";
			
			$todaydate = "$cYear-$cMonth_date-$the_cal_date";
			$event_of_month_sql = "select p.* from $wpdb->posts p where p.post_status in ('publish','private') and p.post_type in ('".CUSTOM_POST_TYPE1."') and p.ID in (select pm.post_id from $wpdb->postmeta pm where pm.meta_key like 'st_date' and pm.meta_value <= \"$todaydate\" and pm.post_id in ((select pm.post_id from $wpdb->postmeta pm where pm.meta_key like 'end_date' and pm.meta_value>=\"$todaydate\"))) order by (select pm.meta_value from $wpdb->postmeta pm where pm.meta_key like 'st_date' and pm.post_id=p.ID) asc";
			$event_post_info_perday = $wpdb->get_results($event_of_month_sql);
			$post_info = '';
				if($event_post_info_perday)
				{
					$post_info .='<span class="popup_event">';
					foreach($event_post_info_perday as $event_post_info_obj)
					{
						$post_info .=' 
						   <a class="event_title" href="'.get_permalink($event_post_info_obj->ID).'">'.$event_post_info_obj->post_title.'</a><small>'.
						  __('<b>Location : </b>').get_post_meta($event_post_info_obj->ID,'address',true) .'<br>'.
						   __('<b>Start Date : </b>').get_formated_date(get_post_meta($event_post_info_obj->ID,'st_date',true)).' '.get_formated_time(get_post_meta($event_post_info_obj->ID,'st_time',true)) .'<br />'. 
						   __('<b>End Date : </b>').get_formated_date(get_post_meta($event_post_info_obj->ID,'end_date',true)).' '.get_formated_time(get_post_meta($event_post_info_obj->ID,'end_time',true)) .'</small>';
					}
					$post_info .='</span>';
				}
				echo "<td class='date_n' >";
				if($event_post_info_perday)
				{
					echo "<div><a class=\"event_highlight\" href=\"$thelink\">". ($cal_date) . "</a>".$post_info;
				}else
				{
						echo "<span class=\"no_event\" >". ($cal_date) . "</span>";
				}
				echo "</div></td>\n";
		}
		if(($i % 7) == 6 ) echo "</tr>\n";
	}
	?></tr>
	</table>
	</td>
	</tr>
	</table>
    </div>
  
<?php }}?>