jQuery(document).ready(function(){


/* ### ACCORDEONS ### */
	var detailInOut = function(e){
		var cur = $(this);

		if(cur.hasClass('open') && $(e.target).hasClass('csc-subheader') ){
			cur.children('div.detail').slideUp(600, function(){ $(this).prev().slideDown(200); cur.removeClass('open'); } );
			
		}else if(cur.children('div.abstract').length > 0 && cur.children('.detail').length > 0 ){
			cur.children('div.abstract').slideUp(200, function(){ $(this).next().slideDown(600); cur.addClass('open'); } );
			
		}else{

		}
	}
	$('div.csc-default').click(detailInOut);


	var detailToggle = function(e){
		if( $(e.target).hasClass('csc-subheader') || $(this).hasClass('hastitle') && !$(this).hasClass('open') ){
			$(this).children('div.detail').slideToggle(300, function(){ $(this).parent().toggleClass('open'); } );
		e.preventDefault();
		}
	}
	$('div.csc-frame-invisible').click(detailToggle);


	$('div.csc-subheader').hover(function(e){
		if( $(this).parent().hasClass('open') ){
			$(this).parent().addClass('toclose');
		}
	},
	function(e){
		$(this).parent().removeClass('toclose');
	});



/* ### INDEX COLUMNS ### */

	if($('#pid11').length > 0){
		//console.log('columnize');
		$('#c20 div.tx-cegallery-pi1').columnize({ width: 320 });
	}



/* ### SEARCH ### */

	//if($('#pid13').length > 0){

		//$('#all').after('<div id="searchoverlay"><div class="nav"><a onfocus="blurLink(this);" href="/">.eclat</a></div><div id="bigsearch"></div></div>');
		var searchoverlay = $('#searchoverlay');
		var searchbox = $('#searchbox');

		var originput = $('#searchbox input.ac_input');
		var biginput = $('#searchboxbig input.ac_input');


		originput.keyup( function(e){
			biginput.focus();
			biginput.val( originput.val() );				
		});

		//var searchorig = searchbox.parent();
		originput.focus( function(e){
			searchoverlay.fadeIn(600, function(e){
				biginput.focus();
				originput.val('');
			});			
		});
		searchoverlay.click(function(e){
			if( e.target == e.currentTarget ) {
				//console.log('fadeout');
				searchoverlay.fadeOut();

			}else{
				// console.log( e );	
			}
		});
	//}



/* ### PROJECT OVERVIEW OR DETAILS ### */

	if($('#pid3').length > 0){
		//console.log('columnize');

		
		if($('#c16 div.inoverlay').length > 0){

			/*
			$('#c16 .albumpictures').masonry({
				columnWidth: 136, 
				itemSelector: '.tx-cegallery-pi1-album_entry:not(.invis)',
				resizeable: true,
				animate: true
			});
			*/
			var mainnode = $('#main');
			var overlaynode;
			var closeOverlay = function(e){
				$('#overlay .tx-cegallery-pi1').height( $('#overlay .tx-cegallery-pi1').height() );
				//overlaynode.slideUp(600, showMain);
				overlaynode.animate({height:'0%',top:'100%'},{complete:showMain});
				mainnode.slideDown();
				//$('#c16 div.detail').masonry();

				$('body').unbind('click');
			}
			var showMain = function(e){
				overlaynode.remove();
				$('#closeoverlay').remove();
				$('body').removeClass('jsSingleview');
				$('style').remove();
				
			}	
			var loaddetail = function(e){
				e.preventDefault();
				//console.log('Open detail '+ e.target );
				if(mainnode.is(':visible')){
					$('body').addClass('jsSingleview');

					/* listen on body to all clicks in order to close the overlay */
					$('body').click(function(e) {
						var tg = $(e.target);
						if(!tg.is('a') && !tg.is('img') && !tg.is('p') && !tg.is('span') ){
							closeOverlay(e);
						}
					});

					mainnode.height( $(window).height()-100 ).slideUp(600, function(){
							//$('body').addClass('jsSingleview');
							$('head').append('<style type="text/css" media="all">	<!--	html, body {height:100%;}	-->	</style>');   
					});
					
					$('#all').after('<div id="overlay"><img src="fileadmin/template/loading.gif" id="loadingani" /></div><div id="closeoverlay"> </div>');
					$('#closeoverlay').click(closeOverlay);
					overlaynode = $('#overlay');

					overlaynode.animate({top:'20%'}, 600);
				}
				overlaynode.load( $(this).attr('href') +'&type=96', function(){
					$('.tx-cegallery-pi1-detail_nav a').click(loaddetail);

					var dispaspect = ($(window).width() - 340) / ($(window).height() *.65 );
					var attrs = $('#cegallery-detailview img').first()[0]['attributes'];

					var imgw =0;
					var imgh =0;
					for(i=0; i<attrs.length; i++){
						if(imgw != 0 && imgh !=0){
							break;
						}else if(attrs[i]['name'] == 'width'){
							imgw = attrs[i]['value'];
						}else if(attrs[i]['name'] == 'height'){
							imgh = attrs[i]['value'];

						}
					}
					var aspect = imgw / imgh;
					if( aspect > dispaspect ){			
						$('#cegallery-detailview').css({'height': (100 / aspect * dispaspect  ) +'%'});
					}else {
						$('#cegallery-detailview').css({'height':'100%'});
					}
				});
			}
			$('#c16 div.albumpictures a').click(loaddetail);
		}else if($('#c16 div.detail').length > 0  && $('#c16 div.albumpictures').length == 0 ){
			$('#c16 div.detail').masonry({
				columnWidth: 126, 
				itemSelector: 'div.tx-cegallery-pi1-album_entry:not(div.invis)',
				resizeable: true,
				animate: true
			});
		}

	}



/* ### CLIENT AND PROJECT FILTERS ### */

	if( $('.filterbox').length > 0){
	
		var filternodes = Array();
		if($('#c16').length > 0){
			filternodes = $('#c16 .detail').children();
		}else if($('#c18').length > 0){
			filternodes = $('#c18 .tx-cegallery-pi1').children();
		}
				

		var setFilter = function(e){
			e.preventDefault();
			var anode = $(this);
			anode.blur();

			$.fn.toggleAni = function(){ return this.slideToggle(); };

			if($('#pid3').length > 0){
				window.setTimeout("$('#c16 .detail').first().masonry()", 100);
				$.fn.toggleAni = function(){ return this.fadeToggle(); };
			}

			if(anode.parent().hasClass('act')){
				$('.filterbox li.act').removeClass('act');

				filternodes.each(function(){
					var cn = $(this);

					if( cn.is(':hidden') ){
						cn.toggleClass('invis').toggleAni();
					}
				});
			}else{
				var fID = anode.attr('class');
			
				filternodes.each(function(){
					var cn = $(this);

					if(cn.is(':visible') && !cn.hasClass(fID) ){
						cn.toggleClass('invis').toggleAni();
					}else if(cn.is(':hidden') && cn.hasClass(fID) ){
						cn.toggleClass('invis').toggleAni();
					}
				});
				$('.filterbox li.act').removeClass('act');
				anode.parent().addClass('act');
			}
			

			return false;
		}

		$('.filterbox a').click(setFilter);
	}




/* ### STD. CONTENT IMAGE SLIDESHOW ### */

	if( $('.csc-textpic-imagewrap').length > 0){
		//console.log('create slideshows');
		var nextSlide = function(i, mynav, imgs){
			imgs.children(':visible').slideUp();
			mynav.children('.act').removeClass('act');

			i++;
			if(i==imgs.children().length){
				i = 0;
			}
			imgs.children(':eq('+ i +')').slideDown();
			mynav.children(':eq('+ i +')').addClass('act');
		}
		var slideImgClick = function(e){
			var i = $(this).children(':visible').index();
			var mynav = $(this).prev();
			var imgs = $(this);

			nextSlide(i, mynav, imgs);
			return false;
		}
/*
		var slideNavClick = function(e){
			var i = $(e.target).index()-1;
			var mynav = $(this);
			var imgs = $(this).next();

			nextSlide(i, mynav, imgs);
			return false;
		}
*/		
		$('.csc-textpic-imagewrap').each( function(i){
			var cur = $(this);
			var curimgs = cur.children();
			if(curimgs.length > 1 ){
				cur.addClass('slider');
				curimgs.css('display', 'none');
				curimgs.first().css('display', 'block');
				/*
				var navstr = '';
				for(i=0; i<curimgs.length; i++){
					navstr += '<span class="item"></span>';
				}
				cur.before('<div class="slideNavi">'+ navstr +'</div>');

				cur.prev().click(slideNavClick);
				cur.prev().children().first().addClass('act');
*/				cur.click(slideImgClick);
				
			}
		});
	}




/* ### INTRO ### */

	var cp=null;
	if($('#pid17').length > 0){
		var resizeIntro = function(e){
			$('#pid17 .tx-cegallery-pi1 .detail').height( $(window).height() -100 );
			$('#pid17 .tx-cegallery-pi1 .detail').width( $(window).width() ); 
		}
		$(window).resize(resizeIntro);
		resizeIntro(null);
		

		/*
// ### SUPER COOL DISTINCT INTRO ANIMATION ###

		cp = {'h': Math.round($(window).height()/2 ),'w': Math.round( $(window).width()/2 )};

		$('#pid17 a.imglink').each(function(){
			
			var poff = $(this).parent().offset();
			
			var offfrom = {'h':0,'w':0};
			var offto = {'h':0,'w':0};
			
			offfrom.h = ( Math.round(poff.top )- cp.h ) * 3;
			offfrom.w = ( Math.round(poff.left )- cp.w) * 3;

			offto.h = ( Math.round(poff.top )- cp.h ) * 8;
			offto.w = ( Math.round(poff.left )- cp.w) * 8;

			$(this).css({'left': offfrom.w +'px','top': offfrom.h +'px'}).delay(400).animate({'left':0,'top':0}, 800).delay(6000).animate({'left': offto.w *-1,'top': offto.h *-1}, 1200, function(){window.location ='/news';});
		});
		*/

// ### LAME STANDARD:
	$('#pid17 .tx-cegallery-pi1').hide().delay(400).fadeIn(600).delay(4000).fadeOut(600, function(){window.location ='/news';} );

	}


});

