﻿

$(document).ready( function() {
	$('#content').css('display', 'block');
});


/* ************************************************************************** */

var show = function(){
	var tgt = '#intro';
	if( window.location.hash ){
		//alert('#: ' + window.location.hash);
		tgt = window.location.hash;
	}
	//expand( tgt );
	setTimeout(function() {
		expand( tgt );
	}, 500);
	return false;
};


var setHash = function( id ){
	if ($.browser.safari) {
		
		// borrowed from jquery.tabs by Klaus Hartl (stilbuero.de)
		// Simply setting location.hash puts Safari into the eternal load state... ugh! Submit a form instead.
		var tempForm = $('<form action="' + id + '"><div><input type="submit" value="h" /></div></form>').get(0); // no need to append it to the body
		tempForm.submit(); // does not trigger the form's submit event...
		
	} else {
		window.location.hash = id.replace('#', '');
	}
	return false;
};



var initDetails = function(){

	// hide imgs in info-short immediately
	$('.left-col img').css('display', 'block');

	// start init with a little delay
	setTimeout(function() {
		_initDetails();
	}, 100);
}
var _initDetails = function(){
	$('.details').css('color', '#fff');
	$('.details p a').css('color', '#fff');
	$('.details p.adr a').css('color', '#333'); /* reset this one... */
	$('.intro .details li').css('background-image', 'url(/gfx/list_bullet.gif)');
	$('.project .details .clear').css('background-color', '#333');
};




var initButtons = function(){

	// add functions to imgs	
	$('.info-short img').click( 
		function(e){
			expand( String( '#' + $(this).parent().parent().parent().attr('id') ) );
			collapseNavi();
			e.preventDefault();
			return false;
		} );
	$('.info-short img').css('cursor', 'pointer');

	// append buttons
	var btn = '<a href="#" class="link-detail" title="Details einblenden">Details einblenden</a><a href="#" class="link-detail-close" title="Details ausblenden">Details ausblenden"</a>';
	$('.project').append( btn );
	$('.intro').append( btn );
	
	$('.link-detail-close').hide();
	
	
	$('.project').find('.link-detail').click( 
		function(e){
			//alert('expand: ' + String( '#' + $(this).parent().attr('id') ));
			expand( String( '#' + $(this).parent().attr('id') ) );
			collapseNavi();
			e.preventDefault();
			return false;
		} );
	$('.project').find('.link-detail-close').click( 
		function(e){
			//alert('collapse: ' + String( '#' + $(this).parent().attr('id') ));
			collapse( String( '#' + $(this).parent().attr('id') ) );
			e.preventDefault();
			return false;
		} );
		

	$('#intro').find('.link-detail').click( 
		function(e){ 
			expand('#intro'); return false; 
			e.preventDefault();
		} );
	$('#intro').find('.link-detail-close').click( 
		function(e){ 
			collapse('#intro'); return false; 
			e.preventDefault();
		} );
		
	// contaktform, newsletterform
	$('#form').find('h2').click( 
		function(e){ 
			expand('#form'); return false; 
			e.preventDefault();
		} );
	$('#form').css('cursor', 'pointer');	
}


var initHome = function(){

	// add functions to imgs	
	$('.info-short img').click( 
		function(e){
			window.location.href =  '/' + $(this).parent().parent().parent().find('.link-detail').attr('href');
			e.preventDefault();
			return false;
		} );
	$('.info-short img').css('cursor', 'pointer');
}


var collapse = function( id ){
	if( $( String( id + ' > .details' )).css('display') == 'block' ){

		unloadPlayer( id );

		setTimeout(function() {
			$( String( id + ' > .details') ).slideUp(800);
			$( String( id + ' > .details') ).highlightFade({start:'#333', end:'#fff', speed:800});
			$( String( id + ' > .info-short') ).highlightFade({start:'#333', end:'#fff', speed:800});
			$( String( id + ' > .info-short') ).highlightFade({start:'#fff', end:'#000', attr: 'color', speed:800});

			//$( String( id + ' > .info-short > [img]') ).slideDown(600);
			//$( String( id + ' > .info-short > .left-col[img]') ).slideDown(600);
			$( String( id + ' > .info-short > .left-col img') ).attr('width', '78');
			$( String( id + ' > .info-short > .left-col img') ).slideDown(500);

			$( String( id + ' > .link-detail') ).show();
			$( String( id + ' > .link-detail-close') ).hide();
		}, 10);
	}
	// reset rollovers
	$('.link-detail-close').css('background-position', '0 0');
	$('.link-detail').css('background-position', '0 0');

	if( id == '#form'){
		$('#form').css('cursor', 'pointer');
	}

};

var expand = function( id ){
	collapseAll( id );

	$( String( id + ' > .details') ).slideDown(800);
	$( String( id + ' > .details') ).highlightFade({start:'#fff', end:'#333', speed:800});
	$( String( id + ' > .info-short') ).highlightFade({start:'#fff', end:'#333', speed:800});
	$( String( id + ' > .info-short') ).highlightFade({start:'#000', end:'#fff', attr: 'color', speed:800});

	//$( String( id + ' > .info-short > [img]') ).slideUp(600);
	//$( String( id + ' > .info-short > .left-col[img]') ).slideUp(600);
	$( String( id + ' > .info-short > .left-col img') ).attr('width', '78');
	$( String( id + ' > .info-short > .left-col img') ).slideUp(500);

	$( String( id + ' > .link-detail') ).hide();
	$( String( id + ' > .link-detail-close') ).show();

	$( String( id + '  .flash-req') ).hide();
	
	if( id == '#form'){
		$('#form').css('cursor', 'normal');
	}

	setTimeout(function() {
		// look for clip ids
		var elm = String(id.replace('#', '') );

		var c = clips[elm];
		if( c ) c = c.replace(' ', '');

		var p = imgs[elm];
		if( p ) p = p.replace(' ', '');

		if ( c && p ){
			c = c.split(',');
			p = p.split(',');
			for( i=0; i<c.length; i++){
				loadPlayer( 	String( '#c-' + id.replace('#p-', '') + '-' + i ),  
								'/vid/' + c[i], 
								'/img/' + p[i] 
							);
			}
		}
	}, 800);


	// setHash( id ); /* scroll bug / cant deal with head-margin... */
	

};

var collapseAll = function( id ){
	if( id != 'intro' && $('#intro').length ) {
		collapse('#intro');
		collapseNavi();
	}
	$('.project').each( function(){ 
		if( id != String('#' + $(this).attr('id') ) ) { 
			collapse( String('#' + $(this).attr('id') ) ); 
		}
	} );	
};


var loadPlayer = function( id, file, image ){
	if( id == '#intro' ){ return; }

	var img = image.length > 0 ? '&image=' + image : '';
	var PL = { 	movie:"/gfx/flvplayer3.swf", 
				width:"534px", 
				height:"325px", /* height + 25px */
				majorversion:"8", /* better flv codec */
				build:"0", 
				wmode:"transparent",
				flashvars:"file="+file+"&autoStart=false"+img
	};
	UFO.create(PL, id.replace('#', '') );
}

var unloadPlayer = function( id ){
	// look for clip ids
	var c = clips[ String(id.replace('#', '') ) ];
	if ( c ){
		c = c.replace(' ', '');
		c = c.split(',');
		for( i=0; i<c.length; i++){
			/*$( String( '#c-' + id.replace('#p-', '') + '-' + i ) ).css("visibility", "visible");*/
			$( String( '#c-' + id.replace('#p-', '') + '-' + i ) ).empty();
		}
	}
	//$( String( '#c-' + id.replace('#p-', '') + '-1' ) ).empty();
}



var collapseNavi = function(){
	$('.home #header').animate({
		height: '95px'
	}, 400);
	$('body.home').animate({
		paddingTop: '96px'
	}, 400);
}
