/*
*	=================
*	  DefaultSlider
*	=================
*
*	Version: 1.0.1
*	Date: 21th November, 2011 - 16:44 GMT+1
*
*	(c)2010 Jelle van der Coelen.
*	http://www.jellevandercoelen.com | jelle@jellevandercoelen.com
*
*	Distributed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands
*	http://creativecommons.org/licenses/by-nc-sa/3.0/nl/deed.en_US
*
*	-------------------------------------------------------
*	This JavaScript function uses:	jQuery 1.7.0
*	Copyright 2011, John Resig
* 	Dual licensed under the MIT or GPL Version 2 licenses.
* 	http://jquery.org/license
*	-------------------------------------------------------
*/
function DefaultSlider(options)
{
    /*
     */
    var defaults = 
    {
        callback: function(){},
        element : '.default-slider',
        delay   : 5000,
        item    : '.ds-item',
        speed   : 1000,
        type    : 'slide'
    };

    this.options = $.extend(defaults, options);
    
    /*
     */
    this.currentItem = 0;
    
    /*
     */
    this.itemCount = 0;
    
    /*
     */
    this.itemWidth = 0;
    
    /*
     */
    this.timer;
    
    /**
     *
     */
    this.init = function()
    {
        this.itemCount = $(this.options.element).find(this.options.item).length;
        this.itemWidth = $(this.options.element).find(this.options.item+':first').width();
        
        this.currentItem = 0;
        
        if(this.options.type == 'fade')
        {
            $(this.options.item).css({position: 'absolute', 'float': 'none'}).hide();
            $(this.options.item+':first').show();
        };
        
        this.queue();
    };
    
    /**
     *
     */
    this.next = function(){ this.change(); }
    this.prev = function(){ this.change((this.currentItem - 1)); }
    
    /**
     *
     */
    this.queue = function()
    {
        var _self = this;
        
        this.timer = setTimeout(function(){ _self.change(); }, this.options.delay);
    };
    
    /**
     *
     */
    this.change = function(s)
    {
        clearTimeout(this.timer);
        
        if(typeof s != 'undefined')
        {
            if(s == -1)
                s = (this.itemCount - 1);
                
            this.currentItem = s;
        }
        else
            this.currentItem++;
        
        if(this.currentItem >= this.itemCount)
            this.currentItem = 0;
        
        var _self = this;
        
        // slider
        if(_self.options.type == 'slide')
        {
            $(_self.options.element+' > :first').animate({marginLeft: (0-(_self.itemWidth * _self.currentItem))}, _self.options.speed, function()
            {
                if(typeof _self.options.callback == 'function')
                    _self.options.callback(_self.currentItem, _self.options);
            });
        }
        
        // fade
        else if(_self.options.type == 'fade')
        {
            $(_self.options.item).fadeOut(_self.options.speed);
            $(_self.options.item+':eq('+_self.currentItem+')').fadeIn(_self.options.speed, function()
            {
                if(typeof _self.options.callback == 'function')
                    _self.options.callback(_self.currentItem, _self.options);
            });
        };
        
        this.queue();
    };
};

var dslider;
var subdslider;
$(window).load(function()
{
    $('.slide img').css({position: 'absolute', width: $('#cb_viewport').width()})

    dslider = new DefaultSlider
    ({
        callback: function(s, o)
                  {
                      $(o.element+' .blok').removeClass('active');
                      $(o.element+' .blok:eq('+s+')').addClass('active');
                  },
        element : '#main-slider',
        item    : '.slide',
        type    : 'fade',
        delay   : 10000
    });
   
    dslider.init();
    
    subdslider = new DefaultSlider
    ({
        element : '#sub-slider',
        item    : '.sub-slide',
        type    : 'fade',
        delay   : 7000
    });
    
    subdslider.init();
});
