/**
* @author daum
*/
var _ua = window.navigator.userAgent.toLowerCase();

var browser = {
    model: _ua.match(/(sonyericssonx1i|ipod|iphone)/) ? _ua.match(/(sonyericssonx1i|ipod|iphone)/)[0] : "",
	skt : /msie/.test( _ua ) && /nate/.test( _ua ),
	lgt : /msie/.test( _ua ) && /([010|011|016|017|018|019]{3}\d{3,4}\d{4}$)/.test( _ua ),
	opera : /opera/.test( _ua ) || /opera mobi/.test( _ua ),
	ipod : /webkit/.test( _ua ) && /\(ipod/.test( _ua ) ,
	iphone : /webkit/.test( _ua ) && /\(iphone/.test( _ua )
};

// Scroll Button _ FB
function setNav() {
	if(!document.getElementById)	return false;
	if(!document.getElementById('scrollNav'))	return false;

	var nav = document.getElementById('scrollNav');
	var prev = getElementsByClassName(document, "btGoPrev");

	if (hasScroll()) {
		if(browser.skt || browser.lgt) { showScroll(nav); }
		else									 { hideScroll(nav); }
	} else { hideScroll(nav); }
}

function moveNav(dir, amount) {
	if (amount == undefined) amount = 250;

	switch (dir)
	{
		case 'top':
			window.scrollTo(0,0);
			break;
		case 'up':
			window.scrollTo(0, document.documentElement.scrollTop - amount);
			break;
		case 'down':
			window.scrollTo(0, document.documentElement.scrollTop + amount);
			break;
		defalut:
			break;
	}
}

function hasScroll(){
	var screenHeight = document.documentElement.scrollHeight;
	var clientHeight = document.documentElement.clientHeight;
	return clientHeight < screenHeight ? true : false;
}

function showScroll(nav){
	var offset = 170;

	nav.style.display = "";
	nav.style.top = (document.documentElement.scrollTop + offset) + 'px';
}

function hideScroll(nav){
	nav.style.display = "none";
}

function getElementsByClassName(_element, className){
	var elem = typeof _element == "string" ? document.getElementById(_element) : _element;

	var _all = elem.getElementsByTagName("*");
	var element = [];
	for(var i=0,len=_all.length; i<len; i++) {
		if(_all[i].className == className) element.push(_all[i]);
	}
	return (element.length > 0) ? element : null;
}

if (window.attachEvent)	window.attachEvent("onscroll", setNav);
else window.addEventListener("scroll", setNav, false);
window.setInterval(setNav, 100);

/* bind */
function $A(iterable)
{
	if (!iterable) return [];
	var results = [];
	for (var i = 0, length = iterable.length; i < length; i++)
	  results.push(iterable[i]);
	return results;
}

Function.prototype.bind = function()
{
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}

Function.prototype.bindEvent = function()
{
	var __method = this, args = $A(arguments), object = args.shift();
	return function(event) {
		return __method.apply(object, [event || window.event].concat(args));
	}
}
//event add
function AddEvent(element, name, func)
{
	if (element.addEventListener) {
		element.addEventListener(name, func, false);
	}
	else if (element.attachEvent) {
		element.attachEvent('on' + name, func);
	}
}
function RemoveEvent(element, name, func)
{
	if (element.addEventListener) {
		element.removeEventListener(name, func, false);
	}
	else if (element.attachEvent) {
		element.detachEvent('on' + name, func);
	}
}

function StopEvent(event) {
	var e=event || window.event;
	if(e.preventDefault) {e.preventDefault(); e.stopPropagation(); }
	else {e.returnValue = false; e.cancelBubble = true;}
};

Tab = function(idSet,eType,onCss,defIdx,addLinkUrl) {
	this.cnt = idSet.length;
	this.eType = eType;
	this.onCss = onCss;
	this.tab = [];
	this.tabbody = [];
	for(var i=0;i<this.cnt;++i)
	{
		var tab = document.getElementById(idSet[i][0]);
		var tabbody = document.getElementById(idSet[i][1]);
		tab.oldCss = tab.className;
		tabbody.oldCss = tabbody.className;
		this.tab.push(tab);
		this.tabbody.push(tabbody);
		if(addLinkUrl && addLinkUrl[i])tab.onclick = this.addLink.bind(this,i,addLinkUrl[i]);
		AddEvent(tab,eType,this.on.bind(this,i));
	}
	(defIdx) ? this.on(defIdx-1) : this.on(0);
}
Tab.prototype = {
	on:function(n) {
		if(this.curIdx == n) return;
		for (var i = 0; i < this.cnt; ++i) {
			if (i == n) {
				this.tab[i].className = (this.tab[i].oldCss) ? this.tab[i].oldCss + " " + this.onCss : this.onCss;
				this.tabbody[i].className = (this.tabbody[i].oldCss) ? this.tabbody[i].oldCss + " " + this.onCss : this.onCss
			}
			else {
				this.tab[i].className = this.tab[i].oldCss;
				this.tabbody[i].className = this.tabbody[i].oldCss;
			}
		}
		this.curIdx = n;
	},
	addLink:function(n,url) {
		if(this.curIdx == n) GoPage(url);
	}
}

Paging = function(listId,maxCount,prevBtId,nextBtId,listTag) {
	this.el = document.getElementById(listId);
	this.tag = (listTag) ? listTag : 'li';
	this.maxCount = maxCount;
	this.prevBt = (prevBtId) ? document.getElementById(prevBtId) : null;
	this.nextBt = (nextBtId) ? document.getElementById(nextBtId) : null;
	if(this.prevBt) AddEvent(this.prevBt,"click",this.prev.bind(this));
	if(this.nextBt) AddEvent(this.nextBt,"click",this.next.bind(this));
	this.init();

}

Paging.prototype = {
	init : function() {
		this.list = this.el.getElementsByTagName(this.tag);
		this.totalCount = this.list.length;
		this.totalPage = Math.ceil(this.totalCount/this.maxCount);
		this.needPaging = (this.totalCount > this.maxCount);
		if(!this.needPaging) {
			if(this.prevBt) this.prevBt.style.display = "none";
			if(this.nextBt) this.nextBt.style.display = "none";
		}
		else {
			if(this.prevBt) this.prevBt.style.display = "";
			if(this.nextBt) this.nextBt.style.display = "";
		}
		this.curPage = 1;
		this.exec(1);
	},
	prev : function() {
		this.exec(--this.curPage);
	},
	next : function() {
		this.exec(++this.curPage);
	},
	exec : function(n) {
		if(n<=0)  this.curPage = this.totalPage;
		else if(n>this.totalPage)  this.curPage = 1;

		var min = this.maxCount*(this.curPage-1), max = this.maxCount*this.curPage;
		for(var i=0;i<this.totalCount;++i) {
			if(min <= i && i < max) this.list[i].style.display = "";
			else this.list[i].style.display = "none";
		}
	}
}

//link
function GoPage(url,target)
{
	if(target) target.location.href=url;
	else window.location.href=url;
}
function GoPageNew(url)
{
	window.open(url,"new");
}
function goPage(page) {
	if(document._form) {
    	var param = "?page="+page;
    	if(document._form.code) param += "&code="+document._form.code.value;
    	if(document._form.stype) param += "&stype="+document._form.stype.value;
		if(document._form.seccode) param += "&seccode="+document._form.seccode.value;
		if(document._form.themecd) param += "&themecd="+document._form.themecd.value;
		if(document._form.groupcd) param += "&groupcd="+document._form.groupcd.value;
		if(document._form.type) param += "&type="+document._form.type.value;
		document.location.href = param;
	}
}

function FontBig(){
	var font = document.getElementById("newsContent");
	var img = document.getElementById("mobile_btn");
	img.src=(font.style.fontSize=="21px")?"http://img.mobile.daum.net/static_img/iphone/estate/bt_zoom.gif":"http://img.mobile.daum.net/static_img/iphone/estate/bt_primal.gif";
	font.style.fontSize = (font.style.fontSize=="21px")?"15px":"21px";
}

/* From JES */
var UI={};
Object.extend=function(a, b){
  for (var property in b) a[property] = b[property];
  return a;
};

UI.Radio = function(name)
{
	this.img_on = "http://img.mobile.daum.net/static_img/iphone/estate/radio_on.gif";
	this.img_off = "http://img.mobile.daum.net/static_img/iphone/estate/radio_off.gif";
	this.radio = {};
	this.checked_id = "";
	var self=this;

	var ra = null,img=null;
	var cid = "";
	for(var i=0; i<document.getElementsByName(name).length; i++)
	{
		
		ra = document.getElementsByName(name)[i];
		cid =ra.id;
		txt=document.getElementById(cid+"_txt");
		if(txt){
			img=ra.parentNode.insertBefore(document.createElement('img'), ra);
			img.src = this.img_off;
			img.style.width = 13;
			img.style.width = 13;
			if(ra.checked)
			{
				img.src = this.img_on;
				this.checked_id = cid;
			}
			
			img.cid = cid;
			img.onclick = function() {
				self.select(this.cid);
				if(self.radio[this.cid].ra.onclick) self.radio[this.cid].ra.onclick();
			};
			txt.cid = cid;
			txt.onclick = function() {
				self.select(this.cid);
				if(self.radio[this.cid].ra.onclick) self.radio[this.cid].ra.onclick();
			};
			ra.style.display="none";
			this.radio[cid] = {ra:ra, img:img};
		}
	}
};
UI.Radio.prototype={
	select : function(cid) {
		this.radio[cid].img.src = this.img_on;
		this.radio[cid].img.style.width = 13;
		this.radio[cid].img.style.width = 13;
		this.radio[cid].ra.checked=true;
		if(this.checked_id && this.checked_id!=cid) this.radio[this.checked_id].img.src = this.img_off;
		this.checked_id=cid;
	},
	showValue : function() {
		return this.radio[this.checked_id].ra.value;
	}
};
UI.$=function(s) { return document.getElementById(s) };
UI.toggleLayer=function(id) { 
	if(id=="close"){
		UI.$('L1').style.display=UI.$('L2').style.display=UI.$('L3').style.display="none";
		UI.$('foot_btn').style.display="block";
	}else{
		UI.$(id).style.display=(UI.getStyle(UI.$(id),'display')=='none') ? 'block':'none';
		if(id=='L1') 
			UI.$('foot_btn').style.display=(UI.$('foot_btn').style.display=='none') ? 'block':'none';;
	}
};
UI.getStyle=function(el, style){
	var value = el.style[style];
	if(!value){
		if(document.defaultView && document.defaultView.getComputedStyle) {
			var css = document.defaultView.getComputedStyle(el, null);
			value = css ? css[style] : null;
		}else if (el.currentStyle) value = el.currentStyle[style];
	}
	return value == 'auto' ? null : value;
};
/* 평형자동계산기 */
UI.PyeongCalc = function( formId, userObjId ){
	this.form = UI.$( formId );
	this.focusObj = null; // 포커스된 입력박스
	this.objId = {
		inputM:"PCalcM",
		inputP:"PCalcP"
	}
	this.prevValue = [];

	Object.extend(this.objId, userObjId);

	this.objInputM = UI.$( this.objId.inputM );
	this.objInputP = UI.$( this.objId.inputP );
	this.objInputM.setAttribute("autocomplete","off");
	this.objInputP.setAttribute("autocomplete","off");
}
UI.PyeongCalc.prototype = {
	calculate : function(){
		var elseObj = null;
		var outVal = null;
		if ( this.focusObj == null ){
			this.objInputM.focus();
			return false;
		} else {
			if ( this.focusObj.id == this.objId.inputM ){
				elseObj = this.objInputP;
			} else {
				elseObj = this.objInputM;
			}
		}

		if ( this.prevValue[0] == this.objInputM.value && this.prevValue[1] == this.objInputP.value ){
			this.objInputM.value = "";
			this.objInputP.value = "";			
			this.focusObj.focus();
			return false;
		}

		var match = this.focusObj.value.match(/^([0-9,.]+)$/);
		if( this.focusObj.value == "" ){
			alert( "값을 입력해 주세요");
			this.focusObj.select();
			this.focusObj.focus();
			return false;
		} else if( match == null ){
			alert( "잘못된 입력 형식입니다");
			this.focusObj.select();
			this.focusObj.focus();
			return false;
		}
		var inVal = parseFloat( this.focusObj.value.replace(/[^.0-9]*/gi , "" ) );
		if ( isNaN( inVal ) == true ) {
			elseObj.value = "";
			this.focusObj.select();
			this.focusObj.focus();
			return false;
		}

		if ( this.focusObj.id == this.objId.inputM ){
			//㎡ -> 평
			outVal = Math.round(inVal * 0.3025 * 100) / 100;
			this.objInputP.value = UI.addComma( outVal );
		} else {
			//평 -> ㎡
			outVal = Math.round(inVal * 3.3058 * 100) / 100; 
			this.objInputM.value = UI.addComma( outVal );
		}
		this.focusObj.value = UI.addComma( this.focusObj.value );
		this.focusObj.select();
		this.focusObj.focus();
		this.prevValue = [ this.objInputM.value, this.objInputP.value ];
	},
	setFocus : function( obj ){
		obj.select();
		this.focusObj = obj;
	}
};
UI.addComma=function(s){
	s+='';
	s=s.replace(/,/gi,'');
	var re=new RegExp('(-?[0-9]+)([0-9]{3})');
	while(re.test(s)) s=s.replace(re,'$1,$2');
	return s;

};
function GoPcPage() {document.location.href = "http://realestate.daum.net/";}


if (browser.ipod || browser.iphone) {window.scrollTo(0, 1);}


/* orientation Change */
AddEvent(window, 'load' ,  function() { setTimeout(orientationChange, 0); setTimeout(function() { window.scrollTo(0, 1); }, 100); })

var size = {lWidth : 480 , lHeight : 323 , pWidth : 320 , pHeight : 323, vHeight : 213};
var currentWidth = 0;
function orientationChange() {

	// Opera 는 portrait 고정
	if ((window.innerWidth  != currentWidth) && !browser.opera) {
		currentWidth = window.innerWidth ;

		var orient = currentWidth  < 321 ? 'portrait' : 'landscape';		
		document.body.setAttribute('orient', orient);
		setTimeout(function() {
			if(orient == 'portrait') {
				document.body.setAttribute("class","port");
				document.body.className = "port";
				if(typeof mobileMap != "undefined" && typeof mobileMap == "object"){
					mobileMap.reLoad({iw:size.pWidth, ih:size.pHeight})
				}	

				if(typeof changeSize != "undefined" && typeof changeSize == "function"){
					changeSize(size.vHeight,size.pWidth)
				} 
				
			} else {
				document.body.setAttribute("class","land");
				document.body.className = "land";
				if(typeof mobileMap != "undefined" && typeof mobileMap == "object"){
					mobileMap.reLoad({iw:size.lWidth ,ih:size.lHeight})
				}	

				if(typeof changeSize != "undefined" && typeof changeSize == "function"){
					changeSize(size.vHeight,size.lWidth)
				} 
			}
		}, 100);
	}
}

setInterval(orientationChange, 300);