﻿// массив фоновых изображений, 
var bgImages = new Array();
var curImage = -1;

bgImages[0] = ["/img/show_1.jpg","Товары для спорта"];
bgImages[1] = ["/img/show_2.jpg","Красота и здоровье"];
bgImages[2] = ["/img/show_3.jpg","Рыбалка"];
bgImages[3] = ["/img/show_4.jpg","Товары для детей"];
bgImages[4] = ["/img/show_5.jpg","Обогреватели"];
bgImages[5] = ["/img/show_6.jpg","Товары для дачи, сада и огорода"];
	
$(document).ready(function(){

	// инициализация	
	cycleBg();
	$("#showcaseBar").css("width",$("#showcase").width());
	$("#showcaseImage").css("width",$("#showcase").width()); 
	  
	$.timer(5000, function (timer) {
		cycleBg() 
		timer.stop();
		});	
		
	var img = new Image();
	
	$(img).load(function () 
	{		
		$("#showcaseImage").fadeOut(function()
				{								
					$("#showcaseImage").fadeIn();
					$("#showcaseImage").css("background-image", "url("+bgImages[curImage][0]+")");
					$("#showcaseImage").css("background-repeat", "no-repeat");
					if (curImage+1 == bgImages.length) {curImage = -1;}				
					$.timer(5000, function (timer) {
						cycleBg() 
						timer.stop();
						});					
				}
			);
    });
	
	$(window).resize(function(){
	  $("#showcaseBar").css("width",$("#showcase").width());
	  $("#showcaseImage").css("width",$("#showcase").width()); 
	});
	
	function cycleBg() {		
		curImage++;	
		
		$("#showcaseNumbers li").each(function(i){
			if (i != curImage) {$(this).removeClass("active")} else {$(this).addClass("active")};
			});		
			
		$("#showcaseCaption").html(bgImages[curImage][1]);
		$(img).attr('src', bgImages[curImage][0]);

		}
});



/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
  jQuery.timer = function (interval, callback)
 {
	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };

