var highlightLoad = Class.create({
	initialize:function(){
		this.curMenuType = -1;
		this.curRankDate = "2000-01-01";
		this.curVideo = 0;
		this.interID = 0;
		this.interDelay = 5000;
		this.rankInfo = new Array();
	},
	autoSlide:function(){
		this.delSlide();
		this.interID = window.setInterval(function(){this.next();}.bind(this),this.interDelay);
	},
	delSlide:function(){
		window.clearInterval(this.interID);
	},
	send:function(menuType,rankDate){
		if(this.curMenuType != menuType || this.curRankDate != rankDate){
			new Ajax.Request('/index/highlight.html',{
				parameters:{
					menuType:menuType,
					rankDate:rankDate
				},
				onSuccess:function(res){
					$("highlight").update(res.responseText);
				}
			}) ;
		}
		this.curMenuType = menuType;
		this.curRankDate = rankDate;
	},
	wordOver:function(u,link,thisObj,style){
		$$("#highlight .rank ." + style).each(function(s){
			s.className = style;
		});
		$("myThumb").src = u;
		$("myThumb").onclick = function(){
			document.location.href=link;
		}
		thisObj.className = style + " select";
	},
	wordOver2:function(thisObj,style){
		$$("#highlight .rank ." + style).each(function(s){
			s.className = style;
		});
		thisObj.className = style + " select2";
	},
	prev:function(){
		var contents = "";
		var videoNum = 0;
		var startNum = 0;
		var nomargin;
		if(this.curVideo-3 < 0){
			startNum = this.rankInfo.length + this.curVideo-3;
		}else{
			startNum = this.curVideo-3
		}
		contents += '<ul onMouseover="highlightLoad.delSlide();" onMouseout="highlightLoad.autoSlide();">';
		for (var iNum = startNum;iNum < (startNum+3) ;iNum++ )
		{
			if (iNum == startNum+2)
			{
				nomargin = "nomargin";
			}else{
				nomargin = "";
			}
			videoNum = iNum % this.rankInfo.length;
			contents += '<li class="lm1 ' + nomargin + '">';
			contents += '<p class="thumb">'
			contents += '<a href="/'+this.rankInfo[videoNum].video_link+'_v'+this.rankInfo[videoNum].video_idx+'/" class="play"><img src="/img/index/miniplay.gif" alt="'+this.rankInfo[videoNum].video_subjectEncode+'"/></a>'
			contents += '<a href="/'+this.rankInfo[videoNum].video_link+'_v'+this.rankInfo[videoNum].video_idx+'/"><img src="'+this.rankInfo[videoNum].video_thumb+'" class="img1" alt="'+this.rankInfo[videoNum].video_subjectEncode+'" /></a>';
			contents += '</p>';
			contents += '<p class="t1"><a href="/'+this.rankInfo[videoNum].video_link+'_v'+this.rankInfo[videoNum].video_idx+'/" title="'+this.rankInfo[videoNum].video_subjectEncode+'">'+this.rankInfo[videoNum].video_subject+'</a></p>';
			contents += '</li>';
		}
		contents += "</ul>";
		$("highlightVideo").update(contents);
		this.curVideo = startNum;
		this.autoSlide();
	},
	next:function(){
		var contents = "";
		var videoNum = 0;
		var nomargin;
		contents += '<ul onMouseover="highlightLoad.delSlide();" onMouseout="highlightLoad.autoSlide();">';
		for (var iNum = (this.curVideo+3);iNum < (this.curVideo+6) ;iNum++ )
		{
			if (iNum == this.curVideo+5)
			{
				nomargin = "nomargin";
			}else{
				nomargin = "";
			}
			videoNum = iNum % this.rankInfo.length;
			contents += '<li class="lm1 ' + nomargin + '">';
			contents += '<p class="thumb">'
			contents += '<a href="/'+this.rankInfo[videoNum].video_link+'_v'+this.rankInfo[videoNum].video_idx+'/" class="play"><img src="/img/index/miniplay.gif" alt="'+this.rankInfo[videoNum].video_subjectEncode+'" /></a>'
			contents += '<a href="/'+this.rankInfo[videoNum].video_link+'_v'+this.rankInfo[videoNum].video_idx+'/"><img src="'+this.rankInfo[videoNum].video_thumb+'" class="img1" alt="'+this.rankInfo[videoNum].video_subjectEncode+'" /></a>';
			contents += '</p>';
			contents += '<p class="t1"><a href="/'+this.rankInfo[videoNum].video_link+'_v'+this.rankInfo[videoNum].video_idx+'/" title="'+this.rankInfo[videoNum].video_subjectEncode+'">'+this.rankInfo[videoNum].video_subject+'</a></p>';
			contents += '</li>';
		}
		
		contents += "</ul>";
		$("highlightVideo").update(contents);
		this.curVideo = (this.curVideo+3) % this.rankInfo.length;
		this.autoSlide();
	}
});






var featureLoad = Class.create({
	initialize:function(){
		this.curMenuType = -1;
		this.curSiteType = -1;
	},
	send:function(menuType,siteType,bestType){
		if(this.curMenuType != menuType || this.curSiteType != siteType || this.curBestType != bestType){
			if (menuType == 0 && siteType == -1)
			{
				new Ajax.Request('/saveHtml/featured_' + bestType.replace("-","") + '.html',{
				onSuccess:function(res){
						$("featured").update(res.responseText);
						this.goviral();
					}.bind(this)
				}) ;
			}else{
				new Ajax.Request('/index/featured.html',{
					parameters:{
						menuType:menuType,
						siteType:siteType,
						bestType:bestType
					},
					onSuccess:function(res){
						$("featured").update(res.responseText);
						this.goviral();
					}.bind(this)
				}) ;
			}
		}
		this.curMenuType = menuType;
		this.curSiteType = siteType;
		this.curBestType = bestType;
	},
	goviral:function(){
		$("featured").select("div.viral table.list a").each(function(s){
			var target = s.href;
			s.href = "javascript:void(0);";
			s.onclick = function(){
				featureLoad.popup(target);
			}
		});
	},
	popup:function(value){
		this.close();

		var size=this.getSize();
		var alphaWrapper=new Element("DIV", { "id":"alphaWrapper" });
		alphaWrapper.observe("click", this.close);
		alphaWrapper.setStyle({
			width:size.width+"px",
			height:size.height+"px",
			top:"0px",
			left:"0px"
		});

		var popWrapper = "<div id='popupParent'><a href='javascript:void(0);' id='popupClose' onclick='featureLoad.close();'></a><iframe width='320' height='337' border='0' frameborder='0' id='popupWrapper' name='popupWrapper' scrolling='no'></iframe></div>";
		Element.extend(document.body).insert(alphaWrapper);
		Element.extend(document.body).insert(popWrapper);
		$("popupWrapper").src = value;
		$("popupParent").setStyle({
			top:Math.floor((document.documentElement.clientHeight-367)/2) + "px",
			left:Math.floor((document.documentElement.clientWidth-320)/2) + "px",
			zIndex:5000
		});
		
		Event.stopObserving(window, "scroll", this._scroll);
		Event.observe(window, "scroll", this._scroll);
	},
	getSize:function(){
		return Element.extend(document.body).getDimensions();
	},
	close:function(){
		if ($("popupParent")!=null) $("popupParent").remove();
		if ($("popupWrapper")!=null) $("popupWrapper").remove();
		if ($("alphaWrapper")!=null) $("alphaWrapper").remove();

		Event.stopObserving(window, "scroll", featureLoad._scroll);
	},
	_scroll:function(evt){
		$("popupParent").setStyle({
			top:Math.floor((document.documentElement.clientHeight-367)/2) + document.documentElement.scrollTop + "px"
		});
	}
});





highlightLoad = new highlightLoad();
featureLoad = new featureLoad();