// JavaScript Document
$(function(){
    // Declare variables
    var allFrames = $('.headerFrame').hide();
    var allH1 = $(allFrames).children().filter('h1');
    var allH2 = $(allFrames).children().filter('h2');
    var totalFrames = $(allFrames).length;
    var fadeInTime = 5000;
    var fadeOutTime = 2000;
    var fadeOutDelay = 45000;
    var textSwing = 4000;
	var headerInterval = null;
	var c;
    // Set click through action
    $(allFrames).click(function(){
            window.location.href = $(this).attr('href');
        });
    // Append header wrap with left and right buttons
    $('.headerSection').wrap("<div class=\"headWrap\"></div>")
    $('.headWrap').css({
        backgroundColor: "#fff",
        position: "relative",
        margin: "auto",
        width: 1080,
        opacity: 0
    });
    $('.headWrap').prepend("<img src=\"wp-content/themes/kba/images/left.png\" id=\"moveLeft\" />");
    $('.headWrap').append("<img src=\"wp-content/themes/kba/images/right.png\" id=\"moveRight\" />");
    var headWrapper = $('.headwWrap');
    var moveLeft = $('#moveLeft').css({
        width: 40,
        height: 40
    });
    var moveRight = $('#moveRight').css({
        width: 40,
        height: 40
    });
    // Check and set cookie
    if ($.cookie("KBA"), null) {
        var c = 0;
        $.cookie("KBA", 0)
    }
    else {
        c = $.cookie("KBA");
        c++;
        if (c >= totalFrames) {
            c = 0;
		}
    };
    $.fn.setCookie = function(){
        $.cookie("KBA", c, {
            expires: 1
        });
    };
    function displayFrame(c){
		var currentFrame=$(allFrames).eq(c);
		$('.headerSection').scrollTo(currentFrame);
		$(currentFrame).fadeIn(fadeInTime);
        swingText(allH1, allH2, c);
    };
	displayFrame(c);
    // Define functions for text animation
    function swingText(allH1, allH2, c){
        $(allFrames).eq(c).addClass('swung');
        $(allH1).eq(c).animate({
            left: "+=" + 50 + "px"
        }, textSwing, "swing"), $(allH2).eq(c).animate({
            left: "-=" + 25 + "px"
        }, textSwing * .5, "swing"), $('.headWrap').animate({
            opacity: 1
        }, fadeInTime).setCookie();
    };
    var resizeTime = 150;
    // Define functions for left and right motion
    $(moveLeft).click(function(){
        var prev;
        if (c == 0) {
            prev = totalFrames - 1;
        }
        else {
            prev = c - 1;
        }
		clearint();
		headerInterval = setInterval(cycleHeader, 15000)
		$(allFrames).eq(prev).show();
        $('.headerSection').scrollTo(allFrames.eq(prev), 800), "", "", c = prev;
        $.cookie("KBA", c, {
            expires: 1
        });
        if (!$(allFrames).eq(prev).hasClass('swung')) {
            swingText(allH1, allH2, prev);
        }
        else {
            return;
        }
    });
    $(moveRight).click(function(){
        var next;
        if (c == totalFrames - 1) {
            next = 0;
        }
        else {
            next = c + 1;
        }
		clearint();
		headerInterval = setInterval(cycleHeader, 15000)
		$(allFrames).eq(next).show();
        $('.headerSection').scrollTo(allFrames.eq(next), 800), "", c = next;
        $.cookie("KBA", c, {
            expires: 1
        });
        if (!$(allFrames).eq(c).hasClass('swung')) {
            swingText(allH1, allH2, next);
        }
        else {
            return;
        }
    });
    // Define arrow animation
    var leftHoverIntent = {
        sensitivity: 1,
        interval: 10,
        over: callLeftHover,
        timeout: 150,
        out: releaseLeftHover
    };
    var rightHoverIntent = {
        sensitivity: 1,
        interval: 10,
        over: callRightHover,
        timeout: 150,
        out: releaseRightHover
    };
    $(moveLeft).hoverIntent(leftHoverIntent);
    $(moveRight).hoverIntent(rightHoverIntent);
    function callLeftHover(){
        $(moveLeft).animate({
            width: 60,
            height: 60,
            left: 0,
            top: 200
        }, resizeTime);
    };
    function releaseLeftHover(){
        $(moveLeft).animate({
            width: 40,
            height: 40,
            left: 10,
            top: 210
        }, resizeTime);
    };
    function callRightHover(){
        $(moveRight).animate({
            width: 60,
            height: 60,
            right: 0,
            top: 200
        }, resizeTime);
    };
    function releaseRightHover(){
        $(moveRight).animate({
            width: 40,
            height: 40,
            right: 10,
            top: 210
        }, resizeTime);
    };
    // Define fade in animation with text swing
	headerInterval = setInterval(cycleHeader, 15000);
	function clearint() {
		clearInterval(headerInterval);
	}
	function cycleHeader(){
		if (c < totalFrames - 1) {
			c++;
			$(allFrames).eq(c).show();
			$.cookie("KBA", c, {
				expires: 1
			});
			var currentFrame = $(allFrames).eq(c);
			$('.headerSection').scrollTo(currentFrame, 800);
			if (!$(allFrames).eq(c).hasClass('swung')) {
				swingText(allH1, allH2, c);
			}
			else {
				return;
			}
		}
		else {
			c = 0;
			$(allFrames).eq(c).show();
			$.cookie("KBA", c, {
				expires: 1
			});
			var currentFrame = $(allFrames).eq(c);
			$('.headerSection').scrollTo(currentFrame, 800);
			if (!$(allFrames).eq(c).hasClass('swung')) {
				swingText(allH1, allH2, c);
			}
			else {
				return;
			}
		}
	}
    
	/*
	// Learn More
    $('.bottomBar').css({
        height: 0
    })
    $('.bottomBar').children().css({
        display: 'none'
    })
    var learnMoreHover = {
        sensitivity: 1,
        interval: 100,
        over: showBar,
        timeout: 300,
        out: hideBar
    };
    function showBar(){
        $('.bottomBar', this).animate({
            height: 50,
            bottom: 0
        }, function(){
            $('h3', this).css({
                display: 'block'
            })
        })
    };
    function hideBar(){
        $('h3', this).css({
            display: 'none'
        });
        $('.bottomBar', this).animate({
            height: 0,
            bottom: -50
        });
    }
    */
    $('.headerFrame').mouseover(function(){
		clearint()
	}).mouseout(function(){
		headerInterval = setInterval(cycleHeader, 15000)
	});
});