document.observe("dom:loaded", function() {
  separateWords();
  qtyControl();
  navRollover();
});

function slideSwitch() {
    var $active = jQuery('#slideshow .active');

    if ( $active.length == 0 ) $active = jQuery('#slideshow :last');

    var $next =  $active.next().length ? $active.next()
        : jQuery('#slideshow :first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

jQuery(document).ready(function() {
	jQuery(".header-banner #scroll-bar").simplyScroll({
		autoMode: 'loop',
		continuous: true,
		frameRate: 10
	});
	var mhbr = 0;
	jQuery("#block-related li").each(function(i){
		ch = jQuery(this).height();
		if(ch > mhbr) mhbr = ch;
	});
	jQuery("#block-related li").css("height",mhbr);

	if(jQuery('body').hasClass('cms-home')) {
		jQuery('.col-main p:first').attr("id","slideshow");
	}

	setInterval( "slideSwitch()", 4000 );
});
/*
 * --------------------------------------------------------------------
 *  separate words inside element into spans
 * --------------------------------------------------------------------
 */
function separateWords() {
    $$('.separate-words').each(function(elem) {
        var wordsArray = elem.innerHTML.split(' ');
        var resultTxt = '';
        for(i=0; i<wordsArray.length; i++) {
            resultTxt += '<span>' + wordsArray[i] + '</span>';
            if(i!=wordsArray.length-1)
                resultTxt += ' ';
        }
        elem.update(resultTxt);
    });
    
}


/*
 * --------------------------------------------------------------------
 *  override decorateTable (js/varien/js.js)
 * --------------------------------------------------------------------
 */
function decorateTable(table, options) {
    var table = $(table);
    if (table) {
        // set default options
        var _options = {
            'tbody'    : false,
            'tbody tr' : ['odd', 'even', 'first', 'last'],
            'thead tr' : ['first', 'last'],
            'tfoot tr' : ['first', 'last'],
            'tr td'    : ['last'],
            'tr th'    : ['first', 'last']
        };
        // overload options
        if (typeof(options) != 'undefined') {
            for (var k in options) {
                _options[k] = options[k];
            }
        }
        // decorate
        if (_options['tbody']) {
            decorateGeneric(table.select('tbody'), _options['tbody']);
        }
        if (_options['tbody tr']) {
            decorateGeneric(table.select('tbody tr'), _options['tbody tr']);
        }
        if (_options['thead tr']) {
            decorateGeneric(table.select('thead tr'), _options['thead tr']);
        }
        if (_options['tfoot tr']) {
            decorateGeneric(table.select('tfoot tr'), _options['tfoot tr']);
        }
        if (_options['tr td']) {
            var allRows = table.select('tr');
            if (allRows.length) {
                for (var i = 0; i < allRows.length; i++) {
                    decorateGeneric(allRows[i].getElementsByTagName('TD'), _options['tr td']);
                }
            }
        }
        if (_options['tr th']) {
            var ths = table.select('tr th');
            if (ths.length) {
                decorateGeneric(ths, _options['tr th']);
            }
        }
    }
}

/*
 * --------------------------------------------------------------------
 *  Quantity increment/decrement control
 * --------------------------------------------------------------------
 */
function qtyControl() {   
    if($$('.qty-control').length) {
        $$('.qty-control .btn-increment').invoke('observe', 'click', function() {
            this.previous('.qty').value++;
        });
        $$('.qty-control .btn-decrement').invoke('observe', 'click', function() {
            inputQty = this.previous('.qty');
            if(inputQty.value > 1)
                inputQty.value--;
        });        
    }
    
}

/*
 * --------------------------------------------------------------------
 *  ie6 menu rollover
 * --------------------------------------------------------------------
 */
function navRollover() {
    if(document.all && $$('.side-nav').length) {
        $$('.side-nav li').invoke('observe', 'mouseenter', function() {
            this.addClassName('over');
        }).invoke('observe', 'mouseleave', function() {
            this.removeClassName('over');
        });
    }
}

