$(function() {
	
	sidebarNavigationEffects();
	
	scrollEffects();

	initialisePageActionsAndComponents();
	
	standardPageValidations();
});

function sidebarNavigationEffects() {
	if($('div.secondaryNavigation').length > 0) {
		$items = $('div.secondaryNavigation li');
		$.each($items, function() {
			$(this).mouseover(function() {
				$(this).addClass('hover');
			});
			$(this).mouseout(function() {
				$(this).removeClass('hover');
			});
		});
	}
	if($('div.quickLinks').length > 0) {
		$items = $('div.quickLinks li');
		$.each($items, function() {
			$(this).mouseover(function() {
				$(this).addClass('hover');
			});
			$(this).mouseout(function() {
				$(this).removeClass('hover');
			});
		});
	}
}

function scrollEffects() {
	$('div#scroller a').click(function() {
		$('div.headerWrapper').scrollTo(1000);
		return false;
	});
	$(window).scroll(function() {
		var $offset = $(window).scrollTop();
		if($offset > 350) {
			$('div#scroller').fadeIn();
		}
		else {
			$('div#scroller').fadeOut();
		}
	});
	$('a.index_scroll').click(function() {
		$linkId = $(this).attr('href').replace('#', '');
		$(this).scrollTo(1000, null, $linkId);
		return false;
	});
}

function initialisePageActionsAndComponents() {
	
	// Logo
	$('div.headerLogo').click(function() {
		window.location.href = '/';
	});
	
	// Page actions
	$('img#text-increase').click(function() {
		if($('div#main-content').hasClass('normalTextSize')) {
			$('div#main-content').addClass('largeTextSize');
		}
	});
	$('img#text-decrease').click(function() {
		if($('div#main-content').hasClass('largeTextSize')) {
			$('div#main-content').removeClass('largeTextSize');
		}
	});
	
	if($('a#content-editor-toggle').length > 0) {
		$('a#content-editor-toggle').click(function() {
			$('div#content-editor').slideToggle(1000);
			return false;
		});
	}
	
	// Home quicklinks
	if($('div.homeBox').length > 0) {
		($('div.homeBox').each(function() {
			$(this).click(function() {
				$href = $(this).find('span.link a').attr('href');
				window.location.href = $href;
			});
		}));
	}
	
	// Lightbox
	$("a.lightbox").lightBox();
	
	// Windows
	$("a.newWindow").click(function() {
		window.open($(this).attr('href'));
		return false;
	});
}

function standardPageValidations() {
	
	// Register interest
	if($('select#InterestedPersonPreferredTime').length > 0) {
		$('select#InterestedPersonPreferredTime').change(function() {
			if($(this).val() == 'Other') {
				$('input#other-contact-time').show();
			} else {
				$('input#other-contact-time').hide();
			}
		});
	}
	if($('select#InterestedPersonTitle').length > 0) {
		$('select#InterestedPersonTitle').change(function() {
			if($(this).val() == 'Other') {
				$('input#other-title').show();
			} else {
				$('input#other-title').hide();
			}
		});
	}
	
	// Job applications
	if($('select#JobApplicantTitle').length > 0) {
		$('select#JobApplicantTitle').change(function() {
			if($(this).val() == 'Other') {
				$('input#other-title').show();
			} else {
				$('input#other-title').hide();
			}
		});
	}
	if($('select#JobApplicantAdvertisementId').length > 0) {
		$('select#JobApplicantAdvertisementId').change(function() {
			if($(this).val() == '9') {
				$('tr#other-hear').show();
			} else {
				$('tr#other-hear').hide();
			}
		});
	}
	
	if($('input#applicant_submit').length > 0) {
		$('input#applicant_submit').click(function() {
			$(this).attr('disabled', 'disabled');
		})
	}
}