jQuery(document).ready(function($) {

	initIsotope();
	addHoverToolTip();

	


// This is used on the All Shows page for the show listing on the bottom
function addHoverToolTip(){
	// For each popup, lets do this.
	$('div.show_box').each(function(){	
		var distance = 10;								// How far vertically the popup should move as it fades in
		var time = 250;									// The time it takes for the popup to fully display
		var hideDelay = 100;							// How long should pass before we fade out the popup
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $(this);							// Which element on hover should trigger the popup
		var triggerPos = 0;
		var info = $(this).find('div.more_show_info');	// The div containing the extra info that popups
		// Mouseover Event
		$(trigger).mouseover(function(){
			triggerPos = $(trigger).position();
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
	        if (beingShown || shown) {
	            // don't trigger the animation again
	            return;
	        } else {
				info.appendTo('#all_shows');
	            // reset position of info box
	            beingShown = true;
				// The styling of the popup
	            info.css({
	                top: triggerPos.top - 180,
	                left: triggerPos.left - 68,
	                display: 'block'
	            }).animate({
	                top: '-=' + distance + 'px',
	                opacity: 1
	            }, time, 'swing', function() {
	                beingShown = false;
	                shown = true;
	            });
	        }
	        return false;
		// Mouseout Event
		}).mouseout(function(){
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
			    hideDelayTimer = null;
			    info.animate({
			        top: '-=' + distance + 'px',
			        opacity: 0
			    }, time, 'swing', function () {
			        shown = false;
			        info.css('display', 'none');
					info.appendTo(trigger);
			    });
			}, hideDelay);
			return false;
		});
		// Hover over for the popup, so we can control when it goes away
		$(info).mouseover(function(){
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			return false;
		}).mouseout(function(){
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
			    hideDelayTimer = null;
			    info.animate({
			        top: '-=' + distance + 'px',
			        opacity: 0
			    }, time, 'swing', function () {
			        shown = false;
			        info.css('display', 'none');
					info.appendTo(trigger);
			    });
			}, hideDelay);
			return false;
		});
	});
}



// Sets up the Isotope plugin
function initIsotope() {
	// Initializes the Isotope plugin
	var $all_shows = $('#all_shows'),
		$all_shows_btns = $('#iso-btns a');
	// Lets setup Isotope
	$all_shows.isotope({
		animationEngine: 'jquery',
		transformsEnabled: true,
		itemSelector : '.show_box',
		layoutMode : 'fitRows',
		getSortData: {
			name : function ( $elem ) {
				return $elem.find('.sb_name').text();
			},
			date : function ( $elem ) {
		    	return parseFloat( $elem.find('.date').text() );
			},
			view_count : function ( $elem ) {
		    	return parseFloat( $elem.find('.view_count').text() );
			} 
		} // close getSortData
	});
	// Select the sort order
	$all_shows_btns.click(function() {
		var btn_type = $(this).attr('class');
		var btn_cat = $(this).attr('id');
		// Lets Check which way we need to sort.
		if(btn_cat == 'name'){
			$all_shows.isotope({
				sortBy: btn_cat,
				sortAscending : true
			});
		}else{
			$all_shows.isotope({
				sortBy: btn_cat,
				sortAscending : false
			});
		}
		$('#iso-btns a').removeClass('active');
		$(this).addClass('active');
		return false;	
	});
}

});
