$(document).ready(function () {
	
	//** Homepage feature areas **//
	$('#active-feature p').css('display', 'block');
	$('#home-features .feature').hover(function () {
		$(this).css('cursor', 'pointer');
		
		if ($(this).attr('id') === 'active-feature')
		{
			return false;
		}
		else
		{
			$('#active-feature').delay(100).animate({width: '169px'}, {duration: 300, easing: 'linear', queue: false, complete: function() { $(this).find('p').css('display', 'none'); }});
			
			$('#active-feature').attr('id', '');
			$(this).animate({width: '275px'}, {duration: 300, easing: 'linear', queue: false, complete: function () { $(this).find('p').css('display', 'block'); }});
			$(this).attr('id', 'active-feature');
		}
		
	}, function () {
		$('#home-features .feature').clearQueue();
		$(this).css('cursor', 'auto');
	});
	
	
	//** Drop nav stuff **//	
	// Tidy up names that are too long to fit in column
	$('a[href="/portfolio/public_sectorcharity/netherlands_foreign_investment_agency.html"]').html('NFIA');
	
	// Hover slide down
	$('#main-nav ul li.dir').hover(function () {
		$(this).find('a:first').addClass('on');
		$(this).find('div.drop').stop(true,true).delay(50).slideDown(700, function () {
			$(this).parents('li.dir').find('a:first').addClass('hover');
		});
	}, function () {
		$(this).find('div.drop').stop(true,true).slideUp(300);
		$(this).find('a:first').removeClass('hover');
		$(this).find('a:first').removeClass('on');
	});
	
	$('.portfolio .drop li.hi a').parents('ul:first').addClass('parent-hi');
	$('.portfolio .drop li.hi a').parents('div.drop:first').addClass('ancestor-hi');
	
	$('.drop ul[class!=parent-hi]').hover(function () {
		$(this).addClass('parent-hover');
	}, function () {
		$(this).removeClass('parent-hover');
	});	
	
	// Set equal column height
	$('.drop').each(function(i) { // Loop through each drop menu
	
		var maxHeight = 0; // reset max height
		$(this).find('ul').each(function (i) { // Loop through each sub-list inside .drop
		
			currHeight = $(this).actual('height');

			if (currHeight > maxHeight)
			{
				maxHeight = currHeight;
			}
		});
		//alert(maxHeight);
		maxHeight = maxHeight-10;
		maxHeight = maxHeight+'px';
		$(this).find('ul').css('height', maxHeight);
	});

	
	//** IE help for underlining a nested span inside a (on the comments count) **//
	$('a.comments').hover(function () {
		$(this).find('span.link').css('text-decoration', 'underline');
	}, function () {
		$(this).find('span.link').css('text-decoration', '');
	});
	
	
	//** Sidebar expandable **//
	$('#sidebar a.expand').click(function() {	
		// If the list is currently hidden, slide it down and replace '+' with '-'
		if ($(this).next('ul.expandable').is(':hidden')) {
			$(this).next('ul.expandable').slideDown();
			$(this).html('<img src="images/sidebar_hide.png" alt="" />');
		} else { // Otherwise, slide it up and switch back to a '+'
			$(this).next('ul.expandable').slideUp();
			$(this).html('<img src="images/sidebar_expand.png" alt="" />');
		}
		return false;
	});
	

	//** Clear form values on click **//
	$('form#commentform input[id!=submit]').click(function () {
		$(this).val('');
	});
	$('form#commentform textarea').click(function () {
		$(this).html('');
	});
	
	//** Temp fix until all new portfolio images are uploaded **//
	if ($('ul#slides li').length <= 0) {
		$('img.img-right').css('display', 'block');
	}
	
			/**
			 * This function splits the long quote into smaller sections each of ~280 characters.
			 * It appends each split into a seperately appended div tag. these take the form of 
			 * (n>0 to n<={quotelength}/280) => #quote-n.
			 * 
			 * @return Nothing.
			 */
			function quotePrep ()
			{
				if ($('#full-quote'))
				{
					//IE6 No trim() support!
					if (!('trim' in String.prototype)) {   
					String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); };    
					} 
					
					full_quote = $('#full-quote').text().trim();
					
					//setup quote blocks
					len = 320;
					_QUOTES = Math.floor(full_quote.length/len);
					for (var i=0;i<=_QUOTES;i++)
					{
						str = kind_split(full_quote,i*len,(i*len)+len);
						
						$('#quote-box').append('<div id="quote-'+i+
						'" class="inner"'+(i>0?'style="display:none;"':'style="display:block;"')+'><img src="/images/2011/quote_symb.png" alt="Quote" /><div class="quote-text">'+
						str+(i<_QUOTES?'... <a id="quote-more-'+i+'">Read more</a>':'.')+'</div></div>');
						
						$('#quote-more-'+i).click(function() { quote_move('forward');});
					}
					
					//arrows done already..
					
					//append circles & line
					spacing = Math.floor((74-(13*_QUOTES+1))/_QUOTES+1);
					for (var i=0;i<=_QUOTES;i++)
					{
						if ($('#quote-'+i))
						{
							$('#circle-box').append('<div id="circle-'+i+'" class="quote-circle'+
							(i==0?' circle-active':'')+'" '+(i>0?'style="margin-left:'+spacing+'px;"':'')+'>'+(i+1)+'</div>');
							$('#circle-'+i).click(function()
							{
								quote_move($(this).html()-1);
							});
						}
					}
				}
			}
			var _QUOTES;
			
			if ($('#quote-box'))
			{
				quotePrep();
			}
			
			/**
			 * Progresses the quote currently being displayed by the page.
			 *
			 * @param move Forward: forward, Backwards: reverse. Or integer to transition to. The direction to move.
			 *
			 * @return Nothing.
			 */
			function quote_move (move)
			{
				speed = 300;
				
				for (var i=0;i<=_QUOTES;i++)
				{
					if ($('#quote-'+i))
					{
						if ($('#quote-'+i).css('display') == 'block')
						{//alert("hello");
							$('#circle-'+i).toggleClass("circle-active");
							
							$('#quote-'+i).fadeOut(speed, function()
							{
								$('#quote-'+i).css('display','none');
								
								if (!(/^\d*$/.test(move)))
								{
									if (move == "forward")
									{
										n = (i+1)%(_QUOTES+1);									
									} else {
										n = i>0?i-1:_QUOTES;									
									}
								} else {
									n = move;
								}
								
								$('#quote-'+n).fadeIn(speed, function()
								{
									$('#quote-'+n).css('display','block');
								});								
								$('#circle-'+n).toggleClass("circle-active");
							});
							break;
						}
					}
				}
				return;
			}
			$('#quote-arrow-forward').bind('click', function() { quote_move('forward'); });
			$('#quote-arrow-reverse').bind('click', function() { quote_move('reverse'); });
			
			
			/**
			 * This function splits a string kindly. Not in the middle of a word,
			 * it will attempt to find the first whitespace after the boundaries given,
			 * generating a iteratively equal split for long strings.
			 * 
			 * @param str The string to find the substring in.
			 * @param from The index for the start of the sub string.
			 * @param to The index for the end of the sub string.
			 *
			 * @return The substring.
			 */
			function kind_split (str,from,to)
			{
				f = str.indexOf(" ",from);
				if (f < 0)
				{
					f = from;
				}
				
				t = str.indexOf(" ",to);
				if (t < 0)
				{
					t = to;
				}
				
				return str.substring(f,t);
			}
	
});

