/*
*
* Stimpack ver 2.0
* reflexion for standard Javascript framework
* Reflexion co.
* make by Dstyle , jejette
*/

var stimpack = new Object();

/*------------------------------Stimpack :: RemoteCtr :: START :: prototype Base --------------------------------------------*/
stimpack.remoteCtr = remoteCtrFn;
function remoteCtrFn(){
	var remoteObj;
	var objWid;
	var objHei;
	var objTop = 0;
	var objBtm = 0;
	var topLimit = 0;
	var btmLimit = 0;
	var moveTop;
	var speedNum = 5;
	var sInterval;
	var Body = document.body ? document.documentElement : document.body ;
	var sideChk = false;
	
	//엘리먼트 (또는 아이디) , 가로값 , 세로값
	this.init = function(oObj,oWid,oHei){
		remoteObj = $(oObj);
		objWid = oWid;
		objHei = oHei;
	};
	
	//실제로 적용 구문
	this.show = function(){
		if(!sideChk){
			remoteObj.setStyle({
				"left":"0px"
			});
			sideChk = true;
		};
		remoteObj.setStyle({
			"position":"absolute",
			"display":"block",
			"top":topLimit+"px",
			"width":objWid+"px",
			"height":objHei+"px"
		});
		sInterval = setInterval(this.sliding,"30");
	};
	
	//슬라이딩 함수
	this.sliding = function(){
		moveTop = topLimit + remoteObj.cumulativeScrollOffset().top;
		btmLimit = Body.scrollHeight - remoteObj.getHeight() - objBtm;
		if(moveTop>btmLimit){
			moveTop = btmLimit;
		}
		objTop += ( Number(moveTop) - Number(objTop) )/speedNum; // 이게 슬라이딩 함수
		
		remoteObj.setStyle({
			"top":objTop+"px"
		});
		
	};
	
	//속도조절
	this.speed = function(num){
		if(num){
			speedNum = Number(num);
		}else{
			speedNum = 5;
		}
	};
	
	//상단에서 얼마나 떨어뜨릴것인가.
	this.top = function(num){
		topLimit = Number(num);
	};
	//하단에서 얼마나 떨어뜨릴것인가.
	this.btm = this.bottom = function(num){
		objBtm = Number(num);
	};
	
	this.left = function(num){
		if(!sideChk){
			remoteObj.setStyle({
				"left":num+"px"
			});
			sideChk = true;
		};
	};
	this.right = function(num){
		if(!sideChk){
			remoteObj.setStyle({
				"right":num+"px"
			});
			sideChk = true;
		};
	};
	this.pos = this.position = function(top,right,bottom,left){
		right = Number(right);
		right = Number(left);
		if(!top.empty()){
			this.top(top);
		}
		if(!right.empty() || right!=0){
			this.right(right);
		}
		if(!bottom.empty()){
			this.bottom(bottom);
		}
		if(!left.empty() || left!=0){
			this.left(left);
		}
	};
	this.fixed = function(chkId){
		$(chkId).checked
	};
	
};
/*------------------------------Stimpack :: RemoteCtr :: END :: prototype Base --------------------------------------------*//*------------------------------Stimpack :: SetEmbedCore :: START :: stimpack-Core Base ----------------------------------------*/

stimpack.swfVersion = "10";
stimpack.updateSay = "플래쉬 플레이어의 버젼이 낮습니다.<br /><a href='http://get.adobe.com/kr/flashplayer/' target='_blank'>플래쉬 플레이어 업데이트하기</a>";


//일정 스트링(url를 불러오거나 경로로 선언된 스트링) 에서 특수문자를 빼는 함수
// => 빠진 스트링 그대로 리턴
String.prototype.getSrc = function(str){
	if(str==null){str = this;};
	if(str.indexOf("#")>0)str = str.substr(0,str.indexOf("#"));
	if(str.indexOf("&")>0)str = str.substr(0,str.indexOf("&"));
	if(str.indexOf("?")>0)str = str.substr(0,str.indexOf("?"));
	return str;
}

//일정 스트링(url를 불러오거나 경로로 선언된 스트링) 에서 특수문자를 빼는 함수
// => 빠진 스트링을 "/" 기준으로 배열화 하여 리턴
String.prototype.getArrSrc = function(str){
	if(str==null){str = this;};
	str = str.getSrc();
	str = str.split("/");
	return str;
}

//일정 스트링(url를 불러오거나 경로로 선언된 스트링) 에서 특수문자를 빼는 함수
// => 빠진 스트링을 "/" 기준으로 배열하여 맨 마지막 스트링을 가져오는 함수
String.prototype.getArrSrcLast = function(str){
	if(str==null){str = this;};
	str = str.getArrSrc();
	str = str[str.length-1];
	return str;
}
//일정 스트링(url를 불러오거나 경로로 선언된 스트링) 에서 특수문자를 빼는 함수
// => 빠진 스트링의 확장자를 얻어오는 함수 
String.prototype.getSrcType = function(str){
	if(str==null){str = this;};
	str = str.getArrSrcLast();
	str = str.split(".")[str.split(".").length-1];
	return str;
}
//일정 스트링(url를 불러오거나 경로로 선언된 스트링) 에서 특수문자를 빼는 함수
// => 빠진 스트링의 파일명을 얻어오는 함수 
String.prototype.getSrcFileName = function(str){
	if(str==null){str = this;};
	str = str.getArrSrcLast();
	str = str.split(".")[0];
	return str;
}


/* setEmbed 계열 codeBase 및 MIME type => Json */
stimpack.embedCore = {
	swf:{
		mime:"application/x-shockwave-flash",
		code:"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0",
		down:"http://www.macromedia.com/go/getflashplayer"
	},
	mov:{
		mime:"video/quicktime",
		code:"http://www.apple.com/qtactivex/qtplugin.cab",
		down:"http://www.apple.com/quicktime/download/"
	},
	mplayer:{
		code:"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#version=6,4,7,1112",
		down:"http://www.microsoft.com/korea/windows/windowsmedia/"
	},
	wmv:{
		mime:"video/x-ms-wmv"
	},
	wma:{
		mime:"audio/x-ms-wma"
	},
	mp3:{
		mime:"audio/mpeg"
	},
	asf:{
		mime:"video/x-ms-asf"
	},
	avi:{
		mime:"video/x-msvideo"
	},
	mpeg:{
		mime:"video/mpeg"
	},
	mpg:{
		mime:"video/mpeg"
	},
	gif:{
		mime:"image/gif"
	},
	jpg:{
		mime:"image/jpeg"
	},
	png:{
		mime:"image/png"
	},
	wav:{
		mime:"audio/x-wav"
	},
	mid:{
		mime:"audio/midi"
	}
}
stimpack.getClaid = function(type){
	var getClaid;
	var getMime = this.embedCore[type].mime;
	getMime = getMime.split("/")[0];
	switch(getMime){
		case "application" : {
			getClaid = "D27CDB6E-AE6D-11cf-96B8-444553540000"
		} break;
		case "video" :
		case "audio" : {
			getClaid = "CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"
		} break;
		case "image" : {
			getClaid = "CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
		} break;
	}
	return getClaid;
};
stimpack.getEmbedCore = function(type){
	//embed코어와 classid를 배열로 가져올수 있도록 하는 함수.
	//arr : [mimetype , codebase, plugindownload, claID , type]
	var getType = type.getSrcType();
	var typeArr = new Array;
	typeArr[0] = stimpack.embedCore[getType].mime;
	typeArr[1] = stimpack.embedCore[getType].code;
	if(!typeArr[1]){typeArr[1] = stimpack.embedCore["mplayer"].code};
	typeArr[2] = stimpack.embedCore[getType].down;
	if(!typeArr[2]){typeArr[2] = stimpack.embedCore["mplayer"].down};
	typeArr[3] = stimpack.getClaid(getType);
	typeArr[4] = typeArr[0].split("/")[0];
	return typeArr;
}
String.prototype.getEmbedCore = function(){
	var getType = stimpack.getEmbedCore(this);
	return getType;
}

//flash version check(Flash Player Version Detection)
stimpack.setEmbedSwfVersion = function(){
	var version;
	var axo;
	var e;
	if(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1){
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
		version = version.split(" ")[1];
		version = version.split(",");
		return version;
	}else{
		var flashVer = -1;
		if(navigator.plugins != null && navigator.plugins.length > 0){
			if(navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]){
				var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
				var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
				var descArray = flashDescription.split(" ");
				var tempArrayMajor = descArray[2].split(".");			
				var versionMajor = tempArrayMajor[0];
				var versionMinor = tempArrayMajor[1];
				var versionRevision = descArray[3];
				if (versionRevision == "") {
					versionRevision = descArray[4];
				}
				if (versionRevision[0] == "d") {
					versionRevision = versionRevision.substring(1);
				} else if (versionRevision[0] == "r") {
					versionRevision = versionRevision.substring(1);
					if (versionRevision.indexOf("d") > 0) {
						versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
					}
				}
				var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
			}
		}
		// MSN/WebTV 2.6 supports Flash 4
		else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
		// WebTV 2.5 supports Flash 3
		else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
		// older WebTV supports Flash 2
		else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
		flashVer = flashVer.split(".");
		return flashVer;
	}
}
/*------------------------------Stimpack :: SetEmbedCore :: END :: stimpack-Core Base ------------------------------------------*//*------------------------------Stimpack :: SetEmbed :: START :: prototype Base --------------------------------------------*/
stimpack.setEmbed = setEmbedFn;
function setEmbedFn(){
	var objType;
	var objSrc;
	var objWid;
	var objHei;
	var objId;	
	var objTxt;
	var objTypeParamTxt = "";
	var objFlashVarsTxt = "";
	var objTypeEmbedTxt = "";
	var objEmbedCore;
	var flashCallTimer;
	var swfVersionChk = false;
	var swfVersion;
	
	this.init = function(oSrc,oWid,oHei,oId){
		if(oSrc){
			objWid = oWid;
			if(!objWid){objWid = "100%"};
			objHei = oHei;
			if(!objHei){objHei = "100%"};
			objSrc = oSrc.getSrc();
			objType = oSrc.getSrcType();
			if(!oId){oId = oSrc.getSrcFileName();};
			objId = oId;
			objEmbedCore = stimpack.getEmbedCore(objSrc);
		};
	};
	
	this.objTypeParam = function(type,arg){
		switch(type){
			case "application" : {
				objTypeParamTxt += "<param name='movie' value='"+ arg + "'>\n";
				objTypeParamTxt += "<param name='quality' value='high'>\n";
				objTypeEmbedTxt += "quality='high'"
			} break;
			case "video" : {
				objTypeParamTxt += "<param name='filename' value='"+ arg + "'>\n";
			} break;
			case "audio" : {
				objTypeParamTxt += "<param name='filename' value='"+ arg + "'>\n";
			} break;
		}
	};
	
	
	this.param = function(parm , value){
		objTypeParamTxt += "<param name='"+parm +"' value='"+ value + "'>\n";
		objTypeEmbedTxt += parm + "='" + value + "'";
	};
	
	
	this.show = function(elemId){
		var objTxt;
		if(!swfVersionChk){
			if(objEmbedCore[4]=="application"){
				swfVersionChk = false;
				swfVersion = stimpack.setEmbedSwfVersion();
				if(swfVersion[0]==stimpack.swfVersion){
					swfVersionChk = true;
				}else{
					swfVersionChk = false;
				};
			}else{
				swfVersionChk = true;
			};
		};
		if(swfVersionChk){
			//flashvars 를 체크해서 값이 있다면 추가한다.
			if(objFlashVarsTxt){
				objTypeParamTxt += "<param name='flashVars' value='"+objFlashVarsTxt+"'>\n";
				objTypeEmbedTxt += "flashVars='"+objFlashVarsTxt+"'";
			}
			
			//특정 타입에 따라 자동으로 저장된 파라매터를 불러온다.
			this.objTypeParam(objEmbedCore[4],objSrc);
			
			//실제 뿌리는 소스를 만드는 구문
			if(objEmbedCore[4]!="image"){
				objTxt = "<object type='"+objEmbedCore[0]+"' codeBase='"+objEmbedCore[1]+"' data='"+objSrc+"' width='"+objWid+"' height='"+objHei+"' id='"+objId+"'>";
				objTxt += objTypeParamTxt;
				objTxt += "<embed src='"+objSrc+"' type='"+objEmbedCore[0]+"' width='"+objWid+"' height='"+objHei+"' id='"+objId+"' pluginspage='"+objEmbedCore[2]+"' "+objTypeEmbedTxt+" />";
				objTxt += "</object>";
			}else{
				objTxt = "<img src='"+objSrc+"' width='"+objWid+"' height='"+objHei+"' alt='"+objTypeParamTxt+"' id='"+objId+"' />";
			}
			eval("window." + objId + " = document.getElementById('" + objId + "');");
		}else{
			objTxt = stimpack.updateSay;
		}
		document.write(objTxt);
	};
	
	this.show2 = function(elemId){
		var objTxt;
		if(!swfVersionChk){
			if(objEmbedCore[4]=="application"){
				swfVersionChk = false;
				swfVersion = stimpack.setEmbedSwfVersion();
				if(swfVersion[0]==stimpack.swfVersion){
					swfVersionChk = true;
				}else{
					swfVersionChk = false;
				};
			}else{
				swfVersionChk = true;
			};
		};
		if(swfVersionChk){
			//flashvars 를 체크해서 값이 있다면 추가한다.
			if(objFlashVarsTxt){
				objTypeParamTxt += "<param name='flashVars' value='"+objFlashVarsTxt+"'>\n";
				objTypeEmbedTxt += "flashVars='"+objFlashVarsTxt+"'";
			}
			
			//특정 타입에 따라 자동으로 저장된 파라매터를 불러온다.
			this.objTypeParam(objEmbedCore[4],objSrc);
			
			//실제 뿌리는 소스를 만드는 구문
			if(objEmbedCore[4]!="image"){
				objTxt = "<object type='"+objEmbedCore[0]+"' codeBase='"+objEmbedCore[1]+"' data='"+objSrc+"' width='"+objWid+"' height='"+objHei+"' id='"+objId+"'>";
				objTxt += objTypeParamTxt;
				objTxt += "<embed src='"+objSrc+"' type='"+objEmbedCore[0]+"' width='"+objWid+"' height='"+objHei+"' id='"+objId+"' pluginspage='"+objEmbedCore[2]+"' "+objTypeEmbedTxt+" />";
				objTxt += "</object>";
			}else{
				objTxt = "<img src='"+objSrc+"' width='"+objWid+"' height='"+objHei+"' alt='"+objTypeParamTxt+"' id='"+objId+"' />";
			}
			eval("window." + objId + " = document.getElementById('" + objId + "');");
		}else{
			objTxt = stimpack.updateSay;
		}
		return objTxt;
	};
	
	
	/**** 플래쉬 관련 setEmbed param method ****/
	this.w = this.wmode = function(){
		if(objEmbedCore[4]=="application"){
			objTypeParamTxt += "<param name='wmode' value='transparent'>\n";
			objTypeEmbedTxt += "wmode='transparent'";
		};
	};
	
	this.s = this.scriptAcc = function(){
		if(objEmbedCore[4]=="application"){
			objTypeParamTxt += "<param name='allowScriptAccess' value='always'>\n";
			objTypeEmbedTxt += "allowScriptAccess='always'";
		};
	};
	
	this.fv = this.flashVars = function(val,valueName){
		if(objEmbedCore[4]=="application"){
			if(!valueName){valueName= "xmlURL";}
			if(!objFlashVarsTxt){
				objFlashVarsTxt = valueName+"="+val;
			}else{
				objFlashVarsTxt += "&"+valueName+"="+val;
			};
		};
	};
	this.aFv = this.allParam = function(val,valueName){
		if(objEmbedCore[4]=="application"){
			this.wmode();
			this.scriptAcc();
			this.flashVars(val,valueName);
		};
	};
	this.updateOff = function(say){
		swfVersionChk = true;
		if(say){
			stimpack.updateSay = say;
		}
	};
	
	/**** 플래쉬 관련 setEmbed after method ****/
	this.hook = function(){
		if (window.addEventListener){
			window.addEventListener('DOMMouseScroll', this.hookCtr, false);
		};
		window.onmousewheel = document.onmousewheel = this.hookCtr;
	}
	
	this.hookCtr = function(event){
		var eventElem;
		if (!event) { event = window.event; };
		if(event.srcElement){
			eventElem = event.srcElement;
		}else if(event.target){
			eventElem = event.target;
		};
		var hookId = objId;
		if(objId == eventElem.id){
			if (event.stopPropagation) { event.stopPropagation() };
		    if (event.preventDefault) { event.preventDefault() };
			event.cancelBubble = true;
		    event.cancel = true;
		    event.returnValue = false;
		}else{
			return false;
		};
	};
	
	var flashCallFunc;
	this.fc = this.flashCall = function(func){
		flashCallFunc = func;
		flashCallTimer = setInterval(this.flashCallApp, 1000);
	}
	
	this.flashCallApp = function(){
		var resultApp = null;
		try{
			var flashAppObj = document.getElementById(objId);
			if(Prototype.Browser.Gecko){
				flashAppObj = flashAppObj.getElementsByTagName("embed")[0];
			}
			resultApp = flashCallFunc.call(null,flashAppObj,false);
		}catch (e) {
		}
		if(resultApp){			
			clearInterval(flashCallTimer);
		};
	}
	
	/**** 이미지 관련 setEmbed method ****/
	this.alt = function(arg){
		if(objEmbedCore[4]=="image"){
			objTypeParamTxt = arg;
		};
	};
	
	
	/**** 미디어 관련 setEmbed method ****/
	this.hc = this.hideControl = function(){
		if(objEmbedCore[4]=="audio" || objEmbedCore[4]=="video"){
			objTypeParamTxt += "<param name='ShowControls' value='false'>\n";
		};
	};
	this.as = this.autoStop = function(){
		if(objEmbedCore[4]=="audio" || objEmbedCore[4]=="video"){
			objTypeParamTxt += "<param name='autoStart' value='0'>\n";
		};
	};
	this.loop = function(arg){
		if(objEmbedCore[4]=="audio" || objEmbedCore[4]=="video"){
			if(!arg){arg = "1000"};
			objTypeParamTxt += "<param name='loop' value='"+arg+"'>\n";
		};
	};
	this.vol = this.volume = function(arg){
		if(objEmbedCore[4]=="audio" || objEmbedCore[4]=="video"){
			if(!arg){arg = "100"};
			objTypeParamTxt += "<param name='Volume' value='"+arg+"'>\n";
		};
	};
};

/*------------------------------Stimpack :: SetEmbed :: END :: prototype Base --------------------------------------------*/

stimpack.popupResize = _popupResize;
function _popupResize(){
	
	this.init = function(container, getWid, getHei) {
		var setWidth = 0;
		var setHeight = 0;
		
		if(getWid) setWidth = getWid;
		else setWidth = $(container).getWidth();
		
		if(getHei) setHeight = getHei;
		else setHeight = $(container).getHeight();
		
		window.resizeTo(setWidth, setHeight);
	}
}

stimpack.tab = _Tab;
function _Tab() {
	this.container = null;
	
	this.init = function(container, initIndex) {
		this.container = $(container);
		var oldIndex = 0;
		if(initIndex) oldIndex = initIndex; 
		
		var tabTitle = this.container.down(".tabTitle");
		var tabContent = this.container.select(".legend");
	
		tabContent.each(function(item, i) {
			if(i == oldIndex) {
				this.onEvent(i);					
			} else {
				this.offEvent(i);					
			}
		}.bind(this));

		tabTitle.select("li").each(function(item, i) {
			item.observe("click", function() {
				if(i != oldIndex) {
					this.onEvent(i);
					this.offEvent(oldIndex);
					oldIndex = i;
				}
			}.bind(this));			
		}.bind(this));
	};
	
	this.onEvent = function(index) {			
		var title = this.container.down(".tabTitle").down("li", index);
		title.addClassName("on");
		this.container.down(".legend", index).show();
		if(title.down("img")) this.imgChange("on", title.down("img"));
		
	};
	
	this.offEvent = function(index) {
		var title = this.container.down(".tabTitle").down("li", index);
		title.removeClassName("on");
		this.container.down(".legend", index).hide();
		if(title.down("img")) this.imgChange("off", title.down("img"));
	};
	
	this.imgChange =  function(state, obj) {
		if(state == "on") {
			obj.src = obj.src.replace(".gif", "On.gif");
		} else if(state == "off") {
			obj.src = obj.src.replace("On.gif", ".gif");
		}
	};
	
	
}
