/* ==================================================

SYSCOM - Promo Slider Gallery - Version 0.2

This plugin is a gallery image slider

Written by Edwin Sandoval <sandovaledwin@hotmail.com>

Changes Log

20 - July - 2010

    * Fixed the bug that let to support images galleries
      with only one item. Version 0.4

14 - June - 2010

    * Was added the support for to manage more than one
      instance of the slider - Version 0.4

    * Was translates the functions to the plugin sintax -
      Version 0.3

10 - June - 2010    

    * Was fixed the bug related with the fadeIn over charge
      when the user made several fast clicks - Version 0.2

09 - June - 2010

    * Was programmed the basic functionality of the slider -
      Version 0.1



================================================== */


$(document).ready( function(){

    (function($) {
        
        $.fn.extend({

            yetAnotherSlider: function( objSettings ) {

                return this.each(function( item, contenedor ){

                    var sliderTimerId = "";

                    $( '#' + contenedor.id + ' div' ).mouseenter(function(){

                        if( $( '#' + contenedor.id + ' ul li' ).size() > 1 ){

                            $( $( '#' + contenedor.id + ' a' )[0] ).fadeIn();

                            $( $( '#' + contenedor.id + ' a' )[1] ).fadeIn();

                        }

                        $( 'p', $( '#' + contenedor.id + ' ul li' ).last() ).show();

                        clearTimeout( sliderTimerId );

                    });

                    $( '#' + contenedor.id + ' div' ).mouseleave(function(){

                        if( $( '#' + contenedor.id + ' ul li' ).size() > 1 ){

                            $( $( '#' + contenedor.id + ' a' )[0] ).fadeOut();

                            $( $( '#' + contenedor.id + ' a' )[1] ).fadeOut();

                        }

                        $( 'p', $( '#' + contenedor.id + ' ul li' ).last() ).hide();

                        sliderTimerId = setTimeout( function(){ sliderLoop(); }, 5000 );

                    });

                    $( $( '#' + contenedor.id + ' a' )[1] ).click(function(){

                        clearTimeout( sliderTimerId );

                        sliderUp();

                        $( 'p', $( '#' + contenedor.id + ' ul li' ).last() ).show();

                        return false;


                    });

                    $( $( '#' + contenedor.id + ' a' )[0] ).click(function(){

                        clearTimeout( sliderTimerId );

                        sliderDown();

                        $( 'p', $( '#' + contenedor.id + ' ul li' ).last() ).show();

                        return false;

                    });

                    var sliderUp = function(){

                        var lastSelected = "";

                        if( $( '#' + contenedor.id + ' ul li' ).size() <= 1 ){

                            return false;

                        }

                        $( '#' + contenedor.id + ' ul li' ).last().fadeOut();

                        lastSelected = "<li>" + $( '#' + contenedor.id + ' ul li' ).last().html() + "</li>";

                        $( '#' + contenedor.id + ' ul li' ).last().prev().fadeIn();

                        $( '#' + contenedor.id + ' ul' ).prepend( lastSelected );

                        $( '#' + contenedor.id + ' ul li' ).last().remove();

                        $( 'p', $( '#' + contenedor.id + ' ul li' ).last() ).hide();

                    }

                    var sliderDown = function(){

                        var lastSelected = "";

                        if( $( '#' + contenedor.id + ' ul li' ).size() <= 1 ){

                            return false;

                        }

                        lastSelected = "<li>" + $( '#' + contenedor.id + ' ul li' ).first().html() + "</li>";

                        $( '#' + contenedor.id + ' ul' ).append( lastSelected );

                        $( '#' + contenedor.id + ' ul li' ).last().fadeIn();

                        $( '#' + contenedor.id + ' ul li' ).first().remove();

                        $( '#' + contenedor.id + ' ul li' ).last().prev().hide();

                    }

                    var sliderLoop = function(){

                        sliderUp();

                        sliderTimerId = setTimeout( function(){ sliderLoop(); }, 5000 );

                    }

                    $( '#' + contenedor.id + ' ul li' ).last().fadeIn();

                    $( $( '#' + contenedor.id + ' a' )[0] ).hide()

                    $( $( '#' + contenedor.id + ' a' )[1] ).hide();

                    if( $( '#' + contenedor.id + ' ul li' ).size() > 1 ){

                        sliderLoop();

                    }                

                });

            }

        });

    })(jQuery);          

    $('.edwinSlider').yetAnotherSlider();


});