$(document).ready(function() {

	// preload CSS hover images
	$('body').prepend('<div id="complexImageContainer"></div>');
	$("#complexImageContainer").jLoader({ simpleFileNames: false, imgContainer:"complexImageContainer", imgDir: "http://www.parentingcounts.org/Images/widgets/bg/", imgFormat: ".png" }, ["star-yellow-18x18"]);
	// load training preview rollovers
	$("body.training #complexImageContainer").jLoader({ simpleFileNames: false, imgContainer:"complexImageContainer", imgDir: "http://www.parentingcounts.org/Images/", imgFormat: ".png" }, ["parenting-counts-professional-training-program-child-pointing-preview-on-450x270","parenting-counts-professional-training-program-child-pointing-launch-preview-on-450x270"]);

	$('.nav li:last-child').addClass('last-child').css('background-image', 'none');

	$('a.mouseover').handleMouseovers();

	// obfuscate e-mail addresses
	if (jQuery.isFunction(jQuery.fn.obfuscateRel) || ($.fn.obfuscateRel)) {
		$('a.obfuscate').obfuscateRel();
	}

	// control orphans: wrap the last two words in a "nowrap" <span> tag
	if (jQuery.isFunction(jQuery.fn.control_orphans)) {
		$('h2 a, h2, .sidebar li a').control_orphans();
		//$('h2 a, h2').css('background-color', 'red').control_orphans();
	}

	// wrap content of ol > li in <span class="text">
	$("ol.training li").each(function (index) {
		$(this).addClass('outer').wrapInner("<span />").children(":first").addClass('text');
	});

	if (($.browser.msie) && ($.browser.version < 9)) {

		//$("#masthead h1, #masthead .deck").textShadow();

		if ($.browser.version > 6) {

			//$(".nav li:not(.thispage) a").textShadow();
			//$(".nav li.thispage a").textShadowRemove();

			$(".nav li:not(.thispage) a").hover(function(){
				//$(this).textShadowRemove();
				$(this).addClass("hover");
			},
			function(){
	 			var option = {
					x:      '1px',
					y:      0,
					radius: 0,
					color:  '#7a8c99'
				}
				//$(this).textShadow(option);
				$(this).removeClass("hover");
			});

			// IE7-only hacks
			if ($.browser.version < 8) {
				$('html>body#professionals #callout-star h2').addClass('ie7');
				//$('#professionals #callout-star h2').css('border', '1px solid green');
				//$('html>body#professionals #callout-star h2').css('margin-right', '100px !important');
				//$('html>body#professionals #callout-star h2').css('margin-left', '-25px !important');
			}

		} else {

			// requires jquery.pngFix.js
			if (jQuery.isFunction(jQuery.fn.pngFix)) {
				$(document).pngFix();
			}

			//$("ul.photos li").css('width', '');
		}

	}

	// initialise lightbox function, if present
	if (jQuery.isFunction(jQuery.fn.lightbox)) {
		$('.lightbox a, a.lightbox').lightbox();
		$('#imageData #bottomNavClose').append('<div class="after"></div>');
		//$('#imageData #bottomNavClose:after').addClass('after');
	}

	// initialise cycle function, if present
	if (jQuery.isFunction(jQuery.fn.cycle)) {
		$('.slideshow, .slideshow ul').cycle({
			fx: 'fade', cleartype: true, cleartypeNoBg: true
		});
	}

	// initialise popup function, if present
	if (jQuery.isFunction(jQuery.fn.popupWindow)) {
		$('a.popup').popupWindow({
			height:800,
			width:900,
			top:0,
			left:50,
			scrollbars:1
		});
	}

	// initialise swf function, if present
	if (jQuery.isFunction(jQuery.fn.flash)) {
		//alert('loaded');
		var $container = $('.swf');
		$container.each(function() {
			var $swffile	= $(this).find('a:first').attr('href');
			var $width		= $(this).css('width');
			var $height		= $(this).css('height');

			$(this).flash({
				swf: 		$swffile,
				width:	$width,	// Default is 320
				height:	$height,	// Default is 180
				params: {
					play: false,
					allowfullscreen: true,
					allowscriptaccess: 'always'
				},
				flashvars: {
					wmode: 'transparent',
					//plugins: 'googlytics-1',	// required for player to track GA events
					hasVersion: 8,	// optional and temporary
					autostart: false
				}
			});
		});
	}

	// initialise equalizeCols function, if present
	if (jQuery.isFunction(jQuery.fn.equalizeCols)) {
		$("ul.photos li").equalizeCols();
	}
		if (!(($.browser.msie) && ($.browser.version < 7))) {
			// initialise equalizeCols function, if present
			if (jQuery.isFunction(jQuery.fn.equalizeCols)) {
				$("ul.photos li span.img").equalizeCols();	// causes IE6 to make <li> full width
			}
		} else {
			$("ul.photos li").css('border', '1px solid #eef');
		}

	// sidebar pillars widget
	// text descriptions begin collapsed
	$('#pillar-description li').addClass('collapse');
	// whichever pillar graphic is active will cause its text counterpart to display
	$('.sidebar .about .pillars li').each(function() {
		var pillar	= $(this).attr('class');
		if ($(this).hasClass('this')) {
			pillar = pillar.replace(' this','');
			targetedpillar	= pillar;
			//alert('After load: class: ' + pillar);
			$('#pillar-description li.' + pillar).removeClass('collapse');
		}
		$(this).mouseover(function() {
			$('#pillar-description li').addClass('collapse');
			$('.sidebar .about .pillars li').removeClass('hover');
			pillar = pillar.replace(' this','');
			$('#pillar-description li.' + pillar).removeClass('collapse');
			//alert('after mouseover: ' + $(this).attr('class'));
		});
		$(this).mouseout(function() {
			$('#pillar-description li').addClass('collapse');
			$('.sidebar .about .pillars li').removeClass('hover');
			var targetedpillar	= $(this).attr('class');
			targetedpillar = targetedpillar.replace(' this','');
			$(this).not('.this').addClass('hover');
			//$(this).addClass('hover');
			$('#pillar-description li.' + targetedpillar).removeClass('collapse').addClass('this');
			//alert('after mouseout: ' + $(this).attr('class'));
		});
	});

	// show/hide references & attachments subheadings
	$('#references-heading').wrapInner('<a class="control closed" id="references-heading-a" />');
	$('#attachments-heading').wrapInner('<a class="control closed" id="attachments-heading-a" />');
	$('#references-list, #attachments-list').hide();
	$("#references-heading-a").toggle(
		function() {
			$('#references-list').slideDown("fast");
			$(this).removeClass('closed').addClass('open');
		},
		function() {
			$('#references-list').slideUp("fast");
			$(this).removeClass('open').addClass('closed');
		}
	);
	$("#attachments-heading-a").toggle(
		function() {
			$('#attachments-list').slideDown("fast");
			$(this).removeClass('closed').addClass('open');
		},
		function() {
			$('#attachments-list').slideUp("fast");
			$(this).removeClass('open').addClass('closed');
		}
	);


});

/* FUNCTIONS */
// handle mouseovers
// $selector will usually be an "a[href]" tag
// usage: 	$('.nav a').handleMouseovers();
// function handles mouseovers, mouseouts, preloading of nested <img>s
(function($) {
	$.fn.handleMouseovers = function() {

		// alert('ok');
		$offsrc	= '-off-';
		$onsrc	= '-on-';
/*
		$offsrc	= '_off';
		$onsrc	= '_on';
 */
		// preload images
		this.each(function() {
			// Set the original src
			rollsrc = $(this).children("img").attr("src");
			rollON = rollsrc.replace($offsrc,$onsrc);
			newImg = new Image(); 				// create new image obj
			$(newImg).attr("src", rollON);	// set new obj's src
		});

		this.mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcON = imgsrc.replace($offsrc,$onsrc);
				$(this).children("img").attr("src", imgsrcON);
			}
		});

		this.mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				$(this).children("img").attr("src", imgsrc);
			}
		});
		return this;
	};
})(jQuery);

(function ($) {
    $.fn.vAlign = function(container) {
        return this.each(function(i){
	   if(container == null) {
	      container = 'div';
	   }
	   var paddingPx = 10; //change this value as you need (It is the extra height for the parent element)
	   $(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
	   var el = $(this).children(container + ":first");
	   var elh = $(el).height(); //new element height
	   var ph = $(this).height(); //parent height
	   if(elh > ph) { //if new element height is larger apply this to parent
	       $(this).height(elh + paddingPx);
	       ph = elh + paddingPx;
	   }
	   var nh = (ph - elh) / 2; //new margin to apply
		//$(el).css('margin-top', nh);
	   $(el).css('margin-bottom', nh);
        });
     };
})(jQuery);

/*
(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('padding-top', mh);
	//$(this).css('margin-top', mh);
	});
};
})(jQuery); */
