
document.observe("dom:loaded",function(){
	var _updateSearchResultByJson = function(json, type){
		var html = "";
		for(i = 0; i < json.category.length; i++){
			is_first = json.category[i].is_first == 'yes' ? ' class="first"' : '';
			html += '<li'+is_first+'><h1 class="'+json.category[i].css_class+'">'+json.category[i].title+'</h1>';
			
			if(json.category[i].is_limit == 'yes'){
				html += '<a href="'+json.category[i].results_href+'" class="search-moreresult" onclick="_moreResult(Element.up(this,\'li\'),\''+json.category[i].results_type+'\');return false">More Results</a>';
			}
			html += '<ul class="categorylist" id="ul_categorylist">';
			num_project = json.category[i].num_project;
			for(j = 0; j < num_project; j++){
				if(json.category[i].project[j].is_exists == 'yes'){
					
					html += '<li>';
					if(json.category[i].project[j].is_show_hit == 'yes'){
						html += '<a rel="'+json.category[i].project[j].rel+'" class="'+json.category[i].project[j].class_+'" target="'+json.category[i].project[j].target+'" href="'+json.category[i].project[j].href+'" onclick="new Ajax.Request(\'hit.php\',{parameters:{id:$(this).readAttribute(\'rel\')},asynchronous:false}) ;if($(this).readAttribute(\'target\')==\'_blank\'){window.open(this.href)}else{window.location.href=this.href;}; if($(this).hasClassName(\'current\')) {  $(\'forseach_'+json.category[i].project[j].forseach+'\').scrollTo(); window._closeResult.delay(0.5); }return false"><img width="125" height="75" alt="'+json.category[i].project[j].alt+'" src="getimage.php?size=125x75&amp;bgcolor=0xFsFFFFF&amp;location='+json.category[i].project[j].location+'" /><h2>'+json.category[i].project[j].name+'</h2></a>';
					} else {
						html += '<a onclick="return false" class="'+json.category[i].project[j].class_+'"><img width="125" height="75" alt="'+json.category[i].project[j].alt+'" src="getimage.php?size=125x75&amp;bgcolor=0xFFFFFF&amp;location='+json.category[i].project[j].location+'" /><h2>'+json.category[i].project[j].name+'</a>';
					}
					html += '<div class="urlprint">'+json.category[i].project[j].urlprint+'</div>';
					html += '</li>';
				}
			}
			html += '</ul></li>';
		}
		$('searchresult').update(html);
		_searchcache["__"+$F(searchbox_ele)]=html;
		searchresult_ele.show();
		$("searchresult_shadowbg").show(); 
		$("searchresult_shadowbg").setStyle({"height":searchresult_ele.getHeight()+"px"});	
	}

	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)]){
			if(/MSIE 6./i.test(navigator.userAgent)){
				/****************************************************/
				$("searchresult").show();
				$("searchresult_shadowbg").show(); 
				
				$("searchresult").update("<img src='images/ajax-loader.gif'/>");
				
				$("searchresult_shadowbg").setStyle({"height":"32px"});
				
				new Ajax.Request(String(window.location).qupdate({"section":"searchresult","is_ie6":'yes',"search":$F(searchbox_ele)}),{	
						method: "post", 
						parameters:{},
						onSuccess: function(transport, json) {
							_updateSearchResultByJson(json,1);}
					}
				)
	
				/****************************************************/
			} else {
				$("searchresult").show();
				$("searchresult_shadowbg").show(); 
				
				$("searchresult").update("<img src='images/ajax-loader.gif'/>");
				$("searchresult_shadowbg").setStyle({"height":"32px"});
				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()){
					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 searched_result_image = function(elm){
	new Ajax.Request('hit.php',
		{
			parameters:{id:$(elm).readAttribute('rel')},
			asynchronous:false
		}
	); 
	
	if($(elm).readAttribute('target')=='_blank'){
		window.open(elm.href)
	} else {
		window.location.href=elm.href;
	}  
	
	val = $(elm).readAttribute('searched_value');
	
	if($(elm).hasClassName('current')){ 
		forseach_id = 'forseach_'+val; 
		$(forseach_id).scrollTo(); 
		window._closeResult.delay(0.5);
	}	
}
	

var _moreResult=function(ele,type){
	$$(".search-moreresult").invoke("hide");
	var startload=function(){
		
		if(/MSIE 6./i.test(navigator.userAgent)){
		/****************************************************/
			$("searchresult").insert("<img src='images/ajax-loader.gif'/>");
		
			new Ajax.Request(String(window.location).qupdate({"section":"searchresult","search":$F("searchbox"),"is_ie6":'yes',"type":type,"more":1}),{	
					method: "post", 
					parameters:{},
					onSuccess: function(transport, json) {
						type = 2;
						
						html = "";
						for(i = 0; i < json.category.length; i++){
							is_first = json.category[i].is_first == 'yes' ? ' class="first"' : '';
							html += '<li'+is_first+'><h1 class="'+json.category[i].css_class+'">'+json.category[i].title+'</h1>';
							
							if(json.category[i].is_limit == 'yes'){
								html += '<a href="'+json.category[i].results_href+'" class="search-moreresult" onclick="_moreResult(Element.up(this,\'li\'),\''+json.category[i].results_type+'\');return false">More Results</a>';
							}
							html += '<ul class="categorylist" id="ul_categorylist">';
							num_project = json.category[i].num_project;
							for(j = 0; j < num_project; j++){
								if(json.category[i].project[j].is_exists == 'yes'){
									
									html += '<li>';
									if(json.category[i].project[j].is_show_hit == 'yes'){
										
										html += '<a rel="'+json.category[i].project[j].rel+'" class="'+json.category[i].project[j].class_+'" target="'+json.category[i].project[j].target+'" href="'+json.category[i].project[j].href+'" searched_value="'+json.category[i].project[j].forseach+'"  onclick="searched_result_image(this); return false;"><img width="125" height="75" alt="'+json.category[i].project[j].alt+'" src="getimage.php?size=125x75&amp;bgcolor=0xFsFFFFF&amp;location='+json.category[i].project[j].location+'" /><h2>'+json.category[i].project[j].name+'</h2></a>';
									} else {
										html += '<a onclick="return false" class="'+json.category[i].project[j].class_+'"><img width="125" height="75" alt="'+json.category[i].project[j].alt+'" src="getimage.php?size=125x75&amp;bgcolor=0xFFFFFF&amp;location='+json.category[i].project[j].location+'" /><h2>'+json.category[i].project[j].name+'</a>';
									}
									html += '<div class="urlprint">'+json.category[i].project[j].urlprint+'</div>';
									html += '</li>';
								}
							}
							html += '</ul></li>';
						}

						$('searchresult').update(html);
						$("searchresult").show();
						$("searchresult_shadowbg").show(); 
						$("searchresult_shadowbg").setStyle({"height":$("searchresult").getHeight()+"px"});
						$$(".search-moreresult").invoke("hide");			
		
	
					}
				}
			)

		/****************************************************/
		} else {
			$("searchresult").insert("<img src='images/ajax-loader.gif'/>");
			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;}
		
