// animation.js
/**
 * @author Bastian Haustein
 * @copyright (c)2009 by source-lounge Bastian Haustein
 * @package pop
 */


var popAniConfig = {
	 aDivs:				['pic1','pic2','pic3','pic4','pic5','pic6']
	,sPicturePath:		'/media/template/global/pictures/'
	,aPictures:			['01.jpg','02.jpg','03.jpg','04.jpg','05.jpg','06.jpg','07.jpg','08.jpg','09.jpg','10.jpg','11.jpg','12.jpg','13.jpg','14.jpg','15.jpg','16.jpg','17.jpg','18.jpg','19.jpg','20.jpg','21.jpg','22.jpg','23.jpg']
	,iTimeToStart:		3000
	,iMinTimeBetween:	3500
	,iMaxTimeBetween:	5000
	,iFadeOutSpeed:		6000
	,iFadeInDelay:		3000
	,iFadeInSpeed:		3000
}

var popAni = {
	init: function() {
		// store avaliable pictures in a data-store
		for(var i in popAniConfig.aPictures) {	
			popAni.aPicturesAvaliable.push(i);
		}
		// initialize pictures
		for(var iDivId in popAniConfig.aDivs) {
			iNewPic = popAni.getRandomPic();
			popAni.aPicturesSet[iDivId]=iNewPic;
			$('#'+popAniConfig.aDivs[iDivId]).css('display','none').html('<img src="" alt=""/>');
			$('#'+popAniConfig.aDivs[iDivId]+' > img').load(function(){
				$(this.parentNode).fadeIn(popAniConfig.iFadeInSpeed);
			}).attr('src', popAniConfig.sPicturePath + popAniConfig.aPictures[iNewPic]);
		}		
		// start animation
		window.setTimeout(popAni.doAni, popAniConfig.iTimeToStart);
	}
	,aPicturesSet: []
	,aPicturesAvaliable: []
	,iLastChangedDiv: -1
	,getRandomPic: function() {
		// get random pic number
		iNewPic = popAni.aPicturesAvaliable[Math.floor(Math.random() * (popAni.aPicturesAvaliable.length))];
		// remove it from the data-store
		for(var i in popAni.aPicturesAvaliable) {
			if(popAni.aPicturesAvaliable[i]==iNewPic) {
				popAni.aPicturesAvaliable.splice(i,1);
				break;
			}
		}
		return iNewPic;
	}
	,doAni: function() {
		// div aussuchen
		while(popAni.iLastChangedDiv == (iDivId = Math.floor(Math.random() * (popAniConfig.aDivs.length))));
		popAni.iLastChangedDiv = iDivId;
		// newPic aussuchen
		iNewPicId = popAni.getRandomPic();
		// helperDiv hinter das aktuelle Bild schieben und mit aktuellem Bild belegen
		$('#pic-anim').css('margin-left', $('#'+popAniConfig.aDivs[iDivId]).css('margin-left'));
		$('#pic-anim').css('opacity', 100);
		$('#pic-anim').html($('#'+popAniConfig.aDivs[iDivId]).html());
		// give the Browser some time to render picture into the new div
		window.setTimeout(function() {popAni.exchangePic(iDivId,iNewPicId)},200);
	}
	,exchangePic: function(iDivId, iNewPic) {
		// rotate the picture store
		popAni.aPicturesAvaliable.push(popAni.aPicturesSet[iDivId]);
		popAni.aPicturesSet[iDivId]=iNewPic;
		// set the "old" picture of invisible so we can  use the assistent picture in the background
		$('#'+popAniConfig.aDivs[iDivId]).css('display', 'none');
		$('#pic-anim').animate({'opacity': 0.40 }, popAniConfig.iFadeOutSpeed);//, '', function() {
		window.setTimeout(function() {
			$('#'+popAniConfig.aDivs[iDivId]+' > img').attr('src', popAniConfig.sPicturePath + popAniConfig.aPictures[iNewPic]);
			var iTimeWaiting = Math.floor(Math.random() * (popAniConfig.iMaxTimeBetween - popAniConfig.iMinTimeBetween + 1)) + popAniConfig.iMinTimeBetween;
			window.setTimeout(popAni.doAni, iTimeWaiting);
		}, popAniConfig.iFadeInDelay);
	}
}

var popResizing =  {
	 init:				function() {
		// register function für resizing
		$(window).bind('resize',popResizing.resize);
		// do it once for initialization
		popResizing.resize();
		popResizing.initScrolling();	
	}
	,resize: 			function() {
		var height	= Math.max($(window).height(),600);
		var width	= $(window).width();
		$('#doc').height(height-60);
		$('#main').height(height-355);
		$('#scroll-pane').height(height-355);
		$('.jScrollPaneContainer').height(height-355);
		$('#banner-overflow').css('top',(height-200)+'px');
		$('#ft').css('top',(height-50)+'px');
		$('#loader').css('top',((height-100)/2)+'px');
		$('#right-pane').css('height',(height-60)+'px');
		var me = $('#scroll-pane');	
		if(me.css('display') == 'none') {
			me.css('visibility', 'hidden');
			me.css('display', 'block');
		}
		popResizing.bSizeChanged = true;
	}
	, bSizeChanged:		false
	, scrollPainHeight:	0
	,initScrolling:		function() {
		var me = $('#scroll-pane');
		var height = me.height();
		if(height>0 && (height != popResizing.scrollPainHeight || popResizing.bSizeChanged == true)) {
			popResizing.resize();
			popResizing.scrollPainHeight = height;
			popResizing.bSizeChanged = false;
			me.jScrollPane({dragMaxHeight:50});
			window.setTimeout(popResizing.initScrolling, 200);
		}	
		else {
			window.setTimeout(popResizing.initScrolling, 200);
		}	
	}
}

var popLogoAni = {
	init: function() {
	if(document.location.href.indexOf('?') != -1) {
		$('#logo').fadeIn('fast');
		$('#right-pane div').fadeIn('fast');
		$('#banner-overflow').fadeIn('fast');
		smartNav.init();
		return;
	}
		$('#doc').prepend('<div id="logoAni" style="text-align:center; width: 600px;position:absolute;top:'+(Math.max($(window).height(),600) -200)/2+'px;left:115px;z-index:999;"><img src="/media/template/global/pop-logo-141.jpg" alt="" /><br /><br /><br /><span id="text1">Institut zur Forschung und Lehre der Psychodynamischen Beratung und des Coaching</span><span id="text2" style="display:none">Business Coaching - von Mensch zu Mensch</span></div>');
		window.setTimeout(
			function() {
				$('#text1').fadeOut(
					'fast'
//					, function() {
//						$('#text2').fadeIn(
//							'fast'
//							, function() {
//								window.setTimeout( 
//									function() {
//									$('#logoAni').fadeOut(
//										1500
										, function() {
												$('#logo').fadeIn('slow');
												$('#logoAni').css('display', 'none');
												$('#right-pane div').fadeIn('slow');
												$('#banner-overflow').fadeIn('slow');
												window.setTimeout(
													function() {
														$('#loader').fadeIn('slow');
														window.setTimeout(
															function() {
																smartNav.init();
															}
															,700
														);
													}
													,700
												);
											}
//										);
//									}
//									, 1500
//								);
//							}
//						);
//					}
				);			
			}
			, 2500
		); 
	}
};


var popContact = {
	init: function() {
		$('input,select').bind('focus', function() {
			$(this.parentNode).css('background-image', 'url(\'/media/template/global/kontakt/kontakt_active.jpg\')');
		}).bind('blur', function() {
			$(this.parentNode).css('background-image', 'url(\'/media/template/global/kontakt/kontakt.jpg\')');
		});
		$('textarea').bind('focus', function() {
			$(this.parentNode).css('background-image', 'url(\'/media/template/global/kontakt/kontakt_notiz_active.jpg\')');
		}).bind('blur', function() {
			$(this.parentNode).css('background-image', 'url(\'/media/template/global/kontakt/kontakt_notiz.jpg\')');
		});
		$('#contact div.button').bind('mouseover',function() {
			$(this).css('background-position', '0 -38px');
		}).bind('mouseout',function() {
			$(this).css('background-position', '0 0');
		}).bind('mousedown',function() {
			$(this).css('background-position', '0 -76px');
		});
		$('#contact div.button').bind('click',function() {
			$('#ccontact').submit()//fadeOut('slow');
			$('#loader').fadeIn('fast');
			/*window.setTimeout(function() {
				$('#contact').html('<p>&nbsp;</p><h2>Vielen Dank für Ihre Anfrage!</h2><p>Wir werden uns umgehend bei Ihnen melden.</p>').fadeIn('slow');
				$('#loader').fadeOut('fast');
			}, 2000);*/
		});
		popResizing.resize();
	}
}


// suppress flickering content
document.write('<style type="text/css">');
document.write('#logo {display:none;} ');
document.write('#loader {display:none;} ');
document.write('#banner-overflow {display:none;} ');
document.write('#right-pane div {display:none;} ');
if($.browser.msie) {
	document.write('#sub-menu ul li img {margin-top:0.4615em;} ');
}
document.write('</style>');

$(document).ready(function(){
	$('#transparent-stripe').css('opacity','0.5');
	popResizing.init();
	popAni.init();
	popLogoAni.init();
});

