$(function() { 
//START THE JQUERY

//RANDOM HEADER IMAGE
  //Define the array
  //var randomImages = ['bg1','bg2','bg3'];
  //Randomise the array
    var numRotatingHeaders = 15;
    var rndNum = 1 + Math.floor(Math.random() * numRotatingHeaders);
  //Change the background image using above variable
  $("div#masthead").css({ background: "#FFF url(/images/header/bg" + rndNum + ".jpg)" });

//STRIPING ANYTHING
	//Putting the class striped on any container will add the class odd to every other nested element, but not anything nested in the nested nested nesty nest.
	$('.striped > *:nth-child(odd)').addClass('odd');
	
//LEFT NAV NESTED UL'S
	//Hide the nested ul
	$('div.left_col div.tab_content ul li ul').hide();
	//Show selected nav
	$('div.left_col div.tab_content ul li#nav'+getQuerystring("nav")+' ul').show();

	//Only apply this to items with nested ul's
	$('div.left_col div.tab_content ul li:has(ul) a:first').click(function(){;
		$(this).toggleClass('active').parent('li').siblings('li').children('a').removeClass('active');
		$(this).next('ul').toggle();
		$(this).parent('li').siblings('li').children('ul').hide();
		return false;
	});	

//GLOBAL TABBED CONTENT
	//Activate the first tab
	$('div.tabbed ul.tabbed_nav li:nth-child(1)').addClass('on');
	//Show the first content
	$('div.tabbed div.tab_content:first').show().siblings('div.tab_content').hide();

	//CLICKING A TAB
	//Create a variable based on the index and then show the correct content
	var li = $('div.tabbed ul.tabbed_nav li').click(function() {
    	var whichTab = (li.index(this));
		$('div.tab_content:eq(' + whichTab + ')').show().siblings('div.tab_content').hide();
		$(this).addClass('on').siblings('li').removeClass('on');
		return false;
	});

//END THE JQUERY
});

/*----------------------------------------------------------
Clear inputs on focus (finds all inputs with a title tag) 
------------------------------------------------------------*/

(function ($) {

$.fn.hint = function (blurClass) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        // get jQuery instance of 'this'
        var $$ = $(this); 

        // get it once since it won't change
        var title = $$.attr('title'); 

        // only apply logic if the element has the attribute
        if (title) { 

            // Note this is a one liner

            // on blur, set value to title attr if text is blank
            $$.blur(function () {
                if ($$.val() == '') {
                    $$.val(title).addClass(blurClass);
                }
            })

            // on focus, set value to blank if current value matches title attr
            .focus(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            })

            // clear the pre-defined text when form is submitted
            .parents('form:first').submit(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            }).end()

            // now change all inputs to title
            .blur();

            // counteracts the effect of Firefox's autocomplete stripping the blur effect
            if ($.browser.mozilla && !$$.attr('autocomplete')) {
                setTimeout(function () {
                    if ($$.val() == title) $$.val('');
                    $$.blur();
                }, 10);
            }
        }
    });
};

})(jQuery);

$(function(){ 
	// find all the input elements with title attributes
	$('input[title!=""]').hint();
	$('textarea[title!=""]').hint();
});
