// Author: accordeon.js is written by Mr. Jean
// Location: www.mrjean.net or www.mrjean.be
// License: Don't use without permission!
//
// v 1.0    - Tuesday, August 19, 2008
//          - First version
//
// v 1.1    - Wednesday, August 20, 2008
//          - Added automatic switching
//          - Added per word variable time calculation
//
// v 1.2	- Tuesday, September 2, 2008
//			- Conversion to jQuery
//
// Description:
// This file provides support for accordeon items
// Supports automatic switching
// Supports the possibility to make the accordeon intelligent and swap faster
// if text is shorter or slower when text is longer.
// A human takes approx. 150ms/word. - FINISHED -


if (!ACCORDEON) { var ACCORDEON = new Object() }

ACCORDEON = {
    
    parents                         :   Array(),
    items                           :   Array(),
    contents                        :   Array(),
    viewed                          :   null,
	preViewed						:	1,
    isSliding                       :   false,
    
    periodicalUpdater               :   null,
    periodicalUpdaterDelayed        :   null,
    auto                            :   false,
    autoInterval                    :   5,
    autoIntervalClicked             :   20,
    
    advancedWordCount               :   false,
    advancedWordTime                :   0.18,
    
    
    executeAccordeon                :   function() {
    
        ACCORDEON.parents = ACCORDEON.getElementsByClassName('accordeon_parent', 'ul');
		
		if (ACCORDEON.parents != null) {

			$('#view'+ACCORDEON.preViewed).hide();
		
			$(ACCORDEON.parents).each(function(parent) {
				ACCORDEON.items = ACCORDEON.getElementsByClassName('title', 'div', this);
				var x = 0;
				$(ACCORDEON.items).each(function(item) {
					if (x > 0) {
						$('#'+this.id).addClass('hover');
					}
					x++;
					$('#'+this.id).bind('click', ACCORDEON.switchItems);
				});
				
				
				ACCORDEON.contents = ACCORDEON.getElementsByClassName('content', 'div', this);
				var i = 0;
				$(ACCORDEON.contents).each(function(content) {
					if (i > 0) {
						this.style.display = 'none';
					} else {
						ACCORDEON.viewed = ACCORDEON.items[0].id;
					}
					i++;
				});
			});
			
			if (ACCORDEON.auto) { 
				ACCORDEON.switchItemsAuto();
			}
			
		}
    },
    
    switchItemsAuto                 :   function() {

        if (ACCORDEON.periodicalUpdaterDelayed != null) {
            ACCORDEON.periodicalUpdaterDelayed.stop();
        }
	
		ACCORDEON.periodicalUpdater = $.timer(ACCORDEON.autoInterval*1000, function (timer) {
																					 
            if (!ACCORDEON.isSliding) {
            
                var itemIndex = parseInt(ACCORDEON.viewed.substr(4, 4))+1;

                var newIndex = ACCORDEON.viewed.substr(0,4)+itemIndex;

				// jQuery object always exists, check length
                if($('#'+newIndex).length == 0) {
                    itemIndex = 1;
                    newIndex = ACCORDEON.viewed.substr(0,4)+itemIndex;
                }

                ACCORDEON.isSliding = true;

				$('#'+ACCORDEON.viewed+'_content').slideUp(500);
				$('#'+newIndex+'_content').slideDown(500, function() {
					ACCORDEON.isSliding = false;
				});

                ACCORDEON.viewed = newIndex;
            }
																					 
			//ACCORDEON.periodicalUpdater.stop();
		});
    },
    
    wordCount                       :   function() {
 
        var text = $('#'+ACCORDEON.viewed+"_content").html().replace(/<\/?[^>]+>/gi, '').replace(/\s/g,' ').split(' ');
        var wordCount = 0;
        for (i=0; i<text.length; i++) {
            if (text[i].length > 0) wordCount++;
        }
        return wordCount;
    },
    
    haltSwitchItemsAuto             :   function() {

        var delay;    

        if (ACCORDEON.advancedWordCount) {
            delay = Math.abs(ACCORDEON.autoInterval + ((ACCORDEON.advancedWordTime * ACCORDEON.wordCount()) - ACCORDEON.autoInterval));
        } else {
            delay = ACCORDEON.autoIntervalClicked - ACCORDEON.autoInterval;
        }
        
		ACCORDEON.periodicalUpdaterDelayed = $.timer(delay*1000, function (timer) {
           ACCORDEON.switchItemsAuto();
		});
        
    },
    
    switchItems                     :   function(e) {
	
        var element = this;
        var clickedWhat;

        if (element.id == "") {
            clickedWhat = element.parentNode.id;
        } else {
            clickedWhat = element.id;
        }
		
		$('#view'+ACCORDEON.preViewed).show();
		ACCORDEON.preViewed = parseInt(clickedWhat.substr(4, 4));
        $('#view'+ACCORDEON.preViewed).hide();
    
        if (!ACCORDEON.isSliding && (ACCORDEON.viewed != clickedWhat)) {

            ACCORDEON.isSliding = true;
    
			$('#'+ACCORDEON.viewed+'_content').slideUp(500);
			$('#'+ACCORDEON.viewed).addClass('hover');
			$('#'+clickedWhat+'_content').slideDown(500, function() {
				$('#'+clickedWhat).removeClass('hover');
				ACCORDEON.isSliding = false;
			});
            
            ACCORDEON.viewed = clickedWhat;
            
            if (ACCORDEON.auto) {        
                ACCORDEON.periodicalUpdater.stop();
                if (ACCORDEON.periodicalUpdaterDelayed != null) {
                    ACCORDEON.periodicalUpdaterDelayed.stop();
                }
                ACCORDEON.haltSwitchItemsAuto();
            }

        }
        
    },
    
    getElementsByClassName          :   function(className, tag, elm) {
        
        var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
        var tag = tag || "*";
        var elm = elm || document;
        var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
        var returnElements = [];
        var current;
        var length = elements.length;
        for(var i=0; i<length; i++){
            current = elements[i];
            if(testClass.test(current.className)){
                returnElements.push(current);
            }
        }
        return returnElements;
        
    }

}

$(document).ready(ACCORDEON.executeAccordeon);
