Project = window.Project || {};

window.isIE 	= jQuery.browser.msie;
window.isIE6	= (isIE && parseInt(jQuery.browser.version)==6);

jQuery(document).ready(function() {
	
	
	jQuery('.filter input[type=checkbox]').click(function() {
		
		
		jQuery(this).attr('class', (this.checked ? 'checked' : ''));
		
		
		jQuery(document.body).removeClass('wettbewerb').removeClass('planer').addClass('filter');
		
		
		if (!this.checked && jQuery('.filter .box:first :checked').length == 0) {
			jQuery('.list>ul>li').removeClass('visible').hide();
			jQuery(document.body).removeClass('filter');
			return;
		}


		
		var selLeistung = [];
		jQuery('.filter .leistung input[type=checkbox]').each(function() {
			if (this.checked) {
				selLeistung.push(":has([name='filter-leistungen']) :has([value*='" + jQuery.trim(this.value) + "'])");
			}
		});
		if (selLeistung.length == 0) selLeistung.push('[class]');
		
		
		var selLand = [];
		jQuery('.filter .land input[type=checkbox]').each(function() {
			if (this.checked) {
				selLand.push(":has([name='filter-land']) :has([value*='" + jQuery.trim(this.value) + "'])");
			}
		});
		if (selLand.length == 0) selLand.push('[class]');
		
		
		var selObjekttyp = [];
		jQuery('.filter .objekttyp input[type=checkbox]').each(function() {
			if (this.checked) {
				selObjekttyp.push(":has([name='filter-objekttyp']) :has([value*='" + jQuery.trim(this.value) + "'])");
			}
		});
		if (selObjekttyp.length == 0) selObjekttyp.push('[class]');


		var sel = ':not('+selLeistung.join(',')+'), :not('+selLand.join(',')+'), :not('+selObjekttyp.join(',')+')';
		
		jQuery('.list>ul>li').addClass('visible').removeClass('hidden').filter(sel).removeClass('visible').addClass('hidden');

		
		jQuery('.list>ul>li.visible').filter(':hidden').show();
		jQuery('.list>ul>li.hidden').filter(':visible').hide();
		
		if (jQuery('.visible .expanded .ref').length > 0) {
			jQuery('.export-html span,.export-doc span').hide();
		}
		
	});
		

	jQuery('#content .content-references .list ul li .object .title, #content .content-references .list ul li .close').click(function() {
					
					

		window.location.hash = jQuery(this).parents('.object').find('.ref').attr('id').substring(4);
			
		var obj = jQuery(this).parents('.object');
		
		jQuery(document.body).addClass('filter');
		obj.parents('li').addClass('visible').show();
		
		//if (obj.hasClass('expanded') && !jQuery(this).hasClass('close')) return;

		// Ignore the onclick event for the duration of the animation.
		if (obj.data('locked')) return;
		obj.data('locked',true);
		setTimeout(function() {obj.data('locked',false)}, 750);
		
	
		if (obj.hasClass('expanded')) {
			
			obj.removeClass('expanded-full');
			setTimeout(function() {
				obj.removeClass('expanded');
			},300);
			
			setTimeout(function() {
				
				// Only animate the height in IE.
				obj.animate({
						height: '35px'
					},
					{ duration: 500, easing: 'easeOutExpo'});
					
				
			},100);
			
			
			// Fade out the content before starting the collapsing animation.
			if (isIE6) {
				obj.find('.details').hide();
			}
			else {
				obj.find('.details').fadeOut(100);
			}
		}
		else {
			
			setTimeout(function() {
				obj.addClass('expanded-full');
			},500);
			
			obj.addClass('expanded');
			
			jQuery('.export-html span,.export-doc span').hide();
			
			
			// Images aren't preloaded. The path of the image is stored in
			// the 'title' attribute of the image container.
			obj.find('.img').each(function() {
				jQuery(this).css('background-image',"url("+jQuery(this).attr('title')+")").removeClass('img').attr('title',obj.find('.title').text());
			});
			
			
			// When expanding the box, we set the new colors immediatly.
			// obj.css({
			// 	backgroundColor: Project.Theme.Colors.dark,
			// 	color: '#ffffff'
			// });

			obj.animate({
					height: '200px'
				},
				{ duration: 300, easing: 'easeInOutExpo'});


				// Don't fade in the content to soon.
				setTimeout(function() {
					if (isIE6) {
						obj.find('.details').show();
					}
					else {
						obj.find('.details').fadeIn(500);
					}
				},300);
		}
		
		
	});
	
	
	
	jQuery('.fancy').fancybox({
		'overlayShow':	true
	});
	
	
	/**
	 * Dock.
	 */

	var dockCounter = 0;
	var w = jQuery('#dock li').length * 50 + 50;
	jQuery('#dock ul').css({paddingLeft: '150px', width: w+'px', margin: '0 auto'});
	jQuery('#dock ul li a').hover(
		function() { // mouse over
			dockCounter++;
			var l = dockCounter*5;
			jQuery(this).parents('ul').stop().animate({left: '-'+l+'px'}, 200);
			jQuery(this).stop().animate({width: '55px', height: '55px'}, 200);
		},
		function() { // mouse out
			var that = this;
			setTimeout(function() {
				dockCounter--;
				var l = dockCounter*5;
			jQuery(that).parents('ul').stop().animate({left: '-'+l+'px'}, 500);
			jQuery(that).stop().animate({width: '45px', height: '45px'}, 500);
			},100);
		}
	);
	
	setTimeout(function() {
		jQuery('#dock ul').fadeIn();
	},100);
	







	/**
	 * References page: Filter box positioning.
	 */

	if (window.location.hash && window.location.hash.length>1) {
					
		var ref = window.location.hash.substring(1);
		

		if (ref.substring(0,3)!='ref') {
			ref = 'ref-' + ref;
		}

		jQuery('.'+ref+' .title').trigger('click'); 
		setTimeout(function() { window.location.hash = ref;}, 250);


	}
	
	
	var animTimerId = 0;
	
	var magneticFilter = function() {
	
	
		clearInterval(animTimerId);
	
		var paddingTop 	= jQuery('#content').position().top;
		var scrollTop	= jQuery(document).scrollTop();

		var f = jQuery('.filter');
		if (scrollTop > paddingTop) {
			if (jQuery.browser.safari) {
				f.css({top:(scrollTop-paddingTop)+'px'});
			}
			else {
				animTimerId = setInterval(function() { 
					clearInterval(animTimerId);
					f.stop().animate({top:(scrollTop-paddingTop)+'px'},{easing: 'easeInExpo'});
				},10);
			}
		}
		else {
			if (jQuery.browser.safari) {
				f.css({top:0});
			}
			else {
				animTimerId = setInterval(function() {
					clearInterval(animTimerId);
					f.stop().animate({top:0},{easing: 'easeInExpo'});
				},10);
			}
		}				
	}

	jQuery(window).scroll(magneticFilter).resize(magneticFilter);



							
	/**
	 * References page: Export.
	 */

	jQuery('.export-html').click(function() {
		buildAndSubmitExportForm("html");
		return false;
	});

	jQuery('.export-doc').click(function() {
		buildAndSubmitExportForm("doc");
		return false;
	});

	function buildAndSubmitExportForm(format) {
		
		
		if (jQuery('.visible .expanded .ref').length == 0) {
			var $a = jQuery('.export-'+format);
			var $error = $a.children('span');
			if ($error.length==0) {
				$error = jQuery("<span>Es wurde keine Referenz angew&auml;hlt.</span>").css({display:'block',color:'#c33'});
				$a.append($error);
			}
			else {
				$error.show()
			}
				
			return false;
		}
		

		jQuery('#exportSelectedForm input').remove();

		// Set export format.
		jQuery('#exportSelectedForm').append('<input type="hidden" name="format" value="'+ format +'" \/>');

		// Collect all expanded items
		jQuery('.visible .expanded .ref').each(function() {
			jQuery('#exportSelectedForm').append('<input type="hidden" name="ref" value="'+jQuery(this).attr('id').substring(4)+'" \/>');
		});
	
	
		jQuery('#exportSelectedForm').submit();
	
	}

	/**
	 * Avoid the displaying of an outline in IE and Firefox.
	 */

	jQuery('.c1 a').focus(function() { this.blur(); });
	jQuery('#content .content-references .list ul li .close').focus(function() { this.blur(); });
	
	
	
	/**
	 * JavaScript Navigation for Filter items.
	 */
	
	jQuery('#content .content-references .box .links a').click(function() {
		jQuery('.filter input[type=checkbox]').attr('checked',false).attr('class','');
		jQuery('.list>ul>li').addClass('hidden').hide();
		jQuery(document.body).removeClass('filter');
		var c = jQuery(this).attr('id');
		if (jQuery(document.body).hasClass(c)) c = '';
		jQuery(document.body).removeClass('wettbewerb').removeClass('planer').addClass(c);
	});
	
	
	/**
	 * Reference table list.
	 */
	jQuery('#content .content-references .table-list tbody tr.item').hover(
		function() {
			if (jQuery(this).find('input[name=ref]').length > 0) {
				jQuery(this).find('td').css({
					backgroundColor: Project.Theme.Colors.dark,
					color: '#fff'
				});
			}
			else {
				jQuery(this).find('td').css({
					backgroundColor: '#727773',
					color: '#fff'
				});
			}
		},
		function() {
			jQuery(this).find('td').css({backgroundColor:'',color:''});
		}
	)
	.click(function() {
		var ref = jQuery(this).find('input[name=ref]'); 
		if (ref.length > 0) {
			window.location.href = Project.PREFIX+'/referenzobjekte?r='+ref.val()+'#'+ref.val();
		}
	});

});







Project.References = {
	
	collapseAll: function() {
		jQuery('.expanded .close').trigger('click');
        jQuery('.filter input[type=checkbox]').attr('checked',false).attr('class','');
        jQuery('.list>ul>li').addClass('hidden').hide();
        jQuery(document.body).removeClass('filter');
	}
	
}





Project.Home = {
	
	references: [],
	
	rows: [],
	timerId: null,
	refs: [],
		
	start: function() {
		for (var i = 0; i < this.references.length; i++) {
			var src = this.references[i].src;
			var img = new Image();
			img.src = src;
			this.references[i].img = img;
		}
		var len = this.references.length;
		
		this.rows = jQuery('.row');
		
		this.load();
		
		var that = this;
		setInterval(function() {
			that.load();
		},10000)
	},
	
	_fill: function() {
		this.refs = [];
		for (var i = 0; i<this.references.length; i++) {
			this.refs.push(this.references[i]);
		}
	},
	
	load: function() {
		
		var t = 500;
		
		var that = this;
		this.rows.each(function() {
			t += 100;			
			var cell = jQuery(this).find('.cell').eq(0);
			setTimeout(function() {
				that.unset(cell);
			},t);
		});
		
		
		t += 1000;
		
		
		var that = this;
		this.rows.each(function() {
			t += 100;			
			var cell = jQuery(this).find('.cell').eq(0);
			setTimeout(function() {
				that.set(cell);
			},t);
		});
	},
	
	set: function(cell) {
		
		if (!cell.hasClass('static')) {
		
			if (this.references.length > 0) {
				if (this.refs.length == 0) this._fill();
				
				var r 	= Math.random();
				var max	= this.refs.length;
				
				r = Math.floor(r*max);
		
				var ref = this.refs[r];
		
				this.refs.splice(r,1);
		
				cell.css('background-image','url('+ref.src+')');
				cell.attr('href', ref.url + '#' + ref.ref);
			}
		
			
			if (isIE6) cell.css({opacity:1});
			else cell.fadeTo(1000,1);

		}
		
		cell = cell.nextAll('.cell:not(.static)');
		if (cell.length > 0) {
			var that = this;
			setTimeout(function() {
				that.set(cell.eq(0));
			},200);
		}
	},
	
	unset: function(cell) {
		if (!cell.hasClass('static')) {
			if (isIE6) cell.css({opacity:0});
			else cell.fadeTo(500,0);
		}
		cell = cell.nextAll('.cell:not(.static)');
		if (cell.length > 0) {
			var that = this;
			setTimeout(function() {
				that.unset(cell.eq(0));
			},200);
		}
	}
	
}
