var default_popup_size = 500;
var small_popup_size = 250;
var restricted_textarea_length = 1500;
var standard_textarea_length = 2000;
var disable_tabs_javascript = false;
var modal_window = null;
$(document).ready(function() {

	$('.javascript_only').show();
	SetTabsBehaviour();
	SetResizerBehaviour();
	SetPopupBehaviour();
	SetModalPopupBehaviour();
	SetPrintAnchors();
	InitialisePopupNavigation();
	
});

function SetPrintAnchors() {
	$('#print').hover(function() {
		$(this).css('color', '#000').css('text-decoration', 'none');
	}, function() {
		$(this).css('color', '#298BBC').css('text-decoration', 'underline');
	});
	$('#print').click(function() {
		window.print();
	});
}

function SetTabsBehaviour() {

	disable_tabs_javascript = (document.getElementsByName('disable_tabs_javascript').length > 0 ? (document.getElementsByName('disable_tabs_javascript')[0].value == 'yes') : false);

	if (!disable_tabs_javascript) {
	
		$('div.tabbed_content').each(function(i) {
			if (i > 0) $(this).hide();
		});

		$('#tabs li').each(function() {
			$(this).css('cursor', 'pointer');
			$(this).click(function() {
				$('#tabs').find('a').attr('class', 'inactive');
				var search_for = $(this).find('a').attr('class', 'active').attr('id') + '_content';
				$('div.tabbed_content').each(function(i) {
					if ($(this).is(':visible')) {
						$(this).hide();
						$('#' + search_for).show();
					}
				});
			});
		});
		
	}
}

function ChangeToTrainingTab() {

    $('#tab1').attr('class', 'inactive');
    $('#tab5').attr('class', 'active');
    $('#tab1_content').hide(); 
    $('#tab5_content').show();

}

function SetResizerBehaviour() {
	$('#resize_normal').click(function() {
		$('body').css('font-size', '89%');
	});
	$('#resize_big').click(function() {
		$('body').css('font-size', '110%');
	});
	$('#resize_biggest').click(function() {
		$('body').css('font-size', '130%');
	});
}

function SetPopupBehaviour() {
	$('a.popup').click(function() {
	 	var new_window = window.open($(this).attr('href'), 'help', 'status=0,toolbar=0,location=0,resizable=1,scrollbars=1,width=' + default_popup_size + ',height=' + default_popup_size);
		new_window.focus();
		return false;
	});
}
function SetModalPopupBehaviour() {
	$('a.modalpopup').click(function() {
	   
		//modal_window = window.showModalDialog($(this).attr('href'), 'help', 'status=0,toolbar=0,location=0,resizable=1,scrollbars=1,width=' + default_popup_size + ',height=' + default_popup_size);
		modal_window = window.open($(this).attr('href'), 'help', 'status=0,toolbar=0,location=0,resizable=0,scrollbars=0,width=450,height=300');
		//modal_window.focus();
		return false;
	});
}
function SetCharLimitEventsForTextArea(textarea, max_length) {
	textarea.keyup(function() {
		SetCharLimitForTextArea($(this), max_length);
	});
	textarea.blur(function() {
		SetCharLimitForTextArea($(this), max_length);
	});
}

function SetCharLimitForTextArea(textarea, max_length) {
	if (textarea.val().length > max_length) {
		textarea.val(textarea.val().substring(0, max_length));
	}
}

function IsNumeric(s, allow_decimal_point)
{
	var i;
	var is_valid = true;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        is_valid = ((allow_decimal_point && c == '.') || (c >= 0 && c <= 9))
        if (!is_valid) return false;
    }
    // All characters are numbers.
    return true;
}

function SetOpacity(ele, amount) {
	ele.css({'filter':'alpha(opacity=' + amount + ')','-moz-opacity':(amount / 100),'opacity':(amount / 100)});
}


/*
This function intitialises the popup navigation. Each navigation item is an HTML list element (LI) containing a
SPAN element for the heading and a DIV element containing the navigation links as anchor (A) elements. These DIVs
are hidden by default and the LI element itself is given a jQuery hover event handler that reveals the DIV on hover 
over and hides it again on hover out.
*/
function InitialisePopupNavigation() {
    if (document.getElementById('global_navigation') != null) {
        // Hide all DIVs by default
        $('#global_navigation div').hide();
        // Add a hover event handler to each list element in the navigation
        $('#global_navigation li').hover(function() {
            if ($(this).find('div').length > 0) {
                // A DIV has been found so reveal it and change the style of the heading to show it's active
                $(this).find('div').show();
                $(this).find('span:first').css('cursor', 'default').css('font-weight', 'bold');
            }
        }, function() {
            if ($(this).find('div').length > 0) {
                // Hide the DIV and revert the heading back to its default style
                $(this).find('div').hide();
                $(this).find('span:first').css('font-weight', 'normal');
            }
        });
    }
}
