/**
	DIV Switcher Object
	Written by Daniel Fekete
	Created for VOOV Ltd. - 2008
	2008 copyright VOOV Ltd. All rights reserved!

*/


switcher = {
	switches: 8,
	switcher_div: "switcher_",
	speed: 4000,
	current_div: 0,
	prev_div: -1,
	switch_timer: null,
	onSwitch: function(idx) {},
	switchTo: function(value) {
		// Az összes divet, ami nem value, azt tüntessük el, a value-t
		// jelenítsük meg
		for(var i=0; i<this.switches; i++) {
			//var o = document.getElementById(this.switcher_div + i);
			
			if (i == value)  this.setOpacity(this.switcher_div + i, 10);
			else this.setOpacity(this.switcher_div + i, 0);
		}
		// töröljük a timer-t, hogy ne lépkedjen tovább
		if (this.switch_timer != null) clearInterval(this.switch_timer);
		this.onSwitch(value);
		this.current_div = value;
		// Az onclick miatt
		return false;
	},
	
	toNext: function() {
		var next = this.current_div + 1;
		if (next == this.switches) next = 0;
		this.switchTo(next);
		
		return false;
	},
	
	toPrev: function() {
		var prev = this.current_div - 1;
		if (prev == -1) prev = this.switches - 1;
		this.switchTo(prev);		
		
		return false;
	},
	
	m_interval: function() {

		this.initFade(this.switcher_div + this.current_div, 0, 10);
		
		// lépjünk a következőre
		this.current_div++;
		if (this.current_div == this.switches) this.current_div = 0;
		

		this.initFade(this.switcher_div + this.current_div, 10, 0);
		this.onSwitch(this.current_div);
		
	},
	
	setOpacity: function(obj_name, value) {
		if (value == 0) document.getElementById(obj_name).style.display = "none";
		else document.getElementById(obj_name).style.display = "block";
		document.getElementById(obj_name).style.opacity = value/10;
		document.getElementById(obj_name).style.filter = "alpha(opacity=" + value*10 + ")";
	},
	
	initFade: function(obj_name, target_op, current_op) {
		var counter = 0;
		while (target_op != current_op) {
			if (target_op > current_op) current_op++;
			else if (target_op < current_op) current_op--;
			var timeout_string = "switcher.setOpacity('" + obj_name + "', " + current_op + ")"; 
			setTimeout(timeout_string, 200*counter);
			counter++;	
		}
		
		
	},
	
	addEvent: function( obj, type, fn ) {
	    if ( obj.attachEvent ) {
	        obj['e'+type+fn] = fn;
	        obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
	        obj.attachEvent( 'on'+type, obj[type+fn] );
	    } else
	        obj.addEventListener( type, fn, false );
	}, 	 
	
	initalize: function(num_of_switches) {
		// Tegyük be document.onload-ba
		if (typeof(num_of_switches) != "undefined")
			switcher.switches = num_of_switches;
			 
		this.addEvent(window, "load", function() {
			// eltüntetjük az összes divet, amit szabad, de
			var o = document.getElementById(switcher.switcher_div + "0");
			o.style.display = "block"; 
			var neg_counter = 0;
			for(var i=1; i<switcher.switches; i++) {
				// Az elsőt nem tüntetjük el
				var o = document.getElementById(switcher.switcher_div + i);
				if (!o) {
					neg_counter++;
					continue;
				} 
				o.style.display = "none";
				// csináljunk timert:
			}
			switcher.switches -= neg_counter;
			switcher.switch_timer = setInterval("switcher.m_interval()", switcher.speed);
		})
	}
}
