/**
 * The hero image carousel for Buick/GMC Exclusive
 * 
 * TODO: Document the assumptions about how the html is structured.
 */
(function($) {
    var defaults = {
    	carouselOptions: {
			scroll: 1,
			animation: "slow",
			wrap: "last",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			itemLoadCallback: function(scope) {scope.onItemLoad()},
			initCallback: function(scope) {scope.onInit()}
		},
		auto: true,
		carouselEl: null,
		controllerEl : null,
		eventNamespace: 'com.cobaltgroup.ws.carousel',
		onPreRender: function(scope) { },
		onDisplayedImageChange: function(e, pubData) { },
		onAtHeadChange: function(e, pubData) { },
		onAtTailChange: function(e, pubData) { },
		onInitialize: function(e, pubdata) { }
    };
	
	if (!EventManager) {
		console.error("EventManager.js required");
	}
	if (!jQuery.jcarousel) {
		console.error("jcarousel plug required");
	}
	jQuery.fn.SlideShowControl = function(o) {
        return this.each(function() {
            new $ssc(this, o);
        });
    };
	
    jQuery.SlideShowControl = function(el, o) {
		el = jQuery(el);
        this.options = jQuery.extend({}, defaults, o || {});

		if (typeof auto == 'undefined')
			auto = true;
	
		var self = this;
		var foo = self.onDisplayedImageChange;
		EventManager.subscribe({
			eventName: [self.options.eventNamespace, 'displayed'].join('.'),
			callback: self.options.onDisplayedImageChange,
			scope: self
		});
	
		EventManager.subscribe({
			eventName: [self.options.eventNamespace, 'atHead'].join('.'),
			callback: self.options.onAtHeadChange,
			scope: self
		});
				
		EventManager.subscribe({
			eventName: [self.options.eventNamespace, 'atTail'].join('.'),
			callback: self.options.onAtTailChange,
			scope: self
		});
		
		EventManager.subscribe({
			eventName: [self.options.eventNamespace, 'initialized'].join('.'),
			callback: self.options.onInitialize,
			scope: self
		});

		// Set up mouse over/out to stop/start auto scrolling
		jQuery('#slideShow_Navigation').hover(function(e) {
			EventManager.publish({
				eventName: [self.options.eventNamespace, 'stop'].join('.'),
				publisherData: e,
				scope: self
			});
		}, function(e) {
			EventManager.publish({
				eventName: [self.options.eventNamespace, 'start'].join('.'),
				publisherData: e,
				scope: self
			});
		}).mousemove(function(e) {
			// This is added to stop the scrolling when the mouse starts over the carousel. Removed by the start and stop events.
			EventManager.publish({
				eventName: [self.options.eventNamespace, 'stop'].join('.'),
				publisherData: e,
				scope: self
			});
		});

		jQuery('#slidePrev').click(function(e) {
			EventManager.publish({
				eventName: [self.options.eventNamespace, 'previous'].join('.'),
				publisherData: e
			});
			return false;
		});
		jQuery('#slideNext').click(function(e) {
			EventManager.publish({
				eventName: [self.options.eventNamespace, 'next'].join('.'),
				publisherData: e
			});
			return false;
		});
		
		// Set up the carousel
		if (typeof this.options.onPreRender == 'function')
			this.options.onPreRender(this);
		
		this.options.carouselOptions.auto = this.options.auto ? 4 : 0;
		this.options.carouselOptions.eventNamespace = this.options.eventNamespace;
		jQuery('#gm-hero-carousel').jcarousel(this.options.carouselOptions);
	};

})(jQuery);


