/**
 * Fancy Slideshow
 *
 * @author	daniel.kocsan@dream.hu
 * @version	1.2
 */

//	----------------------------------------------------------------------------
//	SLIDESHOW HANDLER
//	----------------------------------------------------------------------------
	var slideshow = {

		defaults: {
			container_class: '.slideshowsb_container',
      type: ''
		},

		items: [],

	//	------------------------------------------------------------------------
		add: function( container ){

            var slideshow_element = null;

            switch( this.defaults.type ){
                case 'horizontal':
                    slideshow_element = new class_slideshow_element_horizontal();
                break;
                default:
                    slideshow_element = new class_slideshow_element_basic();
            }

			slideshow_element.init( container, this );
			this.items.push( slideshow_element );
		},

	//	------------------------------------------------------------------------
		init: function( options ){

			var obj = this;

      $.extend( this.defaults, options );

			$( this.defaults.container_class ).each(

				function(){

          var content = $( this ).html();
          var container = $( this ).html( '<div class="container">' + content + '</div>' );
          delete content;

					obj.add( $( this ).children() );
				}
			);
		}
	}

//	----------------------------------------------------------------------------
//	PARENT ELEMENT
//	----------------------------------------------------------------------------
	var class_slideshow_element = function(){}

	class_slideshow_element.prototype.container = null;
	class_slideshow_element.prototype.handler = null;
	class_slideshow_element.prototype.items = [];
	class_slideshow_element.prototype.position = 1;
	class_slideshow_element.prototype.itemHeight = 0;
	class_slideshow_element.prototype.itemWidth = 0;

	class_slideshow_element.prototype.init = function( container, handler ){

		this.container = container;
		this.handler = handler;

    $.extend( this.defaults, handler.defaults.element );

		this.setup();
	}

	class_slideshow_element.prototype.reset = function(){}
	class_slideshow_element.prototype.setup = function(){}
	class_slideshow_element.prototype.setupItem = function(){}
	class_slideshow_element.prototype.setupItems = function(){

		var obj = this;
		obj.items = [];

		this.itemHeight = $( this.links.items ).children( 'li:first' ).height();
		this.itemWidth = $( this.links.items ).children( 'li:first' ).width();

		$( this.links.items ).css(
			{
				position: 'relative',
				height: obj.itemHeight + 'px'
			}
		);

		$( this.links.items ).children( 'li' ).each(
			function(){
				obj.items.push( this );
				obj.setupItem( this )
			}
		);
	}

	class_slideshow_element.prototype.next = function(){}

	class_slideshow_element.prototype.setPosition = function( position ){}

//	----------------------------------------------------------------------------
//	ELEMENT BASIC
//	----------------------------------------------------------------------------
	var class_slideshow_element_basic = function(){}
	class_slideshow_element_basic.prototype = new class_slideshow_element();

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.links = {
		items: null,
		thumbnails: null,
		controlBar: null,
		next: null,
		prev: null,
		pagerHolder: null,
		animationProgress: false,
		pagers: [],
    pointer: null
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.defaults = {
		zindex_base: 1000,
    automate: true,
		modules_enabled: {
			controlBar: true,
			pagers: true,
			thumbnails: false,
			automate: true
		},
		speed: {
			animation: 600,
			automate: 5000
		},
    direction_forward: true,
    timeoutID: null
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setup = function(){


		$( this.container ).css(
			{
				position: 'relative'
			}
		);

		this.links.items = $( this.container ).children( 'ul.items' );
		this.links.thumbnails = $( this.container ).children( 'ul.thumbnails' );
		this.setupItems();

		this.initModules();

		this.setPosition( this.position, this.position );
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.initModules = function(){

    if( this.defaults.modules_enabled.controlBar ){
			var controlBar = new class_slideshow_plugin_controlBar();
			controlBar.init( this );
		}

		if( this.defaults.modules_enabled.pagers ){
			var pager = new class_slideshow_plugin_pager();
			pager.init( this );
		}

		if( this.defaults.modules_enabled.thumbnails ){
			this.thumbnail = new class_slideshow_plugin_thumbnail();
			this.thumbnail.init( this );
		}

		if( this.defaults.modules_enabled.automate )

      this.setAnimation();
    }

//	----------------------------------------------------------------------------
    class_slideshow_element_basic.prototype.setAnimation = function(){

        this.timeoutID = window.setTimeout( this.automate, this.defaults.speed.automate );
    }

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.automate = function(){

    var obj = slideshow.items[0];

		if( obj.defaults.automate ){
      if( obj.defaults.direction_forward )
          obj.page( obj, obj.position + 1 );
      else
          obj.page( obj, obj.position - 1 );
		}
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.reset = function(){

		var obj = this;

		$( this.links.items ).children( 'li' ).each(
			function(){
				obj.setupItem( this );
			}
		);
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setupItem = function( item ){

		$( item )
		.css(
			{
				position: 'absolute',
				top: '0px'
			}
		)
		.hide();
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setPosition = function( current_position, target_position ){

		this.reset();

		this.showItem( current_position, this.defaults.zindex_base );
		this.showItem( target_position, this.defaults.zindex_base - 1 );

		this.position = target_position;

		$( this.links.pagerHolder ).children( 'li' ).children( 'a' ).attr( 'class', '' );

		$( this.links.pagers[ this.position - 1 ] ).children( 'a' ).attr( 'class', 'active' );

	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.showItem = function( position, zindex ){

		var elementIndex = position - 1;

		$( this.items[ elementIndex ] )
		.css(
			{
				'top':  0 + 'px',
				'left':  0 + 'px',
				'z-index':  zindex
			}
		)
		.fadeTo( 0, 1 )
		.show();
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.stepToPage = function( obj, target_position ){

		this.page(
			obj,
			target_position
		);
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.prev = function( obj ){

    window.clearTimeout( obj.timeoutID );

    if( !obj.animationProgress )
		this.page(
			obj,
			obj.position - 1
		);
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.next = function( obj ){

    window.clearTimeout( obj.timeoutID );

    if( !obj.animationProgress )
		this.page(
			obj,
			obj.position + 1
		);
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.page = function( obj, target_position ){

		var current_position = obj.position;

		if( target_position < 1 )
			target_position = obj.items.length;

		if( target_position > obj.items.length )
			target_position = 1;

		if( obj.position != target_position ){

			obj.setPosition( current_position, target_position );

			$( obj.items[ current_position - 1 ] ).animate(
				 {
					// left: -1 * obj.itemWidth + 'px',
					opacity: 0
				 },
				obj.defaults.speed.animation,
				'swing',
				function(){
                  $( this ).hide();
                  obj.animationProgress = false;
                  obj.setAnimation();
                }
			);
		}
	}

//	----------------------------------------------------------------------------
//	ELEMENT HORIZONTAL
//	----------------------------------------------------------------------------
	var class_slideshow_element_horizontal = function(){}
	class_slideshow_element_horizontal.prototype = new class_slideshow_element_basic();

//	----------------------------------------------------------------------------
	class_slideshow_element_horizontal.prototype.setup = function(){

		this.links.items = $( this.container ).children( 'ul.items' );
		this.links.thumbnails = $( this.container ).children( 'ul.thumbnails' );

        var first = $( this.links.items ).children( 'li:first' ).clone();
        var last = $( this.links.items ).children( 'li:last' ).clone();
        $( this.links.items ).prepend( last );
        $( this.links.items ).append( first );
        delete first;
        delete last;
        this.setupItems();
        this.position++;

        $( this.container ).css(
            {
                'width': this.itemWidth + 'px',
                'overflow' : 'hidden',
                'position' : 'relative',
                'margin': '0px auto'
            }
        );

        $( this.links.items ).css(
            {
                'width': (this.items.length + 1) * this.itemWidth + 'px'
            }
        );

		this.initModules();

		this.setPosition( this.position, this.position );
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_horizontal.prototype.setPosition = function( current_position, target_position ){

    $( this.links.items ).css(
        {
            'left': -1 * (current_position-1) * this.itemWidth + 'px'
        }
    );
		this.position = target_position;

    if( this.defaults.modules_enabled.pointer )
    this.pointer.step( target_position - 1 );
    if( this.defaults.modules_enabled.thumbnails ){

      if( target_position > (this.items.length-1) ){
            target_position = 2;
      }

      this.thumbnail.step( target_position - 2 );
    }
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_horizontal.prototype.setupItem = function( item ){

		$( item )
		.css(
			{
				'float': 'left',
				'width': this.itemWidth + 'px'
			}
		);
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_horizontal.prototype.page = function( obj, target_position ){

    var current_position = obj.position;

		if( target_position < 2 ){
			current_position = obj.items.length;
            target_position = obj.items.length-1;
        }

		if( target_position > obj.items.length ){

            current_position = 2;
            target_position = 3;
        }

		if( obj.position != target_position ){

			obj.setPosition( current_position, target_position );
            obj.animationProgress = true;
            $( obj.links.items ).fadeTo( 0, 1 );

            if( target_position < current_position )
            $( obj.links.items ).animate( {'left': '-=60px'}, 100 );
            else
            $( obj.links.items ).animate( {'left': '+=60px'}, 100 );

            $( obj.links.items ).animate(
                {
                    'left': -1 * (target_position) * this.itemWidth + this.itemWidth + 'px',
                    'opacity': 1
                },
                obj.defaults.speed.animation,
                'swing',
                function(){
                    obj.animationProgress = false;
                    //obj.setAnimation();
                }
            );
        }
	}


//	----------------------------------------------------------------------------
//	SLIDESHOW PLUGIN PARENT
//	----------------------------------------------------------------------------
	var class_slideshow_plugin = function(){}
	class_slideshow_plugin.prototype.slideshow_ref = null;

	class_slideshow_plugin.prototype.init = function( slideshow_ref ){

		this.slideshow_ref = slideshow_ref;
		this.create();
		this.setup();
		this.addEvents();
	}

//	----------------------------------------------------------------------------
//	SLIDESHOW CONTROLBAR PLUGIN
//	----------------------------------------------------------------------------
	var class_slideshow_plugin_controlBar = function(){}
	class_slideshow_plugin_controlBar.prototype = new class_slideshow_plugin();

	class_slideshow_plugin_controlBar.prototype.create = function(){

    var slideshow_ref = this.slideshow_ref;

		$( slideshow_ref.container ).append(
			slideshow_ref.links.controlBar = $('<div class="slideshow_contolbar"></div>')
		);

		$( slideshow_ref.container ).append(
			slideshow_ref.links.next = $('<div class="slideshow_next"><a href="#">Next</a></div>')
		);
		$( slideshow_ref.container ).append(
			slideshow_ref.links.prev = $('<div class="slideshow_prev"><a href="#">Prev</a></div>')
		);
	}

	class_slideshow_plugin_controlBar.prototype.setup = function(){

		var slideshow_ref = this.slideshow_ref;

		$( slideshow_ref.links.next ).css(
			{
				'z-index': slideshow_ref.defaults.zindex_base + 1
			}
		);

		$( slideshow_ref.links.prev ).css(
			{
				'z-index': slideshow_ref.defaults.zindex_base + 1
			}
		);
	}


	class_slideshow_plugin_controlBar.prototype.addEvents = function(){

		var slideshow_ref = this.slideshow_ref;

		$( slideshow_ref.links.next ).click(
			function(){
				slideshow_ref.next( slideshow_ref );
				return false;
			}
		);

		$(  slideshow_ref.links.prev ).click(
			function(){
				slideshow_ref.prev( slideshow_ref );
				return false;
			}
		);
	}


//	----------------------------------------------------------------------------
//	SLIDESHOW PAGER PLUGIN
//	----------------------------------------------------------------------------
	var class_slideshow_plugin_pager = function(){}
	class_slideshow_plugin_pager.prototype = new class_slideshow_plugin();

	class_slideshow_plugin_pager.prototype.create = function(){

		var slideshow_ref = this.slideshow_ref;

		$( slideshow_ref.container ).append(
			slideshow_ref.links.pagerHolder = $('<ul class="slideshow_pagers"></ul>')
		);
	}

	class_slideshow_plugin_pager.prototype.setup = function(){

		var slideshow_ref = this.slideshow_ref;

		$( slideshow_ref.links.pagerHolder ).css(
			{
				'z-index': slideshow_ref.defaults.zindex_base + 1
			}
		);
	}


	class_slideshow_plugin_pager.prototype.addEvents = function(){

		var slideshow_ref = this.slideshow_ref;

		for( i = 0; i < slideshow_ref.items.length; i++ ){

			$( slideshow_ref.links.pagerHolder ).append(
				slideshow_ref.links.pagers[ i ] = $('<li><a href="#">' + (i + 1) + '</a></li>')
			);

			$( slideshow_ref.links.pagers[ i ] ).bind(
				'click',
				{position: i+1},
				function( event ){
					slideshow_ref.stepToPage( slideshow_ref, event.data.position );
					return false;
				}
			);
		}
	}



//	----------------------------------------------------------------------------
//	SLIDESHOW THUMBNAIL PLUGIN
//	----------------------------------------------------------------------------
	var class_slideshow_plugin_thumbnail = function(){}
	class_slideshow_plugin_thumbnail.prototype = new class_slideshow_plugin();

	class_slideshow_plugin_thumbnail.prototype.create = function(){}

	class_slideshow_plugin_thumbnail.prototype.setup = function(){

		var slideshow_ref = this.slideshow_ref;
		var obj = this;


    this.elementCount = 0;

		$( slideshow_ref.links.thumbnails ).children( 'li' ).children( 'img' ).each(
			function( index, element ){
				obj.setupItem( this, index );
        obj.elementCount++;
			}
		);

    this.parentWidth = $(this.slideshow_ref.container).width();
    this.elementWidth = $( slideshow_ref.links.thumbnails ).children( 'li:first' ).children().width();
    this.containerWidth = this.elementWidth * this.elementCount;
    $( slideshow_ref.links.thumbnails ).css( {width: this.containerWidth + 'px'} );
	}

	class_slideshow_plugin_thumbnail.prototype.setupItem = function( item, index ){
		
    var obj = this;

    $( item ).wrap( '<a href="#" />' );

    $( item ).parent().bind(
      'click',
      {position: index+2},
      function( event ){
        obj.slideshow_ref.stepToPage( obj.slideshow_ref, event.data.position );
        return false;
			}
    );
	}

	class_slideshow_plugin_thumbnail.prototype.step = function( index ){

    $( this.slideshow_ref.links.thumbnails ).children( 'li' ).removeClass( 'current' );
    $( this.slideshow_ref.links.thumbnails ).children( 'li:eq('+ index +')' ).addClass( 'current' );

    if( (index+2) * (this.elementWidth) > this.parentWidth && index != (this.elementCount-1) ){
      var left = this.parentWidth - (index+2) * (this.elementWidth);
      $( this.slideshow_ref.links.thumbnails ).animate(
        {
          marginLeft: left + 'px'
        },
        300,
        function(){
          
        }
      );
    }
    if( index < 2 ){
      $( this.slideshow_ref.links.thumbnails ).animate(
        {
          marginLeft: '0px'
        },
        300,
        function(){

        }
      );
    }
    if( index >= (this.elementCount-1) ){
      left = -1 * ( this.containerWidth - this.parentWidth );
      $( this.slideshow_ref.links.thumbnails ).animate(
        {
          marginLeft: left + 'px'
        },
        300,
        function(){

        }
      );
    }
  }

	class_slideshow_plugin_thumbnail.prototype.addEvents = function(){

		var slideshow_ref = this.slideshow_ref;

	}

