﻿/*** @author tanya ***/

function Slider(elem, cols, tag, mode, btLeft, btRight, preLoaded){
    this.cols = cols || 4;
    this.tag = tag || 'a';
    this.mode = mode || 'nocycle';
    this.root = elem;
    this.btLeft = this.root.find(btLeft || '.bt_left');
    this.btRight = this.root.find(btRight || '.bt_right');
    this.logos = this.root.find(this.tag);
	this.preLoaded = this.root.find(preLoaded || '.pre-loaded');
    this.init();
    this.attachEvents();
}

Slider.prototype = {
    init: function(){
        this.logos.hide();
        this.logos.slice(0, this.cols).show();
		if (this.mode!='cycle'){
			this.btLeft.addClass("disabled");	
		}
		if (this.logos.filter(":visible").length==this.logos.length){
			this.btRight.addClass("disabled");	
		}
		this.preLoaded.hide();
    },
    attachEvents: function(){
        var me = this;
        this.btLeft.click(function(){
            return me.onLeft();
        });
        this.btRight.click(function(){
            return me.onRight();
        });
//        this.logos.hover(function(){
//            var newsrc = $(this).find("img").attr("src").replace("/clients-bw/", "/clients/");
//            $(this).find("img").attr("src", newsrc);
//        }, function(){
//            var newsrc = $(this).find("img").attr("src").replace("/clients/", "/clients-bw/");
//            $(this).find("img").attr("src", newsrc);
//        });
    },
    onRight: function(){
		this.btLeft.removeClass("disabled");		
        var last_ind = this.logos.index(this.logos.filter(":visible:last"));
        if ((last_ind + 1) == this.logos.length) {
            if (this.mode == 'cycle') {
                last_ind = -1;
                this.logos.hide();
                this.logos.slice(last_ind + 1, last_ind + this.cols + 1).show();
            }
        }
        else {
            this.logos.hide();
            this.logos.slice(last_ind + 1, last_ind + this.cols + 1).show();
        }
		if (((this.logos.index(this.logos.filter(":visible:last"))+1)==this.logos.length)&&(this.mode!='cycle')){
			this.btRight.addClass("disabled");
		}
    },
    onLeft: function(){			
		this.btRight.removeClass("disabled")
        var first_ind = this.logos.index(this.logos.filter(":visible:first"));
        var rest = this.logos.length % this.cols;
        if (first_ind == 0) {
            if (this.mode == 'cycle') {
                if (rest == 0){
					first_ind = this.logos.length - this.cols;
				}                     
                else{
					first_ind = this.logos.length - rest;
				}                     
                this.logos.hide();
                this.logos.slice(first_ind, first_ind + this.cols).show();
            }
        }
        else {
            first_ind = first_ind - this.cols;
            this.logos.hide();
            this.logos.slice(first_ind, first_ind + this.cols).show();
        }
		if ((this.logos.index(this.logos.filter(":visible:first"))==0)&&(this.mode!='cycle')){
			this.btLeft.addClass("disabled");	
		}
    },
}

$(function(){
    $(".slider").each(function(){
        new Slider($(this), 4, 'a', 'nocycle');
    });
  

});

