$(document).ready(function(){
	
	//focus clear fields
	$('.default').each(function(){
		$(this).css({'color' : '#aeaeae'});
		var _default = $(this)[0].defaultValue;//document.getElementById($(this).attr('id')).defaultValue;
		$(this).focus(function(){
			$(this).css({'color' : '#000'});
			if($(this).val() == _default){
				$(this).val('');	
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val(_default);	
				$(this).css({'color' : '#aeaeae'});
			}
		});						
	});
	//end of clear focus
	
	
	//textarea simple autoexpand
	$('textarea').each(function(){
		$(this).css({overflow: 'hidden'});
		$(this).each(function(){
			if($(this)[0].scrollHeight > $(this).height())	$(this).css({height: $(this)[0].scrollHeight + 50});
		});
		$(this).keyup(expandTextarea);
	});
	
	function expandTextarea(){
		if($(this)[0].scrollHeight > $(this).height()){
			$(this).animate({height: $(this)[0].scrollHeight + 50}, {duration:200, queue:false});
		}
	}
	//end of autoexpand
	

	//generic toggler
	$('.toggler').each(function(){
		$('.toggler').next().hide();
	});
	$('.toggler').click(function(){	
		var _h = parseInt($(this).next().css('height'));//height();
		if(_h > 0 || _h == NaN){
			$(this).removeClass('selected');
		}else{
			$(this).addClass('selected');
		}
		$(this).next().animate({height: 'toggle', opacity: 1}, 300);
		if($(this).children('a').text() == '[-]'){
			$(this).children('a').text('[+]');
		}else{
			$(this).children('a').text('[-]');
		}
		return false;
	});
	$('.toggler_close').click(function(){
		obj = $(this);
		while($(obj).prev('.toggler').length == 0){
			obj = $(obj).parent();	
		}
		$(obj).prev().trigger('click');
		return false;
	});
	//end of generic toggler
	
	
	//simple alert
	$(".alert").animate({opacity: 1}, 3000).animate({opacity: 0 }, {duration: 600, easing: 'easeInQuad'}).animate({height: 0, paddingTop: 0, paddingBottom: 0, borderWidth: 0, marginBottom: 0, marginTop: 0}, {duration: 500, easing: 'easeInQuad', complete: 
		function(){$(this).remove();}
	});
	//end of alert
	
	
	//modal
	$('a.question').click(function(){
	
		var modal = $('#modal');
		var overlay = $('#modal-overlay');
		var close = $('<a href="#close" class="close">&times;</a>');
		
		modal.prepend(close);
	
		overlay.css({opacity: 0, display: 'block', position: 'fixed', width: $(window).width(), height: $(window).height()}).animate({opacity: .7}, {duration: 300, queue: false});
		modal.css({opacity: 0, display: 'block', position: 'fixed', left: ( $(window).width() - modal.outerWidth() )/2, top: ( $(window).height() - modal.outerHeight() )/2 }).delay(300).animate({opacity: 1}, {duration: 600});
	
		close.click(function(){
			modal.animate({opacity: 0}, {duration: 300, queue: false, complete: function(){ $(this).css({display: 'none'})}});		
			overlay.delay(300).animate({opacity: 0}, {duration: 500, complete: function(){ $(this).css({display: 'none'})}});	
			return false;
		});
		
		overlay.click(function(){ close.trigger('click');});
	
		return false;
	
	});
	//end of modal

	// staff	
	var width = 109;
	var height = 109;
	var $staff = $('#staff');
	var $info = $staff.find('.info');
	var $prev;
	
	$('#staff div:not(.info)').each(function(){
								  
			var $this = $(this);		
			var clone = $this.find('>img').css({opacity: .6}).clone();	
			
			$this.append(clone);
			clone.css({opacity: 0, left: 0/*, left: '100%', height: '50%'*/});
			
	}).click(function(){	
	
			$info.css({display: 'block', width: 'auto'});
	
			if($prev){
				$prev.find('img:eq(1)').animate({opacity: 0/*, left: '100%', height: '50%'*/}, {duration: 400, queue: false, easing: 'easeInOutQuad'});
				$prev.css({border: 'none', margin: '0 1px 1px 0'});
			}
	
			var $this = $(this);		
			var pos = [$this.index()%7+1, Math.ceil(($this.index()+1)/7)-1];
			
			
			$info.html($this.html()).css({left: pos[0]*width, top: pos[1]*height});
			$this.find('img:eq(1)').animate({opacity: 1/*, left: 0, height: '100%'*/}, {duration: 600, queue: false, easing: 'easeInOutQuad'});
			$info.animate({opacity: .85});
			
			var offset = $staff.width() - $info.position().left - $info.outerWidth();
									
			if(offset <= 0){
				
				while($info.find('strong').height() > 26){
					$info.css({width: $info.width()+2});
				}
				
				$info.css({left: pos[0]*width - 110 - $info.outerWidth()});
			}
			
						
			$prev = $this;	
			
			var idx = $this.index();
			
			$('#staff div:not(.info):not(:eq(' + idx +'))').find('>img:eq(0)').animate({opacity: .2});
			
	}).hover(
		function(){
			$(this).find('img:eq(0)').animate({opacity: 1}, {queue: false, easing: 'easeOutQuad'});
		},
		function(){
			var return_opacity = $prev ? .2 : .6;			
			$(this).find('img:eq(0)').animate({opacity: return_opacity}, {duration: 400, queue: false, easing: 'easeInOutQuad'});			
		}
	);
	
	$staff.find('.close').live('click', function(){
		if(!$prev) return false;
		
		$prev.find('img:eq(1)').animate({opacity: 0}, {duration: 400, queue: false, easing: 'easeInOutQuad'});
		$prev.css({border: 'none', margin: '0 1px 1px 0'});		
		$info.animate({opacity: 0}, {queue: false, complete: function(){$info.css({display: 'none'});}});
		
		$('#staff div:not(.info)').find('>img:eq(0)').animate({opacity: .6});
		$prev = null;
		
		return false;
	});
	

});
