/**
	DIV Switcher Object
	Written by Daniel Fekete
	Created for VOOV Ltd. - 2008
	2008 copyright VOOV Ltd. All rights reserved!

*/


switcher = {
	switches: 2,
	switcher_div: "switcher_",
	speed: 5000,
	current_div: 0,
	prev_div: -1,
	switch_timer: null,
	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);
		
		// Az onclick miatt
		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);

		
	},
	
	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, 100*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"; 
			for(var i=1; i<switcher.switches; i++) {
				// Az elsőt nem tüntetjük el
				var o = document.getElementById(switcher.switcher_div + i);
				o.style.display = "none";
				// csináljunk timert:
				switcher.switch_timer = setInterval("switcher.m_interval()", switcher.speed);
			}
		})
	}
}