//给列表页和详情页使用

var colors_arr2=new Array('#D07B04','#C7170D','#1D79A6','#12A71D','#8CAE0F','#C4A50A','#C1C608','#C7560B');

//列表页使用
function myrepost(id,aid){repost_pro(id,$('#title_'+aid).html(),$('#url_'+aid).val(),$('#brief_'+aid).html());}

//详情页使用
function repost(value){
    var videotitle = $('#atitle').html();
    var inurl = $('#url').val();
    var videobrief = $('#bintro').html();
    repost_pro(value,videotitle,inurl,videobrief);
}



//王宁 7-13 17:56 1/2
function vote(id, type){
	common_vote("survey", type, "感谢您的参与！投票已成功，请勿重复投票！", "article", 1, id, "survey");
}

function vote_commont(id, ud){
	common_vote("comment", id, "感谢您的参与！投票已成功，请勿重复投票！", "comment", ud, id, "comment");
} //对评论列表投票

function vote_article(action,id){  //对文章详情顶踩
	var articleid = $('#articleid').val();
	if(action == 1)	if(common_vote("article_comment", id, "感谢您的参与！顶踩已成功，请勿重复顶踩。", "art_ding", 1, articleid, "article")==false) return false;
	if(action == 2)	if(common_vote("article_comment", id, "感谢您的参与！顶踩已成功，请勿重复顶踩。", "art_cai",  2, articleid, "article")==false) return false;
}
function common_vote_callback(channel,ud,id,item_type){
	if(channel=="art_ding") $('#ding').html(parseInt($('#ding').html())+1);
	else if(channel=="art_cai") $('#cai').html(parseInt($('#cai').html())+1);
	else if(channel=="comment") $("#commont_up_"+id).html(parseInt($("#commont_up_"+id).html())+1);
	else if(channel=="article") {
		$('[optionid='+id+']').find('.rating').html(parseInt($('[optionid='+id+']').find('.rating').html())+1);
		loadVote();
	}//王宁 7-13 17:56, 2/2
}

function sendEmail(type){
	if(isUserLogined()){
		semail();
    }else{
        //弹出登录层
        login(semail);
    }
}

function semail(){
	var type=$('#articletype').val();
	$.post("/ajax/add_email_sub.php?type="+type, { }, function(data){
		if(data == 1){
		
			close_popup();
			alert("邮件订阅成功！");
		}
	});
}	



function get_now_time(){
	var d = new Date();
	if(d.getMonth()   <10) month = "0"+(d.getMonth()+1); else month = d.getMonth()+1;
	if(d.getDate()    <10) day   = "0"+ d.getDate();     else day = d.getDate(); 
	if(d.getHours()   <10) hour  = "0"+ d.getHours();    else hour = d.getHours(); 
	if(d.getMinutes() <10) min   = "0"+ d.getMinutes();  else min = d.getMinutes(); 
	if(d.getSeconds() <10) sec   = "0"+ d.getSeconds();  else sec = d.getSeconds(); 

	var now = d.getFullYear()+"-"+month+"-"+day+" "+hour+":"+min+":"+sec;
	return now;
}




//-------------------------- from topic_vote.js ----------------------
//页面刷新后重新从后台读取投票数 7-15  王宁
function loadVote(){
	var voteids = $('#voteids').val();
	var arrbing=new Array();
	var arrcaitiao=new Array();
	var k =0;
	var n =0;
	if(typeof(voteids)=="undefined" || voteids==null) return; //2010-7-21
	if(voteids.length>0){
		$.post("/ajax/getVoteNums.php?ids="+voteids, { }, function(data){
			for(var i=0;i<data.length;i++){
				if(typeof(data[i].caitiao) != "undefined" ){ 
					arrcaitiao[n++] = data[i].caitiao;
				}
				if(typeof(data[i].bing) != "undefined" ){
					arrbing[k++] = data[i].bing;
				}
			}
			updateBar(arrcaitiao);
			updatePie(arrbing);
		},'json');
	}
}
function updatePie(arr1){
	var i = 0;
	var str = null;
	$('.survey_pie_type').each(function(){
		var survey_id = $(this).attr('survey_id');
		str = arr1[i++];
		var vpie = "pie_num="+str.join(',') +"&pie_color=0xD07B04,0xC7170D,0x1D79A6,0x12A71D,0x8CAE0F,0xC4A50A,0xC1C608,0xC7560B";		
		flash(
				{
					//vars : 'pie_num=40,55,80,50&pie_color=0xFF0000,0xFF9E01,0xF83F01,0x04D215',
					vars : vpie,
					src : '/flash/pie.swf',
					width : 280,
					height : 200,
					bgcolor : 'transparent',
					id : 'pie_flash',
					wmode:'transparent',
					container:'#flash_pie_' + survey_id
		});
	});
}
function updateBar(arr1)
{
	var i = 0;
	var str = null;	
	$('[voteidc]').each(function(){
		var total = 0;
		var tm = 1;
		str = arr1[i++];
		for(var k=0;k<str.length;k++){
			total += str[k];
			if(tm < str[k]) {tm = str[k];}
		}
		if (total == 0) total = 1;
		var m = 0;
		$(this).find('[optionid]').each(function()
		{
			var vid = parseInt($(this).attr('optionid'));
			var v = str[m++];
			$(this).find('.rating').html(v);
			var percent = v/total;
			var percenting = Math.round(v/total*1000);
			$(this).find('.percenting').html(percenting/10);
			$(this).find('.rating').html(v);
			var pp=v/tm;
			var width = Math.floor(pp  * 150);			
			$('[barid='+vid+']').animate({ width:width+'px' },500);
		});
	});
}
//--------------------------------------------------------------------

function replace_faces(){
	$('.content_list_q_area').find('div span').each(function()
	{
		var content = $(this).html();
		if ($.browser.msie && $.browser.version == 6)
			img = '<img src="/images/blank.gif" height="24" width="24" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/faces/$1\',sizingMethod=\'scale\');" />';
		else
			img = '<img src="/images/faces/$1" />';
		content = content.replace(/\[([a-z0-9\_\-]+\.(gif|jpg|jpeg|png))\]/ig,img);
		$(this).html(content);
	});
}

$(function(){
	//处理mp3标记
	$('img.mp3-player').each(function()
	{
		var url = $(this).attr('url');
		var autoplay = $(this).attr('autoplay');
		var loop = $(this).attr('loop');
		var html = '<embed src="/flash/player_mp3_maxi.swf" flashvars="mp3='+url+'&amp;autoplay='+autoplay+'&amp;loop='+loop+'&amp;width=300&amp;height=20&amp;volume=100&amp;showvolume=1&amp;showinfo=1" width="300" height="20" wmode="transparent" bgcolor="transparent" type="application/x-shockwave-flash" />';
		$(this).replaceWith(html);
	});

	setTimeout(function(){
	    $('.advertisement_block').each(function(){
		if($(this).height() < 20) {
		    $(this).css("margin",'0 auto 0 auto');
		}
	    });
	}, 2000);
});

//投票
$(function(){	
	$('[voteid]').each(function(){ //彩条上色	
		var t = 0;
		$(this).find('[barid]').each(function(){
			t++;
			$(this).addClass('color'+t);
		});
	});
	$('.first_content_view_right').each(function(){//饼图
		var c = 0;
		$(this).find('.color_block').each(function(){
			$(this).attr("style",'background-color:'+colors_arr2[c++]);
		});
		
	});
	loadVote();
});

//from htm
function article_detail(comment_last_update,articleid,chat_server,total_comment_page)
{
	//翻页条
	$('#subpage').makePager({url:'detail.php?id=' + articleid, total:total_comment_page,container:'#comments',onPage:replace_faces});

	//配置表情功能
	for(var i=0;i<faces.length;i++){
		var img = $('<img url="/images/faces/'+faces[i]+'" />').data('face',faces[i]);
		img.click(function()
		{
			$('#comment').get(0).value += '['+$(this).data('face')+']';
			$('#faces').fadeOut(200);
			$('#chat_words').focus();
		}).appendTo('#faces');
	}
	$('#faces_button').click(function(event){
		$('#faces img').each(function()
		{
			if ($(this).attr('url'))
			{
				$(this).attr('src',$(this).attr('url'));
				$(this).removeAttr('url');
			}
		});
		$('#faces').slideDown(200);
		event.stopPropagation();
	});
	$(document).click(function(){
		$('#faces').slideUp(200);
	});

	//请求第一页
	$.post('/ajax/comment.php?act=getcomment',{type:'13',id: articleid,comment_last_update:comment_last_update},function(data){
	    if(not_empty(data)) {
			$('#comments').html(data);reply_init();
	    }
		comment_reply_auto_unfold(); //自动展开回复
		replace_faces();//表情解释显示
	});

	//更新访问次数
	update_visit_num('article',articleid);
	get_single_votecnt(articleid);
}

function get_single_votecnt(articleid){ //2010-7-28
	//alert(articleid);
	$.post('/ajax/rating.php?act=getvotecnt',{item_id: articleid, item_type: 'article'},function(data){
		var pos=data.indexOf('-');
		var up=data.substr(0,pos);
		var down=data.substr(pos+1);
		$("#ding").html(up);
		$("#cai").html(down);
	});
}
