var delay = 6500;
var cycle = 1;
var maxCycles = 10;

var newsDelay = 10000; // 10 seconds
var newsTransitionTime = 500; // 0.5seconds
var newsCycle = 0; // start number of cycle
var maxNewsCycles = 10; // cycle through 10 news articles
var currentNewsSelectedIndex = 0;
var intNewsCount = 0;
var booButtonClicked = false;
var booCycleInProgress = false;

$(this).ready(function () {
    ///////////////////////////////////////////
    // 1) Homepage case study code
    ///////////////////////////////////////////
    var selClass = "";
    var selected = 0;

    //Hide all case studies and header (for ie font render)
    //excluding the one rendered by the server
    $("div.case-study-panel").children("div").each(function () {
        if (!$(this).hasClass("hide-case-study")) {
            selClass = $(this);
        }
        else {
            $(this).children("h2").hide();
            $(this).hide();
            $(this).removeClass("hide-case-study");
        }
    });
    selected = selClass.attr("class").substring(11, 12);

    //Dleay before starting the animation of the next slide
    $.doTimeout("case-next", delay, function () {
        SetActiveStudy(-1, delay, false);
    });

    //Add select case study event to case nav
    $("ul.case-switch li a").click(function (e) {
        var selAnc = parseInt($(this).attr("class").substring(6, 8)) - 1;
        e.preventDefault;

        //Stop the rotation and shows study
        cycle = maxCycles + 1;
        SetActiveStudy(selAnc, 5000);

        return false;
    });

    ///////////////////////////////////////////
    // 2) Latest News
    ///////////////////////////////////////////
    // check to see if our div exists
    if ($("div.latest-news ul").length > 0) {
        // remove none js class
        $("div.latest-news.non-js").removeClass("non-js");

        intNewsCount = $("div.latest-news ul li").length - 1; // use 0 index

        if (intNewsCount > 1) {
            $("div.latest-news ul li:gt(0)").each(function () { $(this).hide(); });
            $("div.latest-news ul").after("<div class='news-buttons'><span>&laquo;</span>&nbsp;<a href='#previous' title='View previous article' class='previous'>Previous</a><a href='#next' title='View next article' class='next'>Next</a>&nbsp;<span>&raquo;</span></div>");

            $("div.latest-news div.news-buttons a.previous").click(function (e) {
                //Stop the rotation
                booButtonClicked = true;

                if (!booCycleInProgress) ShowSelectedNews(-1, newsTransitionTime);
                else {
                    $.doTimeout("news-next-article", true);
                    // force completion of the last cycle and then jump back
                    ShowSelectedNews(-1, newsTransitionTime);
                }
                e.preventDefault;
                return false;
            });

            $("div.latest-news div.news-buttons a.next").click(function (e) {
                //Stop the rotation
                $.doTimeout("news-next-article");
                booButtonClicked = true;
                if (!booCycleInProgress) ShowSelectedNews(1, newsTransitionTime);
                e.preventDefault;
                return false;
            });
        

            // auto cycle - doTimeout(name, time delay, callback);
            $.doTimeout("news-next-article", newsDelay, function () {
                if (!booButtonClicked && newsCycle < maxNewsCycles) {
                    if (!booCycleInProgress) ShowSelectedNews(0, 1500);
                    return true;
                }
                else return false;
            });
        }
    }
});         //end doc ready

function ShowSelectedNews(direction, newsDelay) {
    var currentSelectedNewsItem;
    var newSelectedNewsItem;
    
    booCycleInProgress = true;

    // NEXT
    if (direction > 0) {
        if(currentNewsSelectedIndex == intNewsCount) currentNewsSelectedIndex = 0;
        else currentNewsSelectedIndex = currentNewsSelectedIndex + direction;
    }
    // PREVIOUS
    else if(direction < 0){
        if(currentNewsSelectedIndex == 0 || ((currentNewsSelectedIndex - direction) < 0)) currentNewsSelectedIndex = intNewsCount;
        else currentNewsSelectedIndex = currentNewsSelectedIndex + direction;
    }
    // INCREMENT
    else {
        if(currentNewsSelectedIndex == intNewsCount) currentNewsSelectedIndex = 0;
        else currentNewsSelectedIndex += 1;
        newsCycle++;
    }

    currentSelectedNewsItem = $("div.latest-news ul li:visible");
    $("div.latest-news ul li.selected").each(function () {
        $(this).removeAttr("class");
    });
    
    // add selected to our current news item
    $("div.latest-news ul li:eq(" + currentNewsSelectedIndex + ")").attr("class", "selected");
    
    // FADE IN AND OUT
    currentSelectedNewsItem.fadeOut(newsDelay, function () {
        // check that we have the same element to fade in
        newSelectedNewsItem = $("div.latest-news ul li.selected");
        newSelectedNewsItem.fadeIn((newsDelay * 1.5), function () {
            booButtonClicked = false;
            booCycleInProgress = false;
        });
    });
    
}

function SetActiveStudy(next, caseDelay) {
    var current = $("ul.case-switch li a.selected").attr("class").substring(6, 8);
    var cases = new Array(3);
    var buttons = new Array(3);
    
    //Get access to all case studies
    cases[0] = $("div.case-study-1");
    cases[1] = $("div.case-study-2");
    cases[2] = $("div.case-study-3");
    buttons[0] = $("div.case-study-panel a.switch1");
    buttons[1] = $("div.case-study-panel a.switch2");
    buttons[2] = $("div.case-study-panel a.switch3");
    
    //Work out the current selection
    current = parseInt(current) - 1;
    //if there hasn't been a next specified (-1) then increment it
    if (next == -1) {
        next = current + 1;
        if (next > 2) next = 0;

        //Cycle to next slide
        if (cycle < maxCycles) {
            cases[current].children("h2").fadeOut(2300);
            cases[current].fadeOut(2500, function () {
                if (cycle < maxCycles) {
                    cases[next].fadeIn(1500);
                    cases[next].children("h2").fadeIn(2000);
                    buttons[current].removeClass("selected");
                    buttons[next].addClass("selected");

                    cycle = cycle + 1;
                    if (cycle < maxCycles) {
                        $.doTimeout("case-next", caseDelay, function () {
                            SetActiveStudy(-1, caseDelay);
                        });
                    }
                }
            });
        }
    }
    else {
        //Hide the active panel
        cases[current].children("h2").hide();
        cases[current].hide();
        buttons[current].removeClass("selected");

        //Show the selected panel
        buttons[next].addClass("selected");
        cases[next].fadeIn(800);
        cases[next].children("h2").fadeIn(1000);
    }
}

