/**
 * Author: Lee Langley
 * Date Created: 06/09/2011 09:51
 */

$(document).ready(function(){
	/**
	 * shows/hides the sub-navigation menu
	 */
	$('#nav li').hoverIntent({
		over:function(){
			var isActive = $(this).hasClass('active') || ($(this).find('.active:first').length > 0);
			if(!isActive){
				var children = $(this).children('ul');

				if(children.length > 0){
					// the list item has children

					$(this).addClass('current');
					$(children).stop(true, true).hide().slideDown(300);
				}
			}
		},
		out:function(){
			var isActive = $(this).hasClass('active') || ($(this).find('.active:first').length > 0);
			if(!isActive){
				var children = $(this).children('ul');

				if(children.length > 0){
					// the list item has children

					var parent = this;

					$(children).stop(true, true).show().slideUp(300, function(){
						$(parent).removeClass('current');
					});
				}
			}
		},
		timeout:100
	});

	setBackground();
});


/**
 * Takes the current background image and creates a repeating
 * background, beneath it, to stop the background looking blank if the
 * page goes down further than the background image.
 */
function setBackground(){
	var wrap = $('#wrap');
	var totalHeight = $(wrap).innerHeight();
	var imgUrl = $(wrap).css('background-image').replace(/url\( ?('|")(.*?)('|") ?\)/i, '$2');

	var img = $('<img />').load(function(){
		var imgHeight = $(img).height();

		if(imgHeight < totalHeight){
			var spacer = $('<span>').css({'background-image':"url('" + imgUrl + "')"}).addClass('backSpacer');
			for(var y = imgHeight; y < totalHeight; y++){
				var clone = $(spacer).clone();
				$(clone).css('top', y + 'px');
				$(wrap).append(clone);
			}
		}

		$(img).remove();
	});
	$(wrap).append(img);
	$(img).attr({
		src:imgUrl,
		style:'display:none'
	});
}
