var slideCount = 7;
var acordeonWidth = 980;
var acordeonHeight = 306;
var inactiveSlideWidth = 74;
var activeSlideWidth = 539;
var activeSlide;
var allPanels;
var previousSlide = 0;
var accordeonSpeed = 500;

function initAccordeon(ACCID){
	var accID = $(ACCID);
	allPanels = accID.children('.slidePanel');
	activeSlide = accID.children('.active');
	activeSlide.children('.year').css({left:-70});
	activeSlide.children('.blurWrapper').hide();
	allPanels.width(inactiveSlideWidth);
	var i = 0;
	var topOffset = acordeonHeight-50;
	step = (acordeonHeight-50)/slideCount;
	allPanels.each(function(){
		$(this).children('.year').children('.opacityWrapper').css({opacity:0.6});
		$(this).children('.year').css({top:topOffset-step*i});
		$(this).children('.description').children('.opacityWrapper').css({opacity:0.6});
		var height = $(this).children('.description').height();
		$(this).children('.description').css({bottom:(height+36)*-1});
		i++;
	});
	activeSlide.children('.description').css({bottom:0});
	activeSlide.width(activeSlideWidth);
	allPanels.click(function(){
		timerCount = parseInt($(this).attr('id').substr($(this).attr('id').length-1,1));
		allPanels.each(function(){$(this).stop(true,true);});
		activeSlide.children('.year').stop(true,true);
		activeSlide.children('.description').stop(true,true);
		if(previousSlide.length > 0){
			previousSlide.stop(true,true);
			previousSlide.children('.year').stop(true,true);
			previousSlide.children('.description').stop(true,true);
		}
		openActiveSlide(this);
		if(!$(this).hasClass('active')){
			allPanels.removeClass('active');
			activeSlide.children('.blurWrapper').fadeIn();
			$(this).addClass('active');
			previousSlide = activeSlide;
			activeSlide = $(this);
		}
	});
	
	var timerCount = 1;
	accID.everyTime(5000, function(){
		if(timerCount < slideCount){
			var nextObject = activeSlide.next('.slidePanel');
			timerCount++;
		}else{
			nextObject = accID.children('.slidePanel:first');
			timerCount = 1;
		}
		allPanels.each(function(){nextObject.stop(true,true);});
		activeSlide.children('.year').stop(true,true);
		activeSlide.children('.description').stop(true,true);
		if(previousSlide.length > 0){
			previousSlide.stop(true,true);
			previousSlide.children('.year').stop(true,true);
			previousSlide.children('.description').stop(true,true);
		}
		openActiveSlide(nextObject);
		if(!nextObject.hasClass('active')){
			allPanels.removeClass('active');
			activeSlide.children('.blurWrapper').fadeIn();
			nextObject.addClass('active');
			previousSlide = activeSlide;
			activeSlide = nextObject;
		}
	}); //setup timer
	
	accID.hover(
		function(){
			accID.stopTime();
		},
		function(){
			accID.everyTime(5000, function(){
				if(timerCount < slideCount){
					var nextObject = activeSlide.next('.slidePanel');
					timerCount++;
				}else{
					nextObject = accID.children('.slidePanel:first');
					timerCount = 1;
				}
				allPanels.each(function(){nextObject.stop(true,true);});
				activeSlide.children('.year').stop(true,true);
				activeSlide.children('.description').stop(true,true);
				if(previousSlide.length > 0){
					previousSlide.stop(true,true);
					previousSlide.children('.year').stop(true,true);
					previousSlide.children('.description').stop(true,true);
				}
				openActiveSlide(nextObject);
				if(!nextObject.hasClass('active')){
					allPanels.removeClass('active');
					activeSlide.children('.blurWrapper').fadeIn();
					nextObject.addClass('active');
					previousSlide = activeSlide;
					activeSlide = nextObject;
				}
			}); //setup timer
		}
	);// accid hover
}

function openActiveSlide(object){
	var slide = $(object);
	if(!slide.hasClass(".active")){
		recalculatePositions(slide);
		slide.children('.year').animate({left:-70},accordeonSpeed)
		slide.children('.description').css('right','0').animate({bottom:0},accordeonSpeed);
		slide.children('.blurWrapper').fadeOut();
		activeSlide.children('.description').css('left',0).animate({bottom:(activeSlide.children('.description').height()+36)*-1},accordeonSpeed);
		activeSlide.children('.year').animate({left:0},accordeonSpeed);
	}
}

function recalculatePositions(object){
	var slide = $(object);
	var offset = 0;
	var i=0;
	var activeWidth = 0;
	var speed = accordeonSpeed;
	var isGoRight = false;
	allPanels.each(function(){
		var newLeft;
		newLeft =  (inactiveSlideWidth+1)*i+activeWidth;
		var newWidth = inactiveSlideWidth;
		if($(this).attr('id') == slide.attr('id')){
			activeWidth = activeSlideWidth - inactiveSlideWidth;
			newWidth = activeSlideWidth;
			speed = accordeonSpeed-100;
		}
		$(this).animate({width:newWidth, left:newLeft}, speed);
		i++;
	});
}






/*
 // Visual at Services
 // Date: 03.12.2010, 11:52
*/
var activeImage;
var mask;
var previousImage;
var allImages;
var vLeftMenu;
var grayHover;
var blueActive;
var isInAnimate = false;
var textBlock;
var textBlockClose = false;
$(document).ready(function(){
	vLeftMenu = $('#servicesVisual .vLeftMenu');
	grayHover = $('#servicesVisual .vLeftMenu .grayHover');
	blueActive = $('#servicesVisual .vLeftMenu .blueActive');
	textBlock = $('#servicesVisual .textBlock');
	grayHover.css('opacity', '0.6');
	mask = $('#servicesVisual .mask');
	activeImage = $('#servicesVisual .active');
	allImages = $('#servicesVisual .bgImg');
	vLeftMenu.children('.opacityWrapper').css({opacity:0.6});
	blueActive.css({top:0, height:($('.vLeftLinks a:first').height()+30)});
	$(".bgImg").hide();
	$(".bgImg.active").show();
	vLeftMenu.find('.vLeftLinks a').hover(
		function(){
				grayHover.stop(true);
				grayHover.animate({top:$(this).position().top, height:($(this).height()+30)},100);
		},
		function(){
		}
	);
	
	vLeftMenu.hover(
		function(){grayHover.stop(true,true); grayHover.show();},
		function(){grayHover.stop(true,true); grayHover.hide();}
	)
	
	/* $('#NextImg').click(function(){
		if(!isInAnimate){
			textBlockClose = false;
			textBlock.find('.textOpen').hide();
			isInAnimate = true;
			previousImage = activeImage;
			if(previousImage.next('.bgImg').length>0){
				activeImage = previousImage.next('.bgImg');
			}else{
				activeImage = $('#servicesVisual .bgImg:first');
			}
			var activeRel = activeImage.attr('id');
			activeRel = activeRel.substr(3,4);
			var activeLink = $('.vLeftMenu a[rel = '+activeRel+']')
			blueActive.stop(true); 
			textBlock.animate({bottom:-1*textBlock.height()},464, function(){
				textBlock.find('.textContainer').hide();
				textBlock.find('#text'+activeRel).show();
				textBlock.animate({bottom:0},464);
				
			})
			blueActive.animate({top:activeLink.offset().top, height:(activeLink.height()+30)},928);
			previousImage.css({'z-index':2});
			activeImage.css({display:'block','z-index':3, height:0, top:0});
			mask.css({top:-100});
			mask.animate({top:-50}, 114, function(){
				activeImage.animate({height:306},700);
				mask.animate({top:256},700, function(){
					previousImage.removeClass('active');
					activeImage.addClass('active');
					mask.animate({top:306},114,function(){previousImage.hide(); mask.css({top:-100, bottom:'auto'}); isInAnimate = false;});
				});
			});
		}
	}); */
	
	
	$('.vLeftLinks a').click(function(){
		if(!isInAnimate){
			textBlockClose = false;
			textBlock.find('.textOpen').hide();
			blueActive.stop(true); 
			blueActive.animate({top:$(this).position().top, height:($(this).height()+30)},928);
			isInAnimate = true;
			previousImage = activeImage;
			var rel = $(this).attr('rel');
			activeImage = $('#servicesVisual .bgImg[id="img'+rel+'"]');
			if(activeImage.attr('id') != previousImage.attr('id')){
				textBlock.animate({bottom:-1*textBlock.height()},300, function(){
					textBlock.find('.textContainer').hide();
					textBlock.css({bottom:-300});
					textBlock.find('#text'+rel).show();
						activeImage.css({'z-index':4});
						previousImage.fadeOut(1000, function(){previousImage.hide();});
							activeImage.fadeIn(1000, function(){
								previousImage.hide();
								activeImage.addClass('active');
								previousImage.removeClass('active');
								isInAnimate = false; 
								grayHover.show(); 
								textBlock.animate({bottom:0},300);
							});
						});
			}else{isInAnimate = false;}
		}
	});
	
	textBlock.find('.textClose').click(function(){
		if(!textBlockClose){
			textBlock.animate({bottom:-1*textBlock.height()+3},400);
			textBlock.find('.textOpen').show().animate({top:-10},100, function(){textBlockClose = true;});
			textBlock.css('cursor','pointer');
		}
	})
	textBlock.click(function(){
		if(textBlockClose){
			textBlock.animate({bottom:0},400, function(){textBlockClose = false;});
			textBlock.css('cursor','default');
			textBlock.find('.textOpen').animate({top:0},100).hide();
		}
	});
	
	$('#servicesVisual').everyTime(10000, function(){
		if(!isInAnimate){
			textBlockClose = false;
			textBlock.find('.textOpen').hide();
			isInAnimate = true;
			previousImage = activeImage;
			if(previousImage.next('.bgImg').length>0){
				activeImage = previousImage.next('.bgImg');
			}else{
				activeImage = $('#servicesVisual .bgImg:first');
			}
			var activeRel = activeImage.attr('id');
			activeRel = activeRel.substr(3,4);
			var activeLink = $('.vLeftMenu a[rel = '+activeRel+']')
			blueActive.stop(true); 
			blueActive.animate({top:activeLink.position().top, height:(activeLink.height()+30)},928);
			textBlock.animate({bottom:-1*textBlock.height()},300, function(){
					textBlock.find('.textContainer').hide();
					textBlock.css({bottom:-300});
					textBlock.find('#text'+activeRel).show();
						activeImage.css({'z-index':4});
						previousImage.fadeOut(1000, function(){previousImage.hide();});
						activeImage.fadeIn(1000, function(){
								previousImage.removeClass('active');
								activeImage.addClass('active');
								isInAnimate = false; 
								grayHover.show(); 
								textBlock.animate({bottom:0},300);
							});
						});
		}
	});
	
	$('#servicesVisual').hover(
		function(){$(this).stopTime();},
		function(){
		$('#servicesVisual').everyTime(10000, function(){
		if(!isInAnimate){
			textBlockClose = false;
			textBlock.find('.textOpen').hide();
			isInAnimate = true;
			previousImage = activeImage;
			if(previousImage.next('.bgImg').length>0){
				activeImage = previousImage.next('.bgImg');
			}else{
				activeImage = $('#servicesVisual .bgImg:first');
			}
			var activeRel = activeImage.attr('id');
			activeRel = activeRel.substr(3,4);
			var activeLink = $('.vLeftMenu a[rel = '+activeRel+']')
			blueActive.stop(true); 
			blueActive.animate({top:activeLink.position().top, height:(activeLink.height()+30)},928);
			textBlock.animate({bottom:-1*textBlock.height()},300, function(){
					textBlock.find('.textContainer').hide();
					textBlock.css({bottom:-300});
					textBlock.find('#text'+activeRel).show();
						activeImage.css({'z-index':4});
						previousImage.fadeOut(1000, function(){previousImage.hide();});
							activeImage.fadeIn(1000, function(){
								previousImage.hide();
								previousImage.removeClass('active');
								activeImage.addClass('active');
								isInAnimate = false; 
								grayHover.show(); 
								textBlock.animate({bottom:0},300);
							});
						});
		}
	});
		
		}
	)
});
