
//Easings
$.easing.elastic = function (x, t, b, c, d) {
	var s = 1.70158;
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}
$.easing.inOutQuint = function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
}

jQuery.fn.exists = function(){return this.length>0;}

// Load on document.ready
$(document).ready(function(){
	/*$('div.customers .description').hide();
	$("div.customers .logo a img").each(function(i, el) {
		img = $(this).attr('src').replace(/\/kunde_/g, "/grey_kunde_");
		$(this).attr('src', img);
		$(this).addClass('transparent');
	});

	// Customers
	$('div.customers .logo a').click(function(){ // When link is clicked
		$('div.customers .logo').removeClass('active'); // Remove active class from links
		$('div.customers .logo a img').addClass('transparent');

		$("div.customers .logo a img").each(function(i, el) {
			img = $(this).attr('src').replace(/\/kunde_/g, "/grey_kunde_");
			$(this).attr('src', img);
		});

		$(this).parents().addClass('active'); // Remove active class from links
		var currentTab = $(this).attr('href'); // Set currentTab to value of href attribute
		$('div.customers div.description:visible').hide(200, function(){

		});
		if(!$(currentTab).is(':visible')) {
			$(this).find('img').removeClass('transparent');
			img = $(this).find('img').attr('src').replace(/grey_kunde_/g, "kunde_");
			$(this).find('img').attr('src', img);
			$(currentTab).show(200);
		}
		else {
			$(this).find('img').addClass('transparent');
			img = $(this).find('img').attr('src').replace(/\/kunde_/g, "/grey_kunde_");
			$(this).find('img').attr('src', img);
		}
		return false;
	});*/

	$("a.details").fancybox({ // use fancybox as modal screen
		'enableEscapeButton': true,
		'overlayColor' 		: '#000',
		'centerOnScroll'	: true,
		'padding'			: 0,
		'titleShow'			: false,
		'changeSpeed' 		: 0,
		'overlayOpacity'	: 0.4
	});

	// Fancybox
	$("a.zoom").fancybox({ // single fancyboxes
		'overlayColor' : '#000',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'titlePosition' : 'inside',
		'changeSpeed' : 0,
		'overlayOpacity' : 0.4
	});

	// Insert customer logo into modal screen
	/*$('.customers li').each(function(){
		var img = $(this).find('img');
		var clone = img.clone();
		if(img.parent().next().hasClass('modal')) {
			img.parent().next().prepend('<span class="customer-logo"></span>');
			img.parent().next().find('.customer-logo').prepend(clone);
		}
	});*/


	//Skills
	$('div.skills .description').hide();

	$('div.skills .logo a').click(function(){ // When link is clicked
		$('div.skills .logo').removeClass('active'); // Remove active class from links

		$(this).parents().addClass('active'); // Remove active class from links
		var currentTab = $(this).attr('href'); // Set currentTab to value of href attribute
		$('div.skills div.description:visible').hide(200, function(){

		});
		if(!$(currentTab).is(':visible')) {
			$(currentTab).show(200);
		}
		return false;
	});

	// Accordion Menues
	$('.teaserwrapper').each(function(){
		$(this).css('height',$(this).height());
	});
	$('.accordion').each(function(){
		if($(this).hasClass('wrapall')) {
			$('div.teaserwrapper', this).hide();
		} else {
			$('div.teaserwrapper', this).not(':first').hide();
			$('h4:first', this).addClass('down');
		}
	});
	$('.accordion h4').click(function() {
		var thisAccordion = $(this).parents('.accordion');
		if($(this).next().is(':hidden')) {
			$('h4', thisAccordion).removeClass('down');
			$(this).addClass('down');
			var thisAccordion = $(this).closest('.accordion');
			$('div.teaserwrapper', thisAccordion).slideUp('fast');
			$(this).next().slideToggle('fast');
		}
		return false;
	});

	$('.toggle .textcontent').hide();
	$('.toggle .textteaser h4').click(function() {
		var state = $(this).next('div').is(':hidden');
		/*if(state == true) {
			$(this).css('margin','0 0 15px 0');
		} else {
			$(this).css('margin','0');
		}*/
		$(this).next('div').slideToggle();
		return false;
	});

	// Fading Teaserbanner
	$('#teaserline li')
		.removeClass('nojs')
		.find('img').fadeTo(0,.25)
		.end()
		.find('h3').fadeTo(0,.5)
		.end()
		.hover(
		function () {
			$('img, h3', this).stop().animate({opacity: 1}, 400);
		},
		function () {
			$('img', this).stop().animate({opacity: 0.25}, 400);
			$('h3', this).stop().animate({opacity: 0.5}, 400);
		}
	);

	// Toggle through video previews (Manor Project Page)
	$('#video-examples .video-example:gt(0)').hide();
	$('.video-example .topwrapper').each(function(index){
		var allExamples = $('.video-example').length;
		$(this).append('<span class="page-index">'+ (index+1) + '/' + allExamples +'</span>');
	});
	$('#video-examples #nextprev').click(function(event){
		event.preventDefault();
		$('#video-examples .video-example:visible').fadeOut(200, function(){
			if($(this).next('.video-example').exists()) {
				$(this).next('.video-example').fadeIn(200);
			} else {
				$('#video-examples .video-example:first').fadeIn(200);
			}
		});
	});
	$('#video-examples #previousprev').click(function(event){
		event.preventDefault();
		$('#video-examples .video-example:visible').fadeOut(200, function(){
			if($(this).prev('.video-example').exists()) {
				$(this).prev('.video-example').fadeIn(200);
			} else {
				$('#video-examples .video-example:last').fadeIn(200);
			}
		});
	});
});


function navigatetoslide(itemno, total)
{
	 var slide = total - (itemno-1);
	 return slide;
}

/**************************************************************************** TEASER ****************************************************************************/




function getWindowWidth(){
	return $(window).width();
}


function getCurrentBgNo()
{
	var currentitem = $("#flashteaser").find('img.scale.active');
	currentitem = currentitem.attr('id');
	currentitem = currentitem.charAt(7);
	//alert(currentitem);
	return currentitem;
}


function animateStatus(pause)
{
	$("#flashteaser #statuscontainer #status").animate({"width": "100px"}, pause, 'linear');
}


function renewStatus()
{
	$("#statuscontainer").html('<div id="status"></div>');
}

function fadebg(current, next, duration, pause)
{

	current.fadeOut(duration, function(){
		next.fadeIn(duration, function(){animateStatus(pause);}),
		current.removeClass("active");
		next.addClass("active");

	});
}

function getSize(){
		var size = $('#janus-fader li.scrollitem').size();
		return size;
}


function createFasciaNavi(nr) {
    return '<li><div><a href="javascript:;" id="'+nr+'" class="fascialink"><span></span></a></div></li>';
};


function scrollToBg(nextitem, duration, pause){

	nextitem 			= parseInt(nextitem);
	var size 			= getSize();
	if(nextitem > size){nextitem = 1;}
	if(nextitem < 1){nextitem = size;}

	var currentbg 		= parseInt(getCurrentBgNo());
	currentbg 			= "#bgimage"+currentbg;
	var nextbg 			= "#bgimage"+nextitem;
	var currentbg_obj 	= $(currentbg);
	var nextbg_obj 		= $(nextbg);

	duration = duration/2;
	//alert (duration);
	//alert("Current: "+currentbg+" Next: "+nextbg);
	//alert(pause);
	fadebg(currentbg_obj, nextbg_obj, duration, pause);
}

/*************************************************************************** END OF TEASER ***********************************************************************/
