/*!
 * Queensbridge jQuery-plugin
 * http://queensbridge.se/
 *
 * Date: Mon Feb 5 9:54:33 2010 -0500
 */
(function($) {
	var prefs = {
		aniSpeed : 250,
		aniSpeedSlow : 500,		
		expandedHeight : 173,
		collapsedHeight : 73,
		openHeight : 82,
		closeWidth : 0,
		closeHeight : 0,
		headerHeight: 80,
		isOpen : false,
		isMouseOver : false,
		liBgImage : '',
		isOpening: false
	};
	var newMethods = {

		// Init Method
		QBsolution : function() {
		
			// Set some initial prefs
			prefs.closeWidth = $(this).width();
			prefs.closeHeight = $(this).find('.wrapper').height();
			prefs.liBgImage = $(this).css('backgroundImage');
			
			// Bind events
			if( $(this).find('p.more').is('p') ) {
				$(this).find('h2').bind('mouseover', $(this).QBsolutionOver);
				$(this).find('h2').bind('mouseout', $(this).QBsolutionOut);
			}
			$(this).find('h2').bind('click', $(this).QBsolutionClick);
			$(this).find('div.close').bind('click', $(this).QBsolutionClick);
			
			// Align text middle on long H2
			$(this).find('h2.need').each(function(i) {
				h		= $(this).height();
				nh	= prefs.headerHeight - h;
				p		= nh/2;
				$(this).css({
					paddingTop: 		p + 'px',
					paddingBottom: 	p + 'px',
					height:					h + 'px'
				});
			});
			
		},
		
		QBsolutionOver : function() {
			if( !prefs.isOpen ) {		
				prefs.isMouseOver = true;
				var li = $(this).parents('li:first');
				var li_ID = $(li).attr('id');
				$(li).find('.wrapper').animate( { height: prefs.expandedHeight + 'px' }, prefs.aniSpeed );
				$(this).parents('ul').find('li:not(#' + li_ID + ')').stop();
				$(li).fadeTo(0, 1);
				$(this).parents('ul').find('li:not(#' + li_ID + ')').fadeTo(prefs.aniSpeed, 0.5);
			}
		},
		
		QBsolutionOut : function( callback ) {
			if( !prefs.isOpen ) {		
				prefs.isMouseOver = false;
				if( $(this).is('li') ) { var li = $(this); } else { var li = $(this).parents('li:first'); }
				var li_ID = $(li).attr('id');
				if( typeof(callback) == 'function' ) {
					$(li).find('.wrapper').animate({ height: prefs.collapsedHeight + 'px'}, prefs.aniSpeed, callback);
				} else {
					$(li).find('.wrapper').animate({ height: prefs.collapsedHeight + 'px'}, prefs.aniSpeed);
				}
				$(this).parents('ul').find('li:not(#' + li_ID + ')').stop();
				$(this).parents('ul').find('li:not(#' + li_ID + ')').fadeTo(prefs.aniSpeed, 1, function() {
					$(this).fixClearType();
				});
			}
		},
		
		QBsolutionClick : function() {
			
			if( !prefs.isOpening ) {
		
				if( prefs.isOpen ) {
					
					if( $(this).parents('li:first').hasClass('active') ) {
			
						$(this).QBsolutionClose();
	
					} else {
						
						var me = $(this);
						$(this).parents('ul:first li.active:first h2:first').QBsolutionClose( function() { $(me).QBsolutionClick() } );
						
					}
					
				} else {
		
					if( prefs.isMouseOver ) {
		  		 	$(this).QBsolutionOut( function() {  $(this).QBsolutionClick() } );
		  
		  		} else {
			
						$(this).QBsolutionOpen();
						prefs.isOpen = true;
		  
		  		}
				}
				
			}
			
		},
		
		QBsolutionOpen : function() {
			
			prefs.isOpening = true;
		
			// Get the parent LI
			var li = $(this).parents('li:first');
			var li_ID = $(li).attr('id');
			$(li).addClass('active');
			
			// Get the open-content wrapper			
			var o = $(li).find('.open');	
			
			// Fade out others
			$(this).parents('ul').find('li:not(#' + li_ID + ')').fadeTo(prefs.aniSpeed, 0.5);
			$('#companies, #swilkenbridge').fadeTo(prefs.aniSpeedSlow, 0, function() {
				$('#companies, #swilkenbridge').css('visibility', 'hidden');
			});
			
			$(li).css({
				backgroundImage : 'none'
			});
			
			// Do the animation
			$(li).find('.wrapper').animate(
				{
					height: ( o.height() + prefs.openHeight ) + 'px'
				},
				prefs.aniSpeed,
				
					function() {
						$(li).animate(
							{
								width: o.width() + 'px'
							},
							prefs.aniSpeed,
					
							function() {
								prefs.isOpening = false;
							}
					
						);
					}
			);
		},
		
		QBsolutionClose : function( callback ) {
			prefs.isOpening = true;		
			// Get the parent LI
			var li = $(this).parents('li:first');
			var li_ID = $(li).attr('id');
			
			// Get the open-content wrapper			
			var o = $(li).find('.open');
			
			// Do the animation
			$('#companies, #swilkenbridge').css('visibility', 'visible');
			$('#companies, #swilkenbridge').fadeTo(prefs.aniSpeedSlow, 1);			
			$(li).animate(
			  {
			  	width: prefs.closeWidth + 'px'
			  },
			  prefs.aniSpeed,
			  
					function() {
			
						$(li).css({
			  			backgroundImage : prefs.liBgImage
			  		});					
						
						// Fade in others
						$(this).parents('ul').find('li:not(#' + li_ID + ')').fadeTo(prefs.aniSpeed, 1, function() {
							$(this).fixClearType();$(this).fixClearType();
						});

						
						$(li).find('.wrapper').animate(
							{
								height: prefs.closeHeight + 'px'
							},
							prefs.aniSpeed,
							
							function() { 
								
								$(li).removeClass('active'); prefs.isOpening = false; prefs.isOpen = false;
								if( typeof(callback) == 'function' ) { callback(); }
							}
							
						);
					}			  
			);
		}
	}
		
	jQuery.each(newMethods,function(i){
		jQuery.fn[i] = this;
	});
	
})(jQuery);