var xmlHttp;
var myid =0;
function createXMLHttpRequest() 
{
    if (window.ActiveXObject) 
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) 
    {
        xmlHttp = new XMLHttpRequest();
    }
}

	var starImages = new Array("http://image.top100.cn/WebImg/0star.gif",
	"http://image.top100.cn/WebImg/1star.gif",
	"http://image.top100.cn/WebImg/2star.gif",
	"http://image.top100.cn/WebImg/3star.gif",
	"http://image.top100.cn/WebImg/4star.gif",
	"http://image.top100.cn/WebImg/5star.gif");
	var nullStarMessage = "http://image.top100.cn/WebImg/0.gif"
	var starMap = new Array('0,0,22,20',
	'23,0,36,20',
	'37,0,50,20',
	'51,0,64,20',
	'65,0,78,20',
	'79,0,101,20');
	var starMessages = new Array("http://image.top100.cn/WebImg/0.gif",
	"http://image.top100.cn/WebImg/1.gif",
	"http://image.top100.cn/WebImg/2.gif",
	"http://image.top100.cn/WebImg/3.gif",
	"http://image.top100.cn/WebImg/4.gif",
	"http://image.top100.cn/WebImg/5.gif",
	"http://image.top100.cn/WebImg/saved.gif");

	var savedRatings = new Array();
	var changedRatings = new Array();
	var starTwinkler = new Array();
	var msgTwinkler = new Array();
	var isRatingsBarChanged = false;
	var delayTime = 500;
	var allImages = new Array();
function preloadImages()
{
	for (i=0; i < preloadImages.length ;i++)
	{
	allImages[i] = new Image();
	allImages[i].src = preloadImages.arguments[i];
	}
}
preloadImages(starImages);
preloadImages(starMessages);

function SaveRatingAjax(id, ratingType, ratingValue)
{
	rating = ratingValue;
	//var submitURL = ''
	//+ 'ajaxRating.aspx?'
	//+ 'id=' + id
	//+ '&Rating=' + ratingValue ;
	
	myid = id;
	
	isRatingsBarChanged = true;
	addTagAjax.AddScore(myid,rating);
	handleStateChange();
	
}
function handleStateChange() 
{
        //var myVotes = document.getElementById("lblVotes" + myid).innerHTML;
        //myVotes = eval(parseInt(myVotes,10) + parseInt(1,10));
        addTagAjax.GetScore(myid,showScore);
        addTagAjax.RecGraCount(myid,showCount);
        document.getElementById("ltlstars" + myid).innerHTML  = '<IMG src=http://image.top100.cn/WebImg/' + rating + 'star.gif>';
		document.getElementById("ltlMsg" + myid).innerHTML  = '<IMG src=http://image.top100.cn/WebImg/saved.gif>';
        //alert("ltlMsg" + myid);

         
}

function showScore(resValue)
{
	var tempresVal = resValue.value;
	var imgVal = 0;
	if (tempresVal >= 0 && tempresVal < 0.3)
	{
		imgVal = 0;
	}else if (tempresVal >= 0.3 && tempresVal < 0.8) 
	{
		imgVal = 0.5;
	}else if (tempresVal >= 0.8 && tempresVal < 1.3)
	{
		imgVal = 1;
	}else if (tempresVal >= 1.3 && tempresVal < 1.8)
	{
		imgVal = 1.5;
	}else if (tempresVal >= 1.8 && tempresVal < 2.3)
	{
		imgVal = 2;
	}else if (tempresVal >= 2.3 && tempresVal < 2.8)
	{
		imgVal = 2.5;
	}else if (tempresVal >= 2.8 && tempresVal < 3.3)
	{
		imgVal = 3;
	}else if (tempresVal >= 3.3 && tempresVal < 3.8)
	{
		imgVal = 3.5;
	}else if (tempresVal >= 3.8 && tempresVal < 4.3)
	{
		imgVal = 4;
	}else if (tempresVal >= 4.3 && tempresVal < 4.8)
	{
		imgVal = 4.5;
	}else if (tempresVal >= 4.8)
	{
		imgVal = 5;
	}
	document.getElementById("mytrating" + myid).innerHTML = '<IMG src="http://Image.top100.cn/webImg/stars' + imgVal + '.gif">';

	//document.getElementById("lblScore" + myid).innerHTML = myScore;
	//document.getElementById("lblVotes" + myid).innerHTML = myVotes;


}
function showCount(resValue)
{
	var tempCount = resValue.value;
    document.getElementById("lblVotes" + myid).innerHTML = tempCount;

}
function SwapStars(id, rating)
{
	if (rating == undefined)
	{
		rating = 0;
	}
	//alert("stars." + id);
	document.images["stars." + id].src = starImages[rating];

	
}
function SwapStarMsg(id, rating)
{

	if (rating == undefined)
	{
		if ( changedRatings[id] ) 
		{
		
			document.images["messages." + id].src = starMessages[6];
		} 
		else 
		{
		
			document.images["messages." + id].src = nullStarMessage;
		}
	} 
	else 
	{
		if (document.images["messages." + id] != null)
		{
		document.images["messages." + id].src = starMessages[rating];
		}
	}

}
function SaveStarsAjax(id, rating)
{
 
  
	if ((rating==1)||(rating==2)||(rating==3)||(rating==4)||(rating==5))
	{
	savedRatings[id] = rating;
	changedRatings[id] = 1;
	SaveRatingAjax(id, 'onetofive', rating);
	SwapStarMsg(id, 6);
	}
	else
	{
	alert("Rating Value out of the bound, Values can only be 1/2/3/4/5. Current rating value: " + rating);
	}
}

function StarMouseOver(id, rating)
{
//alert("StarMouseOver:" + id + ":" + rating);

	if (starTwinkler[id] != 0)
	{
	window.clearTimeout(starTwinkler[id]);
	starTwinkler[id] = 0;
	}
	if (msgTwinkler[id] != 0)
	{
	window.clearTimeout(msgTwinkler[id]);
	msgTwinkler[id] = 0;
	}
  SwapStars(id, rating);
  SwapStarMsg(id, rating); 
}
function StarMouseOut(id)
{
//alert("StarMouseOut:" + id);

	starTwinkler[id] = window.setTimeout("SwapStars('"+id+"')", delayTime);
	msgTwinkler[id] = window.setTimeout("SwapStarMsg('"+id+"')", delayTime);
}
function DisplayStarsAjax (id, rating)
{
//alert("DisplayStarsAjax:" + id);

	var starID = "stars." + id;
	starTwinkler[id] = 0;
	msgTwinkler[id] = 0;
	document.write("<map name='starmap" + id +"'>");
	var i = 0;
	for (i = 1; i < 6; i++) 
	{
	document.write("<area shape=rect " + 
	"coords='" + starMap[i] + "' " +
	"onMouseOver=\"StarMouseOver('" + id + "'," + i + ");\" " +
	"onMouseOut=\"StarMouseOut('" + id + "');\" " +
	"onClick=\"SaveStarsAjax('" + id + "'," + i + ");" +
	"\" >");
	}
	
	document.write("</map>");
	document.write("<img vspace=2 title = 'Çë´ò·Ö£¡' src='" + starImages[rating] + "'");
	document.write(" border=0 usemap='#starmap" + id);
	document.write("' id='" + starID + "'>");
	
}
function DisplayMsgAjax (id, rating)
{
//alert("DisplayMsgAjax:" + id);
	var msgID = "messages." + id;
	if ( rating == undefined ) 
	{
		document.write("<img vspace=2 height=11 src='" + nullStarMessage + "'");
	}
	else 
	{
	document.write("<img vspace=2 height=11 src='" + starMessages[rating] + "'"); 
	}
	document.write("' id='" + msgID + "'>");
    addTagAjax.RecGraCount(id,showCount);          
}
function initCount(id)
{
    myid = id;
    addTagAjax.RecGraCount(id,showCount);          
}
function initmytrating(id)
{
    myid = id;
    addTagAjax.GetScore(id,showScore);          
}

