(function ($) {

    $.fn.wtdbanner = function (options) {

        var opts = $.extend({}, $.fn.wtdbanner.defaults, options);

        if (this.length) {
            return this.each(function () {
                setBanner($(this), opts);
            });
        }

        function setBanner($obj, opts) {
            if (opts.xmldriven == true) {
                $obj.html('<div class=\'banner-content\'><ul class=\'banner-carousel\'></ul></div><div class=\'banner-nav\'><ul></ul></div>');

                $.ajax({
                    type: 'GET',
                    url: opts.xmlpath,
                    dataType: 'xml',
                    success: function (data) {
                        xmlParser(data, $obj, true);
                    }
                });
            }
            else {
                var data = '';
                xmlParser(data, $obj, false)
            }

            function xmlParser(xml, $obj, noXML) {
                if (noXML == true) {
                    $(xml).find('banner-item').each(function (index, element) {
                        var title = $(element).find('title').text();
                        var subTitle = $(element).find('subtitle').text();
                        var image = $(element).find('image-path').text();
                        var linkText = $(element).find('link-text').text();
                        var linkURL = $(element).find('link-url').text();
                        var navTitle = $(element).find('nav-title').text();
                        $obj.find('div.banner-content ul.banner-carousel').append(
							'<li><a href=\'' + linkURL + '\' title=\'' + linkText + '\'tabindex=\'-1\'>' + linkText + '<img src=\'' + image + '\' alt=\'' + title + '\'/></a></li>'
						);
                        $obj.find('div.banner-nav ul').append(
							'<li><a href=\'' + linkURL + '\' title=\'' + navTitle + '\'>' + navTitle + '</a></li>'
						);
                    });
                }

                $obj.find('div.banner-nav ul li:last-child').addClass('last');
                if ($('div.spotlight').length) {
                    console.log('hi');
                    $obj.find('div.banner-nav ul li').addClass('spotlight-background');
                    $obj.find('div.banner-nav ul li:first-child').addClass('spotlight-active');
                }
                else {
                    $obj.find('div.banner-nav ul li:first-child').addClass('active');
                }
                if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
                }
                else {
                    sIFR.replace(albertBold, {
                        selector: 'div.banner-content h2',
                        css: '.sIFR-root {  color:#24a9c3; font-size:25px; font-weight:bold; line-height:40px;}',
                        wmode: 'transparent',
                        ratios: [8, 1.31, 12, 1.27, 15, 1.21, 16, 1.22, 22, 1.2, 23, 1.17, 25, 1.19, 33, 1.17, 42, 1.16, 47, 1.15, 48, 1.16, 66, 1.15, 70, 1.14, 72, 1.15, 78, 1.14, 79, 1.15, 1.14]
                    });
                }

                sIFR.replace(albertLight, {
                    selector: 'div.banner-content h3',
                    css: '.sIFR-root { color:#24a9c3; font-size:20px; leading:-4; }',
                    wmode: 'transparent',
                    ratios: [7, 1.32, 8, 1.31, 12, 1.27, 15, 1.21, 16, 1.22, 22, 1.2, 23, 1.17, 28, 1.19, 30, 1.17, 31, 1.18, 33, 1.17, 38, 1.16, 39, 1.17, 48, 1.16, 76, 1.15, 77, 1.14, 80, 1.15, 84, 1.14, 85, 1.15, 1.14]
                });

                if ($('div.spotlight').length) {
                    sIFR.replace(albert, {
                        selector: 'div.content.home-page div.section div.banner-nav ul li a',
                        css: '.sIFR-root { color:#FFFFFF; font-size:20px; cursor:pointer; line-height:48px; leading:-4; }',
                        wmode: 'transparent',
                        ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
                    });
                }
                else {
                    sIFR.replace(albert, {
                        selector: 'div.content.home-page div.section div.banner-nav ul li a',
                        css: '.sIFR-root { color:#24a9c3; font-size:20px; cursor:pointer; line-height:48px; leading:-4; }',
                        wmode: 'transparent',
                        ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
                    });
                }

                var bannerInterval;

                if (opts.auto == true) {
                    bannerInterval = setInterval(function () { autoCarousel($obj, opts); }, opts.timer);
                }

                setNavigation($obj, opts, bannerInterval);
            }
        }

        function setNavigation($obj, opts, bannerInterval) {
            $overflowWidth = $obj.find('div.banner-content').width() * 4;
            $obj.find('div.banner-content ul.banner-carousel').css('width', $overflowWidth + 'px');

            $obj.find('div.banner-nav ul li a').bind('mouseenter focus', function () {

                if (opts.auto == true) {
                    clearInterval(bannerInterval);
                }
                if ($('div.spotlight').length) {
                    $obj.find('div.banner-nav ul li').removeClass('spotlight-active');
                    $(this).parent().addClass('spotlight-active');
                }
                else {
                    $obj.find('div.banner-nav ul li').removeClass('active');
                    $(this).parent().addClass('active');
                }
                var index = $obj.find('div.banner-nav ul li').index($(this).parent());
                var bannerPos = ($overflowWidth / 4) * index;

                moveCarousel($obj, opts, bannerPos);

            })

            $obj.find('div.banner-nav ul li a').bind('mouseleave blur', function () {
                if (opts.auto == true) {
                    bannerInterval = setInterval(function () { autoCarousel($obj, opts); }, opts.timer);
                }
            });

            $obj.find('div.banner-content').mouseenter(function () {
                if (opts.auto == true) {
                    clearInterval(bannerInterval);
                }
            }).mouseleave(function () {
                if (opts.auto == true) {
                    bannerInterval = setInterval(function () { autoCarousel($obj, opts); }, opts.timer);
                }
            });
        }

        function moveCarousel($obj, opts, bannerPos) {
            if (opts.slide == true) {
                $obj.find('div.banner-content ul.banner-carousel').stop().animate({
                    left: '-' + bannerPos + 'px'
                }, opts.speed, opts.easing);
            }
            else {
                $obj.find('div.banner-content ul.banner-carousel').css('left', '-' + bannerPos + 'px');
            }
        }

        function autoCarousel($obj, opts) {
            var index = $obj.find('div.banner-nav ul li').index($('div.banner-nav ul li.active'));
            if (index > 2) {
                index = -1;
            }
            index = index + 1;
            var bannerPos = ($overflowWidth / 4) * index;

            $obj.find('div.banner-nav ul li').removeClass('active');

            var activeItem = index;
            $obj.find('div.banner-nav ul li').eq(activeItem).addClass('active');

            if (opts.slide == true) {
                $obj.find('div.banner-content ul.banner-carousel').stop().animate({
                    left: '-' + bannerPos + 'px'
                }, opts.speed, opts.easing);
            }
            else {
                $obj.find('div.banner-content ul.banner-carousel').css('left', '-' + bannerPos + 'px');
            }
        }
    };

    // Banner Defaults
    $.fn.wtdbanner.defaults = {
        xmldriven: true,
        xmlpath: '/Views/common/xml/wtdbanner.xml',
        speed: 700,
        slide: true,
        timer: 5000,
        auto: false,
        easing: 'easeOutQuint'
    };


})(jQuery);
