/*
 * agil Visuelle Kommunikation
 * http://www.agil.de
 *
 * Date: 2010-10
 * Author: Gedankengut.de
 */

$(document).ready(function(){
	// Aktuell: Lazy Image Loader
	$('#content .newsCnt img').lazyload({effect: "fadeIn"});
	
	// Aktuell: Scroll Buttons ggf. ausblenden
	$('#content .newsCnt:first .newsCol1 .up').css('visibility','hidden');
	$('#content .newsCnt:first .newsCol1 .top').css('visibility','hidden');
	$('#content .newsCnt:last .newsCol1 .down').css('visibility','hidden');
	
	// Aktuell: Scrollfunktionen für Buttons
	$('#content .newsCnt .newsCol1 .up').click(function(e){
		e.preventDefault();
		$.scrollTo('-=320px',150);
	});
	$('#content .newsCnt .newsCol1 .down').click(function(e){
		e.preventDefault();
		$.scrollTo('+=320px',150);
	});
	$('#content .newsCnt .newsCol1 .top').click(function(e){
		e.preventDefault();
		$.scrollTo('#pageWrap',150);
	});
	
	// Portfolio: Overview
	$('#portfolioOverview tr.rowHeader').mouseover(function(){
		$('td',this).not('.active').addClass('hover');
		if($(this).prev('tr').hasClass('projectDetails')){
			$(this).prev('tr').prev('tr').addClass('hoverPrev');
		} else {
			$(this).prev('tr').addClass('hoverPrev');
		}
	}).mouseout(function(){
		$('tr.rowHeader td.colCustomer')
		$('td',this).removeClass('hover');
		if($(this).prev('tr').hasClass('projectDetails')){
			$(this).prev('tr').prev('tr').removeClass('hoverPrev');
		} else {
			$(this).prev('tr').removeClass('hoverPrev');
		}
	}).click(function(e){
		if(!$(e.target).hasClass('active')){
			var idDetails = $(this).attr('id') + 'Details';
			$('.projectDetails:visible').hide();
			$('#'+idDetails).show();
			loaderAnimation('#'+idDetails);
			$('.rowHeader td').removeClass('active');
			$('.rowHeader td div.highlight').remove();
			$('td',this).addClass('active');
			$('td:first',this).prepend('<div class="highlight"></div>');	
		}
	});	
	$('#portfolioOverview .projectClose').click(function(){
		$('.projectDetails').hide();
		$('.rowHeader td').removeClass('active');
	});
	
	// Büro: Accordion
	$('#accordion').accordion({autoHeight: false,clearStyle: true});

	// Scrollable
	$('.scrollNav li:first').addClass('active');
	$.easing.custom = function(x, t, b, c, d){
		var s = 0;
		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;
	}
	var api = $('.scrollable').scrollable({ api:true });
	$('.scrollable').navigator('ul.scrollNav');
	$('.scrollable .items .item ').click(function(){
		api.seekTo($(this).index());
	});
	
	// Scrollable dyn. height
	var maxHeight = 0;
	var tmpHeight = 0;
	$('.scrollable .items .item').each(function(){
		tmpHeight = $('img',this).attr('height');
		if(tmpHeight > maxHeight) maxHeight = tmpHeight;
	});
	if(maxHeight > 416) $('.scrollable').css('height',maxHeight+'px');
});

function loaderAnimation(ref){
	initCounter(485,ref);
	$('div.loading div',ref).css('width','0%');
	$('div.loading',ref).css('width','750px');
	$('div.loading',ref).show();
	$('div.loading div',ref).animate({ width: '100%' }, 1000, function(){
		$('div.loading',ref).animate({ width: '0px' }, 400, function(){
			$('div.loading',ref).hide();
		});
	});	
}

function initCounter(timeout,ref){
	var currCount = $('div.loading span',ref).html();
	currCount = parseInt(currCount.substring(0,currCount.indexOf('%',0)));
	$('div.loading span',ref).text((currCount+5)+'%');
	if(currCount+5 < 100){
		setTimeout('initCounter()',timeout);
	} else {
		setTimeout(function(){ $('div.loading span',ref).text('0%'); },100);
	}
}
