UI.Calender = function(year,month){
	var d=new Date();
	if(year) d.setFullYear(year);
	if(month) d.setMonth(month-1);
	this.day=d;
	this.day_sweek=-1;
	this.ymd=this.getYMD(d);
	this.today=new Date();
	this.today_ymd=this.getYMD(this.today);
	this.selday=null;
	this.selday_ymd="";
	this.selbox=null;
	this.is_draw=0;
	this.is_show=0;
	this.show_el=null;
	this.pid = "UI_Calender"+String(Math.random()).substring(2,6);
};

UI.Calender.prototype={
	setBox:function(num,ymd,type){ //type 1:prev,2:next,0:now
		var box=UI.$(this.pid+"_"+num);
		box.ymd=ymd;
					
		if(type==1) box.className="UICalender_box_prev";
		else if(type==2) box.className="UICalender_box_next";
		else 
		{
			box.className="UICalender_box";			
			if(ymd==this.today_ymd) box.className="UICalender_box_today";
		}
		box.innerHTML=ymd.substring(6,8);
		var self=this;
		if(this.onClick) box.onclick=function(){ self.onClick(box) };
	},
	onClick:function(box){
	  var y=box.ymd.substring(0,4);
	  var m=box.ymd.substring(4,6);
	  var d=box.ymd.substring(6,8);
	  if(d<10) d="0"+d;

	  selectDate("Month",y+"년 "+m+"월");
	  selectDate("Day",d+"일");
	  goDateUrl(y+m+d);
	},
	getYMD :function(date){
		var y=date.getFullYear();
		var m=date.getMonth()+1;
		if(m<10) m="0"+m;
		var d=date.getDate();
//		if(d<10) d="0"+d;
		return y+""+m+""+d;
	},
	getLastDay :function(date){
		return new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
	},
	getSweek:function(date){
		return new Date(date.getFullYear(),date.getMonth(),1).getDay();
	},
	goPrev:function(){
		this.draw(this.day.getFullYear(), this.day.getMonth()-1);
	},
	goNext:function(){
		this.draw(this.day.getFullYear(), this.day.getMonth()+1);
	},
	goToday:function(){
		this.draw(this.today.getFullYear(), this.today.getMonth());
	},
	show:function(el){
		this.show_el=el;
		var pos=UI.getPosition(el);
		var ifa=parent.document.getElementById('UICalenderIfa');

		if(!this.is_show)
		{
			UI.addEvent(parent.document,"mousedown",function(){ ifa.style.display='none' })
			this.is_show=1;
		}
		if(this.selbox)	this.selbox.className="UICalender_box";	

		var str=el.value.replace(/[^0-9]/g,"");		
		if(str.length==8)//
		{
			var selday=new Date(str.substring(0,4), str.substring(4,6)-1, str.substring(6,8));
			this.draw(selday.getFullYear(), selday.getMonth());
			
			var d=selday.getDate();
			var n1=Math.ceil((d+this.day_sweek)/7) - 1;
			var n2= d-(1+(7*n1)-this.day_sweek);
			this.selbox=UI.$(this.pid+"_"+n1+""+n2);
			this.selbox.className="UICalender_box_selday";
		}
		ifa.style.top=pos.y+el.offsetHeight+"px";
		ifa.style.left=pos.x+"px";
		ifa.style.display="block";
	},
	print:function(obj){
		var s=this.skin();
		UI.$(obj).innerHTML = '<div id="'+this.pid+'">'+s+'</div>';
		
		var day=this.day;
		var selyear=UI.$(this.pid+"_selyear");
		var selmonth=UI.$(this.pid+"_selmonth");
		var btnprev=UI.$(this.pid+"_btnprev");
		var btnnext=UI.$(this.pid+"_btnnext");

		try{
			var self=this;
			UI.addEvent(selyear, "change", function(){
				self.draw(selyear.value, selmonth.value - 1);
			});
			UI.addEvent(selmonth, "change", function(){
				self.draw(selyear.value, selmonth.value - 1);
			});
			UI.addEvent(btnprev, "click", function(){self.goPrev()});
			UI.addEvent(btnnext, "click", function(){self.goNext()});
		}catch(e){}

		this.draw(day.getFullYear(), day.getMonth());
	},
	draw:function(year,month){
		var day=this.day;
		if(this.is_draw) if(year==day.getFullYear() && month==day.getMonth()) return;
		this.is_draw=1;
		day.setFullYear(year);
		day.setMonth(month);		
		this.day_sweek=this.getSweek(day);

		try{
			UI.$(this.pid+"_selyear").innerHTML=day.getFullYear();
			prtmonth = (eval(day.getMonth()+1) <10)? "0"+(day.getMonth()+1) : (day.getMonth()+1);
			UI.$(this.pid+"_selmonth").innerHTML=prtmonth;
		}catch(e){}

		var d0=new Date(year,month,1);
		d0_lastday=this.getLastDay(d0);

		var d1=new Date(year,month-1,1);	//이전달
		var d2=new Date(year,month+1,1);	//다음달
		var d1_lastday=this.getLastDay(d1);

		var num=null;
		var d0_day=1,d2_day=1;
		for(var i=0; i<6; i++) 
		{			
			for(var j=0; j<7; j++) 
			{
				num=i+""+j;
				
				if(i==0 && j<this.day_sweek) //이전달
				{
					d1.setDate( d1_lastday - (this.day_sweek-j) + 1 );
					this.setBox(num, "") //this.getYMD(d1),1
				}
				else if(d0_day>d0_lastday)
				{
					d2.setDate(d2_day++);
					this.setBox(num, "")//this.getYMD(d2),2
				}
				else 
				{
					d0.setDate(d0_day++);
					this.setBox(num, this.getYMD(d0),0)
				}
			}
		}
	},
	skin:function(){
		var s='';
		day = this.day;
		//-------------------------------------------------------------------------
		//html변경가능
		s='<div id="close"><a href="#" onclick="showCalendar();return false;"><img src="http://img-media.hanmail.net/media3/who/calendar_close.gif" width="13" height="12" alt="닫기"></a></div>'
		+'<table align="center">'
		+'<caption>'
        +'<a href="#" onclick="cal.goPrev()"><img src="http://img-media.hanmail.net/media3/who/calendar_pre.gif" width="13" height="12" alt="이전 달"></a>'
		+'<span id="'+this.pid+'_selyear"></span>.'
		+'<span id="'+this.pid+'_selmonth"></span>'
        +'<a href="#" onclick="cal.goNext()"><img src="http://img-media.hanmail.net/media3/who/calendar_next.gif" width="13" height="12" alt="다음 달"></a><br />'
		+'</caption>'
		+'<tr>'
		+'	<th class="sun">일</th>'
		+'	<th>월</th>'
		+'	<th>화</th>'
		+'	<th>수</th>'
		+'	<th>목</th>'
		+'	<th>금</th>'
		+'	<th class="sat">토</th>'
		+'</tr>'
		+'<tr>'
		+'	<td class="sun"><a href="#" id="'+this.pid+'_00"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_01"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_02"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_03"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_04"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_05"></a></td>'
		+'	<td class="sat"><a href="#" id="'+this.pid+'_06"></a></td>'
		+'</tr>'
		+'<tr>'
		+'	<td class="sun"><a href="#" id="'+this.pid+'_10"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_11"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_12"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_13"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_14"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_15"></a></td>'
		+'	<td class="sat"><a href="#" id="'+this.pid+'_16"></a></td>'
		+'</tr>'
		+'<tr>'
		+'	<td class="sun"><a href="#" id="'+this.pid+'_20"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_21"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_22"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_23"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_24"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_25"></a></td>'
		+'	<td class="sat"><a href="#" id="'+this.pid+'_26"></a></td>'
		+'</tr>'
		+'<tr>'
		+'	<td class="sun"><a href="#" id="'+this.pid+'_30"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_31"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_32"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_33"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_34"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_35"></a></td>'
		+'	<td class="sat"><a href="#" id="'+this.pid+'_36"></a></td>'
		+'</tr>'
		+'<tr>'
		+'	<td class="sun"><a href="#" id="'+this.pid+'_40"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_41"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_42"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_43"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_44"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_45"></a></td>'
		+'	<td class="sat"><a href="#" id="'+this.pid+'_46"></a></td>'
		+'</tr>'
		+'<tr>'
		+'	<td class="sun"><a href="#" id="'+this.pid+'_50"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_51"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_52"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_53"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_54"></a></td>'
		+'	<td><a href="#" id="'+this.pid+'_55"></a></td>'
		+'	<td class="sat"><a href="#" id="'+this.pid+'_56"></a></td>'
		+'</tr>'
		+'</table>';
		//-------------------------------------------------------------------------
		return s;	
	}
};


clickAreaCheck = false;
selectbox = function (objid,value) { //달력 selectbox
  objid_Other = (objid == "Month")? 'Day' : "Month";
  obj = document.getElementById("pop"+objid);

  divDisplay ("calender", 'none');
  if(obj.style.display=="none") {
    clickAreaCheck = true;
    divDisplay ("pop"+objid, 'block');
    divDisplay ("pop"+objid_Other, 'none');
  }else {
    clickAreaCheck = false;
    divDisplay ("pop"+objid, 'none');
  }
}
showCalendar = function () {
  obj = 'calender';
  divDisplay ("popMonth", 'none');
  divDisplay ("popDay", 'none');
  if(UI.$(obj).style.display=="none") {
    clickAreaCheck = true;
    UI.$(obj).style.display = "block";
  }else {
	clickAreaCheck = false;
    UI.$(obj).style.display = "none";
  }
}

getdate ="";
setDate = function (obj) {
  if(!getdate)
  getdate = document.getElementById("date").value;

  if(obj.id.substring(0,2) == "YM") {
    getdate = obj.id.substring(2,obj.id.length);
    selectDate("Month",document.getElementById(obj.id).childNodes[0].nodeValue);
  }
  if(obj.id.substring(0,1) == "D"){
    getdate = getdate + obj.id.substring(1,obj.id.length);
    selectDate("Day",document.getElementById(obj.id).childNodes[0].nodeValue);
    goDateUrl(getdate);
  }
}

selectDate = function(type,value){ //selectValue settting
  document.getElementById("sel"+ type + "Area").innerHTML = value;
}

/*
goDateUrl = function(url) {
	topUrl = top.location.href;	
	topUrl = removeIfUrlText(topUrl,"#");

    if(topUrl.indexOf("date=")>0)
	  goUrl = removeIfUrlText(topUrl,"date=",url)
	else 
	  goUrl = (topUrl.indexOf("?")>0)? topUrl+"&date="+url : topUrl+"?date="+url;

	top.location.href=goUrl;
}
*/

var _calendarParam;
goDateUrl = function(date) {
  topUrl = top.location.href;	
  goUrl = removeIfUrlText(topUrl,"#");
  if(topUrl.indexOf("date=")>0)
	  goUrl = removeIfUrlText(goUrl,"date=",date)
  else {
    if(goUrl.indexOf("?")>0)
        goUrl = goUrl+"&date="+date;
    else{
      if(_calendarParam && _calendarParam != "") goUrl = goUrl + _calendarParam + "&date="+date;
      else goUrl = goUrl+"?date="+date;
    }
  }
top.location.href=goUrl;
}

document.onclick = function (obj) {
  if (!clickAreaCheck) {
    if(document.getElementById("popMonth")) divDisplay ("popMonth", 'none');
    if(document.getElementById("popDay")) divDisplay ("popDay", 'none');
  }else clickAreaCheck = false;
}

divDisplay = function (objId, act) {
  if (document.getElementById(objId)) {
  document.getElementById(objId).style.display = act;
  }
}

removeIfUrlText = function (fullurl,findtext,replace) {
 amp = false;
  if(fullurl.indexOf(findtext) > 0) {
    temp1 = fullurl.substring(0,fullurl.indexOf(findtext))
    temp2 = fullurl.substring(fullurl.indexOf(findtext),fullurl.length)
    if(temp2.indexOf("&") == 0) {
      amp = true;
      temp2 = temp2.substring(1,temp2.length);
    }       
    if(temp2.indexOf("&") > 0) {
      fullfindtext = temp2.substring(0,temp2.indexOf("&"));
    }else{
      fullfindtext = temp2.substring(0,temp2.length);
    }
    if(amp==true)
    fullfindtext = "&"+fullfindtext;

    if(replace) {
      if(fullfindtext.indexOf("=")>0) {
      findtextValue = fullfindtext.substring(fullfindtext.indexOf("=")+1,fullfindtext.length);
      fullurl = fullurl.replace(fullfindtext,findtext+replace);
      }
    } else
    fullurl = fullurl.replace(fullfindtext,"");
  }
    return fullurl;
}