var imageIndex = null;
var imageFolder = '../css/images/banners-large/';
var rotationTime = 1000;
var rotationInterval = 6000;
var intervalID = null;
var clickTimeout = null;
var footerIndex = 0;

var BannerList = [
{
    nickname: "MAPA Award",
	imageName: "banners-mapa-2011",
	destination: "/about-us/awards",
	toRender: "<h2>Got a bright idea?</h2><p>Kiandra specialises in green field start-up and venture capital fuelled websites and software development</p>"
},{
	nickname: "Green IT",
	imageName: "banners-green",
	destination: "/business-perspective/green-it",
	toRender: "<h2>Thinking of green IT?</h2><p>Talk to us about virtualisation and environmentally responsible technology</p>"
},{
	nickname: "BRW Award",
	imageName: "banners-brw",
	destination: "/about-us/awards#brw",
	toRender: ""
},{
	nickname: "Cybercrime/Security",
	imageName: "banners-cybercrime",
	destination: "/infrastructure-solutions/security-solutions",
	toRender: ""
},{
	nickname: "MAC",
	imageName: "banners-mac",
	destination: "/products-and-technologies/apple-solutions",
	toRender: "<h2>Got Mac? Want Mac? We have it covered.</h2><p>Kiandra are experts at Mac and Windows integration and deliver quality, cross-platform solutions.</p>"
},{
	nickname: "Custom Software",
	imageName: "banners-software",
	destination: "/business-perspective/radically-boost-your-bottom-line",
	toRender: "<h2>Custom software</h2><p>The last piece of the IT puzzle that radically boosts your bottom line.</p>"
},{
	nickname: "Fresh",
	imageName: "banners-freshchange",
	destination: "/business-perspective/a-fresh-change",
	toRender: "<h2>Get a fresh change</h2><p>The right IT people looking after the right IT solutions</p>"
}];

$(function () {

    //assign li hover class for IE6
    $("li").hover(
          function () {
              $(this).addClass('hover');
          },
          function () {
              $(this).removeClass('hover');
          }
	);

    //set up search
    $('.search-button').each(function () {

        var searchButton = $(this);
        var searchArea = searchButton.parents('.search-box');

        searchButton.click(function () {
            document.location = $(this).attr('data-url') + '?search=' + searchArea.find(".search-terms").val();
        });

    });


    $('.search-box').each(function () {

        var searchButton = $(this).find('.search-button');

        //set up search so 'enter' submits
        $(this).find(':input').live('keypress', function (e) {

            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {

                searchButton.click();
                return false;
            }
        });
    });
    
    // stop this script from progressing if on an internal page
    if (!document.getElementById("banner-back")) {
        return;
    }

    // create the carousel indicators
    for (var i = 0, len = BannerList.length; i < len; i++) {
        $("<a />")
			.attr("href", "#")
			.data("arrayPos", i)
			.click(function (e) {
			    goToBanner(this);
			    e.preventDefault();
			})
			.appendTo("#banner-counter");
    }

    // randomise start position
    imageIndex = Math.round(Math.random() * (BannerList.length - 1));

    // set initial background images for first transition
    $("#banner-left").css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + "-left.jpg)");
    $("#banner-right").css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + "-right.jpg)");
    $("#banner-wrapper").css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + ".jpg)");

    $("#banner-back").click(function () {
        document.location = getCurrentUrl();
    });

    updateButtons();

    intervalID = setTimeout(function () {
        rotateImages();
    }, rotationInterval);


  
});
	
function rotateImages(manualOverride,arrIndex) {    

	//increment image index
    if (manualOverride) {
        imageIndex = arrIndex;
        window.clearTimeout(clickTimeout);
        clickTimeout = window.setTimeout(function () {
            rotateImages();
        }, 7 * 1000);
    } else {
        imageIndex = getNextImageId();

        if (footerIndex === 2) {
            footerIndex = 0;
        } else {
            footerIndex++;
        }
        goToFooter(footerIndex);
    }

	updateButtons();
	
	$("#banner-left-back").css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + "-left.jpg)");
	$("#banner-right-back").css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + "-right.jpg)");
	$("#banner-wrapper-back").css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + ".jpg)");
		
        //swap images
        $("#banner-wrapper").stop().animate({
				opacity: 0
			},
			rotationTime,
			function() {
				//set front div to what is currently in background and set front div back to visible
				$("#banner-wrapper")
					.css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + ".jpg)")
					.css("opacity", 1);
						  
				//run again
				if (!manualOverride) {
					intervalID = setTimeout(function() {
						rotateImages();
					}, rotationInterval);
				}

				//set background image in 1 second after opacity finishes (prevent ficker issue)
				setTimeout(function() {
					setBackground(imageIndex);
				}, 1000);
			}
		);

        $("#banner-left").stop().animate({
				opacity: 0
			},
			rotationTime,
			function() {
				//set front div to what is currently in background and set front div back to visible
				$("#banner-left")
					.css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + "-left.jpg)")
					.css("opacity", 1);
			}
		);

        //set front div to what is currently in background and set front div back to visible
        $("#banner-right").stop().animate({
				opacity: 0
			},
			rotationTime,
			function() {
				$("#banner-right")
					.css("background-image", "url(" + imageFolder + BannerList[imageIndex].imageName + "-right.jpg)")
					.css("opacity", 1);
			}
        );      
}

function updateButtons() {
		$("#banner-counter a").eq(imageIndex).addClass("active").siblings().removeClass();
}

function setBackground(index) {
    $("#banner-wrapper-back").css("background-image", "url(" + imageFolder + BannerList[index].imageName + ".jpg)")
}

function getCurrentUrl() {
    return BannerList[imageIndex].destination;
}

function goToBanner(obj) {
	var arrIndex = $(obj).data("arrayPos");
	window.clearTimeout(intervalID);
	rotateImages(true,arrIndex);
}

function getNextImageId() {

    var nextImage = imageIndex;

    nextImage++;

    if (nextImage >= BannerList.length) {
        //reset array
        nextImage = 0;
    }

    return nextImage;
}

function goToFooter(index) {
    $("#partners").fadeOut(function () {
        $(this).css("background-position", "center " + (index * -90) + "px").fadeIn();
    });
}
