/* ******************************************
front-end preparation (html, css, jQuery):
steffen heidemann / dev@steffenheidemann.com
*********************************************
cycle-plugin adapted from the implementation
by Kevin Leary - http://www.kevinleary.net
****************************************** */

(function($){

	$(function(){
		/** Add the next and previous buttons with JavaScript to gracefully degrade */
		var cycle_container = $('#cycle-fullwidth');
		cycle_container.append('<div id="cycle-next"></div><div id="cycle-prev"></div>');
		cycle_start(cycle_container, 0);

		/** Stop automatic cycling on startUp */
		cycle_container.cycle('pause');

		/** Restart the slideshow when someonae resizes the browser to ensure that sliding distance matches the correct viewport */
		$(window).resize(function(){
			var current_slide = cycle_container.find('.slide:visible').index();
			if(window.console&&window.console.log) {
				 console.log('current_slide'+current_slide);
			}
			cycle_container.cycle('destroy');
			new_window_width = $(window).width();
			cycle_container.find('.slide').width(new_window_width);
			cycle_start(cycle_container, current_slide);

			/** Stop automatic cycling on reSize */
			cycle_container.cycle('pause');
		});
	});
	
	/** Cycle configurations */
	function cycle_start(container, index){
		var window_width = $(window).width();
		container.find('.slide').width(window_width);
		if (container.length > 0){

			/** Adding hash to cycle in order to make links available  */
			var index = 0, hash = window.location.hash;
			if (hash) {
				index = /\d+/.exec(hash)[0];
				index = (parseInt(index) || 1) - 1; // slides are zero-based
			}

			container.cycle({
				timeout: 5000,
				speed: 800,
				pager: '#cycle-pager',
				prev:'#prev',
				next:'#next',
				slideExpr: '.slide',
				fx: 'scrollHorz',
				fx: 'fade',
				easeIn: 'swing',
				easeOut: 'easeOutQuint',
				startingSlide: index,
				pagerAnchorBuilder: cycle_paginate,

				/** Removing 'redundant' activeClass (use in j.interface.js)  */
				before: function(){
					$(this).parent().find('div.someClassCurrentSlide').removeClass('someClassCurrentSlide');
				},

				/** Applying hash  / + /  Adding 'redundant' activeClass (use in j.interface.js) */
				after: function(curr,next,opts) {
					window.location.hash = opts.currSlide + 1;
					$(this).addClass('someClassCurrentSlide');
				}

			});
		}
	}

	function cycle_paginate(ind, el) {
		return '<a id="slideNum-'+ind+'" href="#slide-'+ind+'"><span>'+ind+'</span></a>';
	}

})(jQuery);
