/* 
	KiwiRail website JavaScript
	© Copyright KiwiRail 2011
	
	Dependencies:
		/j/jquery/jquery-1.6.2.min.js
		/j/jquery/jquery.easing.min.js
		/j/jquery/jquery.bxSlider.min.js

	Written by Frontend Design
	http://www.frontend.co.nz/	
*/

/* --- MINIFIED ANIMATE COLOUR PLUGIN --- */
(function(d){d.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(f,e){d.fx.step[e]=function(g){if(!g.colorInit){g.start=c(g.elem,e);g.end=b(g.end);g.colorInit=true}g.elem.style[e]="rgb("+[Math.max(Math.min(parseInt((g.pos*(g.end[0]-g.start[0]))+g.start[0]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[1]-g.start[1]))+g.start[1]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[2]-g.start[2]))+g.start[2]),255),0)].join(",")+")"}});function b(f){var e;if(f&&f.constructor==Array&&f.length==3){return f}if(e=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)){return[parseInt(e[1]),parseInt(e[2]),parseInt(e[3])]}if(e=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)){return[parseFloat(e[1])*2.55,parseFloat(e[2])*2.55,parseFloat(e[3])*2.55]}if(e=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}if(e=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}if(e=/rgba\(0, 0, 0, 0\)/.exec(f)){return a.transparent}return a[d.trim(f).toLowerCase()]}function c(g,e){var f;do{f=d.curCSS(g,e);if(f!=""&&f!="transparent"||d.nodeName(g,"body")){break}e="backgroundColor"}while(g=g.parentNode);return b(f)}var a={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0],transparent:[255,255,255]}})(jQuery);
/* --- */

// INITIALISE FUNCTION
function init() {
	
	// Set up hero slider
	var heroSlider = $('#hero-slider').bxSlider({
		wrapperClass: 'heroSliderWrapper',
		mode: 'fade',
		auto: true, // Automatically start cycling
		pause: 5000, // Time on each slide
		speed: 1000, // Slide transition speed
		controls: false,
		pager: true,
		pagerSelector: '#heroPager',
		autoHover: true, // Pause cycling on mouse over
		onBeforeSlide: function(currentSlide, totalSlides) {
      	
      	if (currentSlide === 0) {
      		$('#heroBG').stop().animate({'backgroundColor' : '#1f2352'}, 700);
      	}
      	else if (currentSlide === 1) {
      		$('#heroBG').stop().animate({'backgroundColor' : '#6fafed'}, 700);
      	}
      	else {
      		$('#heroBG').stop().animate({'backgroundColor' : '#224f64'}, 700);
      	}
      	
    	}
	});
	// Custom next slide control
	$('#heroNext').click(function () {
		heroSlider.goToNextSlide();
		return false;
	});
	
	$('.sectHover').addClass('jsKick'); // Hides and prepares home page sections for interaction
	$('#heroNext').show(); // display next arrow when script starts
		
}

// INITIAL DOC READY FUNCTION
$(function () {

	$('.no-js').removeClass('no-js');
	
	init(); //Initialise

	// Contact page accordions
	$('#other-contacts a.moreDetails').show();
	$('#other-contacts div.details').hide();
	
});

// INTERFACE INTERACTION
$(function () {
	
	// Home hero slider interactions
	$('#heroNext').css({'opacity' : 0.3});
	$('#heroNext').hover(
		function () { // OVER
			$(this).stop().animate({'opacity' : 1, 'paddingLeft' : 10, 'right' : -30}, 100);
		}, function () { // OFF
			$(this).stop().animate({'opacity' : 0.3, 'paddingLeft' : 0, 'right' : -20}, 100);
		}
	);
	
	// Sidekick threeSect interactions
	$('#home-page #overlap .threeSect article').hover(
		function () {
			$(this).find('.sectHover').css({'opacity' : 1}).stop().animate({'top' : -20}, 500, 'easeOutBack', function () {
				$(this).find('ul').stop().animate({'opacity' : 1}, 200);
			});
		}, function () {
			$(this).find('ul').stop().animate({'opacity' : 0}, 200);
			//$(this).find('.sectHover').stop().animate({'top' : -205}, 500, 'easeInExpo');
			$(this).find('.sectHover').stop().animate({'opacity' : 0}, 300, function () {
				$(this).css({'top' : -205});
			});
		}
	);
	
	// Contact page accordion's
	$('#other-contacts a.moreDetails').click(function () {
		if($(this).hasClass('open')) 
		{
			$(this).siblings('.details').hide();
			$(this).removeClass('open').text('More details +');
		}
		else 
		{
			$(this).siblings('.details').fadeIn();
			$(this).addClass('open').text('Less details -');
		}	
		
		return false;
	});

});

// NAVIGATION SET ACTIVE
// Sets active state on required nav item depending on current page
$(function () {
	
	var currentURL = window.location.href;
	var activeURL = currentURL.split('.co.nz/')[1].split('/')[0].split('.html')[0];
	
	$('#nav-main a, #nav-sub a').each(function () {
		
		if( $(this).attr('href') === ('/' + activeURL) )
		{
			$(this).parents('li').addClass('active');
		}
		
	});
	
});

/* FEATURE PROJECT LINKING */
/* This adds a link to the feature project box to improve usability */
$(function () {
	
	if($("#feature-project").length) {
		$("#feature-project").hover( function () {
			$("#feature-project").addClass('hoverOver'); 
		}, function () {
			$("#feature-project").removeClass('hoverOver'); 
		});
		
		$("#feature-project").click( function () {
			window.location = $("#feature-project").find('a:eq(0)').attr('href');
		});
	}
	
});

// NEWS POSTS / PROJETS - IMAGE CAPTIONS
// Checks for images in news posts and project detail pages and uses its title attribute to make a caption.
$(function () {
	
	if($('.addCaption').length) // if this is a news post page
	{
		$('.addCaption img').each(function () { // search each post img in page
		
			var captionText = $(this).attr('title'); // grab the title attribute
			var imgWidth = $(this).width() - 20; // Find the image width minus the caption padding
			
			if(captionText !== undefined) // Only if title has been specified
			{
				$(this).after('<span class="imgCaption" style="width: ' + imgWidth + 'px">' + captionText + '</span>'); // add the title attribute as a caption
				$(this).removeAttr('title'); // remove the title attribute so it doesn't show on hover
			}
			
		});
	}
	
});

// KEY FACTS SIDEBAR
// Adds key facts to sidebar module of project detail pages if they're specified in content.
$(function () {

	if($('.key-fact-item').length)
	{	
		$('#key-facts').removeClass('hide');	
		$('.key-fact-item').each(function () {
		
			// Visual editor fool-proofing
			if($(this).is('ol')) { // If class has been applied to <ol>
				$(this).find('li').appendTo('#key-facts ul');
			}
			else if($(this).is('li')) { // If class has been applied to <li>
				$('<li>' + $(this).text() + '</li>').appendTo('#key-facts ul');
			}
			else if($(this).is('span')) { // If class has been applied to <span>
				$('<li>' + $(this).text() + '</li>').appendTo('#key-facts ul');
			}
			
			$(this).remove(); // remove this from the main content
			
		});
	}
	
});


// DYNAMIC LINKS SIDEBAR
// Adds links to sidebar module 'Dynamic links' to detail threeCol pages if specified in page content
$(function () {

	if($('.customLink').length)
	{	
	
		var linkString;
	
		$('#dynamic-links').removeClass('hide');	
		$('.customLink').each(function () {
			
			$('<li><a href="' + $(this).attr('href') + '" class="' + $(this).attr('class') + '">' + $(this).text() + '</a></li>').appendTo('#dynamic-links ul');
			$(this).remove(); // remove this from the main content
			
		});
	}
	
});



