﻿    $(function () {

        $(".moveRight").click(function () {
            //alert("right");
            $("#homebannerbg").homepage("moveNext");
        });

        $(".moveLeft").click(function () {
            $("#homebannerbg").homepage("movePrev");
        });

        $(".sel").click(function () {
            $("#homebannerbg").homepage("jumpTo", $(this).attr("id"));
            // for some reason, this didn't work in event
            $(this).addClass("activebg");
        });

        var enterBorder = "1px solid #fff";
        var leaveBorder = "1px solid #B0D5F0";

        $(".hovertarget").mouseenter(function () {
            var border = enterBorder;
            $("#homebannerbg").homepage("stop");
            $(this).css("background-image", "none").css("background-color", "#87B3D0").css("border", border);
            $(this).children(".quicklinkcontent").children("h4").css("border-bottom", border);
            $(this).children("#controlboxcontent").children(".controlheading").css("border-top", border);
            var $activeLi = $(this).children("#controlboxcontent").children("#scroller").children("ul").children("li.sel").children("a");
            $($activeLi).addClass("activebghover");
           // $activeLi.css("border", border); //.width($activeLi.width() - 2).height($activeLi.width() - 2);

        }).mouseleave(function () {
            var border = leaveBorder;
            $("#homebannerbg").homepage("start");
            $(this).css("background-color", "transparent").css("background-image", "url('/css/images/homeboxbg.png')").css("border", border); //.css("background-color", "transparent");
            $(this).children(".quicklinkcontent").children("h4").css("border-bottom", border);
            $(this).children("#controlboxcontent").children(".controlheading").css("border-top", border);
            var $activeLi = $(this).children("#controlboxcontent").children("#scroller").children("ul").children("li").children("a");
            // $activeLi.css("border", leaveBorder); //.width($activeLi.width() + 2).height($activeLi.width() + 2);
            $activeLi.removeClass("activebghover");
        });

        resizeWindow();
        var TO = false;
        $(window).resize(function () {
            if (TO !== false)
                clearTimeout(TO);
            TO = setTimeout(resizeWindow, 300); //200 is time in miliseconds
        });

        function resizeWindow() {
            var viewportWidth = $(window).width();
            var viewportHeight = $(window).height();
            var footerHeight = 75;
            //alert(footerHeight);
            //alert(viewportWidth + " x " + viewportHeight);
            animateHome(viewportWidth, viewportHeight - footerHeight);
        }

        function animateHome(width, height) {
            var minwidth = 910, maxwidth = 1200, minheight = 532, maxheight = 799;
            var easing = "easeOutCubic";
            var duration = 900;

            var origw = width;
            var origh = height;


            if (width < minwidth)
                width = minwidth;
            else if (width > maxwidth)
                width = maxwidth;
            if (height < minheight)
                height = minheight;
            else if (height > maxheight)
                height = maxheight;



            // width
            $("#homebannercontainer").stop().animate({ width: width, queue: false }, duration, easing);
            $("#main").stop().animate({ width: width, queue: false }, duration, easing);

            // height
            $("#homemain").stop().animate({ height: height, queue: false }, duration, easing);
            $("#homebannerbg").stop().animate({ height: height, queue: false }, duration, easing);

        }





    });






(function ($) {


    var total;
    var current;
    var timer;

    var m_cfg = {
        interval: 1000
    };
    var m_data;
    var methods = {
        init: function (options, data) {
            var $this = $(this);
            //alert($this.attr('id'));
            m_cfg = $.extend(m_cfg, options);
            if (data)
                m_data = data;
            total = data.length;

            // preload the images
            initImages(data, $this);

            // set the current 
            current = randomize(total);
            //alert("current " + current);
            return $this;
        },
        start: function () {
            var $this = $(this);

            // show immediately
            //methods.gotoPane.apply($this, [current]);

            // begin the cycle...
            methods.cycle.apply($this, [false]);
            return $this;
        },
        stop: function () {
            var $this = $(this);
            //$this.stop();
            //alert(timer);
            clearTimeout(timer);
            return $this;
        },
        movePrev: function () {
            var $this = $(this);
            methods.stop.apply($this);
            current--;
            methods.cycle.apply($this);
            return $this;
        },
        moveNext: function () {
            var $this = $(this);
            methods.stop.apply($this);
            current++;
            methods.cycle.apply($this);
            return $this;
        },
        jumpTo: function (index) {
            var $this = $(this);
            methods.stop.apply($this);
            current = index;
            // methods.gotoPane.apply($this, [index]);
            // begin the cycle...
            methods.cycle.apply($this);
          
            return $this;
        },
        cycle: function (moveon) {
            var $this = $(this);
            //  alert(moveon);
            if (moveon)
                current++;
            // reset if past the total number
            if (current >= total)
                current = 0;
            else if (current < 0)
                current = total - 1;

            methods.gotoPane.apply($this, [current]);
            // alert(current);
            // call this again...
            clearTimeout(timer);
            timer = setTimeout(function () { methods.cycle.apply($this, [true]) }, m_cfg.interval);
            return $this;
        },
        gotoPane: function (index) {
            //alert(index);
            var $this = $(this);
            var c = m_data[index];
            setImage($this, index);
            setName($this, c.name, c.url);
            movePicker($this, index);
            return $this;
        }
    };

    $.fn.homepage = function (method, options, data) {
        //alert(method);
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.homepage');
        }

    };



    function setImage($selector, index) {
        //alert(imageUrl);
        if ($selector && index > -1) {


            var images = $($selector).children("img");
            var active = $($selector).children("img.active");
            var next = $(images[index]);

            if (next.attr("src") == active.attr("src")) {
                return false;
            }

            if (active.length > 0) {
                active.fadeOut(function () {
                    active.removeClass('active');
                });
            }

            next.fadeIn().addClass('active');

            //$(images.get(index)).fadeIn().addClass('active');


            /*if ($active.length == 0) {
            // nothing has been set yet, fade the active one in
            $($images[index]).fadeIn().addClass("active");
            //alert('no active');
            }
            else {
            // we're in the middle of a run
            // var $next = $images[index]; //$active.next().length > 0 ? $active.next() : $selector.children("img:first");
            //alert($next);
            $active.fadeOut(function () {
            $active.removeClass('active');
            });
            $($images[index]).fadeIn().addClass('active');
            //alert('active');
            }*/
        }
    }

    function setName($selector, name, linkto) {
        if ($selector) {
            //alert($(".controlheading a").html());
            if (name.indexOf('Financial') > -1)
                name = name.replace('Financial', 'Financial<br/>');
            $(".controlheading a").html(name).attr("href", linkto);
        }
    }

    function movePicker($selector, index) {
        $("#scroller ul li").removeClass("activebg");
        //alert($("#scroller ul li:nth-child(" + (index + 2) + ")"));
      //  alert(index);
        $("#scroller ul li:nth-child(" + (index + 2) + ")").addClass("activebg");
    }

    function randomize(total) {
        return Math.floor(Math.random() * (total));
    }

    function initImages(data, $this) {
        for (x = 0; x < data.length; x++) {
            var image = new Image();
            image.src = data[x].imageUrl;
            $this.append("<img src='" + data[x].imageUrl + "' align='center' />");
        }

    }

})(jQuery);


