//<![CDATA[
/*	Polygrid – Grid Layout Function. © Graham Cross 2011		*/
var polygrid = {
	COLMARGIN : 50, 
	COLWIDTH : 210,
	BOTMARGIN : 30,
	dopolygrid : function(who, container, minnum, colww, btm, forceleft) {
		var count = 1;
		var bbtm = btm;
		var colw = colww;
		var colnum = minnum;
		var self = this;
		var pointer = 0;
		var colheights = new Array();
		var itemlist = new Array();
		for(i=0; i<=colnum; i++){
			colheights[i] = 0;
		}
		var itemwidths = new Array();
		if(forceleft){
			var columns = Math.max(colnum, parseInt(($('body').innerWidth() - forceleft) / (colw + this.COLMARGIN))); 
		}else{
			var columns = Math.max(colnum, parseInt(($('body').innerWidth() - parseInt($(container).css('left'))) / (colw + this.COLMARGIN))); 
		}
		
		if($('#thumbs').hasClass("optout")){
			var columns = $('#thumbs').attr('alt');
		}
		if($('#workpics').hasClass("optout")){
			var columns = $('#workpics').attr('alt');
		}
		$(who).each(function(index) {
			var lowest = Math.min.apply(null, colheights);
			var indexlowest = colheights.indexOf(lowest);
			// --
			if ($(this).outerWidth() > (colw + self.COLMARGIN)) {
				//
				var n=0;
				var nextlow = colheights[n]-colheights[(n+1)];
				if (nextlow){}else{nextlow=0};
				while (nextlow<0){
					n++;
					if(n>=colheights.length){
						n=0;
						break;
					};
					nextlow = colheights[n]-colheights[n+1];
				};
				var tempLeft = (n * (colw + self.COLMARGIN));
				indexlowest = n;
				//
			}else{
				var tempLeft = (indexlowest * (colw + self.COLMARGIN));
			}
			$(this).css('left', 0 + tempLeft + 'px');
			var tempTop = 0;
			if (colheights[indexlowest]) { tempTop = colheights[indexlowest];}
			$(this).css('top', tempTop + 'px');
			//
			if($(this).outerHeight() > 1){
				colheights[indexlowest] = tempTop + $(this).outerHeight() + bbtm;
			}
			pointer++;
			if (colheights[pointer] == null) {colheights[pointer] = 0;}
			// --
			if ($(this).outerWidth() > (colw + self.COLMARGIN)) {
				//
				var colsover = parseInt($(this).outerWidth() / (colw + self.COLMARGIN));
				//
				for (i=1;i<=colsover;i++){
					if($(this).outerHeight() > 1){
						colheights[indexlowest+i] = tempTop + $(this).outerHeight() + bbtm;
					}
					pointer++;
				}
				//alert(colheights);
				if (colheights[pointer] == null) {colheights[pointer] = 0;}
				//
			}
			if (pointer >= columns) {
				pointer = 0;
				colheights.pop();
			}
			itemwidths.push($(this).outerWidth());
		});
		var wrapperheight = Math.max.apply(null, colheights);
		$(container).css('height', wrapperheight);
		var wrapperwidth = Math.max((Math.max.apply(null, itemwidths)),((columns*(colw + self.COLMARGIN))-self.COLMARGIN));
		$(container).css('width', wrapperwidth);
	}
};
function menufadeup(){
	$('#menu').stop(true, true)
	$('#menu').css('opacity', 0).animate({'opacity': 1}, 750);
};
function menumotion(who){
	$(who).each(function(i) {
		$(this).css('opacity', 0);
		$(this).stop(true, true)
		to = parseInt($(this).css('top'));
		$(this).css('top', parseInt($(this).css('top')) + 210).delay(i*25).animate(
			{'top': to, 'opacity': 1},{queue: true, duration: 750, easing: 'easeOutCubic'}
		); 
	});	
}
function menumotion2(who){
	$(who).each(function(i) {
			$(this).find('li').css('opacity', 0);
			$(this).find('li').stop(true, true)
			to = parseInt($(this).find('li').css('top'));
			$(this).find('li').css('top', parseInt($(this).find('li').css('top')) + 105).delay(i*25).animate(
				{'top': to, 'opacity': 1},{queue: true, duration: 750, easing: 'easeOutCubic'}
			);
	});	
}
function areyouthere(id){
	var subject = document.getElementById(id);
	if (subject){
		return subject;
	}else{
		return null;
	}
};
$(window).resize(function(){				  
	if(areyouthere('thumbs')){polygrid.dopolygrid('#thumbs li', '#menu', 1, 210, 30);}
	if(areyouthere('workpics')){polygrid.dopolygrid('#workpics li', '#workdisplay', 1, 470, 14, 570);}
});
//$(document).ready(function () {
//jQuery(function($) {
(function($) {
		if(areyouthere('thumbs')){
			polygrid.dopolygrid('#thumbs li', '#menu', 1, 210, 30);
			$('#options').stop(true, true)
			$('#options').css('opacity', 0).animate({'opacity': 1}, 750);
		}
		if(areyouthere('workpics')){
			polygrid.dopolygrid('#workpics li', '#workdisplay', 1, 470, 14, 570);
			$('#work_section').stop(true, true)
			$('#work_section').css('opacity', 0).animate({'opacity': 1}, 750);
		}
		// depend on filter....
		if(filter_is_set_to == "motion" || filter_is_set_to == "video" ||
		   filter_is_set_to == "advert" || filter_is_set_to == "archive" ||
		   filter_is_set_to == "project"){
			if(areyouthere('thumbs')){menumotion2("."+filter_is_set_to);}
		}else{
			if(areyouthere('thumbs')){menumotion('#thumbs li');}
		}
		// HEADER HOVER EFX
		$(".hover").css({'opacity':'0'});
		$('a.hover_fx').hover(
			function() {
				$(this).find('.hover').stop().fadeTo(1, 1);
			},
			function() {
				$(this).find('.hover').stop().fadeTo(345, 0);
		});
		
		$('.scrollpage').smoothScroll({
			offset: -15,
			speed: 500
		});
		$('.scrolldown').smoothScroll({
			offset: -50, 
			speed: 500
		});
})(jQuery);
//
try{jQuery.ready();}catch(e){
		if(areyouthere('thumbs')){
			polygrid.dopolygrid('#thumbs li', '#menu', 1, 210, 30);
			$('#options').stop(true, true)
			$('#options').css('opacity', 0).animate({'opacity': 1}, 750);
		}
		if(areyouthere('workpics')){
			polygrid.dopolygrid('#workpics li', '#workdisplay', 1, 470, 14, 570);
			$('#work_section').stop(true, true)
			$('#work_section').css('opacity', 0).animate({'opacity': 1}, 750);
		}
		// depend on filter....
		if(filter_is_set_to == "motion" || filter_is_set_to == "video" ||
		   filter_is_set_to == "advert" || filter_is_set_to == "archive" ||
		   filter_is_set_to == "project"){
			if(areyouthere('thumbs')){menumotion2("."+filter_is_set_to);}
		}else{
			if(areyouthere('thumbs')){menumotion('#thumbs li');}
		}
		// HEADER HOVER EFX
		$(".hover").css({'opacity':'0'});
		$('a.hover_fx').hover(
			function() {
				$(this).find('.hover').stop().fadeTo(1, 1);
			},
			function() {
				$(this).find('.hover').stop().fadeTo(345, 0);
		});
		
		$('.scrollpage').smoothScroll({
			offset: -15,
			speed: 500
		});
		$('.scrolldown').smoothScroll({
			offset: -50, 
			speed: 500
		});
};

//}); 

//]]>
