(function($) { 
   
	function calendarWidget(el, params) { 
		
	 	var now   = new Date();
		var thismonth = now.getMonth();
		var thisyear  = now.getFullYear();
			
		var now_month = now.getMonth();
		var now_year  = now.getFullYear();
		var now_day = now.getDate();
	 	 
		var opts = {
			month: thismonth,
			year: thisyear
		};
		
		$.extend(opts, params);
		
		var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
		var dayNames = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
		month = i = parseInt(opts.month);
		year = parseInt(opts.year);
	   
		var m = 0;
		var table = '';
		
			// next month
			if (month == 11) {
				var next_month = '<a href="?month=' + 1 + '&amp;year=' + (year + 1) + '" title="' + monthNames[0] + ' ' + (year + 1) + '">' + monthNames[0] + ' ' + (year + 1) + '</a>';
			} else {
				var next_month = '<a href="?month=' + (month + 2) + '&amp;year=' + (year) + '" title="' + monthNames[month + 1] + ' ' + (year) + '">' + monthNames[month + 1] + ' ' + (year) + '</a>';
			}
				
			// previous month
			if (month == 0) {
				var prev_month = '<a href="?month=' + 12 + '&amp;year=' + (year - 1) + '" title="' + monthNames[11] + ' ' + (year - 1) + '">' + monthNames[11] + ' ' + (year - 1) + '</a>';
			} else {
				var prev_month = '<a href="?month=' + (month) + '&amp;year=' + (year) + '" title="' + monthNames[month - 1] + ' ' + (year) + '">' + monthNames[month - 1] + ' ' + (year) + '</a>';
			}		
				
			table += ('<h3 id="current-month">'+monthNames[month]+' '+year+'</h3>');
			// uncomment the following lines if you'd like to display calendar month based on 'month' and 'view' paramaters from the URL
			//table += ('<div class="nav-prev">'+ prev_month +'</div>');
			//table += ('<div class="nav-next">'+ next_month +'</div>');
			table += ('<table class="calendar-month " ' +'id="calendar-month'+i+' " cellspacing="0">');	
		
			table += '<tr>';
			
			for (d=0; d<7; d++) {
				table += '<th class="weekday">' + dayNames[d] + '</th>';
			}
			
			table += '</tr>';
		
			var days = getDaysInMonth(month,year);
            var firstDayDate=new Date(year,month,1);
            var firstDay=firstDayDate.getDay();
			
			var prev_days = getDaysInMonth(month,year);
            var firstDayDate=new Date(year,month,1);
            var firstDay=firstDayDate.getDay();
			
			var prev_m = month == 0 ? 11 : month-1;
			var prev_y = prev_m == 11 ? year - 1 : year;
			var prev_days = getDaysInMonth(prev_m, prev_y);
			firstDay = (firstDay == 0 && firstDayDate) ? 7 : firstDay;
			
			var i = 0;
            for (j=0;j<42;j++){
			  
              if ((j<firstDay)){
                table += ('<td class="other-month"><span>'+ (prev_days-firstDay+j+1) +'</span></td>');
			  } else if ((j>=firstDay+getDaysInMonth(month,year))) {
				i = i+1;
                table += ('<td class="other-month"><span>'+ i +'</span></td>');			 
              }else{
                table += ('<td ');
               if ((j-firstDay+1) == param['day'] && month == param['month'] - 1){
            	   
            	   table += ('style="border: 2px solid #000;" ');
            	   
               }
               
               if ((j-firstDay+1) == now_day && month == now_month && year == now_year){
            	   table += ('style="color: red;" ');
               }
               
                
                //************check day of the month on the event; game****************
              
	                for ($k = 0; $k < events_month_array.length; $k++){
		               
	                	
	                	
	                	//if in this day have events or games then determine that type of the day
	                	if ((j-firstDay+1) == events_month_array[$k][0]){
	                		
	                		//if day type - event then create links on this day and add class - event_day
	                		if (events_month_array[$k][1] == "event"){
			                    
	                			table += ('class="event_day" ><a ');
	                			if ((j-firstDay+1) == now_day && month == now_month && year == now_year){
	                         	   table += ('style="color: red;" ');
	                            }
	                			table += ('href="' + base_url + 'main_controllers.php?pid=calendar&day=' + (j-firstDay+1) + '&month=' + (month + 1) + '&year=' + year + '">'+(j-firstDay+1)+'</a></td>');
	                			
			
	                		//if day type - games then create links on this day and add class - games_day
	                		}else if (events_month_array[$k][1] == "games"){
	                				
		                			table += ('class="games_day" ><a ');
		                			if ((j-firstDay+1) == now_day && month == now_month && year == now_year){
			                         	   table += ('style="color: red;" ');
			                            }
		                			table += ('href="' + base_url + 'main_controllers.php?pid=calendar&day=' + (j-firstDay+1) + '&month=' + (month + 1) + '&year=' + year + '" >'+(j-firstDay+1)+'</a></td>');
	                				//if day type - multy then create links on this day and add class - multy_day
	                			} else{
	                				
	                				table += ('class="multy_day" ><a ');
	                				if ((j-firstDay+1) == now_day && month == now_month && year == now_year){
	 	                         	   table += ('style="color: red;" ');
	 	                            }
	                				table += ('href="' + base_url + 'main_controllers.php?pid=calendar&day=' + (j-firstDay+1) + '&month=' + (month + 1) + '&year=' + year + '" >'+(j-firstDay+1)+'</a></td>');
	                			}
	                			
	                			break;
	                		
	                	//if day pure from events then not create links on the day 
	                	}else {
		                	
		                	if ($k == (events_month_array.length - 1)){
		                		
		                		table += ('class="day"><span>'+(j-firstDay+1)+'</span></td>');
		                		
		                	}
		                	
		                }
	               
	                }
   	
              //***********************************************************************
                
                
              }
              if (j%7==6)  table += ('</tr>');
            }

            table += ('</table>');

		el.html(table);
	}
	
	function getDaysInMonth(month,year)  {
		var daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];
		if ((month==1)&&(year%4==0)&&((year%100!=0)||(year%400==0))){
		  return 29;
		}else{
		  return daysInMonth[month];
		}
	}
	
	function inArray(str, array)  {
		
		for (var $i = 0; $i < array.length; $i++){
			
			if (str == array[$i]){
				return true;
				break;
			}
		}
		return false;
	}
	
	// jQuery plugin initialisation
	$.fn.calendarWidget = function(params) {    
		calendarWidget(this, params);		
		return this; 
	}; 

})(jQuery);

