/*  
	Description: Scripts for Cumberland theme
	Version: 1.0
	Author: Gallivan Media
	Author URI: http://gallivanmedia.com/
	Date modified: February 24, 2010
*/

// INITIALISE: Set up font replacement for homepage

var bensbk = { src: "/wp-content/themes/cumberland/fonts/bensbk__.swf" };
sIFR.useStyleCheck = true;
sIFR.activate(bensbk);
sIFR.replace(bensbk, {
	selector: "#title",
	css: [
		'.sIFR-root { font-size: 35px; font-weight: normal; color: #ffffff; }',
		'a { text-decoration: none; color: #ffffff; }',
		'a:hover { color: #ffffff; }'
	],
	wmode: 'transparent'
});

// FUNCTION: initialise(): initialises site functions

function initialise() {
	
	(function($){
		
		// Activate PNG fix for IE6
		$(document).pngFix();
		
		// Set dynamic tab widths based on number of main sections
		var tabCount = $("#tabs").children().length-1;
		var tabWidth = 1160/tabCount;
		$("#tabs div").css('width', tabWidth);
		$(".paneltab").css('width', tabWidth);

		// Check for smaller screen sizes, and adjust styles accordingly
		if (screen.width<1200) {
			tabWidth = 1002/tabCount;
			$("#wrapper").css({'width': '100%'});
			$(".ie-six #wrapper").css('width', '1006px');
			$("#header").css('width', '100%');
			$("#utilitynav").css('margin-right', '50px');
			$("#tabs").css('width', '1002px');
			$("#tabs div").css('width', tabWidth);
			$(".paneltab").css('width', tabWidth);
			$(".panel").css('width', '1002px');
			$("#menu").css('margin-left', '83px');
			$(".panelmenu").css('margin-left', '83px');
			$("#content").css({'width': '600px', 'padding-right': '40px' });
			$(".ie-six #content-background").css('width', '320px');
			$(".page-template-leadership-php h2").css('width', '604px');
			$("#leader-pad").css('width', '100%');
			$(".leader-content").css('width', '430px');
			$(".leader-content img").css({'width': '150px', 'height': '225px' });
			$("#leader-note").css({'width': '600px', 'padding-right': '0' });
			$("#returnnav").css('margin-left', '86px');
			$("#cipf").css('margin-left', '920px');
			$("#footer").css('width', '100%');
			$("#footertext").css('margin-left', '36px');
			$("#footernav").css('margin-left', '2px');
			$("#background img").css({'width': '1002px', 'height': '664px' });
			$("body.page-template-home-php #page").css({'padding-left': '496px', 'bottom': '110px'});
		}
		
		// Remove last pipe from secondary navigation
		$('#footernav ul li a:last').css({
			'border-right': '0'
		});
		
		// Allow homepage teaser to be dynamically positioned based on browser's window height
		if ($('#page #teaser').length != 0) {
			$(window).resize(function() {
				var windowHeight = $(window).height(0);
				$('#page').css('top', (windowHeight-280+"px"));
				if (windowHeight < 440) {
					$('#page').css('top', (160+"px"));
				}
			});
		}
		
		// Set scrolling for anchors for all other pages
		$('a[href*=#]:not([href=#])').click(function() {
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var heightAdjustment = 0;
					if ($('body').attr('class').split(' ').slice(-1) == "safari") {
						heightAdjustment = 10;
					} else if ($('body').attr('class').split(' ').slice(-1) == "gecko") {
						heightAdjustment = 20;
					} else if ($('body').attr('class').split(' ').slice(-1) == "ie") {
						heightAdjustment = 10;
					} else if ($('body').attr('class').split(' ').slice(-1) == "ie-seven") {
						heightAdjustment = 10;
					} else if ($('body').attr('class').split(' ').slice(-1) == "ie-six") {
						heightAdjustment = 10;
					} else if ($('body').attr('class').split(' ').slice(-1) == "opera") {
						heightAdjustment = -17;
					}
					var targetOffset = 0;
					if ($('body').attr('class').split(' ').slice(-1) == "ie-six") {
						targetOffset = $target.offset().top - 248 + $("body").scrollTop() + heightAdjustment;
					} else {
						targetOffset = $target.offset().top - 248 + heightAdjustment;
					}
					$('html,body').animate({scrollTop: targetOffset}, 750);
					return false;
				}
			}
		});
		
		// Set menu rollover and active states
		$('#menu h5 a').click(function() {
			$('#menu .selected').removeClass('selected');
			$(this).parent().attr('class', 'selected');
		});
		$('#menu h5 a').hover(function() {
			if (!$(this).parent().hasClass('selected')) { 
				$(this).parent().attr('class', 'highlight');
			}
		}, function () {
			if (!$(this).parent().hasClass('selected')) { 
				$(this).parent().attr('class', '');
			}
		});
		$('.panelmenu h5 a').hover(function() {
			$(this).parent().attr('class', 'highlight');
		}, function () {
			$(this).parent().attr('class', '');
		});
		
		// Initialise search box
		$('input#s').focus(function() {
			this.value = '';
		});
		$('input#s').blur(function() {
			this.value = 'SEARCH';
		});
		
		// If there's a leadernav, initialise the anchor highlighting
		if ($('#leadernav').length != 0) {
			$('#leadernav h5 a:first').addClass('selected');
			$('#leadernav h5 a').click(function() {
				$('#leadernav h5 a.selected').removeClass('selected');
				$(this).addClass('selected');
			});
		}
		
	})(jQuery);
	
}
	
function backgroundShow() {
	(function($){
		$('#background').show();
	})(jQuery);
}
