/**
*
*
* suSlider: Content slider / fade   using the jQuery 
*
* Author: alex wang
* Email: wpsnowwolf@gmail.com
* URL: http://www.brightyoursite.com
* 
*
**/

jQuery.fn.suSlider = function(options) {

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Declare variables and functions
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    var defaults = {
        mode: 'slide',
        speed: 500,
        auto: false,
        pause: 3000,
        select: 10000,
        width: jQuery(this).children('.news_box_head').width(),
        wrapper_class: 'container'
    };
    options = jQuery.extend(defaults, options);
    var jQuerythis = jQuery(this);

    var jQueryparent_width = options.width;
    var is_working = false;
    var child_count = jQuerythis.children('.news_box_head').size();
    var idx = 0;
    var isbreak = false;
    var expauuse = 0;
    var gotoidx = -1;
    function animate_idx(i) {

        is_working = true;
        jQuerythis.animate({ 'left': '-' + jQueryparent_width * i + 'px' }, options.speed, function() {
            idx = i;
            is_working = false;
            jQuery('#slnav li:.active').removeClass('active');
            jQuery('#slnav li:eq(' + idx + ')').addClass('active');
            if (options.auto && !isbreak) {

                idx++;
                if (idx == child_count) {
                    idx = 0;
                }
                clearInterval(jQuery.t);
                jQuery.t = setInterval(function() { animate_idx(idx); }, expauuse == 0 ? options.pause : expauuse);
                expauuse = 0;
            }
        });

    }


    function fade_idx(idx) {
        is_working = true;
        jQuerythis.children('.news_box_head').eq(idx).fadeTo(options.speed, 0, function() { jQuery(this).hide(); });
        if (gotoidx < 0) {
            idx++;
            if (idx == child_count) {
                idx = 0;
            }
        }
        else {
            idx = gotoidx;
        }
        gotoidx = -1;
        jQuery('#slnav li:.active').removeClass('active');
        jQuery('#slnav li:eq(' + idx + ')').addClass('active');
        jQuerythis.children('.news_box_head').eq(idx).show().fadeTo(options.speed, 1, function() {

            is_working = false;


            if (options.auto && !isbreak) {

                clearInterval(jQuery.t);
                jQuery.t = setInterval(function() { fade_idx(idx); }, expauuse == 0 ? options.pause : expauuse);
                expauuse = 0;
            }

        });
    }

    function add_controls() {

        jQuery('#slnav').children().click(function() {
           
            var jQuerykids = jQuery('#slnav li').index(this);
            clearInterval(jQuery.t);
            if (!is_working) {

                if (options.select == 0) {
                    isbreak = true;
                }
                else {
                    expauuse = options.select;
                }

                if (options.mode == 'slide') {

                    animate_idx(jQuerykids);

                }
                else {
                    var jQueryon = jQuery('#slnav li').index(jQuery('#slnav li:.active'));
                    gotoidx = jQuerykids;
                    fade_idx(jQueryon);
                }
            }

            return false;

        });
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Create content wrapper and set CSS
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    jQuerythis.wrap('<div class="' + options.wrapper_class + '"></div>');


    if (options.mode == 'slide') {

        jQuerythis.parent().css({
            'overflow': 'hidden',
            'position': 'relative',
            'width': options.width + 'px'
        });

        jQuerythis.css({
            'width': '999999px',
            'position': 'relative'
        });

        jQuerythis.children('.news_box_head').css({
            'float': 'left',
            'width': jQueryparent_width
        });



    } else if (options.mode == 'fade') {

        jQuerythis.parent().css({
            'overflow': 'hidden',
            'position': 'relative',
            'width': options.width + 'px'
        });

        jQuerythis.children('.news_box_head').css({
            'position': 'absolute',
            'width': jQueryparent_width,
            'listStyle': 'none',
            'opacity': 0,
            'display': 'none'
        });

        jQuerythis.children('.news_box_head:first').css({
            'opacity': 1,
            'display': 'block'
        });

    }

    add_controls();

    if (options.auto) {

        clearInterval(jQuery.t);
        if (options.mode == 'slide') {
            jQuerythis.animate({ 'left': '-0px' }, options.speed, function() {
                jQuery.t = setInterval(function() { animate_idx(1); }, options.speed);
            });
        }
        else {

            jQuery.t = setInterval(function() { fade_idx(0); }, options.speed * 2);

        }

    }

}

















