// Home Page Scripts
jQuery.fn.wrapInChunks = function(html, chunkSize) {

    chunkSize = chunkSize || 1;

    var items = this.get(),
        rows = [],
        cur = rows[0] = $(html);

    while (items[0]) {

        if (rows[rows.length - 1].children().length === chunkSize) {
            cur = rows[rows.length] = $(html);
        }

        cur.append( items.shift() );

    }

    return this.pushStack(rows);

};

//document ready
$(function(){
	
	$('#news .item').wrapInChunks('<div class="slide" />', 3).appendTo('#news .items');
	
	$("#homeBanner .prev,#homeBanner .next").css('display','none');
	//build home banner scrollable
	var homeBannerScroller = $("#homeBanner .scrollable").scrollable({
		circular: true,
		mousewheel:true
		})
		.navigator()
		.autoscroll({
			interval:8000,
			autopause:true
			})
		.data("scrollable");
	//home page banners link to a case study if nec
	$("#homeBanner .item").each(function(){
		if($(this).has("span.link0")){
			//$("span.link0",this).remove();
			$("a:first",this).attr("href","/case-studies.htm");
			}
		if($(this).has("span.link1")){
			var linkURL = $("span.link1 a",this).attr("href");
			$("a:first",this).attr("href",linkURL);
			//$("span.link1",this).remove();
			}	
		});
	$("#homeBanner").hover(function(){
		$("#homeBanner .prev,#homeBanner .next").fadeIn();
		},
		function(){
			$("#homeBanner .prev,#homeBanner .next").fadeOut();
			}
		)		
		
	//build the ticker
	$(".blogsitesummary li").unwrap();
	$("span.author").remove();
	$('#js-news').ticker({controls:false,titleText:"Latest News - "});
	
	$("#homeBanner .item img").hover(function(){
		$(this).stop().animate({
			opacity:0
			}),100
		},
		function(){
		$(this).stop().animate({
			opacity:1
			}),100	
		});
	//What Are We About 
	$("#wawaNav li a:first").addClass("selected");
	$("#wawaNav li a").hover(function(){
			$("#wawaNav li a").removeClass("selected");
			var wawaImg = $(this).attr("rel");
			var wawaLink = $(this).attr("href");
			$("#wawaImage img").attr("src",wawaImg);
			$("#wawaImage img").parent("a").attr("href",wawaLink);
			$(this).addClass("selected");
		},
		function(){
			//do nothing on hover out
			})	
	
	});
	$(window).load(function(){
		//build the latest news scrollable after waiting for the other scripts to d their stuff
	var newsScroller = $("#news .scrollable").scrollable({
		circular: true,
		prev: '.prevArrow',
		next: '.nextArrow',
		speed:2000,
		mousewheel:true
		})
		.navigator({
			navi:".navigation"
			})
		.autoscroll({
			autopause:true,
			interval:20000
			})
		.data("scrollable");
		//$(".slide").each(function(){
		//	$("> .item:last",this).addClass("last");})
	});
	
$(window).load(function(){
	$("#homeRollover").fadeIn();
	})	
	
