﻿$(document).ready(function(){
	$('#set1 *').tooltip();
	//
	$('#comment').click(function(){
		$('#note-comment').removeClass();
		$('#note-comment').addClass('form-info');
		$('#note-comment').text('请输入你的评论！');
		$('#note-comment').show();
	});
	
	$('#comment').blur(function(){
		if (this.value.length==0){
			$('#note-comment').removeClass();
			$('#note-comment').addClass('form-error');
			$('#note-comment').text('评论不能为空！');
			$('#note-comment').show();
		}
	});
	//
	
	var options = { 
			dataType:  'json',
			beforeSubmit: validate, 
			success:    processJson
	};
	$('#submitCommentForm').ajaxForm(options);
	
	function validate(formData, jqForm, options) { 	

		
		$('#note-comment').hide();
		
		var msgError = false
		
		var form = jqForm[0];
		
		if (!form.comment.value) {
			$('#note-comment').removeClass();
			$('#note-comment').addClass('form-error');
			$('#note-comment').text('评论不能为空！');
			$('#note-comment').show();
			msgError = true; 
		}
	
		if (msgError == true)
			return false;
			
	}
		
	function processJson(data) {
		
		if (data.success=="true"){
			loadComment(data.product_id,1,10);
			loadRate(data.product_id);
		}
		return true;
	}
	
});

function loadComment(product_id,currentPage,countPerPage){
	
		$.ajax({
			url: 'product_detail_comment.php',
			type:'GET',
			data:{product_id:product_id,currentPage:currentPage,countPerPage:countPerPage},
			dataType:'json',
			success:function(data)
			{

				var bb = data.pagination;
				$('#showPagination').html(bb);
				
				var cc = '';
				if (data.comments.length>0)
				{
					for (i = 0; i < data.comments.length; i++)
					{
						cc = cc+'<div id="license"><h4><a href="user_view.php?user_id='+data.comments[i].user_id+'">'+data.comments[i].lastname+data.comments[i].firstname+'</a></h4><div class="content">';
						
						//cc = cc+'<p class="topright">下载次数:'+data.extras[i].downloadcount+'</p>';
						cc = cc+'<p class="topright"><span class="star" val="'+data.comments[i].rate+'"></span></p>';
						cc = cc+'<p>'+data.comments[i].comment+'</p>';
						cc = cc+'<p class="bottomright">'+data.comments[i].submitdate+'</p>';
																															  
						cc = cc+'</div></div>';
						
					}
				}
				else
				{
					cc = '<h4>没有相关评论……</h4>';
				}
				$('#showComment').html(cc);
				
				//等级显示
				var images=["./images/grey.gif","./images/red.gif"];		
				var options = { 
					img: images,
					enable: false
				};
				$('.star').jStar(options);
				//
				
				
			}
		});
	
}

function loadRate(product_id){
	
		$.ajax({
			url: 'product_detail_rate.php',
			type:'GET',
			data:{product_id:product_id},
			dataType:'json',
			success:function(data)
			{
				var bb='星级评价：<span class="star" val="'+data.rate+'"></span>('+data.number+')';
				$('#showRate').html(bb);	
				
				//等级显示
				var images=["./images/grey.gif","./images/red.gif"];		
				var options = { 
					img: images,
					enable: false
				};
				$('.star').jStar(options);
				//
				
			}
		});
	
}

function DownloadAndCountExtra(product_id,extra_id){
	
		$.ajax({
			url: 'ProductExtra_Download_Count.php',
			type:'GET',
			data:{product_id:product_id,extra_id:extra_id},
			dataType:'json',
			success:function(data)
			{
				var bb='下载次数：'+data.downloadCount;
				$('#showDownloadCount'+extra_id).html(bb);
				
				window.open("productextra_download_page.php?product_id="+product_id+"&product_extra_id="+extra_id, "_blank");
			}
		});
	
}

function applyProduct(product_id){
	if (confirm("你确定要代理此产品吗？"))
	{
		$.ajax({
			url: 'myaccount_reseller_product_apply.php',
			type:'GET',
			data:{product_id:product_id},
			dataType:'json',
			success:function(data)
			{				
				window.open("myaccount_reseller_product.php", "_blank");
			}
		});
	}
}

function ShoppingCart_AddItem2(license_id){
	
	var iscirclepayment =$('#iscirclepayment').attr("value");
	//alert (iscirclepayment);
	if (iscirclepayment==1){
		var circle_id =$('#circle_id').attr("value");
		//alert(circle_id);
		window.open("ShoppingCart_AddItem.php?product_license_id="+license_id+"&iscirclepayment=1&circle_id="+circle_id,"_blank");
	}	
	else if (iscirclepayment==0){
		window.open("ShoppingCart_AddItem.php?product_license_id="+license_id,"_blank");
	}
}
