var slideNum = 0;
var padding = 30;
var firstSlide = 1;
var running = false;
var totalHeight = 0;
var discountsCount = 0;
var contentHeight = 0;
var totalH = 0;
var subscribeOpened = false;

var leftSliderTop = -1;
var loops = 0;
var slideNumber = 1;
var currentSlideHeight = 0;


function imageColorize(){
	jQuery(this).css('margin-left', '30px');
	jQuery(this).animate({'opacity' : 1.0}, 300);
	
}
function imageGrayscale(){
	jQuery(this).animate({'opacity' : 0.0}, 300);
}
function runLeftSliderRun(){
	mt = jQuery('#mycarousel').css("margin-top");
	jQuery('#mycarousel').css("margin-top", parseInt(mt) - 1);
	mt = jQuery('#mycarousel').css("margin-top");
	
	flh = parseInt(jQuery('#mycarousel li:first').css('height')) + parseInt(jQuery('#mycarousel li:first').css('margin-bottom')) +	parseInt(jQuery('#mycarousel li:first').css('margin-top'));
	
	if((parseInt(mt) + flh) == 0){
		jQuery('#mycarousel').append(jQuery('#mycarousel li:first'));
		jQuery('#mycarousel').css('margin-top', 0);
	}
}

function changeSlide(n){
	jQuery(".discounts .slides div").each(function(){
		var cl = jQuery(this).attr("class");
		var num = parseInt(cl.substr(5));
		var next = num + n;
		if (next > discountsCount) next = 1;
		if (next < 1) next = discountsCount;	
		jQuery(this).removeClass("slide" + num);
		jQuery(this).addClass("slide" + next);
		if (next > 5) jQuery(this).hide(); else jQuery(this).show();
	});
}
function showCloud(event){
	jQuery(this).find(".cloudAbs").show();
}
function hideCloud(event){
	jQuery(this).find(".cloudAbs").hide();
}
function tdOver(event){
	jQuery(this).find('td').addClass('hover');
}
function tdOut(event){
	jQuery(this).find('td').removeClass('hover');
}
jQuery(document).ready(function(){
	jQuery('.leftSlider .slides img').css('opacity', 0.0);
	jQuery('.leftSlider .slides img').hover(imageColorize, imageGrayscale);

	setInterval(runLeftSliderRun, 30);

	jQuery('table.styled tr').hover(tdOver, tdOut);

	discountsCount = jQuery(".discounts .slides > div").size();
	jQuery(".discounts .slides div").each(function(){
		var cl = jQuery(this).attr("class");
		var num = parseInt(cl.substr(5));
		if (num > 5) jQuery(this).hide(); else jQuery(this).show();
	});

	var top = 0;
	jQuery(".arrowLeft").click(function(){
		changeSlide(1);
	});
	jQuery(".arrowRight").click(function(){
		changeSlide(-1);
	});
	jQuery(".slider .slides > div").each(function(){
		totalHeight += jQuery(this).height() + padding * 2;
	});
	
	jQuery(".arrowBottom").click(function(){		
		if (!running){					
			var h = jQuery(".slider .slides div:last").height();
			top = parseInt(jQuery(".slider .slides").css("top"));
			if (top > (contentHeight - totalHeight)){
				slideNum++;

				h = jQuery(".slider .slides div:nth-child(" + slideNum + ")").height();
				running = true;
				jQuery(".slider .slides").animate({'top': (top - h - padding * 2)}, 800, function(){running = false;});

			}
		}
	});
	jQuery(".arrowTop").click(function(){
		if (!running){
			var h = jQuery(".slider .slides div:nth-child(" + slideNum + ")").height();
			top = parseInt(jQuery(".slider .slides").css("top"));	
			if (top < -h){
				
				running = true;
				jQuery(".slider .slides").animate({'top': (top + h + padding * 2)}, 800, function(){running = false});						
				slideNum--;
			}					
		}
	});
	
	jQuery(".hasEvent").hover(showCloud, hideCloud);
	jQuery(".hasEvent").each(function(){
		var hght = jQuery(this).find(".cloudAbs").height();
		jQuery(this).find(".cloudAbs").css('top', - hght - 23);
	});
	if (firstSlide > 1){
		totalH = 0;
		for (i = 0; i < firstSlide; i++){
			h = jQuery(".slider .slides div:nth-child(" + i + ")").height();
			totalH = totalH + h;
		}
		slideNum = firstSlide - 1;
		jQuery(".slider .slides").css('top', (top - totalH - padding * 2 * (firstSlide - 1)));
	}


	jQuery('.showSubscribe').click(function(){
		if (subscribeOpened){
			jQuery('.subscribe').slideUp(200, function(){subscribeOpened = false});
			jQuery(".slider .sliderRel").height(contentHeight);
			jQuery(".slider .slides").height(contentHeight);
		} else {
			jQuery(".slider .sliderRel").height(contentHeight - 50);
			jQuery(".slider .slides").height(contentHeight - 50);
			jQuery('.subscribe').slideDown(200, function(){subscribeOpened = true});

		}

	});
});

jQuery(window).load(function(){
	contentHeight = jQuery("#center").height()  - 55;
	if (contentHeight < 350) contentHeight = 350;
	jQuery(".slider .sliderRel").height(contentHeight);
	jQuery(".slider .slides").height(contentHeight);
	jQuery(".leftSlider .sliderRel").height(contentHeight);
	jQuery(".leftSlider .slides").height(contentHeight);
});
