//google.load("jquery", "1.3.2");
//google.load("jqueryui", "1.7.2");

//google.setOnLoadCallback(function() {
    $(document).ready(function() {
        initRotator();
    });
//});

function initRotator() {
    $("#homeOverlay").hide();
    slider();
    loadOverlay();
    //$(".quoteText").hide();
    //$(".quoteText:first").show();
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        quoteRotatorIE6();
    }
    else {
        quoteRotator();
    }
    
    $("*[class^='homeContent']").width(959);
    //Add in if more than two items
    $("#homeContentWrapper").width($(".homeContent").width() * $(".homeContent").length);
    $(".quoteShow").parent().css('cursor', 'pointer').click(function() { window.location = '/news/testimonials' });
    if ($("#subNav .lister > li").length <= 1) {
        $("#subNav").hide();
        $("#parentTitle").attr({ style: "margin-bottom:1em;"});
    }
    icaewTooltip();
}

function icaewTooltip() {
    $(".icaewHover").hover(function() { showTooltip() }, function() { hideTooltip() });
}

function showTooltip() {

    $(".icaewHover").attr('src', '/Content/Images/logo.icaew.overlay.gif');
    var timer = 250;
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $("#ICAEW_tooltip").height($(".ICAEW_tooltip").height());
        $("#ICAEW_tooltip").width($(".ICAEW_tooltip").width());
        $("#ICAEW_tooltip").show();
    }
    else {
            $("#ICAEW_tooltip").show('slide', { direction: 'up', easing: 'swing' }, timer);
    }
}

function hideTooltip(what) {
        $(".icaewHover").attr('src', '/Content/Images/logo.icaew.gif');
        $("#ICAEW_tooltip").hide();
}


var overlayOver = false;
var overlayInaction = false;


function loadOverlay() {

    $("#overlayTrigger").hover(function() {
        if (overlayInaction) {
            return false;
        }
        else {
            overlayInaction = true;
            $("#homeOverlay").slideDown(300, function() { overlayInaction = false })
        }
    },
		function() {
		    if (overlayInaction) {
		        return false;
		    }
		    else {
		        overlayInaction = true;
		        $("#homeOverlay").slideUp(300, function() { overlayInaction = false })
		    }
		});
}

var currentVal;

function slider() {

    currentVal = 1;
    var speed = 13000;
    var run = setInterval('rotate()', speed);

    var item_width = $('#overlayTrigger .homeContent').outerWidth();
    var left_value = item_width * (-1);

    $("#overlayCount").html(1);


    $('#overlayTrigger .homeContent:first').before($('#overlayTrigger .homeContent:last'));
    $('#overlayTrigger #homeContentWrapper').css({ 'left': left_value });

    $('#Prev').click(function() {
 
        var left_indent = parseInt($('#overlayTrigger #homeContentWrapper').css('left')) + item_width;
        $('#overlayTrigger #homeContentWrapper:not(:animated)').animate({ 'left': left_indent }, 1700, function() {
            viewCheck(-1);
            $('#overlayTrigger .homeContent:first').before($('#overlayTrigger .homeContent:last'));
            $('#overlayTrigger #homeContentWrapper').css({ 'left': left_value });
        });
        return false;
    });

    $('#Next').click(function() {
        var left_indent = parseInt($('#overlayTrigger #homeContentWrapper').css('left')) - item_width;
        $('#overlayTrigger #homeContentWrapper:not(:animated)').animate({ 'left': left_indent }, 1700, function() {
            viewCheck(1);
            $('#overlayTrigger .homeContent:last').after($('#overlayTrigger .homeContent:first'));
            $('#overlayTrigger #homeContentWrapper').css({ 'left': left_value });
        });
        return false;
    });

    $('#overlayTrigger').hover(
    	function() {
    	    clearInterval(run);
    	},
    	function() {
    	    run = setInterval('rotate()', speed);
    	}
    );
}

function rotate() {
    $('#Next').click();
}

function viewCheck(direction) {
    currentVal = currentVal + direction;
    if (currentVal < 1) {
    //uncomment if more than two items
        currentVal = $(".homeContent").length;
    }
    //uncomment if more than two items
    else if (currentVal > $(".homeContent").length) {
        currentVal = 1;
    }
    $("#overlayCount").html(currentVal);
}


function quoteRotatorIE6() {
    var index = Math.ceil(Math.random() * $(".quoteText").size()-1);
    $(".quoteShow .text").html($(".quoteText").eq(index).html());
    $(".quoteImagePane").css({ backgroundImage: "url(" + $(".quoteImage").eq(index).html() + ")" });
    var ph = $(".quoteShow").parent().height();
    var el = $(".quoteShow .text").height();
    $(".quoteShow .text").css('padding-top', (ph - el) / 2);
    $(".quoteText p").animate({ opacity: 0 }, 1);
}

function quoteRotator() {

    $(".quoteImage").hide();

    var quoteCount = $(".quoteText").size() -1;
    var currentQuote = 0;

    $(".quoteShow .text").html($(".quoteText:first").html());
    var ph = $(".quoteShow").parent().height();
    var el = $(".quoteShow .text").height();
    $(".quoteShow .text").css('padding-top', (ph - el) / 2);
    $(".quoteText p").animate({ opacity: 0 }, 1);

    window.setInterval(function() {
        currentQuote = currentQuote + 1;

        $(".quoteImagePane").animate({ opacity: 0 }, 1000);
        $(".quoteImagePane").queue(function() {
        if (currentQuote > quoteCount) {
            $(".quoteImagePane").css({ backgroundImage: "url(" + $(".quoteImage").eq(0).html() + ")" });
        }
        else {
            $(".quoteImagePane").css({ backgroundImage: "url(" + $(".quoteImage").eq(currentQuote).html() + ")" });
        }
        $(this).dequeue();
        $(".quoteImagePane").animate({ opacity: 1 }, 1000);
        });


        $(".quoteShow p").animate({ opacity: 0 }, 1000)
        $(".quoteShow p").queue(function() {
            if (currentQuote > quoteCount) {
                $(".quoteShow .text").empty().append($(".quoteText").eq(0).html());
                currentQuote = 0;
            }
            else {
                $(".quoteShow .text").empty().append($(".quoteText").eq(currentQuote).html());
            }
            var ph = $(".quoteShow").parent().height();
            var el = $(".quoteShow .text").height();
            $(".quoteShow .text").css('padding-top', (ph - el) / 2);


            $(this).dequeue();
            $(".quoteShow p").animate({ opacity: 1 }, 1000);

        });


    },
    //set time between fades
	10000
	);
}