﻿var VOTE={
	domID:'',
	objID:'',
	points:0,
	type:'',
	/*回调函数*/
	func:null,
	init:function(domID, objID ,points, type, func){
		VOTE.domID = '#'+domID;
		VOTE.objID = objID;
		VOTE.points = points;
		VOTE.type = type;
		VOTE.func = func;
		var p_css = document.createElement('link');
		p_css.href = '/common/css/fweb/points/base.css';
		p_css.rel='stylesheet';
		p_css.type='text/css';
		document.getElementsByTagName('head')[0].appendChild(p_css);
		VOTE.loadstar();
	},
	loadstar:function()
	{
		var h = '<ul class="star-rating2">';
		h += '<li class="current-rating" id="current-rating"></li>';
		h += '<li><a href="javascript:void(0);" onclick="javascript:VOTE.rateImg(\'1\'); VOTE.showWindow(\'1\');" title="1 星" class="one-star">1</a></li>';
		h += '<li><a href="javascript:void(0);" onclick="javascript:VOTE.rateImg(\'2\'); VOTE.showWindow(\'2\');" title="2 星" class="two-stars" style="left: 25px;">2</a></li>';
		h += '<li><a href="javascript:void(0);" onclick="javascript:VOTE.rateImg(\'3\'); VOTE.showWindow(\'3\');" title="3 星" class="three-stars">3</a></li>';
		h += '<li><a href="javascript:void(0);" onclick="javascript:VOTE.rateImg(\'4\'); VOTE.showWindow(\'4\');" title="4 星" class="four-stars">4</a></li>';
		h += '<li><a href="javascript:void(0);" onclick="javascript:VOTE.rateImg(\'5\'); VOTE.showWindow(\'5\');" title="5 星" class="five-stars">5</a></li>';
		h += '</ul>';
		$(VOTE.domID).html(h);
		VOTE.rateImg(VOTE.points);
	},
	detect_ie:function()
	{
		var bname = navigator.appName;
		if(bname.search(/microsoft/i) == 0)
			return 1;
		return 0;
	},
	rateImg:function(rating)
	{
		rating = rating * 25;
		document.getElementById('current-rating').style.width = rating+'px';
		//if(this.detect_ie())
		//	document.getElementById('current-rating').style.marginLeft = '-63px';
	},
	showWindow:function(score)
	{
		var url = '';
		switch(VOTE.type)
		{
			case 'photo':
				url = '/FAjax/Photo/AddPhotoScore.dhtml?PhotoID=' + VOTE.objID + '&ScoreNum='+ score + '&time='+new Date().getTime(); 
				break;
			case 'catalog':
				url = '/FAjax/Catalog/AddCatalogScore.dhtml?CatalogID=' + VOTE.objID + '&ScoreNum='+ score+ '&time='+new Date().getTime(); 
				break;
			case 'article':
				url = '/FAjax/Article/AddBlogArticleScore.dhtml?ArticleID=' + VOTE.objID + '&ScoreNum='+ score+ '&time='+new Date().getTime();
				break;
			default:
				break;
		}

		if('' == url){
			alert('js错误，请与图说天下联系!');
			return;
		}

		$.getJSON(url,
			function (data){
				if(!data.error){
				    VOTE.points =(0==data.VoteSumCount)?0:data.VoteScore/data.VoteSumCount;
				    VOTE.loadstar();
    				
				    if(!VOTE.func)
				        return;
				    VOTE.func(data);
				}
				else
				{
					alert(data.error);
					VOTE.loadstar();
					return;
				}
			}
		);
	}
}

