function initdials(path,sid) {
	var agt = navigator.userAgent.toLowerCase();
	document._dial_isMacIE = agt.indexOf('mac')!=-1 && agt.indexOf('msie')!=-1 && agt.indexOf('opera')==-1;
	document._dial_path=path+'/';
	document._dial_sid=sid;
	new Image().src = path+'/hourglass.gif';
	for(var i=1;i<=7;++i) new Image().src=path+'/ra_lg_'+i+'.gif';
}

function insertdial(img,loggedin,dtid) {
	var rating_txt;
	rating_txt = 'Average rating ' + img.slice( img.length-5 , img.length-4 )
	document.write('<img alt="' + rating_txt + '" src="',document._dial_path,img,'"',
		' class="dial-interact"',
		' id="dial-', dtid, '"',
		' onmouseover="dial_over(this,',loggedin,',',dtid,',\'',document._dial_sid,'\')"',
		' onmouseout="dial_out(this)"',
		' onmousemove="dial_move(this,event)"',
		' onclick="dial_click(this)"',
		'>');
}

function dial_over(o,logged_in,dt_id) {
	if (typeof(o._saving)!='undefined' && o._saving) return;
	o._logged_in=logged_in;
	o._dt_id=dt_id;
	o._segment=null;
	o._interact=logged_in;
	if (typeof(o._off)=='undefined') o._off=o.src;
}

function dial_out(o) {
	if (typeof(o._saving)!='undefined' && o._saving) return;
	if (typeof(o._off)!='undefined') o.src=o._off;
}

function dial_poll(id) {
	var o=document.getElementById(id);
	if (o.complete) {
		if (o._saving && !o._hourglass) {
			window.clearInterval(o._poll);
		}
		o._onload();
	}
}

function dial_click(o) {
	if (!o._logged_in) {
		alert("Please log in to rate this film.");
	}
	else if (o._interact) {
		o._interact=false;
		o._rating=o._segment;
		o._saving=true;
		o._hourglass=true;
		o._off=document._dial_path+'ra_sm_'+o._rating+'.gif';
		o._savesrc='/dial_log.php'+ 
						'?t='+(new Date).valueOf()+
						'&dt_id='+o._dt_id+
						'&rate='+o._rating+
						'&redirect='+o._off.replace(/\//g, '%2F')+
						'&'+document._dial_sid;
		o._onload=function() {
			if (this._saving) {
				if (this._hourglass) {
					this._hourglass=false;
					this.src=this._savesrc;
				} else {
					this._saving=false;
				}
			}
		}
		if (typeof(o.complete)=='undefined') {
			o.onload=function(){this._onload();};
		} else {
			o._poll=window.setInterval('dial_poll("'+o.id+'")', 250);
		}
		o.src=document._dial_path+'hourglass.gif';
	}
}

function dial_move(o,e) {
	if (o._interact) {
		if (!e) e=window.event;
		if (!e) return;
		if (typeof(e.pageX)=='undefined' || typeof(o.x)=='undefined' && typeof(e.offsetX)!='undefined') {
			var x=e.offsetX;
			var y=e.offsetY;
		} else {
			var x=e.pageX-o.x;
			var y=e.pageY-o.y;
		}
		if (document._dial_isMacIE) {
			x += document.body.scrollLeft;
			y += document.body.scrollTop;
		}
		x -= o.offsetWidth/2;
		y -= o.offsetHeight/2;
		with(Math) {
			var segment=floor((atan2(x,-y)/(2*PI) + 1) * 7) % 7 + 1;
		}
		if (o._segment != segment) {
			o._segment=segment;
			o.src=document._dial_path+'ra_lg_'+o._segment+'.gif';
		}
	}
}

