// JavaScript Document

(function($){
	
	var opts,canvas;
	var ready = false;
	
    $.fn.wtBgSlider = function(settings){
		opts = jQuery.extend({
			noofimage:5,
			avtiveimg:0,
			oldactive:4,
			dirc:1,
			filename:'images/slides/banner-',
			filetype:'jpg',
			nextelm: 'snavi-right',
			prevelm: 'snavi-left',
			colors: ['c82d2d','ff2e5d','a759e8','26aad1','ffd72c'],  // red, pink, violet, blue and yellow
			qlcolors: ['fa3e3d','ff5e8f','af88fb','2ec6f7','ffe937'],  // red, pink, violet, blue and yellow
			qlbcolors: ['c32b2c','f82d5a','a258e2','25a5cb','f8d12c'],  // red, pink, violet, blue and yellow
			onCompleate:null
		},settings);
		
		canvas = this;
		$.fn.wtBgSlider.initialize();
	};
	
	$.fn.wtBgSlider.initialize = function(){		
		 
		wdf = $(window).width();
		if(wdf<960) wdf=960;
		wd = (wdf-1220)/2;
		
		str = '';
		boxdiv = canvas;
		for(k=0; k <opts.noofimage;k++){
			thum_ul = $('<div id="sliderin-'+k+'" class="sliderin" style="background:url('+opts.filename+''+ (k+1)+'.'+ opts.filetype +')  no-repeat center center; background-position:'+(wd + 2565)+'px -687px; "></div>');
			boxdiv.append(thum_ul);
			boxdiv = thum_ul;
		}		
		thum_ul = $('<div class="sliderinwidth"></div>');
		boxdiv.append(thum_ul);
			
		$('#'+opts.nextelm).click(function(){opts.dirc = 1; that.changeImage();  });
		$('#'+opts.prevelm).click(function(){opts.dirc = -1; that.changeImage();  });
		
		
		this.changeImage();		
	};
	
	$.fn.wtBgSlider.changeImage = function(){

		wdf = $(window).width();
		if(wdf<960) wdf=960;
		wd = (wdf-1220)/2;
		
		if(this.started) return;
		this.started =true;

		that = this;
		//clearTimeout(tstime);
		//setTimeout(this.changeImage.bind(this),5000);
		clearTimeout(this.timer);
		this.timer = setTimeout(function () { that.changeImage(); },6000); 

		
		if(opts.dirc>0){					
			this.changeNext();

			$actdiv = $('#sliderin-'+opts.avtiveimg);
			$olddiv = $('#sliderin-'+opts.oldactive);
		
			canvas.find('.sliderin').stop().css({'background-position': (wd+2565)+'px -687px'});    //2565
			$olddiv.stop().css({'background-position': wd+'px 0px'});
			$olddiv.stop().animate({backgroundPosition: (wd-2565)+'px 687px'},600,function(){
																						   
				$('#toplinks a').stop().animate({'backgroundColor': '#'+opts.qlcolors[opts.avtiveimg],'borderColor': '#'+opts.qlbcolors[opts.avtiveimg]},800);
				
				canvas.animate({'backgroundColor': '#'+opts.colors[opts.avtiveimg]},800,function(){
					$actdiv = $('#sliderin-'+opts.avtiveimg);
					$actdiv.stop().animate({backgroundPosition: wd+'px 0px'},600,function(){ $(this).css({'background-position': 'center 0px'}); that.started=false; });
				});
			});
		}else{			
			
			this.changePrev();
			$actdiv = $('#sliderin-'+opts.avtiveimg);
			$olddiv = $('#sliderin-'+opts.oldactive);
			
			canvas.find('.sliderin').stop().css({'background-position': (wd-2565)+'px 687px'});
			$olddiv.stop().css({'background-position': wd+'px 0px'});
			$olddiv.stop().animate({backgroundPosition: (wd+2565)+'px -687px'},600, function(){	
																						   
				$('#toplinks a').stop().animate({'backgroundColor': '#'+opts.qlcolors[opts.avtiveimg],'borderColor': '#'+opts.qlbcolors[opts.avtiveimg]},800);
				
				canvas.animate({'backgroundColor': '#'+opts.colors[opts.avtiveimg]},800,function(){
					$actdiv = $('#sliderin-'+opts.avtiveimg);																						 
					$actdiv.stop().animate({backgroundPosition: wd+'px 0px'},600,function(){ $(this).css({'background-position': 'center 0px'}); that.started=false; });
				});
			});
		}
		
	};
	
	$.fn.wtBgSlider.changeNext = function(){
		opts.oldactive = opts.avtiveimg;
		opts.avtiveimg++;
		if(opts.avtiveimg>=opts.noofimage){
			opts.avtiveimg = 0;// (opts.noofimage-2);			
		}
		
	};
	
	$.fn.wtBgSlider.changePrev = function(){
		opts.oldactive = opts.avtiveimg;
		opts.avtiveimg--;
		if(opts.avtiveimg<0){
//			opts.avtiveimg = 1;
			opts.avtiveimg = (opts.noofimage-1);			
		}
		
	};
	
	
})(jQuery);
