/*

sqGallery: a simple jQuery slideshow plugin

(c)2009 Sqoo Media Ltd.

http://sqoo.co.uk

*/


jQuery.fn.sqGallery = function(options)
{

	//construct settings from defaults and options

	var defaults = 
	{
		prev_button 		: '<span>&lt;</span>',
		next_button 		: '<span>&gt;</span>',
		open_selector		: '.sqGallery_open',
		transport_position 	: 'top',
		border_width 		: 40
	};

	var settings = $.extend({}, defaults, options);

	// add gallery objects to dom
	$viewer = sqGallery_viewer();
	$('body').append($viewer);
	$('.sqGallery_mainContainer').hide();
	$('.sqGallery_imgLoad').animate({opacity: '.6'});

	//iterate though each jQuery match

	return this.each(function(options) 
	{
	
		$slide_list = $(this);
		
		// hide all slides
		$($slide_list).hide();

		//mark first slide active	
		$('li:first',$slide_list).addClass('sqGallery_current');
		
		//bind events
		$('.sqGallery_next').click(function(){sqGallery_transport_next($slide_list)});
		$('.sqGallery_prev').click(function(){sqGallery_transport_prev($slide_list)});
		$('.sqGallery_close').click(function(){sqGallery_close_gallery()});
		$(settings.open_selector).click(function(){sqGallery_open_gallery()});

	});
}


function sqGallery_open_gallery()
{
	//console.log('sqGallery_open_gallery');
	$('.sqGallery_obscure').show().animate({opacity: .8},500);
	//$('.sqGallery_mainContainer').show();
	sqGallery_preload($slide_list);
}

function sqGallery_close_gallery()
{
	$('.sqGallery_obscure').animate({opacity: 0},500,function(){$(this).hide()});//.hide();
	$('.sqGallery_mainContainer').hide();
}

function sqGallery_load($slide_list)
{
	//console.log('sqGallery_load');
	$('.sqGallery_imgLoad').show();
	sqGallery_preload($slide_list);
}

function sqGallery_preload($slide_list)
{
	//console.log('sqGallery_preload');
	//get image from current
	image_src = $('.sqGallery_current a',$slide_list).attr('href');
	$('.sqGallery_imgLoad').show();
	
	//rebind load function
	$('.sqGallery_imgContainer .sqGallery_img:first img').unbind('load').load(function(){sqGallery_show()});
	
	//load new image
	$('.sqGallery_imgContainer .sqGallery_img:first img').attr('src',image_src);
}

function sqGallery_show()
{
	//console.log('sqGallery_show');

	$('.sqGallery_imgLoad').hide();
	
	//get element to place new image in
	$new = $('.sqGallery_imgContainer .sqGallery_img:first');
	$new_img = $('img',$new)
	$new_img.addClass('fubber');

	//get loaded images height

	mc_height = $('.sqGallery_mainContainer').height();
	mc_width = $('.sqGallery_mainContainer').width();
	ic_height = $new.height();
	ic_width = $new.height();

	$('.sqGallery_mainContainer').animate({width: '800px',height: '600px'},0); //IE requires bounding box to be larger than img
	$new.animate({width: '800px', height: '600px'},0); 							//IE requires bounding box to be larger than img

	img_width = $new_img.width();
	img_height = $new_img.height();

	$('.sqGallery_mainContainer').animate({width: mc_width+'px',height: mc_height+'px'},0); //IE requires bounding box to be larger than img

	tsrc = $new_img.attr('class');
	
	//alert('HxW ' + img_height + ' ' + img_width  + ' : ' + $new_img.attr('src'));

	
	//hide $new item
	$new.css({opacity: 0});

	//move $new in front of $old (using natural element order rather than z-order)
	$old = $('.sqGallery_imgContainer .sqGallery_img:last');
	$old.remove();
	$old.insertBefore($new);

	
	//resize elements and anmiate center gallery
	wrap_height = img_height + 80;
	wrap_width = img_width + 80;
	wrap_margin_left = (wrap_width / 2) * -1;
	wrap_margin_top = (wrap_height / 2) * -1;
	wrap_margin_left = wrap_margin_left + 'px';
	wrap_margin_top =  wrap_margin_top + 'px';

	//show $new element
	$new.animate({opacity: 1},500);

	$('.sqGallery_imgContainer').animate({width: img_width + 'px',height:img_height + 'px'},500);
	$('.sqGallery_mainContainer').animate({marginLeft: wrap_margin_left,marginTop: wrap_margin_top,height:wrap_height + 'px',width:wrap_width + 'px'},500);
	$('.sqGallery_transport').animate({width: wrap_width + 'px'},500);
	$('.sqGallery_header').animate({width: wrap_width + 'px'},500);

}

function sqGallery_transport_next($slide_list)
{

	$current = $('.sqGallery_current',$slide_list);
	$next = $current.next();
	if ($next.length > 0)
	{
		$current.removeClass('sqGallery_current');
		$next.addClass('sqGallery_current');
		sqGallery_load($slide_list);
	}
	else
	{
		$('.sqGallery_next').css({opacity: .5});
	}
}

function sqGallery_transport_prev($slide_list)
{
	$current = $('.sqGallery_current',$slide_list);
	$prev = $current.prev();
	if ($prev.length > 0)
	{
		$current.removeClass('sqGallery_current');
		$prev.addClass('sqGallery_current');
		sqGallery_load($slide_list);
	}
	else
	{
		$('.sqGallery_prev').css({opacity: .5});
	}
}



function sqGallery_viewer()
{
	$viewer = $(
	 '<div class="sqGallery_obscure"></div>'
	+'<div class="sqGallery_mainContainer">'
	+'<div class="sqGallery_header">'
	+'	<div class="sqGallery_close">X</div>'
	+'</div>'
	+'<div class="sqGallery_transport">'
	+'	<div class="sqGallery_prev">&lt;</div>'
	+'	<div class="sqGallery_next">&gt;</div>'
	+'</div>'
	+'<div class="sqGallery_imgContainer">'
	+'	<div class="sqGallery_img sqGallery_imgOdd" ><img class="oddImg"  src="/loading.gif" /></div>'
	+'	<div class="sqGallery_img sqGallery_imgEven"><img class="evenImg" src="/loading.gif" /></div>'
	+'	<div class="sqGallery_imgLoad"><img src="/loading.gif" /></div>'
	+'</div>'
	+'</div>'
	);
	
	return $viewer;
}

