/**
* @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));
	}
}

String.prototype.trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/gi, ""); 
} 

//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";
		}
	}
}


/* Cookie */
function getCookie(s){
	var tmp=document.cookie.split('; ');
	for (var i=0; i<tmp.length;i++){
		var c_name = tmp[i].split('=');
		if (c_name[0]==s) return c_name[1];
	}
	return false;
}

function setCookie(_name,_value,_day,_domain){
	var expireDate = new Date(Number(new Date())+Number(_day)*86400000);
	var ckText = _name+'='+_value+';expires='+expireDate.toGMTString()+';path=/;';
	if(_domain) ckText += "domain="+_domain+";";
	document.cookie =ckText;
}

//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 stockSearch(id) {
	var s_url = "";
	var inputObj = document.getElementById(id);

	if(inputObj.value.trim() == "종목명 or 코드" || inputObj.value.trim() == "") {
		alert("종목명 또는 종목코드를 입력해 주십시요.");
		inputObj.value = "";
		inputObj.focus();
		return false;
	}

	if(isNaN(inputObj.value)) {
//		s_url="http://stock.daum.net/search/search.daum?name="+encodeURIComponent(inputObj.value);
		GoPage("/mobile/search/search.daum?name="+encodeURIComponent(inputObj.value));
	} else {
//		s_url="http://stock.daum.net/mobile/item/main.daum?code="+inputObj.value;
		if (inputObj.value.length != 6) {
			GoPage("/mobile/search/search.daum?name="+inputObj.value);
		} else {
			GoPage("/mobile/item/main.daum?code="+inputObj.value);
		}
	}

	return false;
}

function FontBig(){
	var font = document.getElementById("newsContent");
	var img = document.getElementById("mobile_btn");
	img.src=(font.style.fontSize=="34px")?"http://img-media.daum-img.net/media3/mobile/btn_high.gif":"http://img-media.daum-img.net/media3/mobile/btn_low.gif";
	font.style.fontSize = (font.style.fontSize=="34px")?"28px":"34px";
}

function FontBigIPhone(){
	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/stock/btn_big.gif":"http://img-media.daum-img.net/media3/mobile/btn_low.gif";
	font.style.fontSize = (font.style.fontSize=="21px")?"15px":"21px";
}

function getIPhoneMode() {
	if (document.getElementById("wrap").className == "hMode") {
		document.getElementById("wrap").className = "";
	} else {
		document.getElementById("wrap").className = "hMode";
	}
}

function searchOnClick(searchObj) {
	try{
		if(searchObj.value == "종목명 or 코드") {
			searchObj.value = "";
		}
	} catch(e) {}
}

/* iPhone width/height mode change */
function updateOrientation()
{
	try	{
		var orientation = window.orientation;
		switch(orientation)
		{
		
			case 0:
					document.getElementById("wrap").className = "hMode";
					break;	
			case 90:
					document.getElementById("wrap").className = "";
					break;
			case -90:	
					document.getElementById("wrap").className = "";		
					break;
		}
	}
	catch (e) {	}
}


if (browser.ipod || browser.iphone) {window.scrollTo(0, 1);}
window.onorientationchange = updateOrientation;
window.onload = updateOrientation;