//	Site-wide Javascript functions

//	Note: If you plan to use jQuery, it must be wrappered inside an onReady function declaration

jq(document).ready(function() {
	//	jQuery code goes here
	
	// Hide the button on the programme selector
	jq("#programme_selector_button").css({display: 'none'});
	// Auto submit form on change
	jq("#programme_selector").bind("change", function(e) {
		if (jq(this).val() > 0) {
			jq(this).parent('form').get(0).submit();
		}
	});
	
	//	Some CSS corrections
	jq("input[type=submit]").addClass('button');
	jq("input[type=checkbox]").css({background: "#fff", border: "0px"});

	//	Tooltip support

	//	This function gets the tooltip to track the movement of the mouse
	jq.fn.tooltipmove = function(e) {
//		var xMouseSpace = 15;
//		var tipWidth = 250;
//		var windowWidth = jq(window).width();
//		var compareWidth = e.pageX + xMouseSpace + tipWidth + 20; // 20 - counteracts addition of scrollbar
//
//		if (compareWidth > windowWidth) {
//			jq("#helptexthover").css("left",  xMouseSpace - tipWidth);
//		    jq("#helptexthover").css("top", e.pageY + 20);
//		} else {
//		    jq("#helptexthover").css("left", xMouseSpace);
//		    jq("#helptexthover").css("top", e.pageY + 20);
//		}
	};


	//	These set up the display and tracking for all the elements that have help text
	jq("*[help_text]").bind('mouseover', function(e) {
		pos = jq(this).offset();
		jq("#helptexthover").css("top", pos.top + jq(this).height() + 5);
		jq("#helptexthover").css("left", pos.left +250);
        jq("#helptexthover").html(jq(this).attr('help_text')).show();
        //jq(this).addClass("hasFocus");
        //jq("#helptexthover").html(jq(this).attr('help_text')).show();
	});
	jq("*[help_text]").bind('mouseout', function() {
		if (jq(this).hasClass("hasFocus")) {
		//moving focus was causing problems with selects
		//			jq(this).focus();
		} else {
			jq('#helptexthover').hide();
		}
	});
	jq("*[help_text]").bind('mousemove', function(e) {
	    jq.fn.tooltipmove(e);
	});
	jq("*[help_text]").bind('focus', function(e) {
		//	position under the field we're providing help for
		pos = jq(this).offset();
		jq("#helptexthover").css("top", pos.top + jq(this).height() + 5);
		jq("#helptexthover").css("left", pos.left +250);
        jq("#helptexthover").html(jq(this).attr('help_text')).show();
        jq(this).addClass("hasFocus");
	});
	jq("*[help_text]").bind('blur', function(e) {
	    jq("#helptexthover").hide();
        jq(this).removeClass("hasFocus")
	});
});



