﻿
function theRotator(div,interval) {
    //Set the opacity of all images to 0
    $('div#' + div + ' ul li').css({ opacity: 0.0 });
    //Get the first image and display it (gets set to full opacity)
    $('div#' + div + ' ul li:first-child').css({ opacity: 1.0 });
    $('div#' + div + ' ul li:first-child').addClass('show');

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('rotate("' + div + '")', interval);
};

function rotate(div) {
    //Get the first image
    var current = ($('div#' + div + ' ul li.show') ? $('div#' + div + ' ul li.show') : $('div#' + div + ' ul li:first-child'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#' + div + ' ul li:first-child') : current.next()) : $('div#' + div + ' ul li:first-child'));

    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({ opacity: 0.0 })
    .addClass('show')
    .animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
    .removeClass('show');
};
$(document).ready(
    function() {
        //load css for use with jquery
        $('<link>').appendTo('head').attr({ rel: 'stylesheet', type: 'text/css', href: 'CSS/jquery.css' });
        //page load events
        //Load the slideshows

        var rotator = $("#rotator")[0];
        var rotatorList = rotator.childNodes;
        var rotatorUl

        for (var i = 0, ii = rotatorList.length; i < ii; i++) {
            if (rotatorList[i].nodeType == 1) {
                rotatorUl = rotatorList[i];
                i = ii;
            }
        }

        var rotatorListItems = rotatorUl.childNodes;

        var rotatorListItemsFinal = 0;
        for (var i = 0, ii = rotatorListItems.length; i < ii; i++) {
            if (rotatorListItems[i].nodeType == 1) {
                rotatorListItemsFinal++;
            }
        }

        if (rotatorListItemsFinal > 1) {
            theRotator('rotator', 10000);
        }

        /*theRotator('rotator', 10000);
        theRotator('strat-rotator', 10000);*/

        var rotator = $("#strat-rotator")[0];
        var rotatorList = rotator.childNodes;
        var rotatorUl

        for (var i = 0, ii = rotatorList.length; i < ii; i++) {
            if (rotatorList[i].nodeType == 1) {
                rotatorUl = rotatorList[i];
                i = ii;
            }
        }

        var rotatorListItems = rotatorUl.childNodes;

        var rotatorListItemsFinal = 0;
        for (var i = 0, ii = rotatorListItems.length; i < ii; i++) {
            if (rotatorListItems[i].nodeType == 1) {
                rotatorListItemsFinal++;
            }
        }

        if (rotatorListItemsFinal > 1) {
            theRotator('strat-rotator', 10000);
        }
//        // open relevant nav when page loads
//        $clicked = $(".nav a.current");
//        //get the parent li of the a
//        $clickParent = $clicked.parent();
//        //populate a var with the contents of the ul
//        $subItem = $clickParent.find("ul").html();
//        //set the destination of the ul
//        $destination = $("div.sub-nav");
//        if ($subItem != null && $subItem != '' && $subItem != 'null') {
//            //first close the container
//            if ($destination.html() != '') {
//                $destination.animate({ height: "toggle", width: "toggle" }, 0, function() {
//                    toggleSubNav($destination);
//                })
//            } else {
//                toggleSubNav($destination);
//            }
//        }



        //news / client switchers
        // needs re write to make more streamlined
        //lModule
//        $("div#industry").hide();
//        $("a.company").hide();
//        $("div.lModule .title").html("Company News");
//        //rModule
//        $("div#ourclients").hide();
//        $("a.casestudies").hide();
//        $("div.rModule .title").html("Case Studies");
        /*click events*/
//        $("a.company").click(
//            function() {
//                $("div#industry").fadeOut("slow");
//                $("div#company").fadeIn("slow");
//                $("a.company").fadeOut("slow");
//                $("a.industry").fadeIn("slow");
//                $("div.lModule .title").html($("div#company h2").html());
//                return false;
//            });
//        $("a.industry").click(
//            function() {
//                $("div#company").fadeOut("slow");
//                $("div#industry").fadeIn("slow");
//                $("a.industry").fadeOut("slow");
//                $("a.company").fadeIn("slow");
//                $("div.lModule .title").html($("div#industry h2").html());
//                return false;
//            });
//        $("a.ourclients").click(
//            function() {
//                $("div#casestudies").fadeOut("slow");
//                $("div#ourclients").fadeIn("slow");
//                $("a.ourclients").fadeOut("slow");
//                $("a.casestudies").fadeIn("slow");
//                $("div.rModule .title").html($("div#ourclients h2").html());
//                return false;
//            });
//        $("a.casestudies").click(
//            function() {
//                $("div#ourclients").fadeOut("slow");
//                $("div#casestudies").fadeIn("slow");
//                $("a.casestudies").fadeOut("slow");
//                $("a.ourclients").fadeIn("slow");
//                $("div.rModule .title").html($("div#casestudies h2").html());
//                return false;
//            });
        //navigation
        if (!$("div.sub-nav").html()) {
            $("div.sub-nav").hide();
        }
        //poss do something special here
//        $(".nav a").hover(
//            function() {
//                $current = $('.current');
//                if ($current != $('.current')) {
//                    $('nav .current').removeClass('current');
//                } else {
//                    $current.addClass('current');
//                }
//            }
//        );
//        $(".nav a").mouseover(
//            function() {
////                var n = jQuery.queue( $("div")[0], "fx" );
////                if (n.length > 1)
////                {
////                    $(".nav a").clearQueue();
////                }
//            
//                //item being clicked
//                $clicked = $(".nav a.current");
//                //need to create loop to remove the current class from nav
////                $('.nav .current').removeClass('current');
////                $clicked.addClass('current');
//                //get the parent li of the clicked a
//                $clickParent = $clicked.parent();
//                //populate a var with the contents of the ul
////                $subItem = $clickParent.find("ul").html();
//                //set the destination of the ul
//                $destination = $clickParent.find("ul");
//                if ($subItem != null && $subItem != '' && $subItem != 'null') {
//                    //first close the container
//                    if ($destination.html() != '') {
//                        $destination.animate({ height: "toggle", width: "toggle" }, 500, function() {
//                            toggleSubNav();
//                        })
//                    } else {
//                        toggleSubNav();
//                    }
//                }
//            });
//        function toggleSubNav($destination) {
//            $destination = $("div.sub-nav");
//            //once closed populate the text
//            if ($subItem != $destination.find("ul").html()) {
//                $destination.html("<div><ul class='sub'>" + $subItem + "</ul></div>");
//                //and open
//                $destination.animate({ height: "toggle", width: "toggle" }, 500);
//            } else {
//                $destination.html('');
//            }
        //        }

        // Navigation

        $('.nav li.about').live('mouseover mouseout', function (event) {
            if (event.type == 'mouseover') {
                $('.nav li.about ul').show();
            } else {
                $('.nav li.about ul').hide();
            }
        });

        $('.nav li.services').live('mouseover mouseout', function (event) {
            if (event.type == 'mouseover') {
                $('.nav li.services ul').show();
            } else {
                $('.nav li.services ul').hide();
            }
        });
        
        $('.nav li.kpeople').live('mouseover mouseout', function (event) {
            if (event.type == 'mouseover') {
                $('.nav li.kpeople ul').show();
            } else {
                $('.nav li.kpeople ul').hide();
            }
        });

        //keypeople page
        $(".person div.details").click(
                function() {
                    $clicked = $(this);
                    $clickParent = $clicked.parent();
                    $subItem = $clickParent.find("div.addInfo .c");
                    if ($subItem.hasClass("closed")) {
                        $subItem.hide();
                        $subItem.removeClass("closed");
                        $clicked.find("h2").css({ "background": "url(../images/btn-arrow-d.png) no-repeat scroll 2px 50% transparent" });
                    }
                    if ($(".open")) {
                        $(".open").animate({ height: "toggle", width: "toggle" }, 750);
                        $(".open").addClass("closed");
                        $(".open").removeClass("open");
                        $clicked.find("h2").css({ "background": "url(../images/btn-arrow-u.png) no-repeat scroll 2px 50% transparent" });
                    }
                    if (!$subItem.hasClass("closed")) {
                        $clicked.find("h2").css({ "background": "url(../images/btn-arrow-d.png) no-repeat scroll 2px 50% transparent" });
                        $subItem.animate({ height: "toggle", width: "toggle" }, 750);
                        $subItem.addClass("open");
                    }
                });
                //
                //clients page
//                $(".clients .list div.client").click(
//                    function() {
//                        $clicked = $(this);
//                        $desination = $(".portal div.current .client div.b");
//                        $desination.animate({ height: "toggle" }, 750, function() {
//                            populateClient($clicked);
//                            $desination.delay(150).animate({ height: "toggle" }, 750);
//                        });
//                    });
//                    
//                $(".clients .list div.client a").click(
//                    function() {
//                        $clicked = $(this).parents("div.client");
//                        $desination = $(".portal div.current .client div.b");
//                        $desination.animate({ height: "toggle" }, 750, function() {
//                            populateClient($clicked);
//                            $desination.delay(150).animate({ height: "toggle" }, 750);
//                        });
//                        return false;
//                    });

                //cases page
//                $(".clients .list div.case").click(
//                    function() {
//                        $clicked = $(this);
//                        $desination = $(".portal div.current .case div.b");
//                        $desination.animate({ height: "toggle" }, 750, function() {
//                            populateClient($clicked);
//                            $desination.delay(150).animate({ height: "toggle" }, 750);
//                        });
//                    });


//                    $(".clients .list div.case a").click(
//                    function() {
//                        $clicked = $(this).parents("div.case");
//                        $desination = $(".portal div.current .case div.b");
//                        $desination.animate({ height: "toggle" }, 750, function() {
//                            populateClient($clicked);
//                            $desination.delay(150).animate({ height: "toggle" }, 750);
//                        });
//                        return false;
//                    });
                });

function populateClient(clicked) {
    $(".clients .client .holder h2").html(clicked.find("h2").html());
    $(".clients .client .holder h3").html(clicked.find("h3").html());
    $(".clients .client .holder div.profile").html(clicked.find("div.profile").html());
}
//function populateCase(clicked) {
//    $(".clients .case .holder h2").html(clicked.find("h2").html());
//    $(".clients .case .holder h3").html(clicked.find("h3").html());
//    $(".clients .case .holder div.profile").html(clicked.find("div.profile").html());
//}


function newsLoad()
{
 $("#NewsArticlesContainer div.client").click(
    function() {
        $clicked = $(this);
        $desination = $(".portal div.current .client div.b");
        $desination.animate({ height: "toggle" }, 750, function() {
            populateClient($clicked);
            $desination.delay(150).animate({ height: "toggle" }, 750);
        });
    });


$("#NewsArticlesContainer div.client a").click(
    function() {
        $clicked = $(this).parents("div.client");
        $desination = $(".portal div.current .client div.b");
        $desination.animate({ height: "toggle" }, 750, function() {
            populateClient($clicked);
            $desination.delay(150).animate({ height: "toggle" }, 750);
        });
        return false;
    });

}
