/**
 * VisualSwitcher.js - Based on Tabs.js
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

var VisualSwitcher = function(id) {
	var button,
		activeBtn,
		selectedCls = arguments[1] || 'selected',
		autoSwitch = arguments[2] || false,
		interval,
		speed = 5000,
		resourcePath = 'content/referencemodule/',
		visual = $('visual');
	
	return {
		initialize: function() {
			button = WS.DOM.getChildren($(id));
			this.initEvents();
		},
		initEvents: function() {
			var self = this;
			for(var i = 0; i < button.length; i++) {
				if(WS.hasClass(button[i], selectedCls))
					activeBtn = button[i];
				
				WS.Event.addEvent(button[i], 'mouseover', function() {
					clearInterval(interval);
					self.showButtonContent(this);
				});
				
				WS.Event.addEvent(button[i], 'mouseout', function() {
					interval = setInterval(self.next, speed);
				});
			}
			if(autoSwitch) {
				interval = setInterval(self.next, speed);
			}
		},
		showButtonContent: function(el) {
			if(el == activeBtn)
				return;
			
			WS.removeClass(activeBtn, selectedCls);
			WS.addClass(el, selectedCls);
			visual.src = resourcePath + WS.DOM.getChild(el, 1).rel;
			activeBtn = el;
		},
		next: function() {
			WS.removeClass(activeBtn, selectedCls);
			if(activeBtn == button.last()) {
				activeBtn = button.first();
				WS.addClass(activeBtn, selectedCls);
			}
			else {
				activeBtn = button[button.indexOf(activeBtn) + 1];
				WS.addClass(activeBtn, selectedCls);
			}
			visual.src = resourcePath + WS.DOM.getChild(activeBtn, 1).rel;
		}
	}
}

