var curlang = window.location.pathname.indexOf('chi') == '-1' ? 'eng' : 'chi';
var usernamemsg = userpwdmsg = giftcountmsg = contentmsg = loginmsg = ratingmsg = '';

switch (curlang)
{
case 'chi':
	default:
	ErrorMsg = '以下原因導致提交失敗：';
	usernamemsg = '請輸入用戶名稱!';
	userpwdmsg = '請輸入用戶密碼!';
	giftcountmsg = '超過庫存 ';
	contentmsg = '請輸入內容!';
	loginmsg = '請先登錄!';
	ratingmsg = '評分成功!';
	newsposttitlemsg = '請輸入主題!';
	newspostcontentmsg = '請輸入內容!';
	likenamemsg = '請輸入姓名!';
	likeemailmsg = '請輸入電郵!';
	break;

case 'eng':
	ErrorMsg = 'Error Message：'
	usernamemsg = 'Please fill in login name!';
	userpwdmsg = 'Please fill in password!';
	giftcountmsg = 'Gift is out of stock ';
	contentmsg = 'Please fill in content!';
	loginmsg = 'Please login first!';
	ratingmsg = 'Rating submitted successfully!';
	newsposttitlemsg = 'Please fill in Subject!';
	newspostcontentmsg = 'Please fill in Content !';
	likenamemsg = 'Please fill in name!';
	likeemailmsg = 'Please fill in E-mail!';
	break;
}

function checkemail(){
    $.ajax({
		type:'post',
		url: 'register.php',
		data: 'action=checkemail&email='+$('#email').val(),
		success:function(data){
			if (data)
			{
				alert(data);
				$('#email').val('').focus();
			}
		}
	});
}

function checkloginname(){
    $.ajax({
		type:'post',
		url: 'register.php',
		data: 'action=checkloginname&login='+$('#login').val(),
		success:function(data){
			if (data)
			{
				alert(data);
				$('#login').val('').focus();
			}
		}
	});
}

function checkdisplayname(){
	if ($('#name_di').val() != '')
	{
		$.ajax({
			type:'post',
			url: 'register.php',
			data: 'action=checkdisplayname&name_di='+$('#name_di').val(),
			success:function(data){
				if (data)
				{
					alert(data);
					$('#name_di').val('').focus();
				}
			}
		});
	}
	return true;
}

function checklogin(){
    if ($('#login').val() == '') {
        alert(usernamemsg);
        $('#login').focus();
        return false;
    }
    if ($('#password').val() == '') {
        alert(userpwdmsg);
        $('#password').focus();
        return false;
    }
    return true;
}

function checkgiftcount(giftid){
	if (parseInt($('#giftcount'+giftid).val()) > parseInt($('#giftleft'+giftid).val()))
	{	
		alert(giftcountmsg+$('#giftleft'+giftid).val());
		$('#giftcount'+giftid).val($('#giftleft'+giftid).val());
	}
}


function doLike(id, type){
	var likedata = '&id='+id;
	if (document.getElementById('guestname'))
	{
		if ($('#guestname').val() == '')
		{
			alert(likenamemsg);
			$('#guestname').focus();
			return false;
		}
		likedata += '&guestname='+$('#guestname').val();
	}
	if (document.getElementById('guestemail'))
	{
		if ($('#guestemail').val() == '')
		{
			alert(likeemailmsg);
			$('#guestemail').focus();
			return false;
		}
		likedata += '&guestemail='+$('#guestemail').val();
	}

	$.ajax({
		type:'post',
		url: 'album.php',
		data: 'action=like'+likedata,
		dataType:'json',
		success:function(json){
			if (!json.likecount)
			{
				alert(json.error);
			}
			else
			{	
				if (type == 'month')
				{
					$('#like_count_'+id).html(json.monthlikecount);
				}
				else
				{
					$('#like_count_'+id).html(json.likecount);
					$('#monthlike_count_'+id).html(json.monthlikecount);
				}
				alert(json.error);
			}
			$.fancybox.close();
		}
	});
}

function sharePhotoToFB(id, url){
	$.ajax({
		type:'post',
		url: 'album.php',
		data: 'action=sharephototofb&id='+id,
		success:function(data){
			//alert(data);
		}
	});
	window.open(url);
}

function shareArticleToFB(id, url){
	$.ajax({
		type:'post',
		url: 'article.php',
		data: 'action=sharearticletofb&id='+id,
		success:function(data){
			//alert(data);
		}
	});
	window.open(url);
}

function shareGameToFB(url){
	$.ajax({
		type:'post',
		url: 'game.php',
		data: 'action=sharegametofb',
		success:function(data){
			//alert(data);
		}
	});
	window.open(url);
}

function checkComment(){
	if ($('#comment').val() == '')
	{
		alert(contentmsg);
		$('#comment').focus();
		return false;
	}
	$('#commentform').submit();
	return true;
}

function showNews(id){
	$('.shownews').not('#show_news_'+id).hide();
	$('#show_news_'+id).toggle();
}

function Rating(id, score){
	$.ajax({
		type:'post',
		url: 'article.php',
		data: 'action=rating&id='+id+"&score="+score,
		success:function(data){
			if (!data)
			{
				alert(loginmsg);
				return false;
			}
			alert(ratingmsg);
			$('#showrating').html(data);
		}
	});
}

function showScore(id){
	for (var i=1; i<=id; i++)
	{
		$('#star'+i).attr('src', '../images/article_star_on.gif');
	}
	for (var q=id+1; q<=5; q++)
	{
		$('#star'+q).attr('src', '../images/article_star.gif');
	}
}

function hideScoreOn(){
	$('#showscoreimg > img').attr('src', '../images/article_star.gif');
}

function checkNewsPost() {
	 if ($('#title_tc').val() == '') {
        alert(newsposttitlemsg);
        $('#title_tc').focus();
        return false;
    }
    if ($('#content_tc').val() == '') {
        alert(newspostcontentmsg);
        $('#content_tc').focus();
        return false;
    }
    return true;
}

function openwin(page){
	//window.open (page, "", "width=850, toolbar =no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
	window.showModalDialog(page,'','dialogWidth:850px;center:yes');
}

function showScore(type){
	if (type == 'Week')
	{
		$('.thisweek').show();
		$('.alltop').hide();
		$('#changescorebg').css('background', 'url(../images/score_bg.gif)');
	}else{
		$('.thisweek').hide();
		$('.alltop').show();
		$('#changescorebg').css('background', 'url(../images/score_bg2.gif)');
	}

}

function showScoreE(type){
	if (type == 'Week')
	{
		$('.thisweek').show();
		$('.alltop').hide();
		$('#changescorebg').css('background', 'url(../images/score_bge.gif)');
	}else{
		$('.thisweek').hide();
		$('.alltop').show();
		$('#changescorebg').css('background', 'url(../images/score_bg2e.gif)');
	}

}

(function($){
$.fn.imageupdown = function(options){
		var defaults = {
		upCursor:"pre.cur",
		upTitle:"点击查看上一张",
		upUrl:$(this).attr('left'),
		downCursor:"next.cur",
		downTitle:"点击查看下一张",
		downUrl:$(this).attr('right')
		}
		var options = $.extend(defaults, options);
		this.each(function(){
		var thisImage=$(this);
		$(thisImage).bind("mousemove",function(e){
		var positionX=e.originalEvent.x||e.originalEvent.layerX||0;
		if(positionX<=$(this).width()/2){
		this.style.cursor='url('+options.upCursor+'),auto';
		$(this).attr('title',''+options.upTitle+'');
		$(this).parent().attr('href',''+options.upUrl+'');
		}else{
		this.style.cursor=''+options.downCursor+'';
		$(this).attr('title',''+options.downTitle+'');
		$(this).parent().attr('href',''+options.downUrl+'');
			}
		});
		});
	};
})(jQuery);

