Fx.Flow = new Class({
	Extends: Fx,
	initialize: function(options){
		this.parent(options);
		this.index = 0;
	},
	compute: function(from, to, delta){
		this.value = Fx.compute(from, to, delta);
		
		var newWidth, newHeight, newTop, newLeft,level;
		if(document.galleryContainer) {
			var nodes = document.galleryContainer.childNodes;
			
			if(nodes) {
				level = nodes.length - 1;
				
				for(var i = 0; i < nodes.length; i++) {
					el = nodes[i];
			
					if(i < this.value + 4 && i > this.value - 4) {
						newHeight = Math.pow(3.0267, (5 - Math.abs(i - this.value))) + 10;
						newWidth = newHeight * 1.5;
						newLeft = Math.pow(3.6, (4.5955 - Math.abs(i - this.value))) - (newWidth/2);
						if(i - this.value > 0) {
							newLeft = 720 - newLeft - newWidth;
						}
						newTop = (264 - newHeight) / 2;
						
						el.style.left = newLeft + 'px';
						el.style.top = newTop + 'px';
						el.style.height = newHeight + 'px';		
						el.style.width = newWidth + 'px';
						el.style.zIndex = i < this.value ? level++ : level--;
						
						if(el.style.display != 'block') {
							el.style.display = 'block';
						}
					} else if(el.style.display != 'none') {
						el.style.display = 'none';
					}
				}
			}
		}
	
		return this.value;
	},
	toIndex: function(index){
		if(index < 0) {
			index = 0;
		}
		if(index >= document.galleryContainer.childNodes.length) {
			index = document.galleryContainer.childNodes.length - 1;
		}
		this.index = index;
		this.start(this.value, index);
	},
	stepForward: function(){
		this.toIndex(this.index+1);
	},
	stepBackward: function(){
		this.toIndex(this.index-1);
	},
	toFirst: function(){
		this.index = 0;
		this.start(-1, 0);
	},
	toLast: function(max){
		this.index = max - 1;
		this.start(max, this.index);
	},
	getIndex: function(){
		return this.index;
	},
	onStart: function(){
		this.fireEvent('start', this);
	},
	onComplete: function(){
		this.fireEvent('complete', this);
	}
});

document.toggleDisabled = function(from, toEls) {
	if(from) {
		for(var i = 0; i < toEls.length; i++) {
			var el = document.getElementById(toEls[i]);
			
			if(el) {
				if(from.checked) {
					el.value = '';
					el.disabled = true;
					el.style.backgroundColor = '#e4e4e4';
				} else {
					el.disabled = false;
					el.style.backgroundColor = '#ffffff';
				}
			}
		}
	}
}

document.updateDonationPrice = function(basePrice, cleanEl) {
	var priceDisplay = $('donationTotalLabel'), intervalType = $('donateIntervalType'), intervalCount = $('donateIntervalCount');
	if(basePrice == 'other') {
		var otherPrice = $('donateOtherAmount');
		if(otherPrice) {
			basePrice = otherPrice.value.toFloat();
		} else {
			basePrice = 0;
		}
	} else {
		basePrice = basePrice.toFloat();
	}
	
	if(priceDisplay && intervalCount) {
		if(cleanEl = $(cleanEl)) {
			cleanEl.value = cleanEl.value.replace(/[^\d\.]/g, '');
		}
		
		var totalPrice = basePrice;
		if(intervalCount.value == '')
			totalPrice = 0;
		else
			totalPrice *= intervalCount.value.toInt();
		if(intervalType)
			totalPrice *= intervalType.options[intervalType.options.selectedIndex].value.toInt();
		
		if(totalPrice == 0) {
			priceDisplay.innerHTML = '= $0.00';
		} else {
			priceDisplay.innerHTML = '= $'+number_format(totalPrice, 2, '.', ',');
		}
	}
}

/* number_format Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

function createExpirationDate(dateFieldID, monthFieldID, yearFieldID) {
	var dateField = document.getElementById(dateFieldID);
	var monthField = document.getElementById(monthFieldID);
	var yearField = document.getElementById(yearFieldID);

	if (dateField && monthField && yearField) {
		if (monthField.value != "" || yearField.value != "") {
			dateField.value = monthField.value + "/" + yearField.value;
		}
		else {
			dateField.value = "";
		}
		
	}

}

document.initAudio = function(audio) {
	var flashFallback = $(audio+'Fallback');
	audio = $(audio);
	
	if(audio && audio.canPlayType && flashFallback) {
		if(!audio.getElements('source').some(function(el) {return audio.canPlayType(el.get('type')) != '';})) {
			audio.src = '';
			flashFallback.replaces(audio);
		}
	}
}
