$(document).ready(function(){
	//*********************************DOM formed complite*********************************************************************
	
	var now   = new Date();
	var month = now.getMonth();
	var year  = now.getFullYear();
 	
	//***get from address month and year for remember date on calendar****
	var tmp = new Array();      
	var tmp2 = new Array();     
	 param = new Array();
	 
	var get = location.search;  
	if(get != '') {
		tmp = (get.substr(1)).split('&');   
		    for(var i=0; i < tmp.length; i++) {
		    	tmp2 = tmp[i].split('=');       
		        param[tmp2[0]] = tmp2[1];       
		   }
		    
		    for (var key in param) {
		    	
		    	if (key == 'month'){
		    		
		    		month = param[key] - 1;
		    		
		    	}
		    	if (key == 'year'){
		    		
		    		year  = param[key];
		    		
		    	}
		       
		    }
	}
	//************************************************************************
	events_month_array = new Array;
	
	//***check href: if query from admin or guardian area then use ../ because needs use relative path
	var location_url = window.location.href;
	if (location_url.indexOf("/admin/") == -1 && location_url.indexOf("/guardian/") == -1){
		var url = 'ajax/get_event_month_ajax.php';
	}else{
		var url = '../ajax/get_event_month_ajax.php';
	}
	//***end check
	
        $.get(
                url,
                "month=" + month + "&year=" + year,
                function (result) {
                	
                	if (result.type == 'error') {
                		alert('error');
                		return(false);
                		
                	} else {
                		 
                		var numbers_of_rows = 0;

						//if have rows then for each rows create array where save id event, day event, event type	 
	                        $(result.events_array).each(function() {
		                        
	                            events_month_array[numbers_of_rows] = new Array;

	                            events_month_array[numbers_of_rows][0] = $(this).attr('day');

	                            events_month_array[numbers_of_rows][1] = $(this).attr('type');
	                            
		                        numbers_of_rows = numbers_of_rows + 1;
	
	                        });
	                        
                        $('#calendar').calendarWidget({month: month,year: year});
                        events_month_array = new Array;
                        
                      
                	}
                		
                	
                },
                "json"
                 
        );
	//***************************************************************************************************************************


    //**********************************Click on the next month******************************************************************8
     $('#next').click(function(){

    	//***check href: if query from admin or guardian area then use ../ because needs use relative path
    		var location_url = window.location.href;
    		if (location_url.indexOf("/admin/") == -1 && location_url.indexOf("/guardian/") == -1){
    			var url = 'ajax/get_event_month_ajax.php';
    		}else{
    			var url = '../ajax/get_event_month_ajax.php';
    		}
    	//***end check
		
		if (month == 11){

			month = 0;
			year++;
			
		}else{

			month++;

		}
		
        $.get(
                url,
                "month=" + month + "&year=" + year,
                function (result) {
                	
                	if (result.type == 'error') {
                		alert('error');
                		return(false);
                		
                	} else {
                    	
                		var numbers_of_rows = 0;

						//if have rows then for each rows create array where save id event, day event, event type	 
	                        $(result.events_array).each(function() {
		                        
	                            events_month_array[numbers_of_rows] = new Array;

	                            events_month_array[numbers_of_rows][0] = $(this).attr('day');

	                            events_month_array[numbers_of_rows][1] = $(this).attr('type');
	                            
		                        numbers_of_rows = numbers_of_rows + 1;
	
	                        });
	                    
                        $('#calendar').calendarWidget({month: month,year: year});
                        events_month_array = new Array;
                        
                	}
                		
                	
                },
                "json"
                 
        );
		
	});
	//**************************************************************************************************************************8

    //*******************************click prev month***************************************************************************8
     $('#prev').click(function(){

    	//***check href: if query from admin or guardian area then use ../ because needs use relative path
    		var location_url = window.location.href;
    		if (location_url.indexOf("/admin/") == -1 && location_url.indexOf("/guardian/") == -1){
    			var url = 'ajax/get_event_month_ajax.php';
    		}else{
    			var url = '../ajax/get_event_month_ajax.php';
    		}
    	//***end check

  		if (month == 0){

  			month = 11;
  			year--;
  			
  		}else{

  			month--;
			
  		}
  		
          $.get(
                  url,
                  "month=" + month + "&year=" + year,
                  function (result) {
                  	
                  	if (result.type == 'error') {
                  		alert('error');
                  		return(false);
                  		
                  	} else {

                		var numbers_of_rows = 0;

						//if have rows then for each rows create array where save id event, day event, event type	 
	                        $(result.events_array).each(function() {
		                        
	                            events_month_array[numbers_of_rows] = new Array;

	                            events_month_array[numbers_of_rows][0] = $(this).attr('day');

	                            events_month_array[numbers_of_rows][1] = $(this).attr('type');
	                            
		                        numbers_of_rows = numbers_of_rows + 1;
	
	                        });
	                    
                        $('#calendar').calendarWidget({month: month,year: year});
                        events_month_array = new Array;
                        
                  	}
                  		
                  	
                  },
                  "json"
                   
          );
  		
  	});
     //*******************************************************************************************************************************************8
     function getNameBrowser() {
    	 var ua = navigator.userAgent.toLowerCase();
    	 // Определим Internet Explorer
    	 if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
    	   return "msie";
    	 }
     }
});
