jQuery.fn.disableSelection = function() {
    jQuery(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function() { 
               this.onselectstart = function() { return false; };
            });
};


jQuery(document).ready(function($){
	
	// disable text selection in header (looks nicer when clicking buttons)
	// NB: this only works for IE. CSS 'user-select:none' used for other browsers
	$("#header",".news-nav-buttons").disableSelection();
	
	// kill title attribs in top nav (annoying)
	$("#header a").removeAttr('title');
	// boost width of first item in main nav (hack)
	$("a:first", "#main-nav").css("width","77px");

	// remove padding from last item in main menu
	$("#main-nav > ul > li:last").addClass("last");
	
	// add "hasChildren" styles to main menu
	$("li:has(ul)","#main-nav").addClass("has-children");
	$("li.has-children > a", "#main-nav").wrapInner("<span/>"); 
	
	// add "hasChildren" styles to submenu
	$("li:has(ul)","#sidebar .widget_pages").addClass("has-children");
	$("li.has-children > a", "#sidebar .widget_pages").wrapInner("<span/>"); 
	
	// tag first and last LI in lists
	$("li:first", "ul").addClass("first");
	$("li:last", "ul").addClass("last");
	
	////////////////////////////////////////////////////
	// add show/hide toggle to content region
	$("#container").append("<div id='showhidetoggle'>HIDE</div>");
	$("#showhidetoggle").click(function(){
		var t = $(this);
		$("#content").slideToggle(500, function(){
			$(t).text( $("#content").is(":visible") ? "HIDE" : "SHOW"  );
		});
		
	});
	
	//////////////////////////////////////////////////////////
	// fix legends in cforms (chrome) -- not aligned properly
	$(".cform legend").wrapInner("<div style='text-align:right'/>");
	
	
	////////////////////////////////////////////////////
	// ngg gallery customisation (NextGen Gallery)
	$(".ngg-gallery").each(function(n,gal){
		// init index
		$(gal).data('index',0);
		// get num images
		var numImages = $(".ngg-gallery-thumbnail").size();
		// on thumb click update preview image
		$(".ngg-gallery-thumbnail > a", gal).click(function(){
			$(".ngg-gallerypreview > img", gal).attr('src', $(this).attr('href'));			
			$(gal).data('index', $(this).parent().parent().prevAll().length);			
			return false;
		});
		// next/back
		$(".ngg-gallerynav > a",gal).click(function(){
			var index = $(gal).data('index');
			if($(this).is('.ngg_prev')) index--;
			else						index++;
			// boundary check
			if(index < 0) index = numImages-1;
			else		  index = index % numImages;
			// show the image
			var src = $(".ngg-gallery-thumbnail:eq(" + index + ") > a", gal).attr('href');
			$(".ngg-gallerypreview > img", gal).attr('src', src);	
			// update the index
			$(gal).data('index', index);
		});
		
	});
	
	////////////////////////////////////////////////////
	// resize bg image in proportion
	var bgImage = $("#bg > img");
	var imgRatio = bgImage.width()/bgImage.height();
	$(bgImage).load(function(){
		imgRatio = bgImage.width()/bgImage.height();
		resizeBG();
	});
	var resizeBG = function(){
		var winH = $(window).height();
		var winW = $(window).width();
		//var imgRatio = 1200/750;
		var winRatio = winW/winH;
		if(imgRatio > winRatio) {
			$("img", "#bg").css('height', winH + 'px');
			$("img", "#bg").css('width', 'auto');
		}else{
			$("img", "#bg").css('width', winW + 'px');
			$("img", "#bg").css('height', 'auto');
		}	
	}

	////////////////////////////////////////////////////
	// browser resize handler
	$(window).resize(function(){
		var minHeight 		= 300; //$("body").is(".home") ? 0 : 300;
		var mainTop 		= $("#main").position().top;
		var mainHeight 		= $("#main > .inner").outerHeight();
		var footerHeight 	= $("#footer").height();
		var availableHeight	= Math.max(minHeight, $(window).height() - mainTop - footerHeight);
		
		$("#main").height( Math.max(availableHeight, mainHeight) );
		
		resizeBG();
	});
	
	////////////////////////////////////////////////////
	// trigger a resize
	if(navigator.appVersion.indexOf("AppleWebKit") >= 0) {
		// derrr.... chrome/safari screws up the resize if you don't use a short delay
		setTimeout(function(){ $(window).resize(); }, 50);
	}else{
		$(window).resize();
	}
	
	// resize every 2 seconds - hack to make the thing cope with CFORMS resizing on submit
	setInterval(function(){ $(window).resize(); }, 2000);
});


