/*
 * ImageSlider v1.0 - Collapses image to specified height with controls to reopen image.
 *
 * Copyright (c) 2008 Redweb Limited
 *
 * Built by Gavin Cooper gavincooper@redweb.com
 *
 * @params
 *		height			: Height when image control is collapsed.
 *		expandTitle		: Title of the image expansion control.
 *		collapseTitle	: Title of the image collapse control.
 */
(function($) {    
	$.fn.imageSlider = function(options) {  
		var defaults = {  
			'speed'			: 500,
			'height' 		: 200,
			'expandTitle'	: 'Expand Image',
			'collapseTitle'	: 'Collapse Image'
		}, options = $.extend(defaults, options);  
		
		this.each(function() {  
		
		    // This is so the image will only be expandable on the product pages.
		    var expand = $(this).parent().attr('expand');
		    
			var $$ = $(this);
			
			
			if(expand)
			{
			    $$.wrap('<div class="imageContainer" expand="true" />');
			
			    $$.parent().wrap('<div class="imageSlider" />').css({'height'	: options.height,
																     'position'	: 'relative',
																     'overflow'	: 'hidden'
																    });
			}
			else
			{
			    $$.wrap('<div class="imageContainer noWidth" />');
			    
			    $$.parent().wrap('<div class="imageSlider" />').css({'height'	: $$.height(),
																     'position'	: 'relative'
																    });
			}
			
			var $super = $$.parent().parent();
			
			if(expand)
			{
				$super.append('<div class="controls"><a href="#" title="'+options.expandTitle+'" class="expand">'+options.expandTitle+'</a></div>');												   
			
			    $$.css({
				   'top'		: -((parseInt($$.height()) / 2) - (parseInt(options.height) / 2)),
				   'position'	: 'absolute'
				   });	
			}
		
			var $control = $(".controls a", $super);
		
			$(".controls a", $super).click(function() {
				if (parseInt($(".imageContainer", $super).height()) != parseInt($$.height()))	{ // Collapsed thus expand.
					$(".imageContainer", $super).animate({
													  'height'	:	parseInt($$.height())
													  }, options.speed);
					
					$$.animate({'top'	:	0}, options.speed);
					
					$control.addClass('collapse').removeClass('expand').attr('title', options.collapseTitle).html(options.collapseTitle);
					
					return false;
				} else { // Expanded thus collapse.
					$(".imageContainer", $super).animate({
													  'height'	:	parseInt(options.height)
													  }, options.speed);
					
					$$.animate({'top'	:	-((parseInt($$.height()) / 2) - (parseInt(options.height) / 2))}, options.speed);
					
					$control.addClass('expand').removeClass('collapse').attr('title', options.expandTitle).html(options.expandTitle);
					
					return false;
				}
			});
		}); // End each loop.
	}
})(jQuery); 

$(document).ready(function(){

    $(".cms img").imageSlider({'height' : 91});

});