/* 정책플러스 */
/**
 * Scrolling Bnn
 * @param _wrapperName	(Wrapper 객체명)
 * @param _numberTag		(번호의 부모 태크)
 * @param _imageTag			(이미지의 부모 태그)
 * @param _setTime			(시간설정 빠르기 - 작을수록빠름)
 */

var focusStopYn = false;
var issueStopYn = false;
var bnnStopYn = false;
var ScrollingBnn = function() { this.initialize.apply(this, arguments); }
ScrollingBnn.prototype = {
	initialize: function(_wrapperName, _numberTag, _imageTag, _setTime) {
		//Define Configuration

		this.OBJ_WRAPPER = document.getElementById(_wrapperName) || document.getElementById("Bnn-Wrapper");
		this.OBJ_PREV = document.getElementById("Bnn-Prev");
		this.OBJ_NEXT = document.getElementById("Bnn-Next");
		this.OBJ_STOP = document.getElementById("Bnn-Stop");
		this.OBJ_START = document.getElementById("Bnn-Start");
		this.IMAGE_TAG = _imageTag || "div";
		this.SET_TIME = _setTime || 2000;
		//Define Member Variables
		this.IS_ACTIVATED = false;
		this.IMAGE_LIST = new Array();
		this.SCROLL_DIRECTION = "Left";
		this.SCROLL_INTERVAL = null;
		this.INDEX = 0;
		this.prepare();
		this.start();
	},
	
	prepare: function() {
		var index = 0;
		var EventNodes = this.OBJ_WRAPPER.childNodes;
		
		for (var i = 0; i < EventNodes.length; i++) {
			
			if (EventNodes[i].nodeType == 3 || EventNodes[i].tagName != "LI") continue;

			//Define Image List
			var ObjImage = EventNodes[i].getElementsByTagName(this.IMAGE_TAG)[0];

			if (ObjImage) {
				this.IMAGE_LIST[index] = ObjImage;
				this.setOverEvent(ObjImage);
				this.setOutEvent(ObjImage);
				//Define Sub Anchors
				var SubAnchors = ObjImage.getElementsByTagName("a");

				for (var j = 0; j < SubAnchors.length; j++) {
					this.setOverEvent(SubAnchors[j]);
					this.setOutEvent(SubAnchors[j]);
				}
			}
			++index;
		}
		
		if (this.IMAGE_LIST.length > 1) this.IS_ACTIVATED = true;
		if (this.OBJ_PREV) this.setDirectionEvent(this.OBJ_PREV, "Right");
		if (this.OBJ_NEXT) this.setDirectionEvent(this.OBJ_NEXT, "Left");
		if (this.OBJ_STOP) this.setStopEvent(this.OBJ_STOP);
		if (this.OBJ_START) this.setPlayEvent(this.OBJ_START);
	},
	
	setClickEvent: function(_target, _index) {
		var __this = this;
		_target.onclick = function() {
			__this.stop.call(__this);
			__this.doPassive(__this.INDEX);
			__this.doActive(_index);
			__this.INDEX = _index;
			return false;
		}
	},
	
	setOverEvent: function(_target, _index) {
		var __this = this;
		_target.onmouseover = _target.onfocus = function() {
			__this.stop.call(__this);
			/* if (_index) {
				__this.doPassive(__this.INDEX);
				__this.doActive(_index);
				__this.INDEX = _index;
			} */
		}
	},
	
	setOutEvent: function(_target) {
		var __this = this;
		_target.onmouseout = _target.onblur = function() {
			
			if(!bnnStopYn)
			__this.start.call(__this);
		}	
	},
	
	setDirectionEvent: function(_target, _direction) {
		var __this = this;
		_target.onclick = function() {			
			__this.doChangeDirection.call(__this, _direction);
			return false;
		}
	},
	
	setStopEvent: function(_target, _method){
		var __this = this;
		_target.onclick = function() {
			__this.stop();
			bnnStopYn = true;
			document.getElementById("Bnn-Stop").style.display = "none";
			document.getElementById("Bnn-Start").style.display = "block";
			document.getElementById("Bnn-Start").focus();
			return false;
		}
	},
	
	setPlayEvent: function(_target, _method){
		var __this = this;
		_target.onclick = function() {
			__this.start();
			bnnStopYn = false;
			document.getElementById("Bnn-Stop").style.display = "block";
			document.getElementById("Bnn-Start").style.display = "none";
			document.getElementById("Bnn-Stop").focus();
			return false;
		}
	},
	
	doChangeDirection: function(_direction) {
		if(this.IMAGE_LIST.length > 0){
			this.stop();
			this.doPassive(this.INDEX);
			if (_direction == "Left") {
				this.INDEX += 1;
			}else{
				this.INDEX -= 1;
			}
			if (this.INDEX >= this.IMAGE_LIST.length) this.INDEX = 0;
			if (this.INDEX < 0) this.INDEX = this.IMAGE_LIST.length-1;
			this.doActive(this.INDEX);
			this.start();
		}
	},
	
	start: function() {
		this.stop();
		if (this.IS_ACTIVATED) {
			var __this = this;
			//this.SCROLL_INTERVAL = setInterval(function() { __this.run(); }, this.SET_TIME);
			this.SCROLL_INTERVAL = setInterval(function() { eval("__this.scroll"+__this.SCROLL_DIRECTION+"()"); }, this.SET_TIME);
		}
	},
	
	stop: function() {
		if (this.SCROLL_INTERVAL) clearInterval(this.SCROLL_INTERVAL);
	},
	
	scrollLeft: function() {
		this.doPassive(this.INDEX);
		this.INDEX += 1;
		if (this.INDEX >= this.IMAGE_LIST.length) this.INDEX = 0;
		this.doActive(this.INDEX);
	},
	
	scrollRight: function() {
		this.doPassive(this.INDEX);
		this.INDEX -= 1;
		if (this.INDEX >= this.IMAGE_LIST.length) this.INDEX = 0;
		this.doActive(this.INDEX);
	},
	
	doActive: function(_index) {
		this.IMAGE_LIST[_index].style.display = "block";
	},
	
	doPassive: function(_index) {
		this.IMAGE_LIST[_index].style.display = "none";
	},
	
	doPassiveAll: function() {
		for (var i = 0; i < this.IMAGE_LIST.length; i++) {
			this.doPassive(i);
		}
	}
}

$(function() {
	/* 분야별정책 */
		function smain03() {
			
		}

	/* 법령 */
		function smain05() {
			var s05dt = $(".sbm05con .list dt a");
			var s05dd = $(".sbm05con .list dd");

			s05dt.eq(0).addClass("on");
			s05dd.eq(1).hide();

			s05dt.click(function() {
				$(this).addClass("on").parent().next().show();
				s05dt.not(this).removeClass().parent().next().hide();
				return false;
			});
			
			s05dt.focus(function() {
				$(this).addClass("on").parent().next().show();
				s05dt.not(this).removeClass().parent().next().hide();
				return false;
			});
		}
		
		/* 기획재정부 산하기관 보도자료 */		
		function rss_list() {
			var tab01dt = $(".rss_list > ul > li > a");
			var tab01dd = $(".rss_list > ul > li > div");

			tab01dt.eq(0).addClass("on");
			tab01dd.eq(1).hide();
			tab01dd.eq(2).hide();
			tab01dd.eq(3).hide();
			tab01dd.eq(4).hide();
			tab01dd.eq(5).hide();

			tab01dt.click(function() {
				$(this).addClass("on").next().show();
				tab01dt.not(this).removeClass().next().hide();
				return false;
			});
			
			tab01dt.focus(function() {
				$(this).addClass("on").next().show();
				tab01dt.not(this).removeClass().next().hide();
				return false;
			});
		}
		
		/* 찾아오시는길 */
		function map05con() {
			var s05dt = $(".map05con .list dt a");
			var s05dd = $(".map05con .list dd");

			s05dt.eq(0).addClass("on");
			s05dd.eq(1).hide();

			s05dt.click(function() {
				$(this).addClass("on").parent().next().show();
				s05dt.not(this).removeClass().parent().next().hide();
				return false;
			});
			
			s05dt.focus(function() {
				$(this).addClass("on").parent().next().show();
				s05dt.not(this).removeClass().parent().next().hide();
				return false;
			});
		}
		
		/* 기관소개메인 SNS */
		function sns_list() {
			var tab01dt = $(".sns_link .tabs > li > a");
			var tab01dd = $(".sns_link .tabs > li > div");

			tab01dt.eq(0).addClass("on");
			tab01dd.eq(1).hide();

			tab01dt.click(function() {
				$(this).addClass("on").next().show();
				tab01dt.not(this).removeClass().next().hide();
				return false;
			});
			
			tab01dt.focus(function() {
				$(this).addClass("on").next().show();
				tab01dt.not(this).removeClass().next().hide();
				return false;
			});
		}

		smain03();
		smain05();
		rss_list();
		map05con();
		sns_list();
});