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/5_large.jpg' : ''

	};



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

	// Creating the expanding images:
	var picArr = [
	
		new pic({
			top : 1, left : 187, width : 180, height : 180, href:'img/1_large.jpg',
			img : { src : 'img/1b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 367, width : 180, height : 180, href:'img/2_large.jpg',
			img : { src : 'img/2b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 547, width : 180, height : 180, href:'img/9_large.jpg',
			img : { src : 'img/9b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 0, top : 355, width : 180, height : 180, href:'img/5_large.jpg',
			img : { src : 'img/5b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 180, top : 355, width : 180, height : 180, href:'img/6_large.jpg',
			img : { src : 'img/6b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 540, top : 355, width : 180, height : 180, href:'img/7_large.jpg',
			img : { src : 'img/7b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 720, top : 355, width : 180, height : 180, href:'img/8_large.jpg',
			img : { src : 'img/8b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 900, top : 355, width : 180, height : 180, href:'img/4_large.jpg',
			img : { src : 'img/4b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 0, top : 535, width : 180, height : 180, href:'img/10_large.jpg',
			img : { src : 'img/10b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		
		new pic({
			top : 1, left : 360, top : 535, width : 180, height : 180, href:'img/11_large.jpg',
			img : { src : 'img/11b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 540, top : 535, width : 180, height : 180, href:'img/12_large.jpg',
			img : { src : 'img/12b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 187, top : -368, width : 180, height : 180, href:'img/14_large.jpg',
			img : { src : 'img/14b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 367, top : -368, width : 180, height : 180, href:'img/15_large.jpg',
			img : { src : 'img/15b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 1, left : 727, top : -368, width : 180, height : 180, href:'img/3_large.jpg',
			img : { src : 'img/3b.jpg', offsetTop : 10, offsetLeft: 25 }
		})
		,
		new pic({
			top : 0, left : 899, width : 180, height : 180, href:'img/16_large.jpg',
			img : { src : 'img/16b.jpg', offsetTop : 10, offsetLeft: 25}
		})
		,
		new pic({
			top : 1, left : 901, top : 534, width : 180, height : 180, href:'img/17_large.jpg',
			img : { src : 'img/17b.jpg', offsetTop : 10, offsetLeft: 25 }
		})

		/* 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); }
		);
	});
};


