$(function(){
	$("#link-box li a").click(function(){
		$("#banner1,#banner2,#banner3").hide();
		var showId="#"+$(this).attr("banner");
		$(showId).show();
		$("#link-box li").each(function(i){
			if (i == '1'){
				$('a',this).removeClass('now');
				$('span',this).attr('class','icon-iphone');
			} else if(i == '2'){
				$('a',this).removeClass('now');
				$('span',this).attr('class','icon-andriod');
			} else if(i == '3') {
				$('a',this).removeClass('now');
				$('span',this).attr('class','icon-windows');
			}
		});
		$(this).attr('class','now');
		$("span",this).attr('class','icon-now');
	})
	//////////////////////////////////////////
	$("#navi a").hover(
		function(){
			$(this).children(".navi:hidden")
			.addClass("this")
			.css({
				display:"block",
				opacity:"0"
			})
			.stop()
			.animate({
				opacity:"1"
			},500)
		},
		function(){
			$(this).children(".this")
			.hide()
			.removeClass("this")
		})//hoverend
	//////////////////////////////////////////
	$("#search").css("opacity","0.8");
	$("#search form input")
	.focus(function(){

		$("#search").animate({
			opacity:"1"
		},300);

		$(this)
		.animate({
			width:"115px"
		},300)
		.animate({
			width:"220px"
		},200)
		.blur(function(){
			$("#search").stop().animate({
				opacity:"0.8"
			},300);
			$(this).stop().animate({
				width:"120px"
			},300);

		})
	})
	////////////////////////////////////////// rating-bar
	$("#rating-bar a").each(function(i){
		var click=false;
		var score = i+1;
		$(this).mouseover(function(){
			$("#rating-score-img").css('width',score*25-1+'px');
			$("#rating-score").html(score);
			if (score == 0 || score == ''){
				msg = 'Waiting for your rating, thanks!';
			} else if(score <= 3) {
				msg = 'Just so so!';
			} else if(score <= 6) {
				msg = 'Good';
			} else if(score <= 9) {
				msg = 'Excellent';
			} else if(score == 10) {
				msg = 'Amazing';
			}
			$("#rating-score-msg").html(msg);
			click=false;
		})
		$(this).mouseout(function(){
			if (!click){
				$("#rating-score-img").css('width','0px')
				$("#rating-score").html('0');
				$("#comment_form input[name='Review[score]']").val('');
				$("#rating-score-msg").html('Waiting for your rating, thanks!');
			}
		})
		$(this).click(function(){
			$("#rating-score-img").css('width',score*25-1+'px')
			$("#rating-score").html(score);
			$("#comment_form input[name='Review[score]']").val(score);
			click=true;
		})
	})

	$("#rating-bars a").each(function(i){
		var click=false;
		var score = i+1;
		$(this).mouseover(function(){
			$("#rating-score-imgs").css('width',score*25-1+'px');
			$("#rating-scores").html(score);
			if (score == 0 || score == ''){
				msg = 'Waiting for your rating, thanks!';
			} else if(score <= 3) {
				msg = 'Just so so!';
			} else if(score <= 6) {
				msg = 'Good';
			} else if(score <= 9) {
				msg = 'Excellent';
			} else if(score == 10) {
				msg = 'Amazing';
			}
			$("#rating-score-msg").html(msg);
			click=false;
		})
		$(this).mouseout(function(){
			$("#rating-score-imgs").css('width',$("#rating-score-imgs").attr('scoreWidth')+'%')
			$("#rating-scores").html($("#rating-scores").attr('score'));
		})
		$(this).click(function(){
			$("#rating-score-img").css('width',score*25-1+'px')
			$("#rating-score").html(score);
			$("#comment_form input[name='Review[score]']").val(score);
			click=true;
		})
	})

//////////////////////////////////////////
})
//$("#search_software").submit( function () {
//	if($.trim($("#search_key").val()).length>0)
//		window.location.href = '/search/?k='+urlencode($("#search_key").val());
//	else
//		alert('please enter a search keyword');
//	return false;
//});

function Member(){
	this.ShowLogin = function(){
		$('#head-logout').hide();
		$('#head-login').hide();
		if ($.cookie('isLogin') != '1') {
			$('#head-logout').show();
		} else {
			var link='<a href="/account/view.htm?name='+$.cookie('userName')+'" title="'+$.cookie('userName')+'">'+$.cookie('userName')+'</a>';
			var reviewFormShowUsername=document.getElementById('review_form_show_username');
			if (reviewFormShowUsername)
				reviewFormShowUsername.innerHTML=link;
			$("#head-username").html($.cookie('userName'));
			$("#head-username").attr('title',$.cookie('userName'));
			$('#head-login').show();
		}
	}
	this.CheckLogin = function(){
		if ($.cookie('isLogin') != '1') {
			MemberObj.ToggleLoginLayer();
			return false;
		}
		return true;
	}
	this.ToggleRegLayer = function(){
		$("#reg_layer").toggle();
		$('#reg_modalOverlay').toggle();
		$("#login_layer").hide();
		$('#modalOverlay').hide();
	}
	this.ToggleLoginLayer = function(){
		$("#login_layer").toggle();
		$('#modalOverlay').toggle();
	}

	this.Login = function(username,password){
		$.ajax({
			type: "POST",
			url: gSiteRoot+"/login.htm",
			dataType: "json",
			data: "ajaxSubmit=1&LoginForm[username]="+username+"&LoginForm[password]="+password,
			cache: false,
			success: function(response){
				if(response.success){
					alert('Login success');
					MemberObj.ToggleLoginLayer();
					MemberObj.ShowLogin();
					if(document.location.pathname=='/login.htm' || document.location.pathname=='/register.htm')
						location.replace(gSiteRoot);
				} else {
					$("#login_layer_form_error").show();
					$("#login_layer_form_error").html(response.msg);
				}
			},
			timeout:5000,
			error:function(errorObj,errorText){
				alert(errorText)
			}
		});
	}
	this.Register = function(){
		var formData=$("#reg_layer_form").serialize();
		$.ajax({
			type: "POST",
			url: gSiteRoot+"/index.php?r=ajax/register",
			dataType: "json",
			data: formData,
			cache: false,
			success: function(response){
				if(response.success){
					alert('Register success');
					MemberObj.ToggleRegLayer();
					MemberObj.ShowLogin();
					if(document.location.pathname=='/login.htm' || document.location.pathname=='/register.htm')
						location.replace(gSiteRoot);
				} else {
					$("#reg_layer_form_error").show();
					$("#reg_layer_form_error").html(response.msg);
					$('#reg_layer_captcha').click();
				}
			},
			timeout:5000,
			error:function(errorObj,errorText){
				alert(errorText)
			}
		});
	}
}
var MemberObj = new Member();

function ReplayReview(table,id,message){
	if (!MemberObj.CheckLogin()) return false;
	$.ajax({
		type: "POST",
		url: gSiteRoot+"/ajax/review.htm",
		dataType: "json",
		data: "Review[tableName]="+table+"&Review[pid]="+id+"&Review[score]=&Review[content]="+message,
		cache: false,
		success: function(response){
			if(response.success){
				window.location.reload();
			} else {
				$("#replay-box-"+id+" .error_message").show();
				$("#replay-box-"+id+" .error_message").html(response.msg);
			}
		},
		timeout:5000,
		error:function(errorObj,errorText){
			$("#replay-box-"+id+" .error_message").show();
			$("#replay-box-"+id+" .error_message").html("Ajax:"+errorText);
		}
	});
}
function ShowReplayReview(table,id){
	$("#user-review .replay").html('');
	$("#replay-box-"+id).html($("#replay-source").html());
	$("#replay-box-"+id+" .w-submit").bind('click',function(){
		ReplayReview(table,id,$("#replay-box-"+id+" textarea[name='Review[content]']").val())
	});
}

