function showlogin(){
	new Effect.Appear('login',{duration: 0.3});
}

function closelogin(){
	new Effect.Fade('login',{duration: 0.3});
}

var voteid=0;
var cup='arrow up';
var cupa='arrow upactive';
var cdown='arrow down';
var cdowna='arrow downactive';

function update_index(){
	var old_title = $('submit-button').innerHTML;
	$('submit-button').innerHTML = '<img src="/img/ajax_load_01.gif" alt="loading" />';
	new Ajax.Updater('main-links', '/', {method: 'post', parameters: 'ajax=1', asynchronous:true, onComplete: function(){$('submit-button').innerHTML=old_title;}});
	window.setTimeout(update_index,reload_time);
}

function vote(linkid){
	if(voteid != linkid){
		voteid=linkid;
		if(!$('box'+linkid).hasClassName('voted')) new Ajax.Request('/vote/'+linkid, {postBody: 'linkid='+linkid, onComplete: voteDone});
	}else{
		alert("you already vote");
	}
	return false;

}

function expand(id){
	$(id+'-expand').style.display = 'none';
	$(id+'-more').style.display = 'block';
	//new Effect.SlideDown(id+'-more');
}

function collapse(id){
	$(id+'-expand').style.display = 'inline';
	$(id+'-more').style.display = 'none';
	/*new Effect.SlideUp(id+'-more',{
		afterFinish: function(){
			$(id+'-expand').style.display = 'block';
		}
	})*/;
}

function mustlogin(){
	alert('you must login first');
}

function checkVote(linkid){
	var up=0;
	var down=0;
	if($('up'+linkid).className == cup) up=0; else up=1;
	if($('down'+linkid).className == cdown) down=0; else down=1;
	return {"up":up,"down":down};
}

function voteDone(connection,json){
	//Effect.Fade('point'+json.linkid, { duration: 0.5 ,afterFinish:
	voteid=0;
	Effect.FadeKeepSpace('point'+json.linkid, { afterFinish:
	function(){
		$('point'+json.linkid).innerHTML = json.point;
		Effect.AppearKeepSpace('point'+json.linkid);
		$('box'+json.linkid).className = 'box voted shade'+json.shade;
	}
	});
	//$('comment-list-'+json.topicid).innerHTML+=connection.responseText;
	//new Effect.Appear('topic_'+json.topicid,{});
	//new Effect.ScrollTo('topic_'+json.topicid,{});
	//setDragTopic();
}

function checkUsername(username){
	new Ajax.Request('/register/checkusername', {postBody: 'username='+username,
	onComplete: function(connection,json){
		$('checkusername').innerHTML = connection.responseText;
		$('username').value = json.username;
	}
	});
}

function remainText(id,max){
	len = $(id).value.length;
	remain = max-len;
	if(remain<0){
		$(id).value = $(id).value.substring(0,max);
		$(id+'-remain').innerHTML = 0;
	}else
	$(id+'-remain').innerHTML = remain;

	return false;
}

function setReply(replyid,name){
	$('post-comment-detail').innerHTML = "Replying to comment by "+name+" (<a href=\"javascript:cancelReply()\">cancel</a>)";
	$('replyto').value=replyid;
}

function cancelReply(){
	$('post-comment-detail').innerHTML = "";
	$('replyto').value='';
}

function checkSlide(button,id){
	if($(button).checked){
		//new Effect.SlideDown(id);
		$(id).style.display="block";
	}else{
		$(id).style.display="none";
		//new Effect.SlideUp(id);
	}
}

/* stat */
function clickThru(id){
	new Ajax.Request('/trackhit', {postBody: 'id='+id,
	onComplete: function(connection,json){
		var visitinst = parseInt(Cookie.get('visitinstruction'));
		if(!visitinst) Cookie.set('visitinstruction',1); else Cookie.set('visitinstruction',visitinst+1);
		//alert(connection.responseText);
	}
	});
	return false;
}

/* email this */

function emailthis($id){
	
}

var confirmMsg  = 'คุณต้องการที่จะ ';
function confirmLink(theLink, theSqlQuery)
{
	// Confirmation is not required in the configuration file
	if (confirmMsg == '') {
		return true;
	}

	var is_confirmed = confirm(confirmMsg + ' :\n' + theSqlQuery);

	return is_confirmed;
}

/* instruction */
var refer = document.referrer;
var linkID = linkID || null;
function instruction(){
	if(linkID==null) return;
	//alert(Cookie.get('visitinstruction'));
	if((refer.indexOf('google')>-1 && Cookie.get('visitinstruction') <= 2) || Cookie.get('visitinstruction') == null){
		var locate = Position.positionedOffset($('goto'+linkID));
		var div = Builder.node('div',{id:'instruction-box',className:'tooltipinstruction',style:'left:'+(locate[0]-45)+'px;top:'+(locate[1]-53)+'px;'});
		div.innerHTML = '<div id="instruction" style="color:#ff6600">คลิ๊กที่นี้!! เพื่อไปยังเว็บเจ้าของเนื้อหา</div>';
		$('link'+linkID).appendChild(div);
		//$('instruction-box').style.left = locate[0]+'px';
		//$('instruction-box').style.top = locate[1]+'px';
		$('instruction-box').show();
	}
}

Event.observe(window, "load", instruction, false);

/* search 
// Create namespace ------------------------------------------------------------
if (code == undefined) var code = {};
if (code.zickr == undefined) code.zickr = {};

/**
 * Mac OS Search Field
 * Replaces the default input field with the typical Mac OS search field
 

// Search Field class ----------------------------------------------------------
code.zickr.searchField = Class.create();
code.zickr.searchField.prototype = {
	initialize: function()
	{
		this.search_field  = $("searchkeyword");
		this.default_value = "search the story...";
		this.is_safari     = ((parseInt(navigator.productSub)>=20020000)&&(navigator.vendor.indexOf("Apple Computer")!=-1));
		if (this.is_safari) {
			Element.addClassName(this.search_field, "issafari");
			this.replace();
		} else {
			if (this.search_field.value == "") this.search_field.value = this.default_value;
		}
		Event.observe(this.search_field, "focus", this.focus.bindAsEventListener(this), false);
		Event.observe(this.search_field, "blur",  this.blur.bindAsEventListener(this),  false);
	},
	replace: function()
	{
		this.search_field.setAttribute('type', 'search');
		this.search_field.setAttribute('autosave', 'code.zickr.search');
		this.search_field.setAttribute('results', '5');
		this.search_field.setAttribute('placeholder', this.default_value);
	},
	focus: function()
	{
		if (this.search_field.value == this.default_value) {
			this.search_field.value = "";
			Element.addClassName(this.search_field, "focus");
		}
	},
	blur: function()
	{
		if (this.search_field.value == "") {
			if (this.is_safari) {
				this.search_field.value = "";
			} else {
				this.search_field.value = this.default_value;
				Element.removeClassName(this.search_field, "focus");
			}
		}
	}
}

Event.observe(window, "load", function(){new code.zickr.searchField()}, false);*/