﻿/*
 * SDS javascript Module API
 */
var SDS = {};

SDS.XHR = {
	
	xmlhttp : "",				/* SDS 스페이스 내에서 사용되는 XmlHttpRequest 객체*/

	LoadXMLHttp : 			/* 브라우저별 XHR 객체를 생성 한다. */
		function (){
			if (window.XMLHttpRequest) {
				this.xmlhttp=new XMLHttpRequest()
		  	}else if (window.ActiveXObject) {
		  		this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		  	}else{
		  		return false;
			}
		},
		
	StartRequest : 			/* XHR 호출 head 파라메터는 필요한 경우만.. */
		function (url, callback, headName, headValue) {
			xmlhttp = this.xmlhttp;
			
		
			if (xmlhttp!=null) { 
				xmlhttp.open("GET",url,true);		//async
				xmlhttp.onreadystatechange=callback;
				if (headName != undefined){
					if (headName != ""){
						xmlhttp.setRequestHeader(headName, headValue);		//헤더 정보 추가 
					}
				}
				xmlhttp.send(null);
		  	}else{
		  		//alert("Your browser does not support XMLHTTP.")
		  		return false;
		  	}
		},

	SendFormByPOST : 			/* XHR 호출 head 파라메터는 필요한 경우만.. */
		function (url, callback, arrParamName, arrParamValue) {
			var queryString = "";
			xmlhttp = this.xmlhttp;
			if (xmlhttp!=null) { 
				xmlhttp.open("POST",url,true);		//async
				xmlhttp.onreadystatechange=callback;
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=euc-kr");		//헤더 정보 추가 
				if(arrParamName != undefined){
				for (var i=0;i<arrParamName.length;i++){
					if (arrParamValue[i] != ""){
						queryString += escape(arrParamName[i])+"="+escape(arrParamValue[i])+"&"
					}
				}
				}
				xmlhttp.send(queryString);
		  	}else{
		  		//alert("Your browser does not support XMLHTTP.")
		  		return false;
		  	}
		},
		
	CheckReadyState : 	/* XHR 상태 확인 */
		function () {
			xmlhttp = this.xmlhttp;
			if(xmlhttp.readyState == 4){
				if(xmlhttp.status == 200)
					return true;
				else
					alert("Problem retrieving response data (status:[" + xmlhttp.status + "])");
					return false;
			}
		},

	LoadXMLParser : 	/* XML 파서 로드 */
		function (xmlText){
			// code for IE
			if (window.ActiveXObject){	
				var doc=new ActiveXObject("Microsoft.XMLDOM");
				doc.async="false";
				doc.loadXML(xmlText);
			}else {// code for Mozilla, Firefox, Opera, etc.
				var parser=new DOMParser();
				var doc=parser.parseFromString(xmlText,"text/xml");
			}
			//return doc.documentElement;
			return doc;
		},

	GetXMLElements: 	/* parent 노드의 배열을 가져온다. */
		function (response, parentNode){
			return response.getElementsByTagName(parentNode);
		},
		
	GetXMLElementValue : 	/* child 노드값을 가져온다.*/
		function (parentNodeList, idx, childNode){
				var tmpValList = parentNodeList[idx].getElementsByTagName(childNode);
				if (tmpValList[0].firstChild)
					return tmpValList[0].firstChild.data;
				else
					return  "";
		},

	GetXMLElementAttibute : 	/* child 노드어트리뷰트를 가져온다. */
		function (tmpList, idx, childNode, attibuteName){
				var tmpValList = tmpList[idx].getElementsByTagName(childNode);
				if (tmpValList.length)
					return  tmpValList[0].getAttribute(attibuteName);
				else
					return  "";
		},

	CreateXMLNode : 
		function (response, nodeName, nodeValue){
			var doc = response;
			var newItemNode = doc.createElement(nodeName);
			if (nodeValue){
				var MyText = doc.createTextNode(nodeValue);
				newItemNode.appendChild(MyText);
			}
			return newItemNode;
		},

	CreateXMLNodeAttribute : 
		function (response, attrName, attrValue){
			var doc = response;
			var newAttribute = doc.createAttribute(attrName);
			newAttribute.value = attrValue;
			return newAttribute;
		},

	CreateXMLNodeCDATA : 
		function (response, cdataValue){
			var doc = response;
			var newCDATA = doc.createCDATASection(cdataValue);
			return newCDATA;
		},

	AppendChildNode : 
		function (parent, child) {
			parent.appendChild(child)
		},

	SetAttributeNode : 
		function (owner, attribute){
			owner.setAttributeNode(attribute);
		},

	WriteLog : 	/**/
		function (obj, msg) {
			obj.innerHTML = msg;
		}
			

}
/*
var ANIM_BLANK = 0;
var ANIM_SLIDE = ANIM_BLANK + 1;
var ANIM_MOVE = ANIM_BLANK + 2;
var ANIM_PULSATE = ANIM_BLANK + 3;
var ANIM_FADEIN = ANIM_BLANK + 4;
var ANIM_FADEOUT = ANIM_BLANK + 5;
var ANIM_RESIZE = ANIM_BLANK + 6;
*/
SDS.Anim  = {
	obj : "",
	ANIM_BLANK : 0,
	ANIM_SLIDE : 1,
	ANIM_MOVE : 2,
	ANIM_PULSATE : 3,
	ANIM_FADEIN : 4,
	ANIM_FADEOUT : 5,
	ANIM_RESIZE : 6,
	
	info : 
		function (){			
			this.Width = 0;
			this.Height = 0;
			this.Left = 0;
			this.Top = 0;

			this.moveX = 0;
			this.moveY = 0;
			
			this.SetTimer = 1000;

			this.Duration = 1;
			/*Sliding 관련 변수 */
			this.startX = 0;
			this.startY = 0;
			this.destX = 0;
			this.destY = 0;
			this.moveFrame = 0;
			this.slideState = false;
		},

	SetDefault : 
		function (){
			
			this.info.Width = 0;
			this.info.Height = 0;
			this.info.Left = 0;
			this.info.Top = 0;
			this.info.moveX = 0;
			this.info.moveY = 0;
			this.info.SetTimer = 1000;
			this.info.Duration = 1;
			/*Sliding 관련 변수 */
			this.info.startX = 0;
			this.info.startY = 0;
			this.info.destX = 0;
			this.info.destY = 0;
			this.info.moveFrame = 0;
			this.info.slideState = false;
		},


	/* 정보를 설정하는 함수 */	
	SetInfo:
		function(obj_id)	{
			obj_id.style.width = this.info.Width;
			obj_id.style.height = this.info.Height;
			obj_id.style.left = this.info.Left;
			obj_id.style.top = this.info.Top;
		},

	SetValue:
		function(w, h, l, t)	{
			this.info.Width = w;
			this.info.Height = h;
			this.info.Left = l;
			this.info.Top = t;
		},

	/* 정보를 가져오는 함수 */	
	GetInfo:
		function(obj_id)	{
	
			if (obj_id.currentStyle) { //IE
			//val = tg.currentStyle.left;
			this.info.Width = obj_id.currentStyle.width.replace("px", "");	
			this.info.Height = obj_id.currentStyle.height.replace("px", "");	
			this.info.Left = obj_id.currentStyle.left.replace("px", "");	
			this.info.Top = obj_id.currentStyle.top.replace("px", "")
				
			} else { //FF
			//val = document.defaultView.getComputedStyle(tg, '').getPropertyValue("left");
			var css = document.defaultView.getComputedStyle(document.getElementById(obj_id), null);

			this.info.Width = css.getPropertyValue("width").replace("px", "");	
			this.info.Height = css.getPropertyValue("height").replace("px", "");	
			this.info.Left = css.getPropertyValue("left").replace("px", "");	
			this.info.Top = css.getPropertyValue("top").replace("px", "")
			}
					
		},

	/* animation을 선택해주는 함수*/
	Animate : 
		function (obj_id, effect_id)
		{
			this.obj = obj_id;
			switch(effect_id)
			{
				case this.ANIM_SLIDE:
					this.SetSlideMoveFrame();
					this.Slide( );
					break;
				case this.ANIM_MOVE:
					this.Move();
					break;
				case this.ANIM_PULSATE:
					this.Pulsate();
					break;
				case this.ANIM_FADEIN:
					this.FadeIn();
					break;
				case this.ANIM_FADEOUT:
					this.FadeOut();
					break;
				case this.ANIM_RESIZE:
					this.Resize();
					break;
			}
		},

	SetSlideMoveFrame : 
		function(){
			this.info.moveX = Math.round( (Number(this.info.destX) - Number(this.info.startX)) / Number(this.info.moveFrame) );
			this.info.moveY = Math.round( (Number(this.info.destY) - Number(this.info.startY)) / Number(this.info.moveFrame) );
		},
	/* value1 = start left, value2 = start top, value3=tartget left, value4 = target top, value5 = moving interval  */
	Slide : 
		function ()
		{
			var obj_id = this.obj;

			if (this.info.startX > this.info.destX ){
				this.info.startX = this.info.destX;
				this.slideState = false;
			}else if (this.info.startY > this.info.destY){
				this.info.startY = this.info.destY;
				this.slideState = false;
			}else{
				this.slideState = true
			}
			this.info.startX = (Number(this.info.startX)+Number(this.info.moveX));	
			this.info.startY = (Number(this.info.startY)+Number(this.info.moveY));	
			obj_id.style.left = this.info.startX;
			obj_id.style.top = this.info.startY;

			if (this.slideState)setTimeout("SDS.Anim.Slide()", this.info.SetTimer);
			
			
		},

	/* value1 = left, value2 = top  */
	Move : 
		function ()
		{
			this.obj.style.left = Number(this.info.Left);	
			this.obj.style.top = Number(this.info.Top);
		},
		
	Pulsate : 
		function ()
		{	
		try{
			var obj_id = this.obj;
			if (obj_id.style.display != "none"){
				obj_id.style.display = "none";
				setTimeout("SDS.Anim.Pulsate()", this.info.SetTimer);
			}else{
			//alert(this.obj);
				obj_id.style.display = "block";
			}
		}catch(e){
			alert(e)
			}
			
		},

	FadeIn : 
		function () {
			//alert("FadeIn");
			this.obj.filters.blendTrans.duration = this.info.Duration;  		
			this.obj.filters.blendTrans.apply(); 
			this.obj.style.visibility = "visible"; 
			this.obj.filters.blendTrans.play(); 
		},
		
	FadeOut : 
		function (value1) { 
			//alert("FadeOut");
			this.obj.filters.blendTrans.duration = this.info.Duration;  		
			this.obj.filters.blendTrans.apply(); 
			this.obj.style.visibility = "hidden"; 
			this.obj.filters.blendTrans.play(); 
		},

	/* value1 = resize %   ex) 150 =>> 150%, 20 ==> 20%*/
	Resize : 
		function ()
		{	
		//alert("Resize");		
			this.obj.style.width = this.info.Width;
			this.obj.style.height = this.info.Height;
		}
		
}

/*Paging, Sorting 등 일반 유틸 */
SDS.Util  = {

	// Module API - Page Count
	// Input  : itemCount, displayCount
	pagingPageCount : 
		function (itemCount, displayCount)
		{
		    var totalPageCount;

		    if (displayCount < 1)
		        totalPageCount = 0;
		    else
		        totalPageCount = Math.ceil(itemCount / displayCount);

		    return totalPageCount;
		},

	// Module API - Start Index
	// Input  : displayCount, currentPage
	pagingStartIndex : 
		function (displayCount, currentPage)
		{
		    var startIndex;

		    if (displayCount < 1)
		        startIndex = -1;
		    else
		        startIndex = displayCount * (currentPage - 1);

		    return startIndex;
		},

		// Module API - List Sort
// Input  : theList, newList, sortType (1 : ascending, 2 : descending)
	listSort : 
		function (theList, newList, sortType)
		{
		    var i, j;
		    var err = "ERROR";
		    var rok = "OK";
		    var swp, tmp, num, val;
			var SORT_ASC = 1;
			var SORT_DESC = 2;

		    // check input array size
		    if (theList.length < 1)
		        return err;

		    // check, if input string list is numeric
		    num = true;
		    for (i = 0; i < theList.length; i++)
		    {
		        tmp = theList[i];
		        if (!Number(tmp))
		        {
		            num = false;
		            break;
		        }
		    }

		    // if each data list is numeric, sort it
		    if (num)
		    {
		        swp = theList;
		        for (i = 0; i < swp.length; i++)
		        {
		            for (j = 0; j < swp.length - 1; j++)
		            {  
		                if (parseInt(swp[j]) > parseInt(swp[j + 1]))
		                {
		                    val        = swp[j + 1];
		                    swp[j + 1] = swp[j];
		                    swp[j]     = val;
		                }
		            }
		        }
		    }
		    else
		        swp = theList.sort();
		    
		    switch (sortType)
		    {
		    case SORT_ASC:
		        for (i = 0; i < swp.length; i++)
		            newList[i] = swp[i];
		        break;
		    case SORT_DESC:
		        for (i = 0; i < swp.length; i++)
		            newList[i] = swp[swp.length - i - 1];
		        break;
		    default:
		        return err;
		    }

		    return rok;
		}
		
}

SDS.Anim.SetDefault();
