﻿var LastSlideNumber = 0;
var Speed = 1200;
var Speed2 = 5;
var DelayTime = 1200;
var AllreadyRun = false;
var StopAnimation = false;
var StopSlide = false;
var AnimationArrayIndex = -1; 
var TimeOut;
var WindowReady = false;

function Fade(FadeInNumber) {
	if (WindowReady == false) {
		FadeInNumber = false;
		return true;
	}
	else {
		var SomethingVisible = false;
		jQuery('.image_count').each(function(i) {
			if (jQuery('#bg'+i).css('display') != 'none')
			SomethingVisible = true;
		});
		if (SomethingVisible==false) {
			return true;
		}
	}
	StopSlide = true;
	jQuery('.box_hover'+FadeInNumber).css('opacity',1.0);
	if (StopAnimation == false) {
		clearTimeout(TimeOut);
		StopAnimation = true;
		jQuery('.image_count').each(function(i) {
			jQuery('#bg'+i).stop(true, true);
			jQuery('.box_hover'+i).stop(true, true);
			jQuery('#box'+i).stop(true, true);
		
			if (i != FadeInNumber) {
				jQuery('#bg'+i).fadeOut('slow');
				jQuery('.box_hover'+i).fadeOut('slow');
				jQuery('#box'+i).fadeTo(Speed2, 1);
			}
			else {
				jQuery('#bg'+i).fadeIn('slow');
				jQuery('.box_hover'+i).fadeIn('slow');
				jQuery('#box'+i).fadeTo(Speed2, 1);
			}
		});
	}
	if (LastSlideNumber != FadeInNumber && StopAnimation == true && StopSlide == false)	{
		jQuery('#bg'+FadeInNumber).css('display','block');
		jQuery('.box_hover'+FadeInNumber).css('display','block');
		jQuery('#bg'+LastSlideNumber).css('display','none');
		jQuery('.box_hover'+LastSlideNumber).css('display','none');
		LastSlideNumber = FadeInNumber;
	}
	StopAnimation = false;
}

function ScrollElement(element, cnt, pxl) {
	var count = cnt;
	var pixel = -pxl;
	//var isLeft = jQuery('#schuhslider .moving').position().left;
	var isLeft = 0;
	
	//console.info(isLeft);
	
	if (isLeft <= count * pixel) {
		jQuery('#schuhslider .moving').css('left', '0');
	}
	else {
		var addPixel = parseInt(isLeft) + parseInt(pixel);
		jQuery('#schuhslider .moving').css('left', addPixel+'px');
	}
}

function SlideAnimation(animationBoxIds) {
	if (StopAnimation == true || StopSlide == true) {
		return;
	}
	var animationBoxIdsArr = animationBoxIds.split(',');
	var currentBoxId = animationBoxIdsArr[AnimationArrayIndex];

	if (AnimationArrayIndex == 0 && AllreadyRun == true) {
		LastSlideNumber = 0;
		return;
	}
	jQuery("#bg"+currentBoxId).fadeOut(Speed2);
	jQuery('.box_hover'+currentBoxId).fadeOut(Speed2);
	/*jQuery('#box_hover'+Value).css('display', 'none');*/
	jQuery('#box'+currentBoxId).fadeTo(Speed2, 1);
	LastSlideNumber = currentBoxId;
	
	if (currentBoxId == jQuery('.image_count').length - 1) {
		AnimationArrayIndex = 0;
		AllreadyRun = true;
	}
	else {
		AnimationArrayIndex++;
	}
	currentBoxId = animationBoxIdsArr[AnimationArrayIndex];

	jQuery('#box'+currentBoxId).fadeTo(Speed, 1);
	jQuery('.box_hover'+currentBoxId).fadeTo(Speed, 1);
	jQuery('.box_hover'+currentBoxId).css('display', 'block');
	
	jQuery("#bg"+currentBoxId).fadeIn(Speed, function() {
		this.timeout = setTimeout('SlideAnimation("'+animationBoxIds+'")',DelayTime);
	});
}

function showMaf(area, id) {
	var name = 'idx_'+ id;
	if (jQuery('#'+ name).length > 0) {
		//console.info('exist');
		jQuery('#'+ name).show();
	}
	else {
		//console.info('create');
		var dir = jQuery(area).parent().prev().attr('src');
		dir = dir.replace('bg.jpg', 'hover/');
		var src = dir + id +'.png';
		var img = '<img id="'+ name +'" src="'+ src +'" style="position:absolute;" />';
		jQuery('body').append(img);
	}
	jQuery(area).mousemove(function(e){
		var x = e.pageX + 10;
		var y = e.pageY + 10;
		jQuery('#'+ name).css({
			'top': y+'px', 
			'left': x+'px' 
		});
	});
}

function hideMaf(area, id) {
	var name = 'idx_'+ id;
	jQuery('#'+ name).hide();
	jQuery(area).mousemove(null);
}







var hqLastSlideNumber = 0;
var hqSpeed = 1200;
var hqSpeed2 = 5;
var hqDelayTime = 1200;
var hqAllreadyRun = false;
var hqStopAnimation = false;
var hqStopSlide = false;
var hqAnimationArrayIndex = -1; 
var hqTimeOut;
var hqWindowReady = false;

function hqFade(hqFadeInNumber) {
	if (hqWindowReady == false) {
		hqFadeInNumber = false;
		return true;
	}
	else {
		var hqSomethingVisible = false;
		jQuery('.image_count').each(function(hqi) {
			if (jQuery('#hqbg'+hqi).css('display') != 'none')
			hqSomethingVisible = true;
		});
		if (hqSomethingVisible==false) {
			return true;
		}
	}
	hqStopSlide = true;
	jQuery('.box_hover'+hqFadeInNumber).css('opacity',1.0);
	if (hqStopAnimation == false) {
		clearTimeout(hqTimeOut);
		hqStopAnimation = true;
		jQuery('.image_count').each(function(hqi) {
			jQuery('#bg'+hqi).stop(true, true);
			jQuery('.box_hover'+hqi).stop(true, true);
			jQuery('#hqbox'+hqi).stop(true, true);
		
		
			if (hqi != hqFadeInNumber) {
				jQuery('#bg'+hqi).fadeOut('slow');
				jQuery('.box_hover'+hqi).fadeOut('slow');
				jQuery('#hqbox'+hqi).fadeTo(hqSpeed2, 1);
			}
			else {
				jQuery('#bg'+hqi).fadeIn('slow');
				jQuery('.box_hover'+hqi).fadeIn('slow');
				jQuery('#hqbox'+hqi).fadeTo(hqSpeed2, 1);
			}
		});
	}
	if (hqLastSlideNumber != hqFadeInNumber && hqStopAnimation == true && hqStopSlide == false)	{
		jQuery('#bg'+hqFadeInNumber).css('display','block');
		jQuery('.box_hover'+hqFadeInNumber).css('display','block');
		jQuery('#bg'+hqLastSlideNumber).css('display','none');
		jQuery('.box_hover'+hqLastSlideNumber).css('display','none');
		hqLastSlideNumber = hqFadeInNumber;
	}
	hqStopAnimation = false;
}

function hqScrollElement(hqelement, hqcnt, hqpxl) {
	var hqcount = hqcnt;
	var hqpixel = -hqpxl;
	//var isLeft = jQuery('#schuhslider .moving').position().left;
	var hqisLeft = 0;
	
	//console.info(isLeft);
	
	if (hqisLeft <= hqcount * hqpixel) {
		jQuery('#schuhslider .moving').css('left', '0');
	}
	else {
		var hqaddPixel = parseInt(hqisLeft) + parseInt(hqpixel);
		jQuery('#schuhslider .moving').css('left', hqaddPixel+'px');
	}
}

function hqSlideAnimation(hqanimationBoxIds) {
	if (hqStopAnimation == true || hqStopSlide == true) {
		return;
	}
	var hqanimationBoxIdsArr = hqanimationBoxIds.split(',');
	var hqcurrentBoxId = hqanimationBoxIdsArr[hqAnimationArrayIndex];

	if (hqAnimationArrayIndex == 0 && hqAllreadyRun == true) {
		hqLastSlideNumber = 0;
		return;
	}
	jQuery("#bg"+hqcurrentBoxId).fadeOut(hqSpeed2);
	jQuery('.box_hover'+hqcurrentBoxId).fadeOut(hqSpeed2);
	/*jQuery('#box_hover'+Value).css('display', 'none');*/
	jQuery('#hqbox'+hqcurrentBoxId).fadeTo(hqSpeed2, 1);
	hqLastSlideNumber = hqcurrentBoxId;
	
	if (hqcurrentBoxId == jQuery('.image_count').length - 1) {
		hqAnimationArrayIndex = 0;
		hqAllreadyRun = true;
	}
	else {
		hqAnimationArrayIndex++;
	}
	hqcurrentBoxId = hqanimationBoxIdsArr[hqAnimationArrayIndex];

	jQuery('#hqbox'+hqcurrentBoxId).fadeTo(hqSpeed, 1);
	jQuery('.box_hover'+hqcurrentBoxId).fadeTo(hqSpeed, 1);
	jQuery('.box_hover'+hqcurrentBoxId).css('display', 'block');
	
	jQuery("#bg"+hqcurrentBoxId).fadeIn(hqSpeed, function() {
		this.timeout = setTimeout('hqSlideAnimation("'+hqanimationBoxIds+'")',hqDelayTime);
	});
}
var containerSliding = false;
function slideIndexContainer(container, direction) {
    if (containerSliding) {
        return;
    }
    if (container.css('left') == 'auto') {
        container.css('left', 0);
    }
    containerSliding = true;
    //1. Check, if an element is available in the correct direction
    if (direction == 'left') {
        var lastElement  = jQuery(container.find('.index-panel')[container.find('.index-panel').length - 1]);
        var elementWidth = lastElement.outerWidth(true);
        if (parseInt(container.css('left')) == 0) {
            //Take the last element and put it in front of the current first one
            //Set the left value to element's width * -1
            container.prepend(lastElement);
            container.css('left', elementWidth * -1);
        }
        container.animate({
                left: parseInt(container.css('left')) + elementWidth
            },
            'slow',
            function() {
                containerSliding = false;
            }
        );
        
        //Get the previous element
        var elements = container.find('.index-panel');
    } else if (direction == 'right') {
        var firstElement  = jQuery(container.find('.index-panel')[0]);
        var elementWidth = firstElement.outerWidth(true);
        if (parseInt(container.css('left')) + container.outerWidth(true) == container.parent().width()) {
            //Take the last element and put it in front of the current first one
            //Set the left value to element's width * -1
            container.append(firstElement);
            container.css('left', parseInt(container.css('left')) + elementWidth);
        }
        container.animate({
                left: parseInt(container.css('left')) - elementWidth
            },
            'slow',
            function() {
                containerSliding = false;
            }
        );
        
        //Get the previous element
        var elements = container.find('.index-panel');
    } else {
        containerSliding = false;
    }
}


$(document).ready(function(){
	jQuery('#bg1').hide();
	jQuery('#bg2').hide();
	
	jQuery('.image_count').each(function(i) {
		//console.log("add event listener: #bg"+i);
		jQuery('#box'+i).mouseenter(function(){
			changeTeaser(jQuery(this));
		});
	});
	
	jQuery('#homepage-teaser-box a').each(function(i) {
		//console.log("add event listener: #bg"+i);
		jQuery(this).mouseenter(function(){
			changeLTeaser(jQuery(this));
		});
		
		/* 
		jQuery(this).mouseleave(function(){
			changeLTeaserOut();
		});
		*/
		
	});
	
	jQuery('#homepage-teaser-box').mouseleave(function(){
			changeLTeaserOut();
	});
	
});





function changeTeaser(hoverObj){
	
	var box = null;

	jQuery('.image_count').each(function(i) {
		box = jQuery('#box'+i);
		jQuery('#bg'+i).stop(true, true);
		
		if( box[0] == hoverObj[0]) {
			jQuery('#bg'+i).fadeIn();
		}
		else{
			jQuery('#bg'+i).fadeOut();
		}
	});
};


function changeLTeaser(hoverObj){
	
	jQuery('#homepage-teaser-box a').each(function(i) {
		jQuery(this).stop(true, true);
		if( jQuery(this)[0] == hoverObj[0]){
			jQuery(this).fadeTo(200, 1);
			
		}
		else{
			jQuery(this).fadeTo(300, 0.5);
		}
	
	});
}

function changeLTeaserOut(hoverObj){
	jQuery('#homepage-teaser-box a').each(function(i) {
		jQuery(this).fadeTo(300, 1);
	
	});

}

jQuery(window).load(function() {
//WindowReady = true;
//setTimeout('SlideAnimation("0,1,2")',DelayTime);
});
