﻿/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
 function animateBar(mv) {
        

		// Setup
        var bar = $('.bg-photo');

        bar.animate({
            backgroundPosition: '-' + mv + 'px 0px'
        }, 700, 'linear', function () {
            //  animateBar(bar);
			
        });
		
		//ensure correct menu item is highlighted.
		var index = window.location.hash;
		
		//alert(index);
		$(".sub-nav a").each(function() {
			
			
			var href = $(this).attr("href");
			
			if(href == index) {
				$(this).animate({ color: "#fff" }, 300);
			}					 
		});
		
}

var initCount = 0;
var urlHash;
var currentElement;
function mycarousel_initCallback(carousel, state) {

		if(initCount == 0)
		{
			jQuery('.jcarousel-control a').click(function(e) {
														  
				e.preventDefault();
				currentElement = $(this);
				window.location.hash = $(this).attr("href").replace(/#/i, "");
				urlHash = $(this).attr("href").replace(/#/i, "");
				carousel.scroll(jQuery.jcarousel.intval($(this).attr("href").replace(/#/i, "")));
			
			});
			
			if (state == 'init'){
				setTimeout(function(){processHash(carousel)}, 500)
			}
			
			initCount = 1;
		}
};

function scrollcallback() {
	
	var moveby = 150 * jQuery.jcarousel.intval(urlHash);
	var moveto = 150 + moveby;							
	animateBar(moveto);
				
	//ensure colours are correct.
	$('.jcarousel-control a').removeClass("current").css("color", "#bda41a");
	currentElement.addClass("current").css("color", "#fff");
	
}

function processHash(c){
	
	if (window.location.hash == "" 
		|| window.location.hash == null
		|| window.location.hash == 'undefined')
		return;
		
	var myhash = (window.location.hash).replace(/#/i, "")
		
	jQuery('.jcarousel-control a').each(function() {
												 
		var index = parseInt($(this).index() + 1); //index is zero based.
		var openIndex = parseInt(myhash);

		if(index == openIndex) {
			$(this).click();
		}
	});
}


// Ride the carousel...
jQuery(document).ready(function() {
	
	jQuery("#sections").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
       	buttonNextHTML: null,
       	buttonPrevHTML: null,
		itemVisibleOutCallback: {
  			onBeforeAnimation: scrollcallback
		}
    });
});
 

