if (!window.$)
	window.$ = jQuery;
	
var currentFeature = 1;
var featureInterval = null;

function resetFeatureInterval() {
	clearInterval(featureInterval);
	featureInterval = setInterval('nextFeature();', 5000);
}

function galleryShowImage(show, hide) {
	showImage = $('#photo_gallery_image_' + show)
	hideImage = $('#photo_gallery_image_' + hide)

	showImage.hide();
	
	hideImage.css('z-index', 50).fadeOut();
	showImage.css({'z-index': 100, 'position': 'absolute'}).fadeIn();
	showImage.parent().height(showImage.height());
}

function nextFeature() {
	var featureImages = $('#feature_images .feature_image');
	
	var nextFeature = currentFeature + 1;
	if (nextFeature > featureImages.length)
		nextFeature = 1;
	
	// start the image animation
	$(featureImages[nextFeature - 1]).hide();
	$(featureImages[currentFeature - 1]).css('z-index', 50);
	$(featureImages[nextFeature - 1]).css('z-index', 100).fadeIn();

	var buttonHeight = 70;
	if (featureImages.length > 4) {
		switch (featureImages.length) {
			case 5: buttonHeight = 53; break;
			case 6: buttonHeight = 42; break;
			case 7: buttonHeight = 35; break;
		}
		
		$('#feature_button_' + currentFeature).animate({ height: buttonHeight }).children('.lbtn').fadeOut().parent().children('.sbtn').fadeIn();
		$('#feature_button_' + nextFeature).animate({ height: 70 }).children('.lbtn').fadeIn().parent().children('.sbtn').fadeOut();
	}

	// animate arrow
	$('#feature_pointer').animate({ top: (buttonHeight * (nextFeature - 1)) });

	// update counter
	currentFeature = nextFeature;
}

$(function() {
	if (!window.$)
		window.$ = jQuery;
	
	if (($('#feature_images .feature_image') != null) 
		&& (($('#feature_images .feature_image')).length > 0)) {

		// set up resizing buttons
		if (($('#feature_images .feature_image')).length > 4) {
			$('#feature_buttons .feature_button h5').each(function() {
				$(this).clone().removeClass('sbtn').addClass('lbtn').appendTo($(this).parent());
			});		
			$('#feature_buttons .feature_button_current').each(function() {
				$(this).removeClass('feature_button_current').css({ 'height': 70 });
				$(this).children('.lbtn').show().parent().children('.sbtn').hide();
			}); 
		}
		
		resetFeatureInterval();
	}

	var fbCheckCount = 0;
	var fbCheckInterval = setInterval(function() {
		var likeDivs = $('.fb-like');
		if ((likeDivs[0].childNodes.length > 0) || (fbCheckCount > 5)) {
			clearInterval(fbCheckInterval);
		} else {
			FB.XFBML.parse();
		}
		fbCheckCount++;
	}, 1000);
	
	setTimeout(function() { $('#content .category-cypher a[href*=#cypher]').each(function() {
		var href = $(this).attr('href');
		if (href.indexOf('#') != -1)
			$(this).attr('href', href.substring(href.indexOf('#')));
		$(this).fancybox({});
	}) }, 1000);

	
});

