// 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(newwindow) { 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 change_featured_tab(show_tab,tab_id,tab_links_id,no_of_tabs) {
	//loop through all tabs
	for(i=1;i<=no_of_tabs;i++) {
		var tmp_tab_id 	= tab_id + i;
		var tab_class 	= '';
		var tab_content = 'none';

		if(i == show_tab) {
			tab_class 	= 'active';
			tab_content = 'block';
			$('ul > li#' +tmp_tab_id+ ' > a').addClass(tab_class);
		}else {
			$('ul > li#' +tmp_tab_id+ ' > a').removeClass('active');
		}

		//activate tab
		document.getElementById(tmp_tab_id).className = tab_class;

		//show tab content
		document.getElementById(tmp_tab_id + '_content').style.display = tab_content;
	}

	//update tabs class
	document.getElementById(tab_links_id).className = tab_id + show_tab + '_active';

	return false;
}

/* Media Rating - */
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 = '';
		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);
			}
			else { message = 'Unable to process your request!'; }
		}
		
		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();
}


/* other */

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';
	}
}

//tabs.js
function ddtabcontent(a){this.tabinterfaceid=a;this.tabs=document.getElementById(a).getElementsByTagName("a");this.enabletabpersistence=true;this.hottabspositions=[];this.currentTabIndex=0;this.subcontentids=[];this.revcontentids=[];this.selectedClassTarget="link"}ddtabcontent.getCookie=function(a){a=new RegExp(a+"=[^;]+","i");if(document.cookie.match(a))return document.cookie.match(a)[0].split("=")[1];return""};ddtabcontent.setCookie=function(a,b){document.cookie=a+"="+b+";path=/"}; ddtabcontent.prototype={expandit:function(a){this.cancelautorun();var b="";try{if(typeof a=="string"&&document.getElementById(a).getAttribute("rel"))b=document.getElementById(a);else if(parseInt(a)!=NaN&&this.tabs[a].getAttribute("rel"))b=this.tabs[a]}catch(c){alert("Invalid Tab ID or position entered!")}b!=""&&this.expandtab(b);switch(a){case "tab_1_title":document.getElementById("tab").style.backgroundPosition="0px 0px";break;case "tab_2_title":document.getElementById("tab").style.backgroundPosition= "0px -26px";break;case "tab_3_title":document.getElementById("tab").style.backgroundPosition="0px -53px";break;case "tab_4_title":document.getElementById("tab").style.backgroundPosition="0px -79px";break;case "tab_5_title":document.getElementById("tab").style.backgroundPosition="0px -106px";break}},cycleit:function(a,b){if(a=="next")var c=this.currentTabIndex<this.hottabspositions.length-1?this.currentTabIndex+1:0;else if(a=="prev")c=this.currentTabIndex>0?this.currentTabIndex-1:this.hottabspositions.length- 1;typeof b=="undefined"&&this.cancelautorun();this.expandtab(this.tabs[this.hottabspositions[c]])},setpersist:function(a){this.enabletabpersistence=a},setselectedClassTarget:function(a){this.selectedClassTarget=a||"link"},getselectedClassTarget:function(a){return this.selectedClassTarget=="linkparent".toLowerCase()?a.parentNode:a},urlparamselect:function(a){return window.location.search.match(new RegExp(a+"=(\\d+)","i"))==null?null:parseInt(RegExp.$1)},gototab:function(a){switch(a){case "1":tab.expandit("tab_1_title"); break;case "2":tab.expandit("tab_2_title");break;case "3":tab.expandit("tab_3_title");break;case "4":tab.expandit("tab_4_title");break;case "5":tab.expandit("tab_5_title");break}},expandtab:function(a){var b=a.getAttribute("rel"),c=a.getAttribute("rev")?","+a.getAttribute("rev").replace(/\s+/,"")+",":"";this.expandsubcontent(b);this.expandrevcontent(c);for(c=0;c<this.tabs.length;c++)this.getselectedClassTarget(this.tabs[c]).className=this.tabs[c].getAttribute("rel")==b?"selected":"";this.enabletabpersistence&& ddtabcontent.setCookie(this.tabinterfaceid,a.tabposition);this.setcurrenttabindex(a.tabposition)},expandsubcontent:function(a){for(var b=0;b<this.subcontentids.length;b++){var c=document.getElementById(this.subcontentids[b]);c.style.display=c.id==a?"block":"none"}},expandrevcontent:function(a){for(var b=this.revcontentids,c=0;c<b.length;c++)document.getElementById(b[c]).style.display=a.indexOf(","+b[c]+",")!=-1?"block":"none"},setcurrenttabindex:function(a){for(var b=0;b<this.hottabspositions.length;b++)if(a== this.hottabspositions[b]){this.currentTabIndex=b;break}},autorun:function(){this.cycleit("next",true)},cancelautorun:function(){typeof this.autoruntimer!="undefined"&&clearInterval(this.autoruntimer)},init:function(a){var b=ddtabcontent.getCookie(this.tabinterfaceid),c=-1,d=unescape(self.document.location.hash.substring(1)),f=d;if(d!="")f=d-1;this.automodeperiod=a||0;for(a=0;a<this.tabs.length;a++){this.tabs[a].tabposition=a;if(this.tabs[a].getAttribute("rel")){var e=this;this.hottabspositions[this.hottabspositions.length]= a;this.subcontentids[this.subcontentids.length]=this.tabs[a].getAttribute("rel");this.tabs[a].onclick=function(){e.expandtab(this);e.cancelautorun();return false};if(this.tabs[a].getAttribute("rev"))this.revcontentids=this.revcontentids.concat(this.tabs[a].getAttribute("rev").split(/\s*,\s*/));if(f==a||this.enabletabpersistence&&c==-1&&parseInt(b)==a||!this.enabletabpersistence&&c==-1&&this.getselectedClassTarget(this.tabs[a]).className=="selected")c=a}}c!=-1?this.expandtab(this.tabs[c]):this.expandtab(this.tabs[this.hottabspositions[0]]); if(parseInt(this.automodeperiod)>500&&this.hottabspositions.length>1)this.autoruntimer=setInterval(function(){e.autorun()},this.automodeperiod)}};

//scroll to a particular element
function scroll_to(element_id) {
  var xpos = 0;
  var ypos = 0;
	var element = document.getElementById(element_id);
	
	if(element != null) {
		while(element != null) {
			xpos += element.offsetLeft;
			ypos += element.offsetTop;
			element = element.offsetParent;
		}
		
		window.scrollTo(xpos,ypos-5);
	}
}

//Google +1 Code
function plusone_vote( obj )
{
	_gaq.push(['_trackEvent','plusone',obj.state]);
}


	
