
newSwitcher = {
	maxShowIndex : 101,
	nextShowIndex : 100,

	flipImgDuration : 800,
	flipTextDuration : 400,
	
	autoFlipSpeed : 4000,

	create : function(params){
		if (!params.container) return;

		var container;
		container = $(params.container);

		container.bottomBar = $("switcher_bottom_bar_"+container.id); 
		container.bottomBarCursor = $(container.bottomBar.getElementsByTagName("img")[0]);
		
		container.rightLink = document.createElement("a");
		container.rightLink.className = "front_switch_text";
		container.bottomBar.appendChild(container.rightLink);
		container.isSwitching = false;
		container.autoFlipInterval = 0;
		container.flipElements = [];

		var children = container.childNodes;
		var fst = true;
		for (var i=0; i<children.length; i++){
			if (children[i].id && children[i] != container.bottomBar){
				var imgs = children[i].getElementsByTagName("img");
				children[i].bigImg = imgs[0];
				children[i].thumbImg = imgs[1];
				children[i].thumbImgShaded = imgs[2];
				children[i].imgLink = children[i].getElementsByTagName("a")[0];
				children[i].linkText = children[i].getElementsByTagName("span")[0];
				children[i].normalPosition = i;
				children[i].parentSwitcher = container;
				children[i].autoFlipInterval = 0;
				children[i].switchElementInd = container.flipElements.push(children[i]) -1 ;

                                children[i].appendChild(children[i].linkText);
				children[i].imgLink.appendChild(children[i].bigImg);

				if (fst){
					container.selectedElement = children[i];
					children[i].switchSelected = true;
				}else{
					children[i].switchSelected = false;
				}
                                children[i].thumbLink = document.createElement("a");
                                children[i].thumbLink.href = "javascript:void(0);";
                                children[i].thumbLink.className = "front_switch_thumb";
                                children[i].thumbLink.onclick = this.clickSetImage;
                                children[i].thumbLink.switchElement = children[i];

				children[i].thumb = new Image();
				children[i].thumb.src = children[i].switchSelected ? children[i].thumbImg.src : children[i].thumbImgShaded.src;
				children[i].thumbLink.appendChild(children[i].thumb);

				container.bottomBar.appendChild(children[i].thumbLink);

				fst = false;
			}
		}

		var clr = document.createElement("div");
		clr.style.clear = "both";
		container.bottomBar.appendChild(clr);

		if (container.selectedElement){
			container.rightLink.href = container.selectedElement.imgLink.href;
			container.rightLink.innerHTML = container.selectedElement.linkText.innerHTML;
	
			container.switchTextEffect = new Fx.Morph(container.rightLink, {duration: this.flipTextDuration, transition: Fx.Transitions.Sine.easeOut});
			container.switchTextEffect.parSwitcher = container;
	
			container.selectedElement.style.zIndex = this.maxShowIndex;
			
			this.startAutoFlip.apply(container);
		}
	},
	clickSetImage : function(e){
		if (!this.switchElement.parentSwitcher.isSwitching && this.switchElement != this.switchElement.parentSwitcher.selectedElement){

                	newSwitcher.stopAutoFlip.apply(this.switchElement.parentSwitcher);

                	newSwitcher.putElement(this.switchElement);
		}
	},
	putElement : function(new_el){
               	new_el.parentSwitcher.isSwitching = true;

		new_el.parentSwitcher.switchToElement = new_el;

		$(new_el).setStyles({
			"opacity" : 1,
			"zIndex" : newSwitcher.nextShowIndex
		});
		$(new_el.parentSwitcher.selectedElement).setStyles({
			"opacity" : 1
		});

		new_el.parentSwitcher.hideImageEffect = new Fx.Morph(new_el.parentSwitcher.selectedElement, {duration: newSwitcher.flipImgDuration, transition: Fx.Transitions.Sine.easeOut});
		//hideImageEffect.addEvent('complete', remove_front_selected_class.bind(this.switchElement.parentSwitcher.selectedElement));
		new_el.parentSwitcher.hideImageEffect.parSwitcher = new_el.parentSwitcher;
		new_el.parentSwitcher.hideImageEffect.addEvent('complete', newSwitcher.putSelectedElement);
		new_el.parentSwitcher.hideImageEffect.start({'opacity': 0});

/* 		new_el.parentSwitcher.showImageEffect = new Fx.Morph(new_el, {duration: newSwitcher.flipImgDuration, transition: Fx.Transitions.Sine.easeOut});
		new_el.parentSwitcher.showImageEffect.parSwitcher = new_el.parentSwitcher;
		new_el.parentSwitcher.showImageEffect.addEvent('complete', newSwitcher.putSelectedElement);
		new_el.parentSwitcher.showImageEffect.start({'opacity': 1}); */

		new_el.parentSwitcher.switchTextEffect.addEvent('complete', newSwitcher.switchText);
		new_el.parentSwitcher.switchTextEffect.start({'color': '#0875bd'});
	},
	switchText : function(){
		this.parSwitcher.rightLink.href = this.parSwitcher.switchToElement.imgLink.href;
		this.parSwitcher.rightLink.innerHTML = this.parSwitcher.switchToElement.linkText.innerHTML;

                this.parSwitcher.selectedElement.thumb.src = this.parSwitcher.selectedElement.thumbImgShaded.src;
                this.parSwitcher.switchToElement.thumb.src = this.parSwitcher.switchToElement.thumbImg.src;

                $(this.parSwitcher.bottomBarCursor).setStyles({
			"left" : this.parSwitcher.switchToElement.switchElementInd * 77 + 10 + "px"
		});

		this.parSwitcher.switchTextEffect.removeEvent('complete', newSwitcher.switchText);
		this.parSwitcher.switchTextEffect.start({'color': '#FFFFFF'});
	},
	putSelectedElement : function(){
                $(this.parSwitcher.selectedElement).setStyles({
			"opacity" : 1,
			"zIndex" : this.parSwitcher.selectedElement.normalPosition
		});
		$(this.parSwitcher.switchToElement).setStyles({
			"opacity" : 1,
			"zIndex" : newSwitcher.maxShowIndex
		});

		this.parSwitcher.selectedElement = this.parSwitcher.switchToElement;
		this.parSwitcher.switchToElement = null;

		this.parSwitcher.isSwitching = false;

		newSwitcher.startAutoFlip.apply(this.parSwitcher);
	},
	autoFlip : function(){
		if (this.selectedElement.switchElementInd < this.flipElements.length -1){
			/* if (!this.flipElements[this.selectedElement.switchElementInd + 1]){
				whi.degub.error_log("nu e " + (this.selectedElement.switchElementInd + 1));
			} */
			newSwitcher.putElement(this.flipElements[this.selectedElement.switchElementInd + 1]);
		}else if (this.selectedElement.switchElementInd != 0){
			/* if (!this.flipElements[this.selectedElement.switchElementInd + 1]){
				whi.degub.error_log("nu e 0");
			} */
			newSwitcher.putElement(this.flipElements[0]);
		}
	},
	startAutoFlip : function(){
		if (!this.isSwitching && this.autoFlipInterval <= 0){
			this.autoFlipInterval = setInterval(newSwitcher.autoFlip.bind(this), newSwitcher.autoFlipSpeed);
		}
	},
	stopAutoFlip : function(){
		if (this.autoFlipInterval > 0){
			clearInterval(this.autoFlipInterval);
			this.autoFlipInterval = 0;
		}
	}
}