function checkAllParents(node){

	var checkBoxesNode = document.getElementById(node.getAttribute('alt'));
	var children = checkBoxesNode.childNodes;
	var lis = children[1].childNodes;

	var i = 0;
	for(i; i < lis.length; i++){
		if(lis[i] instanceof HTMLLIElement) {
			lis[i].childNodes[1].childNodes[1].setAttribute('checked','checked');
		}
	}
}

// The prototype holds the class methods,
// that are common for all objects.

function pic(options) {

	// All the properties of the options object
	// are copied to the current pic:

	$.extend(this, options);

	// Creating the markup of the pic,
	// and storing it in the elem property:

	this.elem = $('<a>', {
		className: 'pic white',
		href: this.href,
		css : {
			top : this.top,
			left : this.left,
			width: this.width,
			height: this.height,
			zIndex: 2
		}
	});

	var borderWidth = 5;

	// The bottom and right properties are not passed
	// as arguments, so we need to calculate them.

	this.bottom = this.top + this.height + 2 * borderWidth;
	this.right = this.left + this.width + 2 * borderWidth;

	this.image = $('<img>', {
		css:{
			left : -this.img.offsetLeft,
			top : -this.img.offsetTop
		}
	});

	var self = this;

	// Appending the image to the body so we can get
	// its dimensions. After we do this, we remove it
	// and append it to the markup stored in this.elem:

	this.image.hide().appendTo('body').load(
			function() {

				self.img.width = self.image.width();
				self.img.height = self.image.height();
				self.elem.append(self.image.show());

			}).attr('src', this.img.src);

}


pic.prototype = {
	open	: function(){
		if(this.opened){
			return false;
		}

		this.opened = true;

		// Firing our own expand method with a percentage of 100:
		this.expand(100);
	},
	close	: function(){
		if(!this.opened && !this.focused){
			return false;
		}

		this.opened = this.focused = false;
		this.expand(0);
	},
	focus	: function(){
		if(this.focused || this.opened){
			return false;
		}

		this.focused = true;

		//Expanding to 30%:
		this.expand(30);
	},

	near	: function(x,y){
		// Checking whether the passed x and y coordinates are near the current image:
		return (x > this.left-15 && x < this.right+15 && y > this.top-15 && y < this.bottom+15);
	},

	over	: function(x,y){
		// The same, but returning true only when directly above the image:
		return (x > this.left && x < this.right && y > this.top && y < this.bottom);
	},

	expand : function(animPercent){
		if(!this.animateObj){
			this.animateObj = {count:0};
		}

		// We use jQuery's animate method to
		// change the count property of the object:

		$(this.animateObj).stop().animate({
			count:animPercent
		},{
			duration: 250,

			// The step funciton is executed on every animation frame.
			// With jQuery's proxy we pass the "this" of the function:
			step:$.proxy(this.stepAnimation,this)
		});
	},

	stepAnimation : function(p,fx){

		// P holds the current value of the count property,
		// between 0 and 100. Below we are turning it into percentage.

		p = (p)/100;

		// Changing the size and position of the image holder:

		this.elem.css({
			width : (this.img.width - this.width)*p + this.width ,
			height : (this.img.height - this.height)*p + this.height,
			marginTop : -this.img.offsetTop*p,
			marginLeft: -this.img.offsetLeft*p,
			zIndex: 100*(p+2)
		});

		// Moving the image so it appears as if fixed:

		this.image.css({
			marginLeft : p*this.img.offsetLeft,
			marginTop : p*this.img.offsetTop
		});
	}
};

/**
 * Document ready
 */
$(document).ready(function(){
	$("#projectresults").tablesorter({
		sortList: [[1,0]],
		headers: {
            // assign the secound column (we start counting zero)
            0: { sorter: false },
            3: { sorter: false }
        }
	});

	var bigimg = '';
	var maxClicks = -4;
	var currentClick = 0;

	$('#pics').children().each(function(){
		maxClicks +=1;
	});

	var currentPhoto = 0;
	var photos = $('.photos .pho');
	$(photos[currentPhoto]).show();
	$('.description').html($('.photos .pho:eq('+currentPhoto+')').attr('alt'));
	$('.total').html($(photos).size());

	$('#left').click(function(){
		$('.photos .pho:eq('+currentPhoto+')').hide();
		currentPhoto--;
		if (currentPhoto < 0)
			currentPhoto = $(photos).size() -1;
		//console.log(currentPhoto);
		$('.photos .pho:eq('+currentPhoto+')').show();
		$('.counter').html(currentPhoto+1);
		$('.description').html($('.photos .pho:eq('+currentPhoto+')').attr('alt'));
		return false;
	});

	$('#right').click(function(){
		$('.photos .pho:eq('+currentPhoto+')').hide();
		currentPhoto++;
		if (currentPhoto > ($(photos).size() -1))
			currentPhoto = 0;
		//console.log(currentPhoto);
		$('.photos .pho:eq('+currentPhoto+')').show();
		$('.counter').html(currentPhoto +1);
		$('.description').html($('.photos .pho:eq('+currentPhoto+')').attr('alt'));
		return false;
	});


	$('.homegallery').click(function(){
		bigimg = $(this).attr('href');
		$('#gallerybig').fadeOut(400, function(){
			$(this).attr('src', bigimg).load(function(){
				$(this).fadeIn(400);
			})
		});
		return false;
	});

	$('#galleryleft').click(function(){
		if( currentClick > 0 ){
			currentClick -= 1;
			$('#pics').animate({left : '+=' + 113})
		}
		return false;
	});

	$('#galleryright').click(function(){
		if( currentClick < maxClicks){
			currentClick += 1;
			$('#pics').animate({left : '-=' + 113})
		}
		return false;
	});

	$('.placeholder').blur(function(){
		if($(this).val() == '')
			$(this).val($(this).attr('defaultValue'));
	});

	$('.placeholder').focus(function(){
		if($(this).val() == $(this).attr('defaultValue'))
			$(this).val('');
	});

	$('.pic').live('click', function(){
//		console.log($(this).attr('href'));
		if ($(this).attr('href') != "#") {
			$.fancybox({
				'href'  : $(this).attr('href'),
				title   : descriptions[$(this).attr('href')],
				'titlePosition' 		: 'inside',
				padding:    '0'
			});
			return false;
		}
	});


	// These are the descriptions used for the lightbox popup.
	var descriptions = {
		'/img/collage1b.jpg' : 'Residents of Warner were watching their school and town fade away. Learn how ' +
				'their Big Sky thinking created a program that’s revitalized their community. ' +
				'<a href="/be_inspired/warner_hockey_school/">View Case Study.</a>',
		'/img/collage11b.jpg' : '“New businesses are being developed and existing business are hiring new employees.” - Bruce Rutley, Director of CRI ' +
				'<br/><a href="/be_inspired/cri/">View Case Study.</a>',
		'/img/collage9b.jpg' : '"I came back to school because of the career skills and opportunities that mobile trailer provides." - Cody Benson, Student ' +
				'<br/><a href="/be_inspired/the_mobile_trades_foundation/">View Case Study.</a>',
		'/img/collage3b.jpg' : 'In Camrose, homelessness was growing in a quiet way. Learn how Big Sky thinking turned an assuming café into a comprehensive support system for Camrose\'s at-risk teens.' +
				'<br/><a href="/be_inspired/peer_connection/">Peer Connection</a>',
		'/img/collage5b.jpg' : 'First Nations communities were watching their elders pass on without the passion and spirit of their cultural traditions being transferred to the youth. Learn how Best Practices in Rural Alberta Project: Nexen Chair in Aboriginal Leadership is invigorating the youth with pride for their heritage. <a href="/be_inspired/nexen_chair_in_aboriginal_leadership/">View Case Study</a>.',
		'/img/collage10b.jpg' : 'STARS saw how rural practitioners- medical experts from doctors to EMTs- could save more lives from training in STARS\' critical care and transport medicine techniques. <a href="/be_inspired/stars//">View Case Study</a>.',
		
		
		
		

	};



	// Begins collage
	var page = $('#pics_container');

	// Creating the expanding images:
	var picArr = [
		new pic({
			top : 197, left : 21, width : 160, height : 200, href:'/img/collage1b.jpg',
			img : { src : '/img/collage1.jpg', offsetTop : 30, offsetLeft: 30 }
		})
		,
		new pic({
			top : 400, left : 220, width : 160, height : 120, href:'/img/collage5b.jpg',
			img : { src : '/img/collage5.jpg', offsetTop : 30, offsetLeft: 60 }
		})
		,
		new pic({
			top : 180, left : 440, width : 116, height : 160, href:'/img/collage11b.jpg',
			img : { src : '/img/collage11.jpg', offsetTop : 10, offsetLeft: 25}
		})
		,
		new pic({
			top : 5, left : 205, width : 160, height : 176, href:'/img/collage9b.jpg',
			img : { src : '/img/collage9.jpg', offsetTop : 5, offsetLeft: 25}
		})
		,
		new pic({
			top : 5, left : 5, width : 140, height : 176, href:'/img/collage3b.jpg',
			img : { src : '/img/collage3.jpg', offsetTop : 5, offsetLeft: 35}
		})
		,
		new pic({
			top : 380, left : 450, width : 120, height : 140, href:'/img/collage10b.jpg',
			img : { src : '/img/collage10.jpg', offsetTop : 10, offsetLeft: 100}
		})
//		,
//		new pic({
//			top : 20, left : 357, width : 140, height : 190, href:'/img/collage2b.jpg',
//			img : { src : '/img/collage2.jpg', offsetTop : 10, offsetLeft: 146}
//		})

		/* More pics here */

	];

	// Appending the images to the #page div:

	$.each(picArr, function() {
		page.append(this.elem);
	});

	// Setting up an event listener for the window.load event.
	// window.load is executed after all the images have been loaded.

	$(window).load(function() {

		page.mousemove(
				function(e) {

					var left = (e.pageX - page.offset().left),
							top = (e.pageY - page.offset().top),
							pic = null;

					// On each mouse movement, loop through the pics
					// and check whether the cursor is above any of them.

					for (var i = 0; i < picArr.length; i++) {
						pic = picArr[i];

						if (pic.near(left, top)) {

							if (pic.over(left, top)) {
								pic.open();
							}
							else pic.focus();
						}
						else pic.close();
					}

				}).mouseleave(function() {

			// When the mose leaves the #page div,
			// foce a close on all the images.

			for (var i = 0; i < picArr.length; i++) {
				picArr[i].close();
			}

		});
	});

	$("#nav>li").hover(
		function(){
			$('#nav ul').hide();
			$("ul", this).show();
		},
		function() { }
	);
	if (document.all) {
		$("#nav li").hoverClass ("sfHover");
	}



});


$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover(
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


