/* ===========================================================================
 * Filename: theme/javascript/functions.js
 * Author: Webmistress, info@webmistress.com.au, www.webmistress.com.au
 * Copyright: Webmistress
 * Date: 23 Jun 2011
 * Description: Contains JavaScript behaviour for Belinda Winkler's website.
 *
 * This file may not be used for any purpose other than for Belinda Winkler's
 * website and may not be modified without written permission from the author.
 * =========================================================================== 
 */

$(document).ready(function()
{	
	$("a[rel='external']").attr("target","_blank");

	$("#gallery li").css({opacity: 0.0});
	var $current = $('#gallery li.active');
	if ($current.length == 0) $current = $('#gallery li:first');
	$current.css({opacity: 1.0});
	$("#gallerycaption").css({opacity: 0.0}).css("display","block").html($current.children("div.caption").html());
	$("#prev img").css({opacity: 0.0}).css("visibility","visible");
	$("#next img").css({opacity: 0.0}).css("visibility","visible");
	
	$("#gallery").hover(
		function () { $("#gallerycaption").animate({opacity:0.6}, 500); },
		function() { $("#gallerycaption").animate({opacity:0.0}, 500); }
	);

	$("#next").click (
		function() { nextSlide(); return false; }
	);
	
	$("#prev").click (
		function() { prevSlide(); return false; }
	);	
	
	$("#next").hover(
		function () { $(this).css("cursor","pointer").find('img').animate({opacity:0.6}, 500); },
		function() { $(this).find('img').animate({opacity:0.0}, 500); }
	);
	
	$("#prev").hover(
		function () { $(this).css("cursor","pointer").find('img').animate({opacity:0.6}, 500); },
		function() { $(this).find('img').animate({opacity:0.0}, 500); }
	);
	
	$("#thumbnails li").each(
		function( intIndex )
		{
			$(this).click (
				function() { goToSlide(intIndex); return false; }
			);
		}
	);
	
	if ($('.home #gallery').length)
	{
		setInterval( "slideHomeSwitch()", 5000 );
	}
});

function goToSlide(slideNum) 
{
	if ($('#gallery').length)
	{
	    var $current = $('#gallery li.active');
		if ( $current.length == 0 ) $current = $('#gallery li:last');
		var $next =  $("#gallery li").eq(slideNum);
	    
	    var anchorNumberCurr = $current.attr('id');
	    var anchorNumber = $next.attr('id');
	    
	    if(anchorNumberCurr != anchorNumber)
	    {
	    	$("#thumbnails li").removeClass("active");
			$("#thumbnails li a[href='#"+anchorNumber+"']").parent().addClass("active");
		 
			$current.addClass('last-active').removeClass('active');
		    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {$current.removeClass('last-active');});
	    	$("#gallerycaption").html($next.children("div.caption").html());
        }
    }
}

function nextSlide() 
{
	if ($('#gallery').length)
	{
	    var $current = $('#gallery li.active');
		if ($current.length == 0) $current = $('#gallery li:first');
		var $next =  $current.next().length ? $current.next() : $('#gallery li:first');

	    var anchorNumber = $next.attr('id');
	    $("#thumbnails li").removeClass("active");
		$("#thumbnails li a[href='#"+anchorNumber+"']").parent().addClass("active");
		
		$current.addClass('last-active').removeClass('active');
		$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {$current.removeClass('active last-active');});
	    $("#gallerycaption").html($next.children("div.caption").html());  
    }
}

function prevSlide() 
{
	if ($('#gallery').length)
	{
	    var $current = $('#gallery li.active');
	    if ($current.length == 0) $current = $('#gallery li:first');
	    var $prev =  $current.prev().length ? $current.prev() : $('#gallery li:last');

	    var anchorNumber = $prev.attr('id');
	    $("#thumbnails li").removeClass("active");
		$("#thumbnails li a[href='#"+anchorNumber+"']").parent().addClass("active");
		
		$current.addClass('last-active').removeClass('active');
	    $prev.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {$current.removeClass('last-active');});
	    $("#gallerycaption").html($prev.children("div.caption").html());
    }
}

function slideHomeSwitch() 
{
	if ($('.home #gallery').length)
	{
		var $current = $('#gallery li.active');
		if ($current.length == 0) $current = $('#gallery li:last');
		var $next =  $current.next().length ? $current.next() : $('#gallery li:first');
		
		$current.addClass('last-active').removeClass('active');
		$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() { $current.removeClass('last-active'); });
	}
}

