/*
@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

*/

$(document).ready(function() {
	//set the CSS for this width
	pageWidthHelper.setWidthStyles();

	// 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 print page link - since it can only work for JS, only add it for JS users
	$('<li></li>') // create list item
	.addClass('print') //add relevant class to it
	.append('<a href="javascript:window.print();">Print</a>') //put in the link
	.insertBefore('.page-recommend'); //add before the recommend this page item
	
	//attach js funtionality to recommend page
	$('.page-recommend a').bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if(!code || (code && code == 13)) {// if no key code (mouse) or if enter is pressed
			var pageID = $(this).attr('href').replace(/\/applications\/send_to_friend\/compose.rm\?id=/,""); //get the ID of the page to be recommended
			email_to_friend(pageID); //call the email to friend function
			event.preventDefault(); //prevent browser from following the actual href
		}
	});

	
	//attach js funtionality to post a comment
	/*$('.post-comment a').bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if(!code || (code && code == 13)) {// if no key code (mouse) or if enter is pressed
			var pageID = $(this).attr('href').replace(/\/applications\/comments\/comments.rm\?article_id=/,""); //get the ID of the page to be commented on
			post_comment(pageID); //call the post comment function
			event.preventDefault(); //prevent browser from following the actual href
		}
	});*/
	
	//attach js funtionality to video link
	/*$('a.launch-video').bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if(!code || (code && code == 13)) {// if no key code (mouse) or if enter is pressed
			var ids = $(this).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='
			videoconsole(ids[0],ids[1]); //call the video console function
			event.preventDefault(); //prevent browser from following the actual href
		}
	});*/
	
	//IE6 hack for nav drop-downs
	$('ul#nav li').hover (function(event) {
		$(this).addClass('over');}, 
	  function(event) {
		$(this).removeClass('over');
	});
	
	//to submit form when filter select has changed (eg, on Events region select)
	jQuery("form.filter select").change(function () {
		this.form.submit();
	});
	jQuery("form.filter .button").hide();
	
});

//set the CSS for this width on browser resize
jQuery(window).resize(function(){
	pageWidthHelper.setWidthStyles(jQuery(window).width());
});

//newsletter subscribe box
function clearFieldDefault(fieldElem, defaultVal){
	if($(fieldElem).val() == defaultVal){
		$(fieldElem).val('');
	
		$(fieldElem).blur(function(){
			if($(this).val() == ""){
				$(this).val(defaultVal);
			};
		});
	}
};


function email_to_friend(page) {
	window.open('/applications/send_to_friend/compose.rm?id='+page,'Email','width=540,height=510,scrollbars=1');
}

function post_comment(page) {
	window.open('/applications/comments/comments.rm?article_id='+page,'Comment','width=380,height=550,scrollbars=1');
}

function videoconsole(id,page) {
	window.open('/applications/mediaplayer/video.rm?media_id='+id+'&id='+page,'Video console','width=450,height=400,scrollbars=1');
}

/* 
  helper class to set width styles according to page width 
  Requires Jquery 1.2.1 
*/ 
pageWidthHelper = {
    // Properties
    min_width_breakpoint : 960,
    standard_class : 'window_standard',
    narrow_class : 'window_800',
    
    setWidthStyles: function() {
      var winWidth = jQuery(window).width();
      
      //set appropriate CSS for this browser width
      if(winWidth < this.min_width_breakpoint){
        jQuery('body').removeClass(this.standard_class);
        jQuery('body').addClass(this.narrow_class);
      }else{
        jQuery('body').removeClass(this.narrow_class);
				jQuery('body').addClass(this.standard_class);
      }
    }
};  


/* obj = { foo: bar, baz: {...}, blort: function() {} }; */

/*window.Obj = {
  Events: {
    liOver: function(event) {
	},
    liOut: function(event) {
	}
  },
  
  go: function() {
    jQuery('ul#nav li').hover(Obj.Events.liOver, Obj.Events.liOut);
  }
};
jQuery(document).ready(Obj.go);*/
