$(document).ready(function(){
        $('#content .itemlist').equalHeights();

	//###############   PRETTYPHOTO, Zoom functionality throughout content   ###############
	$("a[rel^='prettyPhoto']")
		.each(function() {
			$(this).hover(function() {$(this).children('.zoom').fadeIn(250);}, function() {$(this).children('.zoom').fadeOut(250);});
		})
		.append('<img src="/themes/site_themes/finky/assets/images/icons/zoom.png" alt="" class="zoom" />')
		.prettyPhoto({
			showTitle: true,
			counter_separator_label: " of "
		});

		if ( $(".accordion").length ) {
			$(".accordion").accordion({
				autoHeight: false,
				collapsible: false,
				active: false
			});
		}

	if ( $("#nav_categories").length ) {
		//###   Get the current menu item to open appropriate accordion menus   ###
		$subMenuItem = $("#nav_categories .current").parent().parent().parent();
		$subMenu = $subMenuItem.parent();

		if ($subMenu.attr("id") == "nav_categories") {
			$rootMenu = $subMenu;
			$subMenu = false;
			$rootMenuItem = $subMenuItem;
			$subMenuItem = false;
		} else {
			$rootMenuItem = $subMenu.parent();
			$rootMenu = $rootMenuItem.parent();
		}

		//###   Set indexes for open accordion menus   ###
		rootIndex = $rootMenu.children("li").index($rootMenuItem);
		if ($subMenuItem == false)
			subIndex = false;
		else
			subIndex = $subMenu.children("li").index($subMenuItem);

		//###   Root Level Accordion   ###
		$("#nav_categories").accordion({
			autoHeight: false,
			collapsible: true,
			active: rootIndex,
			changestart: function(event, ui) {
				if (ui.newHeader.hasClass("ui-state-active") == false) {
					location.href = ui.oldHeader.attr("href");
					ui.accordion( "disable" )
				}
			}
		});
		//###   Sub-Level Accordion   ###
		$("#nav_categories > li > ul").accordion({
			autoHeight: false,
			collapsible: true,
			active: subIndex,
			changestart: function(event, ui) {
				if (ui.newHeader.hasClass("ui-state-active") == false || ui.newHeader.siblings().length == 0) {
					location.href = ui.oldHeader.attr("href");
					ui.accordion( "disable" )
				}
			}
		});
	}

	//###   Slider (Homepage or any page it's selected to appear on)   ###
	if ( $(".slider").length ) {
		$('.slider .nav a').click(function() {
			clearInterval(sliderInterval);
			slide(parseInt($(this).parent().attr('class').substr(1)));
			return false;
		});
			
		getCurrentSlide = function() {
			return parseInt($('.slider .slides li.on').attr('class').split(' ').reverse().pop().substr(1));
		}
			
		movePointer = function(index, lastIndex) {
			if (index == lastIndex) {
				location.href = $(".slider .nav .s"+index+" a").attr("href");
			} else {
				$('.slider .pointer').stop().animate({ top: ((index - 1) * 83) + index }, function() {
					$('.slider .n'+lastIndex).parent().removeClass('on').find('a').css({ color: '#666' });				
					$('.slider .n'+index).parent().addClass('on').find('a').css({ color: '#000' });
					Cufon.refresh('.slider .nav a', { fontFamily: 'IceAgeD', hover: true  });
					Cufon('.slider .nav a', { hover: { color: '#000' }});
				});
			}
		}
			
		slide = function(nextIndex) {
				var currentIndex = getCurrentSlide();
				if (nextIndex === undefined || nextIndex < 1) nextIndex = currentIndex == 4 ? 1 : currentIndex + 1;
				$('.slider .slides ul').stop().animate({ top: (-1 * 380 * (nextIndex - 1)) });
				movePointer(nextIndex, currentIndex);
				$('.s'+currentIndex).removeClass('on');
				$('.s'+nextIndex).addClass('on');
		}

		$('.slider .nav a').hover(function() {
			$(this).css({ color: '#000' });
		}, function() {
			if(!$(this).parent().hasClass('on')) $(this).css({ color: '#666' });
		});
			
		sliderInterval = setInterval(function() {slide(0);}, 5000);
	}
});

//###   Cufon - Text replacement   ###
Cufon.replace('.subnav > ul > li > a, a.more', { fontFamily: 'Arial Narrow', hover: true });
Cufon.replace('.article h1', { fontFamily: 'DymoFontInvers' });
Cufon.replace('.slider .nav a', { fontFamily: 'IceAgeD', hover: true });


/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().children('div').each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().children('div').css({'height': currentTallest}); }
		$(this).children().children('div').css({'min-height': currentTallest}); 
	});
	return this;
};
