var snapLimits=Array(1300,1600,2250);

var imagePath=((window.outerWidth>1440) ? 'feimages' : ((window.outerWidth>1200) ? 'feimages2' : 'feimages3'));

if (slideFiles[0]=='standard.jpg')
slideFiles[0]={image: '/pictures/standard.jpg'};
else
{
	for (var i=0;i<slideFiles.length;i++)
	slideFiles[i]={image: '/files/'+imagePath+'/'+slideFiles[i]};
}

jQuery(function($)
{
	theme =
	{
		beforeAnimation : function(direction)
		{
			if ($('.slideCurrent').length) $('#boxMenuSlideNav .slideCurrent').html(pad(vars.current_slide + 1,2));
			
			if ($('#boxCopyright').length) $('#boxCopyright').html((copyright[vars.current_slide]!='') ? '&copy; '+copyright[vars.current_slide] : '');
			
			if ($('#boxDescription').length)
			{
				if (descriptions[vars.current_slide]!='')
				$('#boxDescription').html(descriptions[vars.current_slide]).fadeIn('fast');
				else
				$('#boxDescription').html('').fadeOut('fast');				
			}
	 	}
	 };

	$.supersized({
	slide_interval          :   (noSlide) ? 999000 : 6000,		// Length between transitions
	transition_speed		:	700,		// Speed of transition
	performance				:	1,			// 0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
	vertical_center         :   1,			// Vertically center background
	horizontal_center       :   1,			// Horizontally center background
	fit_always				:	0,			// Image will never exceed browser width or height (Ignores min. dimensions)
	fit_portrait         	:   0,			// Portrait images will not exceed browser height
	fit_landscape			:   0,			// Landscape images will not exceed browser width
	slides 					:  	slideFiles
	});
		
	$('#boxMenuSlideNav .slideNext').click(function(){api.nextSlide();});

	$('#boxMenuSlideNav .slidePrev').click(function(){api.prevSlide();});
});

function updateOnResize()
{
	//Snap-Position Content
	if ($('#boxContent').length>0 && $(window).width()>snapLimits[2]) $('#boxContent').css({'left': (parseInt($('#boxMenu>div:nth-child(4)').offset().left)-parseInt($('#boxMenu>div:nth-child(1)').css('margin-right')))+'px'});
	else if ($('#boxContent').length>0 && $(window).width()>snapLimits[1]) $('#boxContent').css({'left': (parseInt($('#boxMenu>div:nth-child(3)').offset().left)-parseInt($('#boxMenu>div:nth-child(1)').css('margin-right')))+'px'});
	else if ($('#boxContent').length>0 && $(window).width()>snapLimits[0]) $('#boxContent').css({'left': (parseInt($('#boxMenu>div:nth-child(2)').offset().left)-parseInt($('#boxMenu>div:nth-child(1)').css('margin-right')))+'px'});
	else $('#boxContent').css({'left': '0px'});	

	//Textseiten: Content-Position
	if ($('#boxContent2').length>0) $('#boxContent2').css({'left': ($('.menuColumn:first').outerWidth()+parseInt($('#boxMenu').css('padding-left')))+'px'});

	//Textseiten: Content-Position
	if ($('#boxContentDoubleLeft').length>0) $('#boxContentDoubleLeft').css({'left': ($('.menuColumn:first').outerWidth()+parseInt($('#boxMenu').css('padding-left')))+'px'});
	if ($('#boxContentDoubleRight').length>0) $('#boxContentDoubleRight').css({'left': ($('.menuColumn:first').outerWidth()+410+parseInt($('#boxMenu').css('padding-left')))+'px'});
	//Logo-Position	
	if ($('#boxLogo').length>0) $('#boxLogo').css({'left': ($('.menuColumn:first').outerWidth()+parseInt($('#boxMenu').css('padding-left'))+47)+'px'});	

	//Sichtbarkeit Scrollbuttons
	if ($('#menu1ScrollUp').length>0) checkScrollButtons('menu1');
	if ($('#menu2ScrollUp').length>0) checkScrollButtons('menu2');
	if ($('#menu3ScrollUp').length>0) checkScrollButtons('menu3');
	if ($('#content1ScrollUp').length>0) checkScrollButtons('content1');		
	if ($('#content2ScrollUp').length>0) checkScrollButtons('content2');
	
	
	var menuProjectWidth=$('#boxMenu').width();
	$('.menuColumn').each(function(){menuProjectWidth-=$(this).outerWidth()});  		
	$('#boxMenuProject').css({width: (menuProjectWidth-100)+'px'});
	
	$('#boxMenuDataScrollUp').css({left: ($('#boxMenuData').width()+8)+'px'});
	$('#boxMenuDataScrollDown').css({left: ($('#boxMenuData').width()+8)+'px'});
	
	if ($('#boxMenuData').length>0)
	{
		var boxDataMaxHeight=Math.floor(($('#boxMenuProject').height()-$('#boxMenuProject>h3:nth-child(1)').outerHeight()-4)/15)*15;
		
		if ($('#boxMenuData').outerHeight()>boxDataMaxHeight) $('#boxMenuData').css({height: boxDataMaxHeight+'px'});
		
		checkScrollButtons('boxMenuData');
	}	
}

$(window).resize(function()
{
	updateOnResize();
});

$(document).ready(function()
{
	if ($('#boxMenuToggleLink').length>0)
	{
		$('#boxMenuToggleLink').click(function()
		{
			$('#boxLogo').fadeToggle('slow');
			
			$('#boxContent').fadeToggle('slow','',function(){checkScrollButtons('boxContentText');$('#boxMenuToggleLink').toggleClass('toggleLinkActive');
			
			$('#boxMenuToggleLink').html(($('#boxContent').css('display')=='none') ? 'PROJEKTINFORMATION' : 'PROJEKTINFORMATION AUSBLENDEN');});
		});
	}
	
	
	if ($('#boxContent2Text').length>0) $('#boxContent2Text').css({height: Math.round($('#boxContent2Text').parent().height()*0.87)+'px'});
	if ($('#boxContent2bText').length>0) $('#boxContent2bText').css({height: Math.round($('#boxContent2Text').parent().height()*0.87)+'px'});


	//Scroller Init Up
	$('.scrollUp').each(function(){
		$('#'+this.id).bind('mousedown mouseup', function(event)
		{
			var scrollElem=$('#'+this.id.substr(0,this.id.length-8));			
		    if(event.type == 'mousedown')
	        scrollElem.animate({scrollTop: '-=700px'}, 3000, 'linear');
		    else
		    {
	        	scrollElem.stop();
	        	checkScrollButtons(scrollElem.attr('id'));
		    }        	
		});
		
		//init scroll button visibility
		checkScrollButtons(this.id.substr(0,this.id.length-8));
	});

	//Scroller Init Down
	$('.scrollDown').each(function(){
		$('#'+this.id).bind('mousedown mouseup', function(event)
		{
			var scrollElem=$('#'+this.id.substr(0,this.id.length-10));
			
		    if(event.type == 'mousedown')
	        scrollElem.animate({scrollTop: '+=700px'}, 3000, 'linear');
		    else
		    {
	        	scrollElem.stop();
	        	checkScrollButtons(scrollElem.attr('id'));
		    }
		});
	});
	
	//Cycle-Plugin
	if ($('#boxContentImages').length>0)
	{
		doUpdateContentNav=false;
	
	    $('#boxContentImagesContainer').cycle(
	    {
			fx: 'fade',
			timeout: 0,
			next: '#boxContentSlideNav .slideNext', 
	    	prev: '#boxContentSlideNav .slidePrev',
			before: function(currSlideElement, nextSlideElement, options, forwardFlag){if (doUpdateContentNav) $('#boxContentSlideNav .slideCurrent').html(pad(options.nextSlide+1,2)); doUpdateContentNav=true;}	    	
		});
	}

	updateOnResize();



	//expandable button action

	function extendText(expBtnID){
		$('#'+expBtnID).hide('slow');
		
		$('#'+((expBtnID=='exp1Switch') ? 'boxContentDoubleLeft' : 'boxContentDoubleRight')+' > p > img:first').animate({height: '80px'},'slow');
		
		$('#'+expBtnID.substr(0,expBtnID.length-6)).show('slow','',function(){checkScrollButtons((expBtnID=='exp1Switch') ? 'boxContent2Text' : 'boxContent2bText');});
		
		$('#'+((expBtnID=='exp1Switch') ? 'boxContent2Text' : 'boxContent2bText')).css({height: Math.round(($('#'+((expBtnID=='exp1Switch') ? 'boxContent2Text' : 'boxContent2bText')).parent().height()-$('#'+((expBtnID=='exp1Switch') ? 'exp1Switch' : 'exp2Switch')).position().top+150+200)*0.87)+'px'});
	}

	$('.boxTextExpandableSwitch').each(function(){
		$('#'+this.id).click(function(){
			extendText(this.id);
		});
	});
	
	$('#boxContentDoubleLeft > p > img:first').click(function(){
		
		if ($('#exp1Switch').is(':visible'))
		extendText('exp1Switch');
		else
		{
			$(this).animate({height: '280px'},'slow');
			$('#exp1').hide('slow');
			$('#exp1Switch').show('slow');
		}
	});
	
	$('#boxContentDoubleRight > p > img:first').click(function(){
		if ($('#exp2Switch').is(':visible'))
		extendText('exp2Switch');
		else
		{	
			$(this).animate({height: '280px'},'slow');
			$('#exp2').hide('slow');
			$('#exp2Switch').show('slow');
		}
	});
	
	
	$('#boxMenu').hover(function(){$('#boxMenu').stop(true).animate({opacity: 0.95},200)},function(){$('#boxMenu').stop(true).animate({opacity: 0.8},200)});
	
	if ($('ul#menu3Text>li.active').length>0)
	{
		var position = $('ul#menu3Text>li.active').position();
	    $('ul#menu3Text').animate({scrollTop: (position.top-105)+'px'}, 0, 'linear');
	}
});


function checkScrollButtons(scrollElem)
{
	if ($('#'+scrollElem)[0].scrollHeight<=$('#'+scrollElem).height())
	{
		$('#'+scrollElem+'ScrollUp').hide();
		$('#'+scrollElem+'ScrollDown').hide();
	}
	else
	{
		($('#'+scrollElem).scrollTop()==0) ? $('#'+scrollElem+'ScrollUp').hide() : $('#'+scrollElem+'ScrollUp').show();
		
		if ($('#'+scrollElem).scrollTop()+$('#'+scrollElem).height()==$('#'+scrollElem)[0].scrollHeight)
		$('#'+scrollElem+'ScrollDown').hide();
		else
		$('#'+scrollElem+'ScrollDown').show();
	}
}


function pad(n, len)
{
    s = n.toString();

    if (s.length < len) s = ('0000000000' + s).slice(-len);

    return s;
}
