// requires jquery library
/*
events:
  slides_el: triggers slideChanged when a slide is changed 
  nav_el: triggers slideTo when nav changes a slide
*/

var SSNav = {};
(function($) {
	
	default_options = {
		plugin: {
		},
		image_flow: {}
	};
	

	SSNav.initSlides = function(jq_selector, user_options) {
		var options = {};
		
		function initTabNav(slides_el) {
			// init either the slider or fader
			if (options.transition.type == 'slide') {
				initSlider(slides_el, options);
			} else if (options.transition.type == 'fade') {
				initFader(slides_el, options);
			}
			
			// set frame size
			initSliderSizes(slides_el, options);

			// set up the slides navigator
			initNavigator(slides_el, options);
		}
		
		// initialize the left-right slider
		function initSlider(slides_el) {
			var
				scrollable,
				api,
				nav_el,
				plugin_options = options.plugin;
				
			// set easing
			if (options.transition.easing !== undefined) {
				plugin_options.easing = options.transition.easing;
			}
			// set duration
			if (options.transition.duration !== undefined) {
				plugin_options.speed = options.transition.duration;
			}
			

			scrollable = slides_el.scrollable(plugin_options);

			// seek to 0 to active the "active" tab
			api = slides_el.data('scrollable');
			api.seekTo(options.default_slide === undefined ? 0 : options.default_slide);

			// when the user clicks a slide, send the slideChanged event
			api.onSeek(function(event, offset) {
				slides_el.trigger('slideChanged', offset);
			});

			// if we are using mouseover, listen to the nav tab fader
			nav_el = getNavElement(slides_el);
			nav_el.bind('slideTo', function(evt, new_offset) {
				api.seekTo(new_offset);
				slides_el.trigger('slideChanged', [new_offset]);
			});
			// if (options.tab.handler == 'mouseover') {
			// }
			
		}

		function initFader(slides_el) {
			var
				plugin_options = $.extend({}, options.plugin, options);
				
			plugin_options.nav_el = getNavElement(slides_el);
			plugin_options.default_slide = options.default_slide;

			slides_el.SSFrameFader(plugin_options);

		}

		function initNavigator(slides_el) {
			var
				nav_el = getNavElement(slides_el),
				plugin_options = {};

			plugin_options = $.extend({}, options.tab);
			plugin_options.slides_el = slides_el;
			plugin_options.default_slide = options.default_slide;
			
			if (options.type == 'tab_nav') {
				plugin_options.allow_click = true;
			} else {
				plugin_options.allow_click = false;
			}
			
			
			nav_el.SSNavNavigator(plugin_options);
		}

		function initSliderSizes(slides_el, options) {
			var
				container_el = slides_el.parent('.AWCSSscrollableContainer');
			
			if (options.size.width !== undefined) {
				slides_el.css({'width':options.size.width});

				// only stretch the navigator for tab nav
				if (options.type == 'tab_nav') {
					getNavElement(slides_el).css({'width':options.size.width});
				}
				
				// set the width of each item
				$('.items div', slides_el).css({'width':options.size.width});
				
				
				// add left and right width
				var container_width = parseInt(options.size.width, 10);
				container_width = container_width + container_el.children('.AWCSSSliderLeft').width();
				container_width = container_width + container_el.children('.AWCSSSliderRight').width();
				
				container_el.css({'width':container_width});
			}
			if (options.size.height !== undefined) {
				slides_el.css({'height':options.size.height});
				container_el.css({'height':options.size.height});
			}
		}


		function getNavElement(slides_el) {
			return $('#'+(slides_el.attr('id')).replace('AWCSlides_','AWCSSNav_'));
		}
		
		

		
		//////////////////////////////////////////////////////
		// Image Flow

		function initImageFlow(slides_el) {
			var
				plugin_options = $.extend({}, options, default_options.image_flow, options.plugin);
				
			slides_el.SSImageFlow(plugin_options);
		}
		
	
		//////////////////////////////////////////////////////
		// Slider Nav

		function initSliderNav(slides_el) {
			initTabNav(slides_el);
		}
		
		//////////////////////////////////////////////////////
		// Slider Nav
		
		function initHorizAccordionNav(slides_el) {
			slides_el.SSHorizAccordion(options);
		}
		



		////////////////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////////////////////////////////


		// apply user options
		$.extend(true, options, user_options, (user_options === undefined ? {} : user_options));
		
		// init on document ready
		$(function() {
			var
				slides_el = $(jq_selector);	

			// add a custom easing function
			$.easing.tinyslider = function(x, t, b, c, d) {    
				ts=(t/=d)*t;
				tc=ts*t;

				return b+c*(12.245*tc*ts + -33.5825*ts*ts + 34.58*tc + -17.79*ts + 5.5475*t);
			};
			
			
			// init the tab navigation
			if (options.type == 'tab_nav') {
				initTabNav(slides_el);
			}

			// init the image flow
			if (options.type == 'image_flow') {
				initImageFlow(slides_el);
			}

			// init the tab navigation
			if (options.type == 'slider_nav') {
				initSliderNav(slides_el);
			}

			if (options.type == 'horizontal_accordion_nav') {
				initHorizAccordionNav(slides_el);
			}
		});
	};

})(jQuery);

