/*
 * 	easyAccordion BETA - jQuery plugin

 *
 */
 
	


 
(function (jQuery) {
    jQuery.fn.easyAccordion = function (options) {

        var defaults = {
            slideNum: false,
            slideNumPrefix: '0',
            autoStart: false,
            slideInterval: 6000
        };

        this.each(function () {

            //var dlEl = jQuery(this);
            var settings = jQuery.extend(defaults, options);
            // -------- Set the variables ------------------------------------------------------------------------------


            // Set Variables

            jQuery.fn.setVariables = function () { // This fixes an issue that occurs when there are borders on the DTs
                dlWidth = $(this).width();
                dlHeight = $(this).height();
                dtWidth = $(this).find('dt').outerHeight();
                if (jQuery.browser.msie) { dtWidth = $(this).find('dt').outerWidth(); /* IE */ }
                dtHeight = dlHeight - ($(this).find('dt').outerWidth() - $(this).find('dt').width());
                verticalExtra = parseInt($(this).find('dt').css('padding-left')) + parseInt($(this).find('dt').css('padding-right')) + parseInt($(this).find('dt').css('border-left-width')) + parseInt($(this).find('dt').css('border-right-width'));
                //alert(verticalExtra);
                if (jQuery.browser.msie) { dtHeight = dlHeight - verticalExtra; /* IE */ }
                //alert(dtHeight);
                slideTotal = $(this).find('dt').size();
                ddWidth = dlWidth - (dtWidth * slideTotal) - ($(this).find('dd').outerWidth(true) - $(this).find('dd').width());
                ddHeight = dlHeight - ($(this).find('dd').outerHeight(true) - $(this).find('dd').height());
            };

            $(this).setVariables();

            if (jQuery.browser.safari) { var dtTop = (dlHeight - dtWidth) / 2; var dtOffset = -dtTop; $(this).find('.slide-number').css({ 'bottom': '40px' });  /* Safari and Chrome */ }
            if (jQuery.browser.mozilla) { var dtTop = dlHeight - 20; var dtOffset = -20; /* FF */ }
            if (jQuery.browser.msie) { var dtTop = 0; var dtOffset = 0; /* IE */ }

            //alert(verticalExtra);

            //alert('dlWidth '+dlWidth);
            //alert('dlHeight '+dlHeight);
            //alert('$(this).find(dt).outerWidth() '+$(this).find('dt').width());
            //alert('$(this).find(dt).width() '+$(this).find('dt').outerWidth());
            //alert('outer '+$(this).find('dt').outerWidth());
            //alert('inner '+$(this).find('dt').width());



            // -------- Getting things ready ------------------------------------------------------------------------------

            //var spanOffset =60;
            //alert(dtWidth);
            //$('.slide-number').css({'bottom':spanOffset+'px'});		
            var f = 1;
            $(this).find('dt').each(function () {
                $(this).css({ 'width': dtHeight, 'top': dtTop, 'margin-left': dtOffset });
                if (settings.slideNum == true) {
                    $('<span class="slide-number">' + settings.slideNumPrefix + f + '</span>').appendTo(this);
                }
                f = f + 1;
            });

            if (jQuery.browser.safari) { $(this).find('.slide-number').css({ 'bottom': '-3px', 'left': '5px' });  /* Safari and Chrome */ }
            if (jQuery.browser.msie) { $(this).find('.slide-number').css({ 'bottom': '-8px', 'left': '5px' });  /* Safari and Chrome */ }
            if (jQuery.browser.msie && $.browser.version < 8) { $(this).find('.slide-number').css({ 'bottom': '8px', 'left': '5px' });  /* Safari and Chrome */ }

            $(this).find('dt').hover(function () {
                $(this).addClass('hover');
            }, function () {
                $(this).removeClass('hover');
            });

            if ($(this).find('.active').size()) {
                $(this).find('.active').next('dd').addClass('active');
            } else {
                $(this).find('dt:first').addClass('active').next('dd').addClass('active');
            }
            
            if (jQuery.browser.mozilla && ($.browser.version.substr(0, 3) == "1.9" && $.browser.version.length == 3) || 
		($.browser.version.substr(0, 5) == "1.9.0")) {
                $(this).find('dd').css({ 'left': 0 });
                $(this).find('dt').css('display', 'none');
            }
            else {
                $(this).find('dt:first').css({ 'left': '0' }).next().css({ 'left': dtWidth });
            }
            $(this).find('dd').css({ 'width': ddWidth, 'height': ddHeight });

            // -------- Functions ------------------------------------------------------------------------------


            jQuery.fn.findActiveSlide = function () {
                var i = 1;
                this.find('dt').each(function () {
                    if ($(this).hasClass('active')) {
                        activeID = i; // Active slide
                    } else if ($(this).hasClass('no-more-active')) {
                        noMoreActiveID = i; // No more active slide
                    }
                    i = i + 1;

                });
            };

            jQuery.fn.calculateSlidePos = function () {
                var u = 2;
                $(this).find('dt').not(':first').each(function () {
                    var activeDtPos = dtWidth * activeID;
                    if (u <= activeID) {
                        var leftDtPos = dtWidth * (u - 1);
                        $(this).animate({ 'left': leftDtPos }, 700);
                        if (u < activeID) { // If the item sits to the left of the active element
                            $(this).next().css({ 'left': leftDtPos + dtWidth });
                        } else { // If the item is the active one
                            $(this).next().animate({ 'left': activeDtPos }, 700);
                        }
                    } else {
                        var rightDtPos = dlWidth - (dtWidth * (slideTotal - u + 1));
                        var rightDdPos = rightDtPos + dtWidth;
                        $(this).animate({ 'left': rightDtPos }, 700);
                        $(this).next().animate({ 'left': rightDdPos }, 700);
                    }
                    u = u + 1;
                });
                setTimeout(function () { // This fixes an issue that occurs when there are borders on DTs
                    $('.easy-accordion').find('dd').not('.active').each(function () { // Try to use this here...
                        $(this).css({ 'display': 'none' });
                    });
                }, 400);

            };


            // activate a slide within an accordian
            // this is used on click and also for automatic rotation
            jQuery.fn.activateSlide = function () {
                this.parent('dl').setVariables();
                this.parent('dl').find('dd').css({ 'display': 'block' });
                this.parent('dl').find('dd.plus').removeClass('plus');
                this.parent('dl').find('.no-more-active').removeClass('no-more-active');
                this.parent('dl').find('.active').removeClass('active').addClass('no-more-active');
                this.addClass('active').next().addClass('active');
                this.parent('dl').findActiveSlide();
                if (activeID < noMoreActiveID) {
                    this.parent('dl').find('dd.no-more-active').addClass('plus');
                }
                this.parent('dl').calculateSlidePos();
            };

            jQuery.fn.activateFireFox3Slide = function () {
                this.parent('dl').setVariables();
                this.parent('dl').find('dd').css({ 'display': 'block' });
                this.parent('dl').find('dd.plus').removeClass('plus');
                this.parent('dl').find('.no-more-active').removeClass('no-more-active');
                this.parent('dl').find('.active').removeClass('active').addClass('no-more-active');
                this.addClass('active').next().addClass('active');
                this.parent('dl').findActiveSlide();
                if (activeID < noMoreActiveID) {
                    this.parent('dl').find('dd.no-more-active').addClass('plus');
                }
                this.parent('dl').find('dd.no-more-active').fadeOut(); // fadeout the old slide
                this.parent('dl').find('dd.active').fadeIn(); // fadein the old slide
            };

            jQuery.fn.rotateSlides = function (slideInterval, timerInstance) {
                // a variable is required to hold this accordian instance, because
                // $(this) will no longer refer to it within the anonymous function
                // passed into setTimeout
                var accordianInstance = $(this);

                timerInstance.value = setTimeout(function () { accordianInstance.rotateSlides(slideInterval, timerInstance); }, slideInterval);

                // determine which slide to show next
                $(this).findActiveSlide();
                var totalSlides = $(this).find('dt').size();
                var activeSlide = activeID;
                var newSlide = activeSlide + 1;
                if (newSlide > totalSlides) newSlide = 1;
                if (jQuery.browser.mozilla && ($.browser.version.substr(0, 3) == "1.9" && $.browser.version.length == 3) || 
		($.browser.version.substr(0, 5) == "1.9.0")) {
                    $(this).find('dt:eq(' + (newSlide - 1) + ')').activateFireFox3Slide(); // activate the new slide
                }
                else {
                    $(this).find('dt:eq(' + (newSlide - 1) + ')').activateSlide(); // activate the new slide
                }
            }


            // -------- Let's do it! ------------------------------------------------------------------------------
            function trackerObject() {
                this.value = null;
            }

            var timerInstance = new trackerObject();

            $(this).findActiveSlide();
            if (jQuery.browser.mozilla && $.browser.version.substr(0, 3) == "1.9" && $.browser.version.length == 3) {
            }
            else {
                $(this).calculateSlidePos();
            }

            if (settings.autoStart == true) {
                var accordianInstance = $(this);
                var interval = parseInt(settings.slideInterval);
                timerInstance.value = setTimeout(function () {
                    accordianInstance.rotateSlides(interval, timerInstance);
                }, interval);
            }

            $(this).find('dt').not('active').click(function () {
                // activate the current slide
                $(this).activateSlide();
                clearTimeout(timerInstance.value);
            });

        });
    };
})(jQuery);
