
homecontentSliderAnimation = false;
homecontentSliderOpen = false;
originalHomecontentSliderWidth = 0;

/* HomeContent-Slider */
function bindHomeContentSlider() {
	
	jQuery('body.home .home-contents div.panecnt').bind('mouseenter', function() {
		if( originalHomecontentSliderWidth==0 ) {
			originalHomecontentSliderWidth = jQuery(this).width();
		}
		
		// Variable
		if( !homecontentSliderAnimation && !homecontentSliderOpen ) {
			homecontentSliderAnimation = true;
			homecontentSliderOpen = true;
			homecontentSliderActiveIndex = jQuery(this).index();
			
			// show further contents
			jQuery(this).find('.show-wide').fadeIn(500).delay(200);
			jQuery(this).find('.panecontent').fadeIn(500).delay(200);
			jQuery(this).find('.pane > ul').first().addClass('floated');
			jQuery(this).find('.pane > ul li:nth(0)').trigger('click').delay(700);
			
			// Animate active
			jQuery(this).animate({
				width: originalHomecontentSliderWidth*1.7 // Breite verdoppelen
			}, 500, function() {
				// Animation complete
				homecontentSliderAnimation = false;
				return true;
			}).delay(200);
			jQuery('body.home .home-contents div.panecnt').each(function(i, el) {
				if(i!=homecontentSliderActiveIndex) {
					jQuery(el).fadeOut(200);
				}
			});
			
			jQuery(this).find('.pane').first().bind('mouseleave', resetHomeContentSlider);
		}
	});
	
	// Bind reset
	/*
	jQuery('body.home, body.home div:not(.panecnt,.pane,.clear), body.home h1, body.home h2').bind('mousemove', function() {
		if( typeof(homecontentSliderActiveIndex)=='number' && !homecontentSliderAnimation && jQuery('body.home .home-contents > div:nth('+homecontentSliderActiveIndex+')').width()>originalHomecontentSliderWidth ) {
			jQuery('body.home .home-contents > div:nth('+homecontentSliderActiveIndex+')').trigger('mouseout');
		}
	});
	*/
}

resetHomeContentSlider = function(e) {
	
	var th = jQuery(e.target);
	var t = jQuery(e.target).parents('.panecnt').first();
	
	// Variable
	if( !homecontentSliderAnimation && homecontentSliderOpen ) {
		homecontentSliderAnimation = true;
		homecontentSliderActiveIndex = jQuery(t).index();
		
		jQuery(th).unbind('mouseleave', resetHomeContentSlider);
		
		// show further contents
		jQuery(t).find('.pane > ul').first().removeClass('floated');
		jQuery(t).find('.panecontent').fadeOut(500);
		jQuery(t).find('.show-wide').fadeOut(500);
		
		jQuery(t).find('.show-wide').fadeOut(100);
		// Animate active
		jQuery(t).animate({
			width: originalHomecontentSliderWidth // Breite verdoppelen
		}, 500, function() {
			// Animation complete
			homecontentSliderAnimation = false;
			homecontentSliderOpen = false;
			jQuery('body.home .home-contents div.panecnt').each(function(i, el) {
				if(i!=homecontentSliderActiveIndex) {
					jQuery(el).fadeIn(200);
				}
			});
			
			// Unbind reset
			//jQuery('body.home, body.home div:not(.panecnt,.pane,.clear), body.home h1, body.home h2').unbind('mousemove');
		
			return true;
		});
		
		
	}
}

function bindHomeContentTabs() {
	jQuery('body.home .home-contents > div .pane > ul li').bind('click', function() {
		var pane = jQuery(this).parents('.pane').first();
		var tablist = jQuery(pane).find('.tablist').first();
		var myindex = jQuery(this).index();
		jQuery(pane).find('ul li.active').removeClass('active');
		jQuery(this).addClass('active');
		jQuery(tablist).find('div.panecontent').hide();
		jQuery(tablist).find('div.panecontent:nth('+myindex+')').show();
	});
}

function showHomeContentLayer() {
	if( parseInt(jQuery('#homecontentlayer').css('top'), 10) > screen.height ) {
		// Not yet visible
		var homecontentsOffset = jQuery('body.home .home-contents').first().offset();
		var layerHeight = jQuery('#site').height()-homecontentsOffset.top;
		console.log('layerHeight = '+layerHeight);
		jQuery('#homecontentlayer').height(layerHeight).animate({
			'top': String(homecontentsOffset.top)+'px'
		},{
			step: function(now, fx) {
				jQuery('body.home .home-contents').css('opacity', '-=0.2');
				console.log('home-contents opacity -0.2');
			}	
		}, 400);
		return true;
	} else {
		// Visible already
		return false;
	}
}

jQuery(document).ready(function() {
	bindHomeContentSlider();
	bindHomeContentTabs();
});
