(function($){
	
	$(document).ready(function(){
		
		$('.media .video a, .videos .video a').fancybox({
			type: 'iframe',
			width: 640,
			height: 390
		});
		
		$('.media .photos a, .gallery .gallery-item a').fancybox();
		
		if ( $('#sidebar').find('.image img').length != 0 ) {
			var height = $('#sidebar').find('.image img').innerHeight();
			if ( $('#sidebar').innerHeight() < height ) {
				$('#sidebar').css('height', height);
			};
		};
		
		$("#navigation li").not(".twitter").not(".facebook").not(".amazon").filter(":last").addClass("last");
		
		$("table").each(function() {
			$("tr:even", this).addClass("even");
			$("tr", this).each(function() {
				$("td:first, th:first", this).addClass("first");
				$("td:last, th:last", this).addClass("last");
			});
			$("td:first", this).parent().addClass("first");
			$("td:last", this).parent().addClass("last");
		});
		
		$("#bottom-nav li:last").addClass("last");
		
		$(".entry table tr:even").addClass('even');
		
		$("dt").click(function() {
			if( $(this).is(".active") ) {
				
				var $t = $(this);
				var $next = $t.next();
				
				$next.animate({
					height:0
				});
				$t.removeClass("active");
				
			
			} else {
			
				var $t = $(this);
				var $next = $t.next();
				var $open = $next.siblings("dd");
				
				$next.animate({
					height: $next.find(".dd-inner").outerHeight()
				});
				$open.animate({
					height:0
				});
				$t.addClass("active").siblings("dt").removeClass("active");
				
			}
		});
		
		$("#home-tabs .navigation a").append("<span class='edge' />");
		
		/*$("#home-tabs .navigation a").click(function() {
			$("#home-tabs .navigation a").removeClass("active");
			$(this).addClass("active");
			$("#home-tabs .content .tab").hide().eq( $(this).parent().index() ).show();
			return false;
		}).eq(0).click();*/
		
		if($.browser.msie && $.browser.version.substr(0,1) == 6 ) { 
			DD_belatedPNG.fix('#home-tabs .image img');
		}
		
		hideAllStoreListings();
		
		$('tr.state').click(function(){
		
			var currentElement = $(this);
			var looping = true;
			
			testElement = currentElement.next();
			
			if (testElement.is(":visible")){ currentElement.removeClass('active'); } else { $('tr.state').removeClass('active'); currentElement.addClass('active'); hideAllStoreListings(); }
			
			while (looping == true){
				currentElement = currentElement.next();
				if (currentElement.hasClass('state')){ looping = false; } else { currentElement.toggle(); }
			}
			
			return false;
			
		});
	
	});

})(jQuery);

function hideAllStoreListings() {
	jQuery("table.state-listing").find("tr:not(.state)").each(function(){
		jQuery(this).hide();
	});
}
