$(document).ready(function(){
	$('.idirContent').css({'visibility': 'visible'}).hide();
	$('.synop').css({'visibility': 'visible'}).hide();
	var idirPosition = 0;
	var offset = 0;
	var notAnimating = true;
	var nHeight = 0;
	
	$('.opener').attr('href','#');
	
	$('.idirBlock').mouseenter(function(){
		if(!($(this).hasClass('open'))){
			if (notAnimating) {
				$(this).css({'z-index': '2000', 'border-right-style': 'solid', 'border-right-width': '10px'}).height(255).width(270);
				var newLink = '#' +  $(this).attr('id');
				$('.opener').attr('href', newLink);
				$(this).children('.synop').show();
				var colorClass = $(this).find('.catlink').attr('class');
				var colorClass = '.' + colorClass.substr(8);
				var colorBorder = $(this).css('color');
				$(this).find('.catlink').css({'color': colorBorder});
				$('#content').children(colorClass).find('.openerWrap').css({'border-right-style': 'solid', 'border-right-width': '10px', 'border-color': colorBorder});
				$('#content').children(colorClass).find('.attachment-post-thumbnail').css({'opacity': '0.8'});
				$(this).find('.openerWrap').css({'border-right-style': 'none'});
				$(this).find('.attachment-post-thumbnail').css({'opacity':'1'});
			}
		}
	});
	
	$('.idirBlock').mouseleave(function(){
		if(!($(this).hasClass('open'))){
			if (notAnimating) {
				$(this).css({'z-index': '0', 'border': 'none'}).height(185).width(220);
				$(this).children('.synop').hide();
				var colorClass = $(this).find('.catlink').attr('class');
				var colorClass = '.' + colorClass.substr(8);
				$(this).find('.catlink').css({'color': '#000'});
				$('#content').children(colorClass).find('.openerWrap').css({'border-right-style': 'none'});
				$(this).find('.attachment-post-thumbnail').css({'opacity': '0.6'});
				$('#content').children(colorClass).find('.attachment-post-thumbnail').css({'opacity': '0.6'});
			}
		}
	});
	
	$('.opener').click(function(){
		if($(this).parent().parent().hasClass('open')){
			notAnimating = false;
			idirClose($(this).parent().parent());
		} else {
			notAnimating = false;
			idirClose($('.open'));		
			$(this).parent().parent().height(185);
			$(this).parent().parent().css({'z-index': '0', 'border': 'none'})
			$(this).parent().parent().children('.synop').hide();
			offset = ($(this).parent().parent().attr('id'));
			offset = offset.slice(5);
			$(this).parent().parent().children('.idirContent').load('/?page_id=6&offset='+offset, function(){
				idirOpen($(this).parent());
			});
		}
	});
	
	function prepareSlider(workOn) {
		workOn.children('.entry').children('.cats').after('<div class="slideshowHolder"></div>');
		workOn.find('img').filter(function(index) {
  			return $(this).attr('width') == '680';
		}).appendTo(workOn.children('.entry').children('.slideshowHolder'));
		var $kids = workOn.children('.entry').children('.slideshowHolder').children();
     	var len = $kids.length;
		return len;
	}
	
	function buildSlider() {
		$('.open').find('.idirCloser').click(function(){
			notAnimating = false;
			idirClose($(this).parent().parent().parent());	  
		});
		$('.open').find('.slideshowHolder').nivoSlider({
			effect:'fold', //Specify sets like: 'fold,fade,sliceDown'
			slices:1,
			animSpeed:500,
			pauseTime:8000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:true, //Next & Prev
			directionNavHide:true, //Only show on hover
			controlNav:true, //1,2,3...
			controlNavThumbs:false, //Use thumbnails for Control Nav
			keyboardNav:false, //Use left & right arrows
			pauseOnHover:true, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:1, //Universal caption opacity
		});
		var theHeight = $('.open').find('.slideshowHolder').height();
		$('.open').find('.nivo-directionNav a').height(theHeight);
		return false
	}
	
	function idirOpen(toOpen) {
		toOpen.addClass('open');
		toOpen.children('.thumb').hide('fast');
		var removeHeight = prepareSlider(toOpen.children('.idirContent'));
		var nHeight = toOpen.children('.idirContent').children('.entry').children('.slideshowHolder img:first').attr('height');
		removeHeight = (removeHeight - 1)*nHeight;
		hOfObject = (toOpen.children('.idirContent').outerHeight(true));
		if (removeHeight > 0) {
			hToOpen = (((Math.ceil((hOfObject - removeHeight )/ 185)))*205)-20;
		} else {
			hToOpen = (((Math.ceil(hOfObject / 185)))*205)-20;
		}
		toOpen.css('z-index','2000').animate({
			width: 700,
			height: hToOpen,
			duration: 1650,
		}, function(){
			toOpen.children('.idirContent').show('fast');
			buildSlider();
			idirMason();
			notAnimating = true;
			toOpen.css('z-index','0')
		});	
	}
	
	function idirClose(toClose) {
		toClose.children('.idirContent').hide('fast');
		toClose.css('z-index','0').removeClass('open').animate({
			width: 220,
			height: 185,
		}, function(){
			toClose.children('.thumb').show('fast');
			idirMason();
			notAnimating = true;
		});	
	};
	
	function idirMason(){
		$('#content').masonry({
			columnWidth: 240, 
			animate: true,
			itemSelector: '.idirBlock:not(.invis)',
			animationOptions: {
				duration: 650,
				easing: 'linear',
				queue: false
			}
		});
	};
	
	$('li.categories a').click(function(){
		var colorClass = '.' + $(this).attr('class');
		if(colorClass=='.all') {
			// show all hidden boxes
			$('#content').children('.invis')
				.toggleClass('invis').animate({opacity: 1},{ duration: 150 });
		} else {    
			// hide visible boxes 
			$('#content').children().not(colorClass).not('.invis')
				.toggleClass('invis').animate({opacity: 0},{ duration: 150 });
			// show hidden boxes
			$('#content').children(colorClass+'.invis')
				.toggleClass('invis').animate({opacity: 1},{ duration: 150 });
		}
		$('#content').masonry();
		return false;
	});
	
	$('li.categories a').mouseenter(function() {
		var theText = $(this).text();
		var colorClass = '.' + $(this).attr('class');
		var colorBorder = $(this).css('color');
		$('#content').children(colorClass).find('.catlink').filter(function(index) {
  			return $(this).text() == theText;
		}).css({'color': colorBorder});
		$('#content').children(colorClass).find('.openerWrap').css({'border-right-style': 'solid', 'border-right-width': '10px', 'border-color': colorBorder})
		$('#content').children(colorClass).find('.attachment-post-thumbnail').css({'opacity': '0.8'});
	});
	
	$('li.categories a').mouseleave(function() {
		var colorClass = '.' + $(this).attr('class');
		$('#content').children(colorClass).find('.catlink').css({'color': '#000'});
		$('#content').children(colorClass).find('.openerWrap').css({'border-right-style': 'none'})
		$('#content').children(colorClass).find('.attachment-post-thumbnail').css({'opacity': '0.6'});
	});
	
	idirMason();
	notAnimating = true;
});
