var popularUrl = '/ts/ts_search.do?method=getPopular';
var upKeywordUrl = '/ts/ts_search.do?method=upKeyword';
var poplist1 = [];
var poplist2 = [];
var rollingStat = "top";
var DIVID = "";
var COUNT = 5;
var LOOPWAIT = 500;
var ROLLINGWAIT = 100; 
var TOTALCOUNT = 10;
var rtsObject;
var upObject;
var rts;
var upKeyword;
var MENU_TYPE;

var RealTimeSearch=function() {
	if (typeof(Function.prototype.apply)=="undefined") {
		Function.prototype.apply = function(object, parameters) {
			var parameterStrings = new Array();
			if (!object)     object = window;
			if (!parameters) parameters = new Array();
			for (var i = 0; i < parameters.length; i++) parameterStrings[i] = 'parameters[' + i + ']';
			object.__apply__ = this;
			var result=eval('object.__apply__('+ parameterStrings.join(', ') +')');
			object.__apply__ = null;
			return result;
		}
	}
	 
	if(typeof([].push) == "undefined") Array.prototype.push = function(v){return this[this.length] = v}
	 
	if(typeof(Function.prototype.bind)=="undefined") { 
		Function.prototype.bind = function(object) { 
			var __method = this;
			return function() { 
				return __method.apply(object, arguments);
			}
		}
	}
	 
	var __U2base_version__=0.2;
	var __U2base_use__=(typeof(U2)=="undefined" ? true : (__U2base_version__>U2.version ? true : false));
	if(__U2base_use__==true) {
		var U2={
			version : __U2base_version__,
			_Class : {},
			_Event : {},
			_Element : {},
			_Package : {}
		};
	 
		U2._Class={
			Create : function() { 
				var Methods=arguments[0];
				var Extends=arguments;
				var returnObj=function(){
					if(this.Constructor) this.Constructor.apply(this,arguments);
				} 
				if(Extends.length>1) {for (var i=1;i<Extends.length;i++){returnObj=this.Extend(returnObj,Extends[i]);}}
				returnObj=this.Extend(returnObj,Methods);
				return returnObj;
			},
			Extend : function(returnObj/*, Define Method */) {
				var Methods=arguments[1];
				var methodName=null;
				if(typeof returnObj!="function") {throw new Error("[_Class Function] Arguments[0] is not Function");}
				if(typeof Methods!="object") {throw new Error("[_Class Function] Arguments[1] is not Object");} 		
				for(var methodName in Methods) {returnObj.prototype[methodName]=Methods[methodName];} 
				return returnObj;
			}
		};
	 
		U2._Element={
			getBadBrowser : function() {
				if(navigator.userAgent.toLowerCase().indexOf("msie 5.0")>0 || navigator.userAgent.toLowerCase().indexOf("msie 5.5")>0) {return true;}else{return false;}
			},
			getBrowser : function() {
				if(navigator.userAgent.toLowerCase().indexOf("msie")>=0) {
					return "ie";
				}else if (navigator.userAgent.toLowerCase().indexOf("firefox")>=0) {
					return "ff";
				}else if (navigator.userAgent.toLowerCase().indexOf("opera")>=0) {
					return "op";
				}else if (navigator.userAgent.toLowerCase().indexOf("safari")>=0) {
					return "sf";
				}
			},
			getStyle : function(element, property) {
				if (element.style[property]) {
					return element.style[property]
				} else if (element.currentStyle) {
					return element.currentStyle[property]
				} else if (document.defaultView && document.defaultView.getComputedStyle) {
					var style = document.defaultView.getComputedStyle(element, null)
					return style.getPropertyValue(property)
				} else {
					return null
				}
			},
			setStyle : function(element, styles,sw) {
				var e;
				if(!sw) { // ie 5.0
					if(this.getBadBrowser()==true) {
						e=element.id!="" ? document.getElementById(element.id) : element;
					}else{
						e=element;
					}
				}else{
					e=element;
				}
	 
				for(var styleName in styles) {
					if(this.getBadBrowser()==true) {if(styleName=="cursor") styleName="hand";}
					e.style[styleName]=styles[styleName];
				} 
			},
			setStyles : function() {
				var aAs=arguments;
				for (var i=0;i<aAs.length;i++) {
					var sVarguments=aAs[i];
					U2._Element.setStyle(sVarguments[0],sVarguments[1]);
				}
			}
		};
	};

	var Rolling=U2._Class.Create({
		Constructor : function() {
			this._def={
				oP : arguments[0],
				iD : arguments[1],
				oFrame : {
					oDomUl : null,
					oDomLi01 : null,
					oDomLi02 : null
				},
				oTid : null,
				iHeight : 18,
				iMove : 0,
				iStatus : 0
			}
			this.Initialize();		
		},
		Initialize : function() {
			if(typeof(this._def.oUl)=="undefined") this.setDraw("newLine",this._def.oP);
		},
		setDraw : function() {
			var s=arguments[0],o=arguments[1];
			switch(s) {
				case "newLine" :
					var cUl=this._def.oFrame.oDomUl=document.createElement("li");
					var cLi01=this._def.oFrame.oDomLi01=document.createElement("p");
					var cLi02=this._def.oFrame.oDomLi02=document.createElement("p");
					var iWidth=U2._Element.getBrowser()=="ie" ? "202" : "182";
					U2._Element.setStyles(
						[cUl,{position : "relative",height : this._def.iHeight+"px",overflow : "hidden"}],
						[cLi01,{position : "absolute",top : "0px"}],
						[cLi02,{position : "absolute",top : this._def.iHeight+"px"}]
					);
					cUl.appendChild(cLi01);
					cUl.appendChild(cLi02);
					o.appendChild(cUl);
				break;
			}
		},
		setChange : function() {
			if(this._def.iStatus==0) {
				this._def.oFrame.oDomLi01.innerHTML=this._def.oFrame.oDomLi02.innerHTML;
			}else{
				this._def.oFrame.oDomLi02.innerHTML=this._def.oFrame.oDomLi01.innerHTML;
			}
			this._def.iStatus=(this._def.iStatus==1 ? 0 : 1);
		},
		setValue : function() {
			var oValue=arguments[0];
			var flag=arguments[1];
			if(typeof(oValue)!="undefined") {
					var sHtml  = "	<span class=\"rank\"><var>"+oValue.rank+"</var></span> \n";
						sHtml += "	<span class=\"word\"><strong><a href=\"/ts/ts_search.do?wtClick_index=149&keyword="+oValue.title+"&menuType="+MENU_TYPE+"\">"+oValue.title+"</a></strong></span> \n";
						sHtml += "	<span class=\"stat "+oValue.status+"\"><var>"+oValue.num+"</var></span> \n";

				if(flag==1) {
					this._def.oFrame.oDomUl.className="o"+oValue.rank;
					this._def.oFrame.oDomLi01.innerHTML=this._def.oFrame.oDomLi02.innerHTML=sHtml;
				}else{
					if(this._def.iStatus==0) {
						this._def.oFrame.oDomLi02.className=oValue.className;
						this._def.oFrame.oDomLi02.innerHTML=sHtml;
					}else{
						this._def.oFrame.oDomLi01.className=oValue.className;
						this._def.oFrame.oDomLi01.innerHTML=sHtml;
					}
				}
			}
		},
		onFinished : function() {},
		doRolling : function() {
			var iTime=U2._Element.getBadBrowser()=="ff" ? 200 : 20;
			this._def.oTid=setInterval(this._doMotion.bind(this),iTime);
		},
		_doMotion : function() {
			this._def.iMove++;
			if(this._def.iStatus==0) {
				U2._Element.setStyles(
					[this._def.oFrame.oDomLi01,{top:this._def.iMove+"px",filter:"alpha(opacity:60)",opacity:"0.6"}],
					[this._def.oFrame.oDomLi02,{top:(this._def.iMove-this._def.iHeight)+"px",filter:"",opacity:""}]
				);
			}else{
				U2._Element.setStyles(
					[this._def.oFrame.oDomLi02,{top:this._def.iMove+"px",filter:"alpha(opacity:60)",opacity:"0.6"}],
					[this._def.oFrame.oDomLi01,{top:(this._def.iMove-this._def.iHeight)+"px",filter:"",opacity:""}]
				);
			}

			if(this._def.iMove==this._def.iHeight) {
				this._def.iMove=0;
				clearInterval(this._def.oTid);
				this.setChange();
				this.onFinished();
			}
		}
	});

	var RollingAdmin=U2._Class.Create({
		Constructor : function() {
			var oId=document.getElementById(arguments[0]);
			var options=arguments[1];
			var sInitailizeValue=arguments[2];
			if(U2._Element.getBadBrowser()=="ff") options.loopWait *=10;
			if(U2._Element.getBadBrowser()=="ff") options.rollingWait *=10;

			this._def={
				oFrame : oId,
				oRolling : [],
				oData : [],
				oTid : null,
				oTid2 : null,
				aTmpValue : [],
				iRolling : 0,
				iCount : options.count,
				iLoopWait : options.loopWait,
				iRollingWait : options.rollingWait,
				iTotalCount : options.totalcount,
				iTimer : 0
			}

			this.Initialize(sInitailizeValue);
		},
		Initialize : function() {
			try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}
			var aValue=arguments[0];
			var oUls=this._def.oFrame.getElementsByTagName("ul");
			this._def.oFrame.innerHTML="";
			for(var i=0;i<this._def.iCount;i++) {
				this._def.oRolling[i]=new Rolling(this._def.oFrame,i);
				this._def.oRolling[i].setValue(aValue[i],1);
			}
			this.setTimer();
			this.doStart();
		},
		setApplyTmpValue : function() {
			//alert("setApplyTmpValue");·Ñ¸µ ´Ù ÇÏ¸é È£Ãâ
			for(var i=0;i<this._def.iCount;i++) {
				this._def.oRolling[i].setValue(this._def.aTmpValue[i]);
			}
		},
		setTmpValue : function() {
			//º¯°æ È£Ãâ
			this._def.aTmpValue=arguments[0];
		},
		setValue : function() {
			var id=arguments[0]-1;	
			var oValue=arguments[1];
			this._def.oRolling[id].setValue(oValue);
		},
		getSleep : function (sec) {
			if(sec> 60*60) return 600;
			if(sec> 60*5) return 60;
			if(sec< 60*1) return 10;
			if(sec< 60*2) return 20;
			if(sec< 60*3) return 30;
			if(sec< 60*4) return 40;
			if(sec< 60*5) return 50;
		},
		setTimer : function () {},
		doStart : function() {
			//alert("doStart");	·Ñ¸µ ½ºÅ¸Æ®
			this._def.oRolling[this._def.iRolling].onFinished=function() {
				this._def.iRolling++;
				if(this._def.iRolling==this._def.iCount) { // end 
					this._def.iRolling = 0;
					if(this._def.oTid2 == null) {
						this.setApplyTmpValue();
						this._def.oTid2=setTimeout(this.doStart.bind(this),this._def.iLoopWait);
					}
				}else{
					this._def.oTid2=null;
					this._def.oTid=setTimeout(this.doStart.bind(this),this._def.iRollingWait);
				}
			}.bind(this);
			this._def.oRolling[this._def.iRolling].doRolling();

		}
	});

	this.Admin=RollingAdmin;
}


function getData ( divId , viewCnt , lwait , rWait , totcnt , type, change , menuType ){
	var getUrl;
	DIVID = divId;
	COUNT = viewCnt;
	LOOPWAIT = lwait; 
	ROLLINGWAIT = rWait; 
	TOTALCOUNT = totcnt;
	MENU_TYPE = menuType;
	if(type == 'upKeyword'){
		getUrl = upKeywordUrl;
	}else{
		getUrl = popularUrl;
	}

	var httpObj = new Ajax.Request (
			getUrl, {
			method : 'post',
			onSuccess: function(responseHttpObj) {

				var returnData = responseHttpObj.responseText.trim();
				returnData = eval("("+returnData+")");

				if(viewCnt == totcnt){
					for(i=0; i<viewCnt; i++){
						poplist1[i] = returnData[i];
					}
				}else{
					for(i=0; i<viewCnt; i++){
						poplist1[i] = returnData[i];
					}

					var num = 0;
					for(i=viewCnt; i<totcnt; i++){
						poplist2[num++] = returnData[i];
					}
				}
				
				if(type == 'upKeyword'){
					upKeyword=new RealTimeSearch();
					upObject = new upKeyword.Admin(divId,{ "count" : COUNT , "loopWait" : LOOPWAIT , "rollingWait" : ROLLINGWAIT , "totalcount" : TOTALCOUNT },poplist1);
				}else{
					rts=new RealTimeSearch();
					rtsObject = new rts.Admin(divId,{ "count" : COUNT , "loopWait" : LOOPWAIT , "rollingWait" : ROLLINGWAIT , "totalcount" : TOTALCOUNT },poplist1);
				}
			},
			onFailure: function(){}
		}
	)
}

function up() {
	rtsObject = new upKeyword.Admin('popularKeyword',{ "count" : COUNT , "loopWait" : LOOPWAIT , "rollingWait" : ROLLINGWAIT , "totalcount" : TOTALCOUNT },poplist1);
}

function down() {
	rtsObject = new upKeyword.Admin('popularKeyword',{ "count" : COUNT , "loopWait" : LOOPWAIT , "rollingWait" : ROLLINGWAIT , "totalcount" : TOTALCOUNT },poplist2);
}

function changeRank(fName){
	if(fName == 'up'){
		$("changeRank").update("<a href=\"javascript:changeRank('down')\"></a>"); 
	}else{
		$("changeRank").update("<a href=\"javascript:changeRank('up')\"></a>"); 
	}
	eval(fName+"();");
}


