/* @author thomas.bran */

// if the site is opened within a frameset, pop it out
function breakOut(){
	if (self != top) top.location.replace(self.location);
}
breakOut();

/* custom JQuery ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

// tootip rollover for forms
this.tooltip = function(){				
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){		
						  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

$(document).ready(function(){
// allow number-only input on forms
	$("input.numberOnly").keypress(function (e) {
		var nums = "0123456789";
		var keycode = String.fromCharCode(e.which);
		// don't block L/R, Delete, Backspace, or '+'
		if ((e.which==null) || (e.which==0) || (e.which==8) || (e.which==9) || (e.which==13) || (e.which==27) || (e.which>=37 && e.which<=40) || e.which==43 || e.which==46) {
		   return true;
		// limit to numbers
		} else if (nums.indexOf(keycode) > -1) {
			return true;
		} else {
			return false;
		}
	})
// make all links with the class 'external' open in a new window
	$("a.external").attr({"target": "_blank"});
	
// use title attribute for value in form field
	var input = $(".useTitleForValue");
	input.attr({"value": input.attr("title")})
	input.addClass("inactive")
	
	input.focus(function(){
		if ($(this).val() == input.attr("title")) {
			input.removeAttr("value");
			input.removeClass("inactive");
		}
	})
	input.blur(function(){
		if ($(this).val() == "") {
			input.attr({"value": $(".useTitleForValue").attr("title")});
			input.addClass("inactive");
		}
	})
	
	// focus/blur states for form elements
	$("input[@type='text'], input[@type='password'], textarea").focus(function(){$(this).css("background-color", "#fff");})
	$("input[@type='text'], input[@type='password'], textarea").blur(function(){$(this).css("background-color", "#f6f4f9");})
	$("input[@disabled]").css({"background-color": "#eeeeee", "color": "#555555"});
	
	
// add the 'print page' link to the page
	$("#pageActions").append('<li class="print"><a href="javascript:window.print();" title="Print this page">Print page<\/a><\/li>');
	
// add the toggle to the 'show more' links on the search results
	$(".more").hide();
	$(".more").after('<p class="expand"><a href="#" title="Link title">Show more</a></p>');
	$("p.expand a").click(function(){
		$(this).parent().siblings("div.more").slideToggle("fast")
		$(this).parent().toggleClass("open")
		return false;
	}).toggle(function(){$(this).text("Show less");},function(){$(this).text("Show more");}).parent().show();

// piano-key-colour (i.e. alternate) the rows of the tables
	$("table:not('.noRowColours') tr:odd(), ul.pianokeys li:odd()").addClass("odd");
	
// tootip rollover for forms
	//tooltip();	
})

/* custom JS functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*function toggleContrast(){
	$('body').toggleClass('hc')
}
*/
function updateRelatedFormFields(){
// var options is loaded in an external file - see formoptions.js
	var option = $("#selectOne").val();
	
	if (option != "_") {
		$("#selectTwo").children().remove();
		for(i=0;i<options[option].length;i++){
			$("<option value='"+i+"'>"+options[option][i]+"<\/option>").appendTo("#selectTwo");
			//$("#selectTwo").css("background-color", "#ffffaa").end().animate({backgroundColor: "white"},2000);
		}
	}
	return false;
}