/*
@description Add general site-wide js funtionality to the page (e.g. print page button)
@author	Bryan Gullan
@created 2007-08-28

Notes: Uses jQuery; written on v. 1.1.4

*/

$(function() {
	// deal with search box
	var defaultText = "Enter search term";
	$('#searchinput').val(defaultText).focus(function() {
		if (this.value == defaultText) {
			$(this).val('');
		}
	}).blur(function() {
		if (this.value == '') {
			$('#searchinput').val(defaultText);
		}
	});
	
	// add text resize and contrast controls
	$('<li class="text-small"><a href="javascript:textsizer(\'\')" title="Small text">Small</a></li><li class="text-medium"><a href="javascript:textsizer(\'med\')" title="Medium text">Medium</a></li><li class="text-large"><a href="javascript:textsizer(\'lrg\')" title="Large text">Large</a></li><li class="contrast-normal"><a href="javascript:contrastchanger(\'\')" title="Normal contrast">Normal contrast</a></li><li class="contrast-soft"><a href="javascript:contrastchanger(\'soft\')" title="Soft contrast">Soft contrast</a><li class="contrast-inverse-soft"><a href="javascript:contrastchanger(\'inversesoft\')" title="Inverse soft contrast">Inverse soft contrast</a></li>')
	.insertBefore('.accessibility');
	
	// add print page link - since it can only work for JS, only add it for JS users
	$('<li></li>') // create list item
	.addClass('page-print') //add relevant class to it
	.append('<a href="javascript:window.print();">Print page</a>') //put in the link
	.insertAfter('.page-recommend'); //add before the recommend this page item
	
	//attach js funtionality to recommend page
	$('li.page-recommend a').click(function(event){
		email_to_friend()
		event.preventDefault();
	});
		
	//attach js funtionality to video link
	$('a.launch-video').click(function(event){
		videoconsole(event.target);
		event.preventDefault();
	});
	
	$('a.microsite').click(function(event){
		launchmicrosite(event.target);
		event.preventDefault();
	});
	
	//IE6 hack for nav drop-downs
	$('ul#nav li[@id]').bind('mouseover', function(event) {
		$(this).addClass('over');
	});
	$('ul#nav li[@id]').bind('mouseout', function(event) {
		$(this).removeClass('over');
	});
	
});


// SEND TO FRIEND


function email_to_friend() {
	var pageID = $('li.page-recommend a').attr('href').replace(/\/applications\/send_to_friend\/compose.rm\?id=/,""); //get the ID of the page to be recommended
	window.open('/applications/send_to_friend/compose.rm?id='+pageID,'Email','width=380,height=550,scrollbars=1');
}

function post_comment() {
	var pageID = $('li.post-comment a').attr('href').replace(/\/applications\/comments\/comments.rm\?article_id=/,""); //get the ID of the page to be commented on
	window.open('/applications/comments/comments.rm?article_id='+pageID,'Comment','width=380,height=550,scrollbars=1');
}

function videoconsole(element) {
	var ids = $(element).attr('href').replace(/\/applications\/mediaplayer\/video.rm\?media_id=/,""); //get the media and page ids
	ids = ids.split("&id="); // split the two ids; they're either side of the '&id='
	window.open('/applications/mediaplayer/video.rm?media_id='+ids[0]+'&id='+ids[1],'Videoconsole','width=450,height=450,scrollbars=1');
}

function launchmicrosite(element) {
	var siteURL = $(element).attr('href');
	window.open(siteURL,'microsite','width=760,height=700,scrollbars=1');
}
