var startInd = 0;
var current_special_offer;	
var flag_show_more = false;
var ctime;	

function show_next_special_offer() {

	new_id = parseInt(current_special_offer.attr("id")) + 1;
	if(new_id > special_offers_count - 1) 
		new_id = 0;
	if(new_id >= 4 && special_offers_count > 4) 
		flag_show_more = true;
	var obj = $("#" + new_id);
	show_big_image(obj);
	if(flag_show_more) 
		show_more_down();	
	ctime = setTimeout("show_next_special_offer()", timeout);
	return true;
}	

function show_big_image(obj) {

	var largePath = obj.attr("href");
	var largeAlt = obj.attr("title");
	var id = parseInt(obj.attr("id"));

	$("#largeImgLink").attr({ href: largePath });
	$("#largeImg").attr({ src: largePath, alt: largeAlt, title: largeAlt });
	
	$(".special_offer").removeClass("act");
	obj.find("span").addClass("act");

	current_special_offer = obj;
	
	return true;
}	

function show_more_down() {

	if (startInd >= special_offers_count - 1)
		startInd = 0;
	else
		startInd++;
	var j = startInd;
	
	$(".thumbnails").hide();
	for (i = 0; i < 4; i++)	{
		if (i == 0) 	{
			$("#thumbnails_" + j).prependTo(".scrollable");
			var el = $("#thumbnails_" + j);
		}
		else {
			$("#thumbnails_" + j).insertAfter(el);
			el = $("#thumbnails_" + j);
		}
		$("#thumbnails_" + j).show();
		j++;
		if (j >= special_offers_count)
			j = 0;
	}
	
	return true;
}

function show_more_up() {

	if (startInd == 0)
		startInd = special_offers_count - 1;
	else
		startInd--;
	var j = startInd;
	
	$(".thumbnails").hide();
	for (i = 0; i < 4; i++) {
		if (i == 0)	{
			$("#thumbnails_" + j).prependTo(".scrollable");
			var el = $("#thumbnails_" + j);
		}
		else {
			$("#thumbnails_" + j).insertAfter(el);
			el = $("#thumbnails_" + j);
		}
		$("#thumbnails_" + j).show();
		j++;
		if (j >= special_offers_count)
			j = 0;
	}
	
	return true;
}

function start_timer() {
	if(special_offers_count > 1)
		ctime = setTimeout("show_next_special_offer()", timeout);
}

$(document).ready(function(){	

	start_timer();
	function preload(images) {
		if (typeof document.body == "undefined") return;
		try {
			var div = document.createElement("div");
			var s = div.style;
			s.position = "absolute";
			s.top = s.left = 0;
			s.visibility = "hidden";
			document.body.appendChild(div);
			div.innerHTML = "<img src=\"" + images.join("\" /><img src=\"") + "\" />";
			var lastImg = div.lastChild;
			lastImg.onload = function() { document.body.removeChild(document.body.lastChild); };
		} catch(e) {
			// Error. Do nothing.
		}
	}

	if(special_offers_count > 1)	
		preload(preloadImages);
		
	current_special_offer = $("#0");
	
	$("#area_info").click(function(){
		window.location = $("#largeImgLink").attr("href");
		return false;
	});					
	$(".thumbs").mouseover(function(){
		show_big_image($(this));
		clearTimeout(ctime);
		return false;
	});	

	if (special_offers_count > 4) {
		$(".thumbnails").hide();
		$(".scrollable").show();
		for (i = 0; i < 4; i++)	{
			$("#thumbnails_" + i).show();
		}
	}
	else {
		$("#item_more_down").hide();
		$("#item_more_up").hide();
		$(".scrollable").show();
	}
	
	$("#item_more_down").click(function(){
		show_more_down();
		return false;
	});	

	$("#item_more_up").click(function(){
		show_more_up();
		return false;
	});	

	$("a.thumbs, a#largeImgLink").fancybox({						
		"padding" : 5, // отступ контента от краев окна
		"speedIn" : 500,	// скорость анимации в мс при увеличении фото (по умолчанию 0)
		"speedOut" : 500,	// скорость анимации в мс при уменьшении фото (по умолчанию 0)
		"changeSpeed" : 500, // скорость анимации в мс при смене фото (по умолчанию 0)
		"overlayOpacity" : 0.8,	 // Прозрачность затенения 	(0.3 по умолчанию)	
		"cyclic" : true, 
		"centerOnScroll" : true
	});	
	$(".iframe").fancybox({
		"width" : 750,
		"height" : 660,
		"overlayOpacity" : 0.8	 // Прозрачность затенения 	(0.3 по умолчанию)		
	});		
});
