/*
 * cycle.js 1.1
 * By Komunikado (http://komunikado.dk)
 * Copyright 2011
 */
(function( $ ){
$.fn.cycle = function ( options ) {    
	return this.each(function () {
        
		var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
			t = null, s = null;
			
		var settings = {
            'singleWidth' 			: $single.outerWidth(),
            'visible' 				: Math.ceil($wrapper.innerWidth() / $single.outerWidth()),
			'pages' 				: Math.ceil($items.length),
			'itemsinside' 			: 1,
			'bigwidth' 				: 261,
			'bigheight' 			: 362,
			'smallwidth' 			: 120,
            'currentPage' 			: 1,
			'autoplay' 				: false,
			'autoRestart' 			: true,
			'interval' 				: 5000,
			'restarttime' 			: 15000,
			'clearAutoplayonClick' 	: true
		};
		
		if ( options ) { 
			$.extend( settings, options );
		}
		
		settings['smallheight'] = Math.round((settings['smallwidth']/settings['bigwidth'])*settings['bigheight']),
		settings['marginTop'] = Math.round(($single.outerHeight()-settings['smallheight'])/2);

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- settings['visible']).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, settings['visible']).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item and scroll to desired active item
		$wrapper.scrollLeft(settings['singleWidth'] * settings['visible']);
        if( settings['startAt'] > 0 ) {
			var goTo = settings['pages'] > settings['startAt'] ? settings['startAt'] :  settings['startAt']-(settings['pages']+1);
			gotoPage(goTo);
		}
		switch(settings['animType']) {
			default:
			case 'choose':
        
			break;
		}
		
        // 4. paging function
        function gotoPage(page) {
        	page = page*1;
            var dir = page < settings['currentPage'] ? -1 : 1,
                n = Math.abs(settings['currentPage'] - page),
                left = settings['singleWidth'] * dir * n;
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(settings['singleWidth'] * (settings['visible'] + settings['pages'] - 1 ) );
                    page = settings['pages'];
                } else if (page > settings['pages']) {
                    $wrapper.scrollLeft(settings['singleWidth'] * settings['visible']);
                    // reset back to start position
                    page = 1;
                } 

                settings['currentPage'] = page;
            });
			switch(settings['animType']) {
				default:
				case 'choose':
					var go = page > 5 ? 1 : ( page < 1 ? 5 : page ); 
					$("#start-menu a.active").removeClass('active');
					$("#start-menu a[href='#"+ go +"']").addClass('active');
				break;
			}
            return false;
        }
		function globalGotoPage( page ) {
			gotoPage( page );
		}
		window.gotoPage = globalGotoPage;
		
        // 5. Bind to the forward and back buttons
        $('a.nav_left', this).click(function () {
			if( settings['clearAutoplayonClick'] ) {
				clearInterval(t);
				if( settings['autoRestart'] ) {
					clearTimeout(s);
					s = setTimeout(autoPlay, settings['restarttime']);
				}
			}
            return gotoPage(settings['currentPage'] - 1);
        });
        $('a.nav_right', this).click(function () {
			if( settings['clearAutoplayonClick'] ) {
				clearInterval(t);
				if( settings['autoRestart'] ) {
					clearTimeout(s);
					s = setTimeout(autoPlay, settings['restarttime']);
				}
			}
            return gotoPage(settings['currentPage'] + 1);
        });
		
		// 6. Autoplay
		function autoPlay() {
			t = setInterval(function() {
				if( $(".b-pop_up:visible").length < 1 ) {
					return gotoPage(settings['currentPage'] + 1);
				}
			}, settings['interval']);
		}
		
		if( settings['autoplay'] ) {
			autoPlay();
		}
		
    });  
};
})( jQuery );
