var $j = jQuery.noConflict();

$j.Isotope.prototype._getCenteredMasonryColumns = function(){
	this.width = this.element.width();
	var parentWidth = this.element.parent().width();
	var colW = this.options.masonry && this.options.masonry.columnWidth || this.$filteredAtoms.outerWidth(true) || parentWidth;
	var cols = Math.floor(parentWidth/colW);
	cols = Math.max(cols, 1);
	
	this.masonry.cols = cols;
	this.masonry.columnWidth = colW;
	
	console.log("change");
	console.log(this.width);
	$j('#header').width(cols*colW);
};

$j.Isotope.prototype._masonryReset = function(){
	this.masonry = {};
	this._getCenteredMasonryColumns();
	var i = this.masonry.cols;
	this.masonry.colYs = [];
	while(i--){
		this.masonry.colYs.push(0);
	}
	
};

$j.Isotope.prototype._masonryResizeChanged = function(){
	var prevColCount = this.masonry.cols;
	this._getCenteredMasonryColumns();
	return(this.masonry.cols !== prevColCount);
};

$j.Isotope.prototype._masonryGetContainerSize = function(){
	unusedCols = 0;
	i = this.masonry.cols;
	while(i--){
		if(this.masonry.colYs[i] !== 0){
			break;
		}
		unusedCols++
	}
	return {
		height: Math.max.apply(Math, this.masonry.colYs),
		width: (this.masonry.cols - unusedCols) * this.masonry.columnWidth
	};
};

$j(document).ready(function(){
	$j('#wrapper-main').isotope({ 
		itemSelector: 'article',
		masonry:{
			columnWidth: 310
		}
	});
	
	$j('#wrapper-singlepost').isotope({
		masonry: {
			columnWidth: 310
		}
	});
	
	$j('#wrapper-page').isotope({
		masonry: {
			columnWidth: 310
		}
	});
	
	$j('article.front-page, article.axle-gallery').live({
		mouseenter: function() {
			$j(this).addClass('article-hover');
		},
		mouseleave: function(){
			$j(this).removeClass('article-hover');
		}
	});
	
	$j('a:has(img.attachment-thumbnail), .axle-single-post-gallery').fancybox({
		'centerOnScroll': 'true',
	});
	
	$j('a#cform').fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	//Navigation
	$j('#menu-navigation li').hover(function(){
		$j('.sub-menu', this).slideDown(100);
	},function(){
		$j('.sub-menu', this).slideUp(100);
	});

	var open = false;
	$j('#footer-overlay-toggle').click(function(){
		if(open === false){
			$j('#footer-overlay-content').show().animate({ height: '220px' }, { duration: 1000, easing: 'easeOutBounce'});
			$j('#footer-credits').fadeIn(1000);
			$j('#footer-overlay-toggle').text('Close');
			open = true;
		} else {
			$j('#footer-credits').fadeOut();
			$j('#footer-overlay-content').animate({ height: '0px' }, { duration: 1000, easing: 'easeOutBounce'}).hide('fast');
			$j('#footer-overlay-toggle').text('Expand');
			open = false;
		}
	});
	
	/*=======================================================
		Infinite Scroll
	=======================================================*/
	
	$j('#wrapper-main').infinitescroll({
		contentSelector: 	'#wrapper-main',
		donetext: 			'That\'s it! Thanks for stopping by',
		itemSelector: 		'#wrapper-main article',
		loadingImg:			'../images/loading.gif',
		loadingText: 		'Loading new posts',
		navSelector: 		'#pagination',
		nextSelector: 		'#pagination a:first',
		}, function( newElements ){
			window.setTimeout(function(){
				$j('#wrapper-main').isotope("appended", $j(newElements));
			}, 1);
	});
	
	
	
});

