var current_rating = 0;
var my_rating = 0;
var is_guest = true;

function showRelated(entryid)
{
	$.facebox('<iframe src="' + BASE_URL + '?act=related&entryid=' + entryid + '" frameborder="0" scrolling="auto" noresize="noresize" width="560" height="320"></iframe>');
}

$(document).ready(function () {
	$("a[href$='.jpg']").lightBox();
	
	$('ol.commentlist li:odd').addClass('altline');
	
	// Bind click function for sumbit button
	$('#comment-submit').bind('click', function () {
		doComment();
	});
	
	$.get(BASE_URL + 'httprequest.php?act=track_view&entryid=' + entryid + '&jsoncallback=?', function (count) {
		$('#entry_views').text(count);
	});
	
	$.get(BASE_URL + 'httprequest.php?act=get_recommend&entryid=' + entryid + '&jsoncallback=?', function (count) {
		$('#entry_recommend').text(count);
	});
	
	//new curvyCorners(radius_5px, document.getElementById('comment-message')).applyCornersToAll();
	
	$.getJSON(BASE_URL + 'httprequest.php?act=check_login&jsoncallback=?', function (userinfo) {
		if (userinfo['id'])
		{
			is_guest = false;
			
			$('#username').text(userinfo['username']);
			
			//$('#email').attr({'value':userinfo['email'], 'disabled':'disabled'});
		}
		/*else if ($.cookie('<?php echo COOKIE_PREFIX; ?>user_email'))
		{
			$('#email').attr('value', $.cookie('<?php echo COOKIE_PREFIX; ?>user_email'));
		}*/
		
		if (!userinfo['id'])
		{
			$('.validate').show();
		}
	});
	
	/*$.get(BASE_URL + 'httprequest.php?act=get_my_rating&entryid=' + entryid + '&jsoncallback=?', function (rating) {
		if (rating > 0)
		{
			my_rating = rating;
			
			$('#star-rating li a').removeClass();
			$('#star-rating li').unbind();
			
			$('#my-rating').html(', 我的评分: ' + rating + ' 分');
		}
	});
	
	$.get(BASE_URL + 'httprequest.php?act=get_current_rating&entryid=' + entryid + '&jsoncallback=?', function (rating) {
		if (rating > 0)
		{
			current_rating = rating;
			
			$('#current-rating').animate({
				width : rating * 20 + '%'
			}, 3000);
		}
		
		$('#current-rating_number').text(rating);
	});*/
});

function doComment()
{
	$('#notic').hide();
	
	//var message = $('#message').html().toLowerCase();
	
	var message = $('#message').val();
	
	if (message.replace(/(^\s*)|(\s*$)/g, "") == '')
	{
		$('#notic').text('你忘记输入评论内容了.').fadeIn('fast');
		
		return false;
	}
	
	if (message.indexOf('http:') != -1 || message.indexOf('https:') != -1)
	{		
		if ($('#validatecode').val() == '' && is_guest == false)
		{
			$('#notic').html('您的评论内容含有网址链接,请输入验证码继续.<br />提示: 发布广告会被停止帐户,本站评论中的链接经过特殊处理搜索引擎不会跟踪.').fadeIn('fast');
			
			$('#validate').attr('src', BASE_URL + 'httprequest.php?act=validate&id=comment_' + entryid + '&' + Date());
			
			$('.validate').show();
			
			return false;
		}
	}
	
	// UnBind click function and add disabled class for sumbit button
	$('#comment-submit').unbind().addClass('disabled');
	
	document.getElementById('comment-form').submit();
}

function doRecommend(entryid)
{
	var recommend_inner = $('#recommend').html();
	
	$('#recommend_message').hide();
	
	$('#recommend').html('<img src="images/animation_loading_small.gif" alt="Loading..." style="margin: 6px 52px 0 0" />');
	
	$.post('httprequest.php?act=do_recommend', 'entryid=' + entryid, function (data) {
		$('#recommend_message span').text(data);
		$('#recommend').html(recommend_inner);
		$('#recommend_message').fadeIn();
	});
}

function star_rating(level)
{
	$('#my-rating').html(' &nbsp <img src="images/animation_loading_small.gif" alt="Loading..." title="Loading..." class="lineobj" />');
	
	$('#current-rating').css('width', 0);
	
	$('#star-rating li a').removeClass();
	$('#star-rating li').unbind();
	
	$.post(BASE_URL + 'httprequest.php?act=do_rating', 'rating=' + level + '&entryid=' + entryid, function (rating) {
		current_rating = rating;
		
		$('#current-rating').animate({
			width : current_rating * 20 + '%'
		}, 3000);
		
		$('#current-rating_number').text(rating);
		
		$('#my-rating').html(', 我的评分: ' + level + ' 分');
	});
}

var current_page = 0;

function loadMore(target)
{
	current_page++;
	
	$('#loadmore').addClass('loading').html('<img src="images/animation_loading_small.gif" alt="Loading..." title="Loading..." class="lineobj" style="padding: 3px 0 0 0;" />');
	
	$.getJSON(BASE_URL + 'httprequest.php?act=get_comments&entryid=' + entryid + '&page=' + current_page + '&jsoncallback=?', function (data) {
		html = '';
		
		var oddcomment = '';
		
		if (data.length == 0)
		{
			$('#loadmore').text('没有更多评论了');
			
			return true;
		}
		
		$.each(data, function(i, a) {
			if ('alt' == oddcomment)
			{
				oddcomment = 'altline';
			}
			else
			{
				oddcomment = 'alt';
			}
				
			html += '<li class="' + oddcomment + '" id="comment-' + a['id'] + '"><img src="' + a['avatar'] + '" class="avatar" alt="" /><cite>';
			
			if (a['homepage'])
			{
				html += '<a href="' + a['homepage'] + '" target="_blank" rel="nofollow">' + a['username'] + '</a>';
			}
			else
			{
				html += a['username'];
			}
			
			html += ' 说:</cite><small class="commentmetadata"><a href="#comment-' + a['id'] + '" title="">' + a['time'] + '</a></small><div class="list">' + a['message'] + '</div></li>';
		});
		
		target.html(target.html() + '<ol class="commentlist">' + html + '</ol>');
		
		$('ol.commentlist li:odd').addClass('altline');
			
		$('#loadmore').removeClass('loading').text('更 多');
	});
}

/*$().ready(function() {
	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : BASE_URL + 'javascript/tiny_mce/tiny_mce.js',
	
		// General options
		theme : "advanced",
		skin : "o2k7",
		skin_variant : "silver",
	
		plugins : "safari,bbcode,xhtmlxtras,inlinepopups",
				
		theme_advanced_buttons1 : "undo,redo,|,blockquote,|,image,|,link,unlink",
		theme_advanced_buttons2 : "removeformat,|,bold,italic,underline,|,forecolor,|,fontselect,fontsizeselect",
		theme_advanced_buttons3 : "",
				
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,
		
		remove_linebreaks : false,
	
		// Example content CSS (should be your site CSS)
		content_css : BASE_URL + "css/global.css",
	
		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js"
	});
});*/