// JavaScript Document
var interval;
function previewImage(newImg)
{
	if(interval) clearTimeout(interval);
	var prev=$("previewImg");
	if(prev)
	{
		var img=new Image();
		img.src=newImg.value;
		if(img.width>100 || img.height>80);
		{
			if(parseFloat(img.width/100)>parseFloat(img.height/80))
			{
				prev.width=100;
				prev.height=img.height/parseFloat(img.width/100);
			}
			else
			{
				prev.height=80;
				prev.width=img.width/parseFloat(img.height/80);
			}
		}
		prev.src='file://'+newImg.value;
	}
	interval=setTimeout(function(){previewImage(newImg);},200);
	return true;
}
function updateRating(txt)
{
	var r=$("current-rating");
	var c=$("rater-count");
	var s=$("current-score");
	var vc=$("viewCount");
	var data=txt.split(';');
	var rating = data[0];
	var counter = data[1];
	var response = data[2];
	r.style.width=parseInt(parseFloat(rating)*16)+'px';
	c.innerHTML="Raters: <b>"+parseInt(counter)+"</b>";
	s.innerHTML="Current Rating: <b>"+parseFloat(rating)+"/5</b>";
	
	var oldResult=$("rating-result");
	if(oldResult) vc.removeChild(oldResult);
	
	var div = document.createElement("div");
	div.className="center bold";
	div.setAttribute("id","rating-result")
	div.innerHTML=response;
	vc.appendChild(div);
	
	//remove the links
	var star_rating=$("star-rating");
	var target=star_rating.firstChild;
	while(target.nodeName.toUpperCase() != "LI")
	{
		target=target.nextSibling;
	}

	while(target.nextSibling)
	{
		star_rating.removeChild(target.nextSibling);
	}

	removeLoadingImg("loadingImg");
}
function updateAVRating(txt)
{
	var r=$("current-rating");
	var c=$("rater-count");
	var s=$("current-score");
	var vc=$("ratingmemsg");
	var data=txt.split(';');
	var rating = data[0];
	var counter = data[1];
	var response = data[2];
	r.style.width=parseInt(parseFloat(rating)*16)+'px';
	c.innerHTML="<b>"+parseInt(counter)+"</b>&nbsp;ratings";
	s.innerHTML="Current Rating: <b>"+parseFloat(rating)+"/5</b>";
	
	var oldResult=$("rating-result");
	if(oldResult) vc.removeChild(oldResult);
	
	var div = document.createElement("div");
	if (response.indexOf("Thank you") == -1)
	{
	   div.className="bold red";
	   response="You have rated this "+jswhat+" already.";
	}
	else
	{
	   div.className="bold";
	   response="Thank you for rating.";
	}
	div.setAttribute("id","rating-result")
	div.innerHTML=response;
	vc.appendChild(div);
	
	//remove the links
	var star_rating=$("star-rating");
	var target=star_rating.firstChild;
	while(target.nodeName.toUpperCase() != "LI")
	{
		target=target.nextSibling;
	}
	while(target.nextSibling)
	{
		star_rating.removeChild(target.nextSibling);
	}
	removeLoadingImg("loadingImg");
}

function loadingImg(id)
{
	var target=$(id);
	if(target)
	{
		if(!target.firstChild)
		{
			var loading=document.createElement("img");
			loading.setAttribute("src","images/loading.gif");
			target.appendChild(loading);
			return true;
		}
	}
	return false;
}
function removeLoadingImg(id)
{
	var loading=$(id);
	if(loading)
	{
		loading.removeChild(loading.firstChild);
		return true;
	}
	return false;
}
function resizeadsvertical(id, w, gweight)
{
	var img=new Image();
	img.src=id.src;
	if (img.width>gweight)
	{
		id.style.width=gweight+'px';
	}
	return true;
}

function resizeme(id, h, gheight)
{
	var img=new Image();
	img.src=id.src;
	if ((img.width==id.width)&&(img.height==id.height)) { return true; }
	if ((img.width<id.width)&&(img.height<id.height))
	{
		id.style.width=img.width+'px';
		id.style.height=img.height+'px';
	}
	else if ((h==1)&&(img.height>img.width))
	{
		ratio=img.width/img.height;
		newwidth=parseInt(ratio*id.width + 0.5);
		id.style.width=newwidth+'px';
	}
	else
	{
		ratio=img.height/img.width;
		newheight=parseInt(ratio*id.height + 0.5);
		if (newheight>gheight)
		{
			id.style.height=newheight+'px';
		}
		//else if ((parseInt(newheight/gheight)<50)&&(gheight>=100))
		else if ((newheight/gheight)>0.1)
		{
					id.style.height=newheight+'px';
		}
		}
	return true;
}
function moderate_hints(msgid,msg)
{
 document.getElementById(msgid).innerHTML=msg;
 return true;
}
function submitForm(fid,action)
{
	var form=$(fid);
	if(form)
	{
		form.action.value=action;
		form.submit();
	}
	return false;
}
