﻿// jQuery extension to CloudCarousel.
(function($) {	
	var _carousels = [];
	var _triggerInitialized = false;
	var _defaults = 
		{
			minScale: 0.25,
			reflHeight: 30,
			reflGap:2,
			yRadius:40,
			speed:0.15,
			mouseWheel:true			
		};	

	$.LigetisCarouselDefaults = function(options) {
		_defaults = $.extend(_defaults, options);
	};
	
	// Add target as a new carousel definition to the collection
	$.fn.LigetisCarousel = function(options) {
		$.AddLigetisCarousel(this.attr('id'), options); 
	};	
	
	// Add a new carousel definition to the collection
	$.AddLigetisCarousel = function(id, options) {
		id = id.substr(0,1) == '#' ? id.substr(1) : id;
		if (_carousels[id] != null) return; // Already defined
		var settings = $.extend(
		{
			altBox: $("#carousel-alt-text"),
			titleBox: $("#carousel-title-text"),
			buttonLeft: $("#" + id + "-left-but"),
			buttonRight: $("#" + id + "-right-but")
		}, _defaults, options);
				
		var carousel = $('#' + id);
		if (settings.width 	!= null) {
			carousel.css('width', settings.width + 'px');
			if (settings.xRadius == null) settings.xRadius = settings.width * 0.40;
		}
		if (settings.height != null) carousel.css('height', settings.height + 'px');
		carousel.css('overflow', 'scroll');
		
		_carousels[id] = function() {$('#' + id).CloudCarousel(settings); return null;};
		
		if (!_triggerInitialized) {
			$(document).ready(function() {$.StartLigetisCarousel()});	
			_triggerInitialized = true;
		}
	};
		
	// View or hide a carousel
	$.ViewLigetisCarousel = function (carousel, visible) {
		var id = carousel.attr('id');
		if (visible) {
			carousel.css('display', 'block');
			$('a[rel=' + id + ']').attr('class', 'selected');
			if (_carousels[id] != null) _carousels[id] = _carousels[id]();	
		}
		else {
			carousel.css('display', 'none');
			$('a[rel=' + id + ']').attr('class', 'unselected');
		};
	};

	// View a carousel, and hide all the others
	$.SelectLigetisCarousel = function (id) {			
		$('div[class=carousel]').each(function (index) {
			var carousel = $(this);
			$.ViewLigetisCarousel(carousel, carousel.attr('id') == id);
		});
	};
	
	// Initialize all carousels
	$.StartLigetisCarousel = function() {			
		$('div[class=carousel]').each(function (index) {
			$.ViewLigetisCarousel($(this), index == 0);
		});
					
		$('a[rel|=carousel]').each(function (index) {
			$(this).attr('href', 'JavaScript:$.SelectLigetisCarousel("' + $(this).attr('rel') + '")');
		});
	};
		
})(jQuery);

