$(document).ready(function() {
    loadcalendaractivity();
});

function loadcalendaractivity() {
    
    var currentPropertyType = $('#currenttype').val();
    
    // calendar menu over  start  -----------------
    jQuery("ul#navi_calendar-activity-Tab > li, ul#navi_ap-project-catagory-Tab > li").click(function() {
        
        var menuIndex = $(this).index(); // เช็คตำแหน่งที่เท่าไหร่
        currentPropertyType = $('#currenttype').val();
        
        jQuery("#ajax-jquery-activity").html("");
        jQuery("ul#detail_calendar-activity-Tab > li:visible").hide();
        jQuery("ul#detail_calendar-activity-Tab > li").eq(menuIndex).fadeIn();  //้เปลี่ยนเนื้อหา

        menuIndex = menuIndex+1;	//เพิ่มไปอีก 1 อันดับ
        //alert(menuIndex);
        // index, ap-society page
        jQuery("#navi_calendar-activity-Tab").removeClass().addClass("menuCalendarClass"+ menuIndex);	 // removeClass ของเก่าออก แล้วเอาตำแหน่งใหม่ใส่เข้าไป
        // property-type page
        if (typeof currentPropertyType != "undefined") {            
            jQuery("#navi_ap-project-catagory-Tab").removeClass().addClass("menuap-project-"+currentPropertyType+"Class"+menuIndex);	 // removeClass ของเก่าออก แล้วเอาตำแหน่งใหม่ใส่เข้าไป
            //menuIndex = 1; //สำหรับหน้า Property Type ที่ tab 2 ต้องแสดงข่าวทั้ง 4 และ 5
        }
        loadCalendar(menuIndex);
    });
    
    if (typeof currentPropertyType != "undefined") {
        $('.tabcalendar-activityNavi2').click();
        $('.tabap-project-catagoryNavi2').click();
    } else {
        $('.tabcalendar-activityNavi1').click();
        $('.tabap-project-catagoryNavi1').click();
    }

}

function loadCalendar(menuIndex)
{
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    var currentPropertyTypeId = $('#currentPropertyTypeId').val();
    
    $('#calendar').html("");

    TypeId = (typeof currentPropertyTypeId=="undefined")? "" : '&type='+currentPropertyTypeId;
    
    $("#popcalendar").mouseenter(function(){   
        
      $(this).stop(true, true).fadeIn(100, function() {$(this).css('opacity','0.98');});
            
    });

    $("#popcalendar").mouseleave(function(){              
      $(this).fadeOut(500);
    });
    
    var vurl = "";
    if (TypeId != "") {
        if (menuIndex == 1) {
            vurl = '/service/ap-society/calendar-activity-event/?event='+menuIndex;
        } else {
            vurl = '/service/ap-society/calendar-activity-event/?event=1'+TypeId;
        }        
    } else {
        vurl = '/service/ap-society/calendar-activity-event/?event='+menuIndex;    
    }
    
    $('#calendar').fullCalendar({

        header: {
            left: '',
            center: 'title',
            right: ''
        },

        editable: false,

        //-events: { url: '/ajax/calendar-activity-event.php?t='+menuIndex+'&cat='+urlGetValue },
        
        events: {url: vurl},

        eventMouseover: function(calEvent, jsEvent, view) {

            //alert(calEvent.no);
                        
            var offset = $(this).children().children().offset(); 
                        
            //alert('Left: '+offset.left+' Top: '+offset.top);
            
                                 
            $("#popcalendar").show();
            $("#popcalendar").stop(true, true).fadeIn().css('opacity','0.75');
            $("#popcalendar").offset({left: offset.left,top: offset.top+10});   
            //$("#popcalendar").css('opacity','0.8')
            
            //$("#popcalendar").fadeIn();                                                                        
            $("#popcalendar").html(calEvent.list);                                
            $('.newslist').click(function(){
                $("#ajax-jquery-activity").html("");
                $.ajax({
                    url : $(this).attr('href'),
                    success :function(data){
                        $("#ajax-jquery-activity").html(data).fadeIn();
                    }
                });
                return false;
            });
            
            
            return false;
        },
        
        eventMouseout: function(calEvent, jsEvent, view) {
            
            $("#popcalendar").fadeOut(500);            

        },

        eventClick: function(calEvent, jsEvent, view) {

            //alert(calEvent.no);
/*                        
            var offset = $(this).children().children().offset(); 
            $("#popcalendar").offset({left: offset.left+10,top: offset.top+10});
            
            //alert('Left: '+offset.left+' Top: '+offset.top);
            
            $("#popcalendar").offset({left: offset.left,top: offset.top+10});                        
            $("#popcalendar").show();
            $("#popcalendar").css('opacity','1')
            $("#popcalendar").stop(true, true); 
            $("#popcalendar").fadeIn();                                                                        
            $("#popcalendar").html(calEvent.no);                                
            $("#popcalendar").fadeOut(1200);                    
                                                         
            $("#popcalendar").mouseenter(function(){              
              $(this).stop(true, true).fadeIn().css('opacity','1');
            });
            
            $("#popcalendar").mouseleave(function(){              
              $(this).fadeOut(1200);
            });
*/                                       
            //jQuery("#ajax-jquery-activity").html("");
/*
            jQuery("#loading-activity").ajaxStart(function(){
                $(this).show();
            }).ajaxStop(function() {
                jQuery (this).hide();
            });
*/
//            jQuery.ajax({
//                url : calEvent.url,
//                success :function(data){
//                    jQuery("#ajax-jquery-activity").html(data).fadeIn();
//                }
//            });

            return false;
        },

        eventAfterRender: function(event, element, view) {

            var events = $("#calendar").fullCalendar("clientEvents");                                  
            
            if (events.length > 0) {

                // load first next event.
                var d = new Date();
                var cdate = dateString(d);

                var e;

                for (var i=0; i < events.length ;i++) {
                  ed = events[i].start;
                  edate = dateString(ed);

                  if (edate >= cdate) {
                    e = events[i];
                    break;
                  }
                }

                if (i == events.length) {
                  e = events[events.length-1];
                }

                jQuery.ajax({
                    url : e.url,
                    success :function(data){
                        jQuery("#ajax-jquery-activity").html(data).fadeIn();
                    }
                });
            }
        }
    });
    
    //$("#calendar").append('<div id="popcalendar" style="border-radius: 4px 4px 4px 4px;display:none;top:0px;left:0px;position:absolute;background-color: red;"></div>');
}

function dateString(date)
{
  var d = date.getDate();
  var m = date.getMonth() + 1;
  var y = date.getFullYear();

  if (d < 10) d = "0" +d;
  if (m < 10) m = "0" +m;

  return y + "-" + m + "-" + d;
}

