document.observe("dom:loaded",function(){
	
	if(Prototype.Browser.IE){
		$$("a").each(
			function(e){
				Event.observe(e,"focus",function(){
					e.blur();
				}.bind(e));
			}			 
		);
		
		document.observe("component:ajax-updated",function(evt){
			Element.select($(evt.memo.section_id),"a").each(
				function(e){
					Event.observe(e,"focus",function(){
						e.blur();
					}.bind(e));
				}			 
			);
		});
	}
	var _applyFadebtn=function(e){
		var cell = new Element("div",{"class":"_fadebtn_cell"});
		cell.update(e.innerHTML);
		e.update(cell);
		cell.setOpacity(0);
		cell.addClassName(e.readAttribute("fadebtn:classname"));
		Event.observe(e,"mouseover",function(evt){
			var cell=this.down("._fadebtn_cell");
			var cop=cell.getOpacity();
			cell._btnEffect = new Effect.Tween(null,cop,1,{duration:1*(0.5-cop)},function(p){this.setOpacity(p)}.bind(cell));
	   }.bind(e));
		Event.observe(e,"mouseout",function(){
			var cell=this.down("._fadebtn_cell");
			if(cell._btnEffect){
				cell._btnEffect.cancel();
			}
			var cop=cell.getOpacity();
			cell._btnEffect = new Effect.Tween(null,cop,0,{duration:0.5*cop},function(p){this.setOpacity(p)}.bind(cell));
		}.bind(e))
	}
	$$(".fadebtn").each(
		function(e){
			_applyFadebtn(e);
		}					
	);
	document.observe("component:ajax-updated",function(evt){
		Element.select($(evt.memo.section_id),".fadebtn").each(
			function(e){
				_applyFadebtn(e);
			}			 
		);
	});
	
	$$(".main-menu li a, #ysdlogo").each(
		function(e){
			e.setOpacity(0);
			if(e.id!="ysdlogo"){
			e.setStyle("display:block");
			}
			Event.observe(e,"mouseover",function(){
				if(this._btnEffect){
					this._btnEffect.cancel();
				}
				var cop=this.getOpacity();
				this._btnEffect = new Effect.Tween(null,cop,0.95,{duration:1*(0.5-cop)},function(p){this.setOpacity(p)}.bind(this));
			}.bind(e))
			Event.observe(e,"mouseout",function(){
				if(this._btnEffect){
					this._btnEffect.cancel();
				}
				var cop=this.getOpacity();
				this._btnEffect = new Effect.Tween(null,cop,0,{duration:0.5*cop},function(p){this.setOpacity(p)}.bind(this));
			}.bind(e))
		}
	);


	var searchresult_ele = $("searchresult");
	var searchbox_ele = $("searchbox");
	
	var _updateShadowBg=function(){
		var obg = $("searchresult_shadowbg") || new Element("div",{id:"searchresult_shadowbg",style:"display:none"});
		if(!$("searchresult_shadowbg")){
			searchresult_ele.insert({after:obg});
		}
		var h=searchresult_ele.getHeight();
		obg.setStyle({height:h+"px"});
	}
	_updateShadowBg();
	
	var _searchcache={};
	var _showResult=function(){
		
		if(!_searchcache["__"+$F(searchbox_ele)]){
			
			$("searchresult").update("");
			
			
			new Ajax.Updater("searchresult",String(window.location).qupdate({"section":"searchresult","search":$F(searchbox_ele)}),
				{
					onComplete:function(t,r){
						_searchcache["__"+$F(searchbox_ele)]=t.responseText;
						searchresult_ele.show();
						$("searchresult_shadowbg").show(); 
						$("searchresult_shadowbg").setStyle({"height":searchresult_ele.getHeight()+"px"});
					}
				}
			);
		}else{
			$("searchresult").update(_searchcache["__"+$F(searchbox_ele)]);
			searchresult_ele.show();
			$("searchresult_shadowbg").show(); 
			$("searchresult_shadowbg").setStyle({"height":searchresult_ele.getHeight()+"px"});
		}
	}
	
	var _closeResult=function(){
		if(!searchresult_ele._effect){
			searchresult_ele._effect = new Effect.Tween(null,0,1,{onAfterFinish:function(){this._effect=null}.bind(searchresult_ele)},function(){});
		}
		searchresult_ele.hide();
		$("searchresult_shadowbg").hide();
	}
	
	window._closeResult=_closeResult;
	
	if(searchbox_ele.present()) searchbox_ele.addClassName("search-fill");
	Event.observe(searchbox_ele,"focus",function(evt){Event.element(evt).addClassName("search-fill")});
	Event.observe(searchbox_ele,"blur",function(evt){
		ele=Event.element(evt);
		if(!ele.present()){
			ele.removeClassName("search-fill");
		}
	});
	
	new Form.Element.Observer(
	  searchbox_ele,
	  0.4,  // 200 milliseconds
	  function(ele, value){
		if(ele.present()){
			ele.addClassName("search-fill");
			_showResult();
		}else{
			_closeResult();
		}
	  }
	)
	
	
	document.observe(
		"click",
		function(evt){
			evt_ele=Event.element(evt);
			if( (Element.descendantOf(evt_ele,searchresult_ele) || evt_ele==searchresult_ele) ||
				(Element.descendantOf(evt_ele,searchbox_ele) || evt_ele==searchbox_ele)){
				if(searchbox_ele.present()){
					//console.log(searchresult_ele.visible());
					if(!searchresult_ele.visible()){
						
						_showResult();
					}
				}
			}else{
				_closeResult();
			}
		}
	);
	
	var backtotop_ele = $("backtotop");
	scrolloffsets = document.viewport.getScrollOffsets();
	backtotop_ele[scrolloffsets.top>0?"show":"hide"]();
	if( /msie|MSIE [1-6]/.test(navigator.userAgent)){
		backtotop_ele.setStyle({"top":(scrolloffsets.top+document.viewport.getHeight()-backtotop_ele.getHeight()-50)+"px"});
	}
	Event.observe(window,"scroll",function(){
		scrolloffsets = document.viewport.getScrollOffsets();
		backtotop_ele[scrolloffsets.top>0?"show":"hide"]();
		if( /msie|MSIE [1-6]/.test(navigator.userAgent)){
			backtotop_ele.setStyle({"top":(scrolloffsets.top+document.viewport.getHeight()-backtotop_ele.getHeight()-50)+"px"});
		}
	});
});

var _moreResult=function(ele,type){
	$$(".search-moreresult").invoke("hide");
	var startload=function(){
		new Ajax.Updater("searchresult",String(window.location).qupdate({"section":"searchresult","search":$F("searchbox"),"type":type,"more":1}),
			{
				onComplete:function(t,r){
					//_searchcache[$("searchresult")]=t.responseText;
					$("searchresult").show();
					$("searchresult_shadowbg").show(); 
					$("searchresult_shadowbg").setStyle({"height":$("searchresult").getHeight()+"px"});
					$$(".search-moreresult").invoke("hide");
				}
			}
		);
	}
	
	
	var i=0;
	var elist = $$("#searchresult > li");
	var ll = elist.length-1;
	if(elist.length>1){
		elist.each(
			function(e){
				if(ele!=e){
					//e.hide();
					(function(){
						var h=function(p){
							$(this).setStyle({"height":p+"px"});
						}.bind(e);
						e.setStyle("overflow:hidden");
						e._orig_height = e.getHeight();
						e._effect=new Effect.Tween(null,e.getHeight(),1,
								{	
								duration:0.6,
								afterFinish:function(){
									this._effect=null;this.hide();
									if(!(--ll)) startload();
								}.bind(e)
								}
							,h);
					}).delay(i+=0.3);
				}
			}											
		);
	}else{
		startload();
	}
	
}
function _enterdomain(){
	$("domain_name").value=$F("domain_url")+$F("domain_root");
}
var _checkdomainframeopened=false;
function _popcheckdomain(form){
	var domain_name = $("domain_name").value=$F("domain_url")+$F("domain_root");
	$("checkdomainframe").down("iframe").remove();
	var ele_iframe = new Element("iframe",{"frameborder":"0"});
	$("checkdomainframe").insert({Bottom:ele_iframe});
	
	var o = document.viewport.getScrollOffsets();
	var d = document.viewport.getDimensions();
	//$("checkdomainframe").setStyle({"top":(o.top+((d.height-$("checkdomainframe").getHeight())/2))+"px"});
	$("checkdomainframe").show();
	ele_iframe.src="http://www.checkdomain.com/cgi-bin/checkdomain.pl?domain="+domain_name;
	_checkdomainframeopened=true;
}
document.observe(
	"click",
	function(evt){
		evt_ele=Event.element(evt);
		if( (Element.descendantOf(evt_ele,$("checkdomainframe")) || evt_ele==$("checkdomainframe")) ||
			(Element.descendantOf(evt_ele,$("enterdomain_fields")) || evt_ele==$("enterdomain_fields")) ){
			
		}else{
			
			$("checkdomainframe").hide();
		}
	}
);

function fbs_click(u,t) {openwin('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),{width:626,height:436});return false;}
		
