$(document).ready(function(){

	// Delcare JS enabled
	$('body')
		.addClass('js')
		.removeClass('js-loading')
		.removeClass('js-disabled');

	// External Links
	$("a[href^=http]").each(
		function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).attr('target', '_blank');
			}
	
		}
	);
	$("#nav-print a").click(function(){
		window.open($(this).attr("href"));
		return false;
	});
	
	// Content External Links
	$("#content-main a[href^=http], #content-sub a[href^=http]").each(
		function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).attr('target', '_blank')
					.attr('class', 'external-link')
					.attr('title', 'External link (will open in new window)');
			}
	
		}
	);
	
	// Content External Links
	$("#content-sub #arrow-icon-header a[href^=http]").each(
		function(){
				$(this).attr('target', '_blank')
					.attr('class', 'external-link')
					.attr('title', 'External link (will open in new window)');
	
		}
	);
	
	// Multiple Page Heading Fix
	if ($("h1#multiple-page-heading").size()) {
		$("h1#multiple-page-heading").prev("h1").remove();
	}
	

	/*
	 * Form Labels
	 * 
	 * Sexy labels in inputs for search 
	 *
	 */
	var searchBlock = $("fieldset#search");
	$(searchBlock).find("input[type=text]").each( function() {
		var input = $(this);
		var labelObj = $("label[for=" + input.attr("id") + "]");
		var label = $(labelObj).text();
		
		// Add and Broadcast labeled input
		input
			.addClass("labeled")
			.val(label);
			
		// Hide label element
		labelObj.hide();
			
		// Remove label if no input has been made
		input.bind("focus", function() {
			if ($(this).val() == label) {
				$(this).val("").removeClass("labeled");
			}
		});
		
		// Add label again if no input added
		input.bind("blur", function() {
			if ($(this).val().length == 0) {
				$(this).val(label).addClass("labeled");
			}
		});
	});
	
	// Remove labels on submit
	inputEls = $(searchBlock).find("input[type=text]");
	$(searchBlock).find("input[type=text]").parents("form").bind("submit", function() {
		inputEls.each( function() {
			if ($(this).val() == $("label[for=" + $(this).attr("id") + "]").text()) {
				$(this).val("");
			}
		});
		return true;
	});
	
});


//ADD ALL OPTIONS TO SELECT BOXES
$('#queries_topic_query').prepend('<option value="all">All</option>');
$('#queries_type_query').prepend('<option value="all">All</option>');
$('#queries_region_query').prepend('<option value="all">All</option>');

//SELECT ALL FUNCTION
function selectAll(selectBox,selectAll) {
    // have we been passed an ID
    if (typeof selectBox == "string") {
        selectBox = document.getElementById(selectBox);
    }

    // is the select box a multiple select box?
    if (selectBox.type == "select-multiple") {
        for (var i = 0; i < selectBox.options.length; i++) {
            selectBox.options[i].selected = selectAll;
        }
    }
}

//SELECT BOX FUNCTIONS
function topic_selectall()
{
var test = $('#queries_topic_query').val();
if (test=="all")
    selectAll('queries_topic_query',true);
}

function type_selectall()
{
var test = $('#queries_type_query').val();
if (test=="all")
    selectAll('queries_type_query',true);
}

function region_selectall()
{
var test = $('#queries_region_query').val();
if (test=="all")
    selectAll('queries_region_query',true);
}


function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

