/*
 * Title:	rating.js
 * Author:	Peter Sterling
 * URI:		http://www.sterling-adventures.co.uk/
 * Date:	29 December 2007
 * Version:	1.0 Initial version.
 * Purpose:	Rating of images on a web page.
 */

// Hide the rating box if the user presses escape.
document.onkeypress = function esc(e)
{
	if(typeof(e) == "undefined") { e = event; }
	if(e.keyCode == 27) { do_hide_rating_box(); }
}


// AJAX image rating.
function rate(uri, r)
{
	ajax_request(rate_path + '/rating/rating.php?act=rate&uri=' + uri + '&val=' + r + '&p=' + rate_path, update_content);
}


// Make the buttons green.
function go_green(i)
{
	var el;
	for(var j = 0; j <= i; j++) {
		el = document.getElementById('pill-' + j);
		el.src = rate_path + '/rating/img/green' + (j + 1) + '.gif';
	}
}


// Put the buttons back to their original colour.
function go_back(i)
{
	var el;
	for(var j = 0; j <= i; j++) {
		el = document.getElementById('pill-' + j);
		el.src = old[j];
	}
}


// Update the content of the rating box.
var old = new Array();
function update_content(txt)
{
	var o = document.getElementById('content');
	var str = txt;
	str += '<br />Press ESC to hide';
	o.innerHTML = str;

	var el;
	for(var j = 0; j < 10; j++) {
		el = document.getElementById('pill-' + j);
		old[j] = el.src;
	}
}


// AJAX to get the rating content.
function get_content(uri, loc)
{
	ajax_request(rate_path + '/rating/rating.php?act=get&uri=' + uri + '&p=' + rate_path, update_content);
	return "<div id='content' class='rate' style='position: absolute; z-index: 999; background: transparent; text-align: center; margin: 4px; " + loc + " padding: 10px;'> </div>";
}


// Show the rating box.
function show_rating_box()
{
	var el = document.getElementById("rate");

	var obj = this;
	c_left = obj.offsetLeft;
	c_top = obj.offsetTop;
	c_height = obj.offsetHeight;
	while(obj = obj.offsetParent) {
		c_left += obj.offsetLeft;
		c_top += obj.offsetTop;
	}

	var location_str = 'left: ' + Math.max(c_left + ((this.width / 2) - 170), c_left + 10) + 'px; top: ' + (c_top + 10) + 'px; width: 320px; height: 100px;';

	var str = "<div id='rating_back' class='rate' style='position: absolute; z-index: 998; background: black; text-align: center; margin: 4px; padding: 10px; ";
	str += location_str + "'>&nbsp;</div>";
	str += get_content(this.src, location_str);

	el.innerHTML = str;
	change_opacity(40, 'rating_back');
	el.style.visibility = "visible";
}


// Hide the rating box.
function do_hide_rating_box()
{
	var el = document.getElementById("rate");
	el.style.visibility = "hidden";
}


// Hide the rating box, if the mouse isn't still over an object of class 'rate'.
function hide_rating_box()
{
	var e = window.event;
	var relTarg = e.relatedTarget || e.toElement;
	if(relTarg.className != 'rate') do_hide_rating_box();
}


// Initialise the rating system for images within the named section (element) of a page.
var rate_path;
function start_image_rating(section)
{
	var el = document.getElementById(section);

	if(el) {
		var imgs = el.getElementsByTagName('IMG');
		for(var i = 0; i < imgs.length; i++) {
			if(!imgs[i].className.match("wp-smiley|no-rate|attachment-.")) {
				imgs[i].onmouseover = show_rating_box;
				imgs[i].onmouseout = hide_rating_box;
			}
		}
	}

	// This is to get out of the WordPress directory structure!
	rate_path = (typeof(window['blog_path']) != "undefined" ? blog_path + '/..' : '..');
}
