/*
ARIAwork.com, Professional Script creating Group

ARIAfindAddress.js 2007/11/21

desc    : find address Class / UTF-8

history : 2008/7/21, squall, 최초작성

Copyright (c) 2000-2008 NAMUsoft, Inc. All Rights Reserved.

This software is the proprietary information of NAMUsoft, Inc.
Use is subject to license terms.
*/

var ARIAfindAddress = Class.create({
  initialize: function(TGid, oOpt) {
    this.TGID = TGid.id;
    this.OPT  = oOpt;
    this.parentTG = document.body;
    this.OPT.box = {width:430, height:300};

    var newElement = new Element("div", {id:this.TGID});
    Element.insert(this.parentTG, {top:newElement});
    newElement.className = "AriaPopup2";
    newElement.absolutize();
    newElement.hide();
    this.TG = newElement;
    this.makeHead();

    Event.observe(window, 'resize', this.onresize.bindAsEventListener(this));
    Event.observe(this.TG, 'click', this.onclick.bindAsEventListener(this));
    Event.observe(this.TG, 'keydown', this.onKeyPress.bindAsEventListener(this));
  },
  /* event */
  onresize: function(){
    if(this.resizeObserver) clearTimeout(this.resizeObserver);
      this.resizeObserver = setTimeout(this.setPosition.bind(this), 100);
  },
  onclick: function(event){
    var TGID = this.TGID;
    var eventIMG = Event.findElement(event, "IMG");
    if(eventIMG){
      var btnType = ARIA.findID(eventIMG.id, "btn");
      if(btnType){
        if(btnType == "find"){
          this.addressFind();
        } 
        if(btnType == "select"){
          this.selectZipcode();
        } 
        if(btnType == "cancle"){
          this.boxClose();
        } 
      }
    }
  },
  onKeyPress: function(event){
    switch(event.keyCode){
      case Event.KEY_TAB:
      case Event.KEY_RETURN:
        this.addressFind();
        break;
      case Event.KEY_ESC:
        this.boxClose();
        break;
      case Event.KEY_LEFT:
      case Event.KEY_RIGHT:
      case Event.KEY_UP:
      case Event.KEY_DOWN:
    }
  },
  /* event */
  makeHead: function(){
    var box = this.OPT.box;
    var color = this.OPT.color;
    var btn = this.OPT.btn;
    var TGID = this.TGID;
    var URL = this.OPT.URL;

    var p_out = [];
    p_out.push("<div id=\""+TGID+"#contentBody\" style=\"background:#fff;height:100%;border:1px solid #ccc;\"></div>");
    this.TG.update(p_out.join(''));

    var c_out = [];
    c_out.push("<form name=\""+TGID+"#sendform\" id=\""+TGID+"#sendform\" onsubmit=\"return false;\">");
    c_out.push("<table width=100% height=100% cellpadding=3 cellspacing=0 >");
    c_out.push("  <tr height=20>");
    c_out.push("    <td class=\"ARIAprocessingTitle\"><b>우편번호 찾기</b>- (읍/면/동)이름을 입력하세요</td>");
    c_out.push("  </tr>");
    c_out.push("  <tr height=20>");
    c_out.push("    <td>(읍/면/동)이름 <input type=\"text\" name=\"dongName\" id=\""+TGID+"#input{dongName}\" size=10 class=\"inputDF\"> <img src=\""+btn.find+"\" align=absmiddle id=\""+TGID+"#btn{find}\" style=\"cursor:pointer;\"></td>");
    c_out.push("  </tr>");
    c_out.push("  <tr>");
    c_out.push("    <td style=\"padding-top:5px;height:200px;\">");
    c_out.push("    <div  id=\""+TGID+"#list{address}\" style=\"border:1px solid #E4E4E5;background:#ffffff;\" class=\"fbw_box\"  onselectstart=\"return false;\" oncontextmenu=\"return false;\">&nbsp;</div>");
    c_out.push("    </td>");
    c_out.push("  </tr>");
    c_out.push("  <tr height=20>");
    c_out.push("    <td align=right><img src=\""+btn.select+"\" align=absmiddle id=\""+TGID+"#btn{select}\" style=\"cursor:pointer;\"> <img src=\""+btn.cancle+"\" align=absmiddle id=\""+TGID+"#btn{cancle}\" style=\"cursor:pointer;\"></td>");
    c_out.push("  </tr>");
    c_out.push("</table>");
 
 
    c_out.push("</form>");
    $(TGID+"#contentBody").update(c_out.join(''));
    
    var myselectZipcode = this.selectZipcode.bind(this);
    
    this.myList = new ARIAlist($(TGID+"#list{address}"),
    	{
    	  Columns:[
      	{idx:0, name:'우편번호', type:'String', width:55, align:"center", pattern:"", onclick:myselectZipcode},
      	{idx:1, name:'주소', type:'String', width:160, align:"left", pattern:"", onclick:myselectZipcode},
      	{idx:2, name:'상세주소', type:'String', width:160, align:"left", pattern:"", onclick:myselectZipcode}
      	],
      	debug:false,
      	urlTest:false,
      	AutoStart:false,
      	rowHeight:24,
      	rowConut:50,
      	Sorted:{index:0,order:"asc"},
      	bgColors:{over:"EEEEDC",on:"D7D7AF",off:"ffffff"},
    		URL:{Load:URL.Load},
    		Pars:URL.Pars,
    		Loading:{Img:"/_img/aria/loading_circle_dc.gif", Text:"목록을 준비중입니다<br>잠시만 기다려 주세요.."}
    	}
    );

  },
  /* method */
  addressFind: function(){
    var TGID = this.TGID;
    var form = $(TGID+"#sendform");
    if(this.OPT.URL.Pars != "") var pars = this.OPT.URL.Pars+"&"+form.serialize();
    else var pars = form.serialize();
    
    this.myList.setPars(pars);
    this.myList.listLoad();
  },
  selectZipcode: function(clickObj){  
    if(clickObj){
      this.OPT.zipcode.value = clickObj.Datas[0].decode();
      this.OPT.address.value = clickObj.Datas[1].decode() + " " + clickObj.Datas[2].decode();
      /* kilmmk 임시 수정 09. 11. 01*/
      //this.OPT.addressDetail.value = clickObj.Datas[2].decode();      
      this.boxClose();
      this.OPT.addressDetail.select();
    }else{
    	
    	
      var myselectObj = this.myList.getSelectObject();
      var myObj = myselectObj.first();
      if(myObj){
	      this.OPT.zipcode.value = myObj[0].decode();
	      this.OPT.address.value = myObj[1].decode() + " " + myObj[2].decode();
	      /* kilmmk 임시 수정 09. 11. 01*/
	      //this.OPT.addressDetail.value = myObj[2].decode();
	      this.boxClose();
	      this.OPT.addressDetail.select();
	    }else{	    	
	    	alert("주소를 선택해주십시요");	    	
	    }
    }
  },
  boxOpen: function(){
    var TGID = this.TGID;
    this.setPosition();
    
    $(this.TGID).show();
    var form = $(TGID+"#sendform");
    form["dongName"].select();
    this.ListAlign();
    
  },
  boxClose: function(){
    $(this.TGID).hide();
  },
  ListAlign: function(){
    this.myList.alignHeader();
    this.myList.alignResizer();
  },
  setPosition: function(){
    var box = this.OPT.box;
    var PTGbox = this.parentTG.getDimensions();
    var myLeft, myTop;
    myLeft = PTGbox.width/2 - box.width/2;
    myTop  = PTGbox.height/2 - box.height/2;

    this.TG.setStyle({
      left:myLeft, top:myTop, width:box.width, height:box.height
    });
  },
  setProgress: function(totalCount){
    this.totalCount = totalCount;
    $(this.TGID+"#status").update("0/"+this.totalCount);
  },
  setMessage: function(sStr){
    $(this.TGID+"#message").update(sStr);
  },
  setStep: function(step){
    $(this.TGID+"#status").update(ARIA.roundNumber(step/this.totalCount * 100, 1)+"% "+step+"/"+this.totalCount);
    $(this.TGID+"#progress").style.width = ARIA.roundNumber(step/this.totalCount * 100, 2)+"%";
  }
  /* method */
}); // ARIAfindAddress End
