	$(document).ready(function(){
//		$("form input[@type=radio]").dblclick(function (){$(this).attr("checked", false);});
//		$("form input[@type=radio]").attr("title", "双击取消选择");
		$(".switcher").click(function(){
			if(!$(this).hasClass('on'))	{
				$('.on').removeClass('on');$(this).addClass('on');
				$('.switchlayer').hide();
				$('#'+$(this).attr('id')+'_c').show();
			}
		})
		$(".hl").click(function(){
			if(!$(this).hasClass('curr_tab'))	{
				$('.curr_tab').removeClass('curr_tab').addClass('comm_tab');
				$(this).removeClass('comm_tab').addClass('curr_tab');
				$('.btab_layer').hide();
				$('#'+$(this).attr('id')+'_c').show();
			}
		})
		$("#app_compare").click(function (){
			var str = '';
			$('.appinput').each(function (){
				str+= this.value.replace(/,/g, '')+',';
			});
			str = encodeURI(str);
			jQuery.get('/api/chart.php', 'a=get_flash_chart&type=sum_install&id='+str, function(msg){
				document.getElementById('chart_install').innerHTML = msg;
			});
			jQuery.get('/api/chart.php', 'a=get_flash_chart&type=sum_active&id='+str, function(msg){
				document.getElementById('chart_active').innerHTML = msg;
			});
			jQuery.get('/api/chart.php', 'a=get_flash_chart&type=sum_price&id='+str, function(msg){
				document.getElementById('chart_price').innerHTML = msg;
			});
		});
		$('input#search_app').click(function(){
			if($('input#search_app').val()=='找应用...')	$('input#search_app').val('');
		});
		
		$('input.submit_comment').click(function(){
			save_comment(this);
		});
		$('a.quk_reply').click(function(){
			add_clkevent(this);
		});
		$('a.c_del').click(function(){
			var id = this.rel;
			var todel=confirm('确实要删除这条评论吗？');
			if(todel) {del_comment(id)};
		});
		$('.site_tab').click(function (){
			$('.curr_tab').addClass('comm_tab').removeClass('curr_tab');			
			$(this).addClass('curr_tab').removeClass('comm_tab');	
			$('.topboard').css('display', 'none');
			$('.'+this.id).css('display', 'block');
		});
	});

	function add_clkevent(obj) {
		var pid=$(obj).attr('rel');
		var content = '';
		content = '<div id="post-'+pid+'" class="comment_box">';
		content+= '<textarea cols="30" rows="5"></textarea><br/><input type="submit" value="回复" class="submit_comment"><input type="button" value="取消" class="cancel_comment">';
		content+= '<input type="hidden" name="pid" class="pid" value="'+pid+'"/>';
		content+= '</div>';	
		if ($('#post-'+pid).length > 0) {
			$('#post-'+pid).css('display','block');
		} else {		
			$(obj).parent().append(content);
			$(obj).parent().find('.submit_comment').bind('click', function(){
				save_comment(obj);
			});
			$(obj).parent().find('.cancel_comment').bind('click', function(){
				$('#post-'+pid).css('display','none');$('#post-'+pid+" textarea").val("");
			});	
		}		
	}	
	function del_comment(id){
		jQuery.post(
			'/api/api.php?a=del&c=comment', 
			'id='+id, 
			function (msg){
				var msg_json = eval('('+msg+')');
				if(msg_json.state=='1'){
					$('#comm_'+id).hide();
					var sum_comment = parseInt($('.totalnum b').html());
					var num = parseInt(msg_json.num);
					num = msg_json.num>1 ? msg_json.num : 1;
					if(sum_comment>1){
						$('.totalnum b').html(sum_comment-num);
					}else{
						$('.totalnum b').html('0');
					}
				}
			}
		);
	}
	function save_comment(obj){
		var type_id = $('#comment_type_id').val();
		var r_id = $('#comment_rid').val();
		var content = $('textarea', $(obj).parent()).val();
		var pid = parseInt($('input.pid', $(obj).parent()).val());
			pid = (isNaN(pid) ? 0 : pid);
		jQuery.post(
			'/api/api.php?a=add&c=comment', 
			'type_id='+type_id+'&r_id='+r_id+'&content='+content+'&pid='+pid, 
			function(msg){
				var msg_json = eval('('+msg+')');
				var class_msg = msg_json.state=='1' ? 'succbox' : 'failbox';
/*				var str = '<div class="'+ class_msg +'">'+ msg_json.msg +'</div>';
				$(obj).parent().prepend(str);
				$('.'+class_msg).fadeIn("slow", function (){
					setTimeout(function(){$('.'+class_msg).slideUp("slow", function(){$('.'+class_msg).remove();});}, 1500);
				});*/
				if(msg_json.state=='1'){
					var commObject = msg.parseJSON();
					if(commObject.pid!=0){
						$("#comm_"+commObject.pid+" .comm_replies").append("<div class=\"comm_reply\" id=\"comm_"+commObject.id+"\"><div class=\"author_avatar\"><div class=\"author_img\"><a href=\""+commObject.uid+"\"><img src=\""+commObject.uavatar+"\"/></a></div></div><div class=\"comm_content\"><div class=\"author_info\"><span class=\"time\">"+commObject.posttime+" &nbsp; <a href='javascript:;' rel='"+commObject.id+"' class='c_del'>删除</a></span><a href=\""+commObject.uid+"\">"+commObject.uname+"</a>说：</div><div class=\"comm_text\">"+commObject.content+"</div></div></div>");
						$('#comm_'+commObject.id).highlightFade();
						$('#post-'+pid).css('display','none');
						$('#post-'+pid+" textarea").val("");
					}else{
						$(".commlist").append("<div id=\"comm_"+commObject.id+"\"><div class=\"comm_main\"><div class=\"author_avatar\"><div class=\"author_img\"><a href=\""+commObject.uid+"\"><img src=\""+commObject.uavatar+"\"/></a></div></div><div class=\"comm_content\"><div class=\"author_info\"><span class=\"time\">"+commObject.posttime+" &nbsp; <a href='javascript:;' rel='"+commObject.id+"' class='c_del'>删除</a></span><a href=\""+commObject.uid+"\">"+commObject.uname+"</a>说：</div><div class=\"comm_text\">"+commObject.content+"</div></div></div><div class=\"comm_replies\"></div><div class=\"comm-action\"><a class=\"quk_reply\" href=\"###\" rel=\""+commObject.id+"\">回复</a></div></div>");
						$('#comm_'+commObject.id).highlightFade();
						$('#comm_post textarea').val("");
					}
					$('a.quk_reply').click(function(){add_clkevent(this);});
					$('.totalnum b').html(parseInt($('.totalnum b').html())+1);
					
					$('#comm_'+commObject.id+' a.c_del').bind('click', function(){
						var todel=confirm('确实要删除这条评论吗？')
						if(todel) {del_comment(this.rel)};
					});
				}else if(msg_json.state=='2' && msg_json.url!=undefined){
					alert(msg_json.msg);
					window.location.href = msg_json.url;
				}else{
					alert(msg_json.msg);
				}
			}
		);
	}
	
	function get(val){
		var str = String(window.location.href);
		var arr, arr1, arr2, a;
		a = str.substr(str.indexOf("?")+1);
		var arr1 = a.split("&");
		for (key in arr1){
			var arr2 = arr1[key].split("=");
			if (val==arr2[0]){
				return arr2[1];
			}
		}
		return null;
	}
	
	String.prototype.parseJSON = function () {
	    try {
	        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
	                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
	            eval('(' + this + ')');
	    } catch (e) {
	        return false;
	    }
	};	
	
	function make_compare_url(){
		$('#comparenow').attr('href','/compare_result.php?a1='+$('#a1').val()+'&a2='+$('#a2').val()+'&a3='+$('#a3').val()+'&a4='+$('#a4').val()+'&KeepThis=true&TB_iframe=true&height=300&width=550&modal=false;');
		if($('#a1').val() || $('#a2').val() || $('#a3').val() || $('#a4').val()) {
			$('#comparenow').click();
		} else {
			alert("请至少输入一个应用的名称");
		}
	}
