// useful javascript functions

// create a popup window - 21/03/09 - gavin taylor
function popup(link, windowname, width, height, scrollbars, status, resizable, location, toolbar, menubar)
{
	if(!window.focus) { return true; }

	//check if additional options are set
	if(windowname	== null) 	{ windowname 	= 'popup_window'; }
	if(scrollbars	== null) 	{ scrollbars 	= 'no'; }
	if(status			== null) 	{ status 			= 'no'; }
	if(resizable 	== null) 	{ resizable 	= 'no'; }
	if(location 	== null) 	{ location 		= 'no'; }
	if(toolbar 		== null) 	{ toolbar 		= 'no'; }
	if(menubar 		== null) 	{ menubar 		= 'no'; }

	var href;
	if(typeof(link) == 'string') { href=link; }
	else { href=link.href; }

	//window position
	var posleft = (screen.width - width) / 2;
	var postop 	= ((screen.height - height) / 2) / 2;

	newwindow = window.open(href, windowname, 'width=' + width + ', height=' + height + ',left=' + posleft + ',top=' + postop + ',scrollbars=' + scrollbars + ',status=' + status + ',resizable=' + resizable + ',location=' + location + ',toolbar=' + toolbar + ',menubar=' + menubar + '');
	if(window.focus) {newwindow.focus()}
	return false;
}

//creates an HTTP Request Object to be used as required
function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
  {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
  }

	if (window.ActiveXObject)
  {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
  }

	return null;
}

//display flash banner
function show_banner(banner_src, banner_width, banner_height)
{
	document.write('  	  <object width="' + banner_width + '" height="' + banner_height + '" id="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+location.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0">');
	document.write('            <param name="movie" value="' + banner_src + '" ');
	document.write('            <param name="quality" value="high" />');
	document.write('            <param name="wmode" value="transparent" />');
	document.write('            <param name="allowScriptAccess" value="always" />');
	document.write('            <embed src="' + banner_src + '" quality="high" type="application/x-shockwave-flash" pluginspage="'+location.protocol+'://www.macromedia.com/go/getflashplayer" width="' + banner_width + '" height="' + banner_height + '" wmode="transparent" allowScriptAccess="always"></embed>');
	document.write('      </object>');
}

function default_rating(curval,default_message)
{
	for (i=1;i<=5;i++)
	{
		//reset image
		document.getElementById('rating' + i).style.backgroundPosition = '-203px -1px';

		//update if required
		if(i <= curval)
		{
			document.getElementById('rating' + i).style.backgroundPosition = '-220px -1px';
		}
	}
	document.getElementById('pre_message').innerHTML = default_message;
}

function update_rating(rating_value,media)
{
	switch(rating_value)
	{
		case 1:
			message = 'Poor';
			break;

		case 2:
			message = 'Nothing special';
			break;

		case 3:
			if (media == 'video')
			{
				message = 'Worth watching';
			}
			else
			{
				message = 'Worth reading';
			}
			break;

		case 4:
			message = 'Pretty cool';
			break;

		case 5:
			message = 'Excellent!';
			break;

		default:
			message = 'unknown rating selected';
			rating_value = 0;
			break;
	}

	//update stars
	for (i=1;i<=5;i++)
	{
		//reset image
		document.getElementById('rating' + i).style.backgroundPosition = '-203px -1px';

		//update if required
		if(i<=rating_value)
		{
			document.getElementById('rating' + i).style.backgroundPosition = '-237px -1px';
		}
	}

	//update message
	document.getElementById('pre_message').innerHTML = message;
}

function submit_rating(rating_value,user_id,session_id,media_id,media_type)
{
	//create AJAX request variable
	var http = getXmlHttpObject();

	//remove javascript calls from stars to prevent future submission
	for (i=1;i<=5;i++)
	{
		document.getElementById('rating' + i).onmouseover	= null;
		document.getElementById('rating' + i).onmouseout	= null;
		document.getElementById('rating' + i).onclick 		= null;
		document.getElementById('rating' + i).title 		  = 'Thanks for rating!';
	}

	//submit rating details
	http.open("POST", "/scripts/process_rating.php", true);
	var parameters = "user_id=" + user_id + "&session_id=" + session_id + "&media_type=" + media_type + "&media_id=" + media_id +"&media_rating=" + rating_value;
	http.onreadystatechange=function()
	{
		var message = 'Unable to process your request!';
		if(http.readyState == 4)
		{
			if (http.status==200)
			{
				//update message
				message = http.responseText;

				//rewrite message as too long for the pre_message container
				if(message=='Thanks for rating!') { message = 'Thank you.'; }

				//update cookie
				update_cookie('ukfast_' + media_type + '_ratings',media_id);
			}
		}
		document.getElementById('pre_message').innerHTML = message;
	}
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.send(parameters);
}

function update_cookie(cookie_name,cookie_value)
{
	var data = '';

	//check if cookie exists
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(cookie_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + cookie_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			data = unescape(document.cookie.substring(c_start,c_end)) + "|";
		}
	}

	//update cookie data
	data = data + cookie_value;

	//create new cookie to store data in
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+300);
	document.cookie = cookie_name + "=" + data + ";path=/;expires=" + exdate.toGMTString();
}

function toggle_faq(id) {
	if (document.getElementById('faq_'+ id).style.display == 'none')
	{
		document.getElementById('faq_'+ id).style.display = '';
	}
	else
	{
		document.getElementById('faq_'+ id).style.display = 'none';
	}
}
