// Title: Pink Blossom Tree - javascript
// Author: Kalon Edser, http://www.kalonedser.com/
// Updated: 24 November 2009

jQuery(document).ready(function() {
	
	$('body').addClass('js');
	
	// fix png images with alpha channels
	$.ifixpng('../img/pixel.gif');
	$('.png').ifixpng();
	
	// global
	var fadespeed = 1500;
	var halfspeed = fadespeed / 2; // half transition speed
	
	// submenu navigation hover
	$('.nav li').hover(
	  function () {
		$(this).find("ul").stop(false,true).hide().show("slide", { direction: "up", duration: 300 });
	  },
	  function () {
		$(this).find("ul").stop(false,true).show().hide("slide", { direction: "up", duration: 300 });
	  }
	);
	
	function slideshow() {
		// sideshow navigation
		$('#slide-info').append('<a href="#" id="prev">Prev</a> <a href="#" id="next">Next</a>');
	
		// slideshow image caption
		function onBefore() { 
			var thiscaption = $(this).attr('alt');
			$('#image-title').fadeOut(halfspeed, function () {
				$('.thumbnails').scrollTo($('.activeSlide'), {duration:750});
				$('#image-title').text(thiscaption).fadeIn(halfspeed);
			}); 
		};

		// slideshow
		$('.slideshow').cycle({ 
			fx: 'fade', 
			speed: fadespeed, 
			timeout: 5000,
			pause: 1,
			pager: '.thumbnails',
			next: '#next', 
			prev: '#prev',
			before: onBefore,
			pagerAnchorBuilder: function(idx, slide) { 
			    // return selector string for existing anchor 
			    return '.thumbnails li:eq(' + idx + ') a'; 
			} 
		});
		
		$('.thumbnails a, #prev, #next').click(function(){
			$('.thumbnails').scrollTo($('.activeSlide'), {duration:250});
		});
		
		// toggle preface on 'order form' hover
		$('.order-form a').hoverIntent(
		  function () {
			showPreface();
			$('.slideshow').cycle('pause');
			$('#image-title').fadeOut(halfspeed);
			$('a.activeSlide').removeClass('activeSlide').addClass('pendingSlide');
		  },
		  function () {
			hidePreface();
			$('.slideshow').cycle('resume');
			$('#image-title').fadeIn(halfspeed);
			$('a.pendingSlide').removeClass().addClass('activeSlide');
		  }
		);
		
	};

	// slideshow preface
	function initialPreface() {
		$('<div class="deactivate"></div>').appendTo('#thumbnails').css('opacity','0.33');
		$('#slide-info').append('<a href="#" id="view-slideshow">View Series</a>');
		
		// preface hide
		var $prefaceImage = $('#preface');
		$prefaceImage.hide();
		// show after image loads
		$prefaceImage.each(function() {
			if ($prefaceImage[0].complete) {
				showPreface();
			} else {
				$prefaceImage.bind('load', showPreface);
			}
		});
	};
	
	function showPreface() {
		$('#preface').show("slide", { direction: "down" }, fadespeed);
		$('#thumbnails .deactivate').fadeIn(halfspeed);
	};
	
	function hidePreface() {
		$('#preface').hide("slide", { direction: "down" }, fadespeed);
		$('#thumbnails .deactivate').fadeOut(halfspeed);
	};
	
	initialPreface()

	// activate slideshow and hide preface
	$('#preface, #view-slideshow, #thumbnails .deactivate').click(function() {
		$('#view-slideshow').remove();
		hidePreface();
		slideshow();
		return false;
	});

	// open new window for external links
	$('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

});