/*
	Class:    	Rotator
	Author:   	Martin Cadorette
	Website:    http://farao.org
	Version:  	0.0.1
	Date:     	2009/11/17
	Built For:  MooTools 1.2
*/



var Rotator = new Class({
	//implements
	Implements: [Options, Events],

	//options
	options: {
		fixPngForIe : 1,		// In styles.css
		defaultSlide: '1',		// Starting Slide
		totalSlides: '10',		// Total number of Slides
		switchOn: 'over',		// When to switch Slide (click | over)
		delay: '500',			// Delay between each rotation
		transMethod: 'easeOut',	// Possible value: ease, bounce, elastic, linear, quad, cubic, quart, quint, pow, expo, circ, sine, back.
								// 		combined with: Out, In or InOut
		transType: 'fade',		// Possible value: fade, reveal, morph, slideLeft, slideRight, slideUp, slideDown
		transDuration: '2000',
		transDurationIn: '2000',
		transDurationOut: '2000'
	},
	
	//initialization
	initialize: function(element, options) {
		//set options
		//alert("Initi 2");
		this.element = $(element);
		this.setOptions(options);
		this.options.element = $$(this.options.element);
		
		maxSlides = this.options.totalSlides;
		maxConts = this.element.getElements("*[class*=rot_content]").length;
		maxButs =  this.element.getElements("*[class*=rot_button]").length;
		if (maxSlides>maxConts) {maxSlides=maxConts;}
		if (maxSlides>maxButs) {maxSlides=maxButs;}
		this.maxSlides = maxSlides;
		
		new Element('input', {id: 'sel_rotSlide', type: 'hidden', value: this.options.defaultSlide}).injectInside(this.element)
		new Element('input', {id: 'sel_rotTotal', type: 'hidden', value: maxSlides}).injectInside(this.element)
		
		var coorMain = $("rot_content_1").getCoordinates();
		this.element.getElements("*[class*=rot_content]").each(function(el) {
			el.setStyles({visibility: 'hidden', position: 'absolute', opacity: 0, top: coorMain['y'], left: coorMain['x']});
		}, this);
		
		this.show(this.options.defaultSlide);
		$("rot_button_"+this.options.defaultSlide).removeClass("rot_button").addClass("rot_button_on");
		if (this.options.delay>0) {myTimer = this.switchSlide.periodical(this.options.delay);}
		
		for (i=1; i<=maxSlides; i++) {
			if (this.options.switchOn=="over") {
				this.element = $("rot_button_"+i).addEvents({
					'mouseover' : this.pause.bind(this,i),
					'mouseout' : this.resume.bind(this,i)
				});
			}
			if (this.options.switchOn=="click") {
				this.element = $("rot_button_"+i).addEvents({
					'click' : this.show.bind(this,i),
					'mouseover' : this.over.bind(this,i),
					'mouseout' : this.resume.bind(this,i)
				});
			}
		}
		
		if(this.options.fixPngForIe) this.fixIeStuffs();
		document.addEvent('mousewheel', function(){
			this.isScrolling = false;
		}.bind(this));
	},
	
	pause: function(el) {
		if (myTimer) {$clear(myTimer);}
		myTimer = null;
		for (i=1; i<=this.maxSlides; i++) {if (i!=el) {this.hide(i);}}
		this.show(el);
	},
	
	resume: function(el) {
		//$("rot_button_"+el).removeClass("rot_button_on").removeClass("rot_button_over");
		if (this.options.delay>0) {myTimer = this.switchSlide.periodical(this.options.delay);}
	},

	hide: function(el) {
		if ($("rot_content_"+el).getStyle("visibility")!="hidden" || $("rot_content_"+el).getStyle("opacity")!="0") {
			$("rot_button_"+el).removeClass("rot_button_on").removeClass("rot_button_over").addClass("rot_button");
			$("rot_content_"+el).fade("out");
		}
	},

	show: function(el) {
		//alert(el)
		for (i=1; i<=this.maxSlides; i++) {if (i!=el) {this.hide(i);}}
		$("rot_button_"+el).removeClass("rot_button").addClass("rot_button_over");
		$("rot_content_"+el).fade("in");
		$("sel_rotSlide").value = el;
	},

	over: function(el) {
		$("rot_button_"+el).addClass("rot_button_on");
	},
	
	switchSlide: function() {
		totSld = $("sel_rotTotal").value;

		for (i=1; i<=totSld; i++) {
			$("rot_button_"+i).removeClass("rot_button_on").removeClass("rot_button_over").addClass("rot_button");
			if ($("rot_content_"+i).getStyle("visibility")!="hidden") {$("rot_content_"+i).fade("out");}
		}
		
		sCur = parseInt($("sel_rotSlide").value)+1;
		if (sCur>totSld) {sCur=1;}
		$("sel_rotSlide").value = sCur;
		$("rot_button_"+sCur).removeClass("rot_button").addClass("rot_button_over");
		$("rot_content_"+sCur).fade("in");
	},
	
	fixIeStuffs : function () {
		if (Browser.Engine.trident4) {
			//We fix png stuffs
			var rpng = new RegExp('url\\(([\.a-zA-Z0-9_/:-]+\.png)\\)');
			var search = new RegExp('(.+)formcheck\.css');
			for (var i = 0; i < document.styleSheets.length; i++){
				if (document.styleSheets[i].href.match(/styles\.css$/)) {
					var root = document.styleSheets[i].href.replace(search, '$1');
					var count = document.styleSheets[i].rules.length;
					for (var j = 0; j < count; j++){
						var cssstyle = document.styleSheets[i].rules[j].style;
						var bgimage = root + cssstyle.backgroundImage.replace(rpng, '$1');
						if (bgimage && bgimage.match(/\.png/i)){
							var scale = (cssstyle.backgroundRepeat == 'no-repeat') ? 'crop' : 'scale';
							cssstyle.filter =  'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src=\'' + bgimage + '\', sizingMethod=\''+ scale +'\')';
							cssstyle.backgroundImage = "none";
						}
					}
				}
			}
		}
	}

});