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){
		this.start(this.value, index);
		this.index = index;
	},
	stepForward: function(max){
		if(this.index < max - 1) {
			this.index++;
			this.start(this.value, this.index);
		}
	},
	stepBackward: function(){
		if(this.index > 0) {
			this.index--;
			this.start(this.value, this.index);
		}
	},
	getIndex: function(){
		return this.index;
	},
	onStart: function(){
		this.fireEvent('start', this);
	},
	onComplete: function(){
		this.fireEvent('complete', this);
	}
});

document.mapInit = function() {
	var markerHTML = '<table cellpadding="0" cellspacing="0" border="0" style="color: black; font-size: 10px;"> <tr> <td rowspan="3" valign="middle" align="left" style="padding-right: 5px;"><img src="images/icons/map_logo.jpg" width="42" height="37" border="0" alt="Next FX" title="Next FX"  /></td> <td valign="middle" align="left"><div style="font-weight: bold;">Next FX</div><span style="Xwhite-space: nowrap;">1&nbsp;Nickel&nbsp;Way</span><br />Columbus, MT 59019</td> </tr></table>';
	
	if(GBrowserIsCompatible()) {
		var point = new GLatLng(45.68442, -109.27045);
		if(point) {
			var map = new GMap2(document.getElementById('gmap'));

			map.setMapType(G_SATELLITE_MAP);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.enableScrollWheelZoom();
			map.setCenter(new GLatLng(45.68933, -109.26805), 14);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(markerHTML);});
			marker.openInfoWindowHtml(markerHTML);
		}
	}
}

document.aniMiniCart = function() {
	var el = $('cartLink');
	
	if(el) {
		if(!el.ani) {
			el.ani = new Fx.Tween(el.id, {duration: 2500, property: 'color', link: 'chain'});
		}
		
		el.ani.start('#d90000', '#ffffff');
	}
}

document.loginError = function(markUsername, markPassword, errorMessage) {
	var usernameLabelEl = document.getElementById('loginFormUsernameLabel');
	var passwordLabelEl = document.getElementById('loginFormPasswordLabel');
	var errorMessageEl = document.getElementById('loginFormErrorMessage');
	
	if(usernameLabelEl && passwordLabelEl && errorMessageEl) {
		if(markUsername) {
			usernameLabelEl.className = 'error';
		} else {
			usernameLabelEl.className = '';
		}
		if(markPassword) {
			passwordLabelEl.className = 'error';
		} else {
			passwordLabelEl.className = '';
		}
		errorMessageEl.innerHTML = errorMessage;
	}
}

document.loginFinish = function(redirect) {
	//jt.hideSlip('detail');
	if(redirect) {
		eval(redirect);
	} else {
		jt.ajax.processCurrentState();
	}
}

document.updateQuantity = function(who, id, price) {
	var priceEl = document.getElementById('jt_product_price_'+id);
	
	if(priceEl && who) {
		who.value = who.value.replace(/[^\d]/g, '');
		
		if(who.value == '') {
			priceEl.innerHTML = '-';
		} else {
			priceEl.innerHTML = '$'+number_format(who.value*price, 2, '.', ',');
		}
	}
}

document.setVideoTab = function(on) {
	var videoTab = $('videoTab'), imageTab = $('imageTab'), videoContainer = $('detailVideoContainer'), imageContainer = $('detailImageContainer');
	if(videoTab && imageTab && videoContainer && imageContainer) {
		if(on) {
			videoTab.select('mVideo', true);
			imageTab.select('mImage', false);
			imageContainer.hide();
			videoContainer.show();
		} else {
			videoTab.select('mVideo', false);
			imageTab.select('mImage', true);
			videoContainer.hide();
			imageContainer.show();
		}
	}
}

/* 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];
}
