$(document).ready(function () {
	$('.header li').hover(function(){ //topmenu active
		$(this).toggleClass("active")
	});
	 $('.manbann').cycle({ //manbann slideshow
		fx: 'fade'
	});
	$('.bann li').hover(setActive,setNotActive); //minibann animate
	
	$('.menu li a').hover(function(){
		if(($(this).parent().hasClass("action") && $(this).parent().hasClass("hover")) || !$(this).parent().hasClass("action")){
			$(this).parent().toggleClass("action");
			$(this).parent().addClass("hover");
		}
			
	});
	
	
	$(".phone").mask("+9 (999) 999-9999");

	
	
	$(".table-format-1").each(function(index, obj){
		table = $(obj);
		$("tr:even", table).addClass("gray");
		$("tr:first", table).toggleClass("header");
		 
	});
	
	var rightOuterHeight = $(".rightbann").outerHeight(true);
	var contentOuterHeight = $(".page").outerHeight(true);
	
	if(rightOuterHeight > contentOuterHeight){
		var contentHeight = rightOuterHeight - 25;
		$(".page").height(contentHeight);
	}	
})

function setActive() {
	var currentBG = $(this).css('background-image');
	var activeBG;
	if(currentBG.indexOf('active')==-1) {
	activeBG = currentBG.replace('_off','_on');
//	$(this).css('background-image', activeBG);
	}

}

function setNotActive() {
	if(!$(this).hasClass('current')||$(this).hasClass('download')) {
		var currentBG = $(this).css('background-image');
		var notActiveBG;
		notActiveBG = currentBG.replace('_on','_off');	
	//	$(this).css('background-image', notActiveBG);
	}
}

/*Баннеры*/
banner = {};
banner.params = {
	rotate_timer: 5000,
	effect_timer: 500,
	current: 0
};
banner.timer = null;
banner.animated = false;
banner.prev = null;
banner.current = banner.params.current;
banner.count = 0;
banner.collection = {};
banner.getNext = function(){
	return this.current*1 + 1 >= this.count ? 0 : this.current*1 + 1;
};

banner.showBanner = function(index){
	clearTimeout(this.timer);
	objNext = $(this.collection[index]);
	objCurrent = $(this.collection[this.current]);
	if(banner.animated){
		
	}
	this.prev = this.current
	this.current = index;
	this.animated = true;
	objCurrent.fadeOut(banner.params.effect_timer);
	objNext.fadeIn(banner.params.effect_timer);
	
	$("#selectec-banner").animate({"left": 54*index+"px"}, banner.params.effect_timer);
	this.timer = setTimeout(function(){
		banner.showBanner(banner.getNext());
	}, banner.params.rotate_timer);
}

$(function(){
	banner.count = $(".banner", "#banners").length;
	$(".banner", "#banners").each(function(index, obj){
		banner.collection[index] = obj;
		$(obj).attr("rel", index);

		if(index != banner.params.current){
			$(obj).css({"display": "none"});
		}
	});

	$("#banners-preview img").each(function(index, obj){
		$(this).attr("rel", index);
	});
	$("#banners-preview img").click(function(){
		next = $(this).attr("rel");
		if(next != banner.current)
			banner.showBanner($(this).attr("rel"));
		return false;
	});
	banner.timer = setTimeout(function(){
		banner.showBanner(banner.getNext());
	}, banner.params.rotate_timer);
});
