var path = "";
var cityCode = "";
var cityName = "";
var sstFriend=false;
var popUpCal = {
    selectedMonth: new Date().getMonth(),   // 0-11
    selectedYear: new Date().getFullYear(), // 4-digit year
    selectedDay: new Date().getDate(),
    today : new Date().getDate(),
    thisMonth : new Date().getMonth(),
    thisYear : new Date().getFullYear(),
    calendarId: 'calendarDiv',
    inputClass: 'calendarSelectDate',
        init: function () {
        var x = $('.'+popUpCal.inputClass);
        var y = $('#'+popUpCal.calendarId)[0];
        // set the calendar position based on the input position
        $('.'+popUpCal.inputClass).each(function(){
            if(/inline/.test(this.className)){
                setPos(this, y);
                popUpCal.linkTo = window.location.pathname + '?p=';
                $(y).show();
                popUpCal.drawCalendar(this); 
                popUpCal.setupLinks(this);
            }
            else if(/link/.test(this.className)){
                $(this).click(function(){
                    setPos(this, y,true);
                    popUpCal.linkTo = "/event/";
                    if(/location/.test(window.location.pathname)){
                        loc = window.location.pathname.split("/location/")[1].split('/')[0];
                        popUpCal.linkTo = "/location/" + loc + "/";
                    }
                    $(y).show();
                    popUpCal.drawCalendar(this); 
                    popUpCal.setupLinks(this);
                });
            }else{
                $(this).focus(function(){
                    setPos(this, y); 
                    $(y).show();
                    popUpCal.drawCalendar(this); 
                    popUpCal.setupLinks(this);
                })
            }
        })
    },
    
        drawCalendar: function (inputObj) {
        if($(inputObj).attr('rev')){
            var _ = $(inputObj).attr('rev').split('-');
            popUpCal.today = _[2];
            popUpCal.selectedMonth = popUpCal.thisMonth = _[1] - 1;
            popUpCal.selectedYear = popUpCal.thisYear = _[0];
            $(inputObj).attr('rev','');
        }
		var html = '';
		html += '';
		html += '<table cellpadding="0" cellspacing="0" id="linksTable"><tr>';
    	html += '	<td><a id="prevMonth"><< 上月</a></td>';
    	html += '	<td><a id="closeCalender">关闭</a></td>';
		html += '	<td><a id="nextMonth">下月 >></a></td>';
		html += '</tr></table>';
		html += '<table id="calendar" cellpadding="0" cellspacing="0"><tr>';
		html += '<th colspan="7" class="calendarHeader">'+getMonthName(parseInt(popUpCal.selectedMonth))+' '+popUpCal.selectedYear+'</th>';
		html += '</tr><tr class="weekDaysTitleRow">';
        var weekDays = new Array('日','一','二','三','四','五','六');
        for (var j=0; j<weekDays.length; j++) {
			html += '<td>'+weekDays[j]+'</td>';
        }
        var daysInMonth = getDaysInMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
        var startDay = getFirstDayofMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
        var numRows = 0;
        var printDate = 1;
        if (startDay != 7) {
            numRows = Math.ceil(((startDay+1)+(daysInMonth))/7); // calculate the number of rows to generate
        }
		
        // calculate number of days before calendar starts
        if (startDay != 7) {
            var noPrintDays = startDay + 1; 
        } else {
            var noPrintDays = 0; // if sunday print right away	
        }
        
        for (var e=0; e<numRows; e++) {
			html += '<tr class="weekDaysRow">';
            // create calendar days
            for (var f=0; f<7; f++) {
				if ( (printDate == popUpCal.today) 
					 && (popUpCal.selectedYear == popUpCal.thisYear) 
					 && (popUpCal.selectedMonth == popUpCal.thisMonth) 
					 && (noPrintDays == 0)) {
					html += '<td id="today">';
				} else {
                	html += '<td class="weekDaysCell">';
				}
                if (noPrintDays == 0) {
					if (printDate <= daysInMonth) {
						html += '<a>'+printDate+'</a>';
					}
                    printDate++;
                }
                html += '</td>';
                if(noPrintDays > 0) noPrintDays--;
            }
            html += '</tr>';
        }
		html += '</table>';
        
        // add calendar to element to calendar Div
        var calendarDiv = $('#'+popUpCal.calendarId)[0];
        calendarDiv.innerHTML = html;
       
        $('#prevMonth').click(function(){
            popUpCal.selectedMonth--;
            if(popUpCal.selectedMonth < 0) {
                popUpCal.selectedMonth = 11;
                popUpCal.selectedYear--;
            }
            popUpCal.drawCalendar(inputObj); 
            popUpCal.setupLinks(inputObj);
        });
        $('#nextMonth').click(function(){
            popUpCal.selectedMonth++;
            if (popUpCal.selectedMonth > 11) {
                popUpCal.selectedMonth = 0;
                popUpCal.selectedYear++;
            }
            popUpCal.drawCalendar(inputObj); 
            popUpCal.setupLinks(inputObj);
        });
        $('#closeCalender').click(function(){
        	document.getElementById(popUpCal.calendarId).style.display = 'none';
        });
    },
    
    setupLinks: function (inputObj) {
        var x = $('a','#calendar');
        for (var i=0; i<x.length; i++) {
            x[i].onclick = function () {
                popUpCal.selectedDay = this.innerHTML;
                if(popUpCal.linkTo == undefined){
                    document.getElementById(popUpCal.calendarId).style.display = 'none';
                    var sv = formatCalendarDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);
                    document.getElementById("selCalendarDiv").innerHTML=sv+"&nbsp;&nbsp;的活动";
                    getPageData(1,"&mth=time&selCalendar=true&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType=day&beginTime="+sv+"&endTime="+sv+"&sstFriend="+sstFriend);
                }else{
                    if(/event|location/.test(popUpCal.linkTo)){
                        location.href = popUpCal.linkTo+(compactDate2(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear)+js_event_type);
                    }else{
                        location.href = popUpCal.linkTo+(compactDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear));
                    }
                }

            }
        }
    }
    
}

/************************************************/

//截取时间字段
function formatActivityDates(str,stt,timeType){

	if(timeType == "over")
	{
		return "活动已过期";
	}
	else
	{
		var eDate=str.split("-");
		var eDa=new Date(Number(eDate[0]),Number(eDate[1]-1),Number(eDate[2].substring(0,2)),0,0,0);
		var newsDay = new Date();
		
		if(eDa <= newsDay)
		{
			return "活动进行中";
		}
		else
		{
			var year=str.substring(0,4);
			var month=str.substring(5,7);
			if(month.substring(0,1)==0)month=month.substring(1,2);
			var day=str.substring(8,10);
			if(day.substring(0,1)==0)day=day.substring(1,2);
			
			var showStr=month+"月"+day+"日&nbsp;&nbsp;"+parseToXingqi(month+"/"+day+"/"+year);
			if(str.substring(0,10)==stt.substring(0,10))
			{
				var strTime=str.substring(11,16);
				if(strTime.substring(0,1)==0)strTime=strTime.substring(1,6);
				var sttTime=stt.substring(11,16);
				if(sttTime.substring(0,1)==0)sttTime=sttTime.substring(1,6);
				showStr+="&nbsp;&nbsp;"+strTime+"-"+sttTime;
			}
  			return showStr;
  		}
 		}
 	}



function parseToXingqi(str)
{
	var val=Date.parse(str);
	var day=new Date(val);
	var today=new Array("周日","周一","周二","周三","周四","周五","周六");  
	return today[day.getDay()];
}


function queryTime(showType){
	var timeType = showType;
	if(timeType == "all")
	{
		url=""+path+"/activityIndex.html?cityCode="+cityCode+"&cityName="+encodeURI(cityName);
		getPageData(1,"&cityCode="+cityCode+"&cityName="+encodeURI(cityName));
	}
	else
	{
		url=""+path+"/activityIndex.html?mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+timeType;
		getPageData(1,"&mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+timeType);
	}
}


//根据活动标题关键字搜索
function queryActivity(titleValue){
	var title = document.getElementById("activityTitle").value;
	if(titleValue != null){
		title = titleValue;
		document.getElementById("activityTitle").value = title;
		document.getElementById("activityTitle").style.color='black';
	}
	if(title == "" || title == "请输入搜索文章标题...")
	{
		getScreenByStatus("jsAddScreenInfo","info","","","","","");
		showScreen("","ScreenInForss","promptBoxInForss",null,260,20);
		document.getElementById("p_content").innerHTML="请输入活动标题关键字进行搜索！";
	}
	else
	{
		
		getPageData(1,"&mth=title&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&typeContent="+encodeURI(jsTrim(title)));
	}
}

 	
//根据活动举办城市查询
function queryByCity(showType,sessionUser)
{
	if(sessionUser != null && sessionUser != "")
	{
		var city = showType;
		getPageData(1,"&mth=city&typeContent="+encodeURI(city));
	}
	else
	{
		getScreenByStatus("jsAddScreenNologin","noLogin","","","","","");
		showScreen("ifrNoLogins","ScreenNoLogins","waitBoxNoLogins",'','','');
	}
}
//根据活动类型查询
	function queryByType(showType)
	{
		var type = showType;
		if(type == "all")
		{
			getPageData(1,"&cityCode="+cityCode+"&cityName="+encodeURI(cityName));
		}
		else
		{
			getPageData(1,"&mth=type&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&typeContent="+encodeURI(type));
		}
	}

//根绝活动发布者查询
	function queryByCreateUser(showType,sessionUser){
		var createUser = showType;
	   if(createUser == "all")
	   {
			getPageData(1,"&cityCode="+cityCode+"&cityName="+encodeURI(cityName));
	   }
		else
		{
			if(sessionUser != null && sessionUser != "")
			{
				getPageData(1,"&mth=createUser&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&createUserType="+createUser);
			}
			else
			{
				getScreenByStatus("jsAddScreenNologin","noLogin","","","","","");
				showScreen("ifrNoLogins","ScreenNoLogins","waitBoxNoLogins",'','','');
			}
		}
	}
 

function fillstring(str)
 {
     if(str.length==1)
    {
         str = "0" + str; 
     }
     return(str);
 }
 
 //根据时间查询
    function quickseldate(showType)
    {
        var begintime,endtime;
        var oneminute = 60*1000;
        var onehour   = 60*oneminute;
        var oneday    = 24*onehour;
        var oneweek   = 7*oneday;
        
        var todayDate = new Date();
        var date = todayDate.getDate();
        var month= todayDate.getMonth() +1;
        var year= todayDate.getYear();
        year = (year<1900?(1900+year):year); 
        var day = todayDate.getDay();
        var minute =todayDate.getMinutes();
		var second = todayDate.getSeconds();
        
        var type = showType;

        //-->
                    
        if(type=="day")
        {
            begintime = year.toString() + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            endtime = begintime;
           getPageData(1,"&mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+type+"&beginTime="+begintime+"&endTime="+endtime);
        }
        else if(type=="week")
        {
            var daytoMon = day-1;
            if(day==0) 
                daytoMon = 6;
            
            todayDate.setTime(todayDate.getTime()-daytoMon*oneday);
            date = todayDate.getDate();
            month= todayDate.getMonth() +1;
            year= todayDate.getYear();
            year = (year<1900?(1900+year):year); 
            day = todayDate.getDay();
            
            begintime = year + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            todayDate.setTime(todayDate.getTime()+6*oneday);
            
            date = todayDate.getDate();
            month= todayDate.getMonth() +1;
            year= todayDate.getYear();
            year = (year<1900?(1900+year):year);
            endtime = year + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            
           // window.location.href="<%=path%>/activityIndex.html?mth=time&cityCode=${cityCode}&cityName=${cityName}&timeType="+type+"&beginTime="+begintime+"&endTime="+endtime;
            getPageData(1,"&mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+type+"&beginTime="+begintime+"&endTime="+endtime);
        }
        else if(type=="month")
        {
            var dateto1 = date-1;
            
            todayDate.setTime(todayDate.getTime()-dateto1*oneday);
            date = todayDate.getDate();
            month= todayDate.getMonth() +1;
            year= todayDate.getYear();
            year = (year<1900?(1900+year):year); 
            day = todayDate.getDay();
            
            begintime = year + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            
            todayDate.setMonth(month);
            todayDate.setTime(todayDate.getTime()-oneday);
            
            date = todayDate.getDate();
            month= todayDate.getMonth() +1;
            year= todayDate.getYear();
            year = (year<1900?(1900+year):year); 
            endtime = year + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            
             getPageData(1,"&mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+type+"&beginTime="+begintime+"&endTime="+endtime);

        }else  if(type=="all"){
			window.location.href=""+path+"/activityIndex.html?cityCode="+cityCode+"&cityName="+encodeURI(cityName);
		}else  if(type=="nextMonth"){
			var dateto1 = date-1;
            
            todayDate.setTime(todayDate.getTime()-dateto1*oneday);
            date = todayDate.getDate();
            month= todayDate.getMonth() +2;
            year= todayDate.getYear();
            year = (year<1900?(1900+year):year); 
            day = todayDate.getDay();
            
            begintime = year + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            
            todayDate.setMonth(month);
            todayDate.setTime(todayDate.getTime()-oneday);
            
            date = todayDate.getDate();
            month= todayDate.getMonth() +1;
            year= todayDate.getYear();
            year = (year<1900?(1900+year):year); 
            endtime = year + "-" + fillstring(month.toString()) + "-" + fillstring(date.toString());
            getPageData(1,"&mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+type+"&beginTime="+begintime+"&endTime="+endtime);
		}else{
			getPageData(1,"&mth=time&cityCode="+cityCode+"&cityName="+encodeURI(cityName)+"&timeType="+type);
		}
	}

function getPageData(pageNo,params,status,titleValue,cId,sUser,cityCode,cityName)
{
	path = document.getElementById("path").value;
	cityCode = document.getElementById("cityCodeJs").value;
	cityName = document.getElementById("cityNameJs").value;
	var statusQueryVal = document.getElementById("statusQueryVal").value;
	if(status=='plazaIndex')
	{
		queryByCreateUser('friend',sUser);
		
	}else if(status=='plazaA'){
		queryActivity(titleValue);
		changeClassFun(1);
	}	
	else if(status=='otherActivity'){
		cityCode=cityCode;
		cityName=cityName;
		queryActivity(titleValue);
		
		changeClassFun(1);
	}
	else if(status=='myCityActivity')
	{
		queryByCity(cId,sUser);
		//changeClassFun(21);
	}
	else if(status=='myCityTodayActivity')
	{
		cityCode=cityCode;
		cityName=cityName;
		quickseldate('day');
		changeClassFun(2);
	}
	else
	{
		if(statusQueryVal=="plazaIndex")sstFriend=true;
		XMLHttp.sendReq('post', ''+path+'/ajax/activityListAjax.html?cityCode='+cityCode+'&cityName='+encodeURI(cityName)+'&pageNo='+pageNo+params+"&sstFriend="+sstFriend, '', showActivityList);
		document.getElementById("activityListShowSpan").innerHTML="<div style='margin-left:30px;float:left;margin-top:20px;'><img src='"+path+"/common/images/loading_animation_liferay.gif'/></div><div style='margin-left:5px;float:left;margin-top:20px;'>数据载入中，请稍后...</div>";
		document.getElementById("activityPageShowSpan").innerHTML="";
	}
}
	function showActivityList(http_request){
	   if(http_request.responseText!="")
	   {
			var results=http_request.responseXML;
			var roots=results.documentElement;
			var change =roots.getAttribute("ajaxMessage");
			var page =roots.getAttribute("page");
			var pageContent=roots.getAttribute("pageContent");
			document.getElementById("activityListShowSpan").innerHTML=pageContent;
			document.getElementById("activityPageShowSpan").innerHTML=page;
		}
	} 
	
	function changeClassFun(num)
	{
		for(var i=1;i<20;i++)
		{
			document.getElementById("changeClass"+i).className="";
		}
		document.getElementById("selCalendarDiv").innerHTML="";
		document.getElementById("changeClass"+num).className="activityNavOn";
	}
	
	//按钮回车执行
	function searchActivityToSubmit(e)
	{
		var k = window.event?window.event.keyCode:e.which;
		if(k==13)
	 	{
	   		queryActivity();
	 	}
	}