//=======================================================================
// 
// API : api.js
// Copyright (c) 2005-2008 Indie Labs, LLC. All rights reserved.
// 
//=======================================================================

var API = {
  
	REQUIRED_PROTOTYPE: '1.6.0',
  	
	initialize: function() {
		if((typeof Prototype == 'undefined') || (typeof Element == 'undefined') || (typeof Element.Methods=='undefined') || (Format.version(Prototype.Version) < Format.version(API.REQUIRED_PROTOTYPE)))
			this.onError('Prototype ' + API.REQUIRED_PROTOTYPE + '+ is required!');
	},	
	
	send: function(action, params, callback, options) {
		var options 		   = options || {};
			options.parameters = params;
			options.onSuccess  = function(result) {
				try {  
					var result = this.parse(result);
					!result.errors ? callback(result) : this.onError(result.errors);
				} catch(error) { 
					this.onError(error + '\n\n' + result.responseText); 
				}
			}.bind(this);
		new Ajax.Request('/' + action + '.js', options);
	},
	
	register: function(options) {
		Ajax.Responders.register(options);
	},
	
	parse: function(result) {
		return $(eval('(' + result.responseText + ')'));
	},
	
	onError: function(error) {
		if(error instanceof Array) error = error.join(', ');
		try { 
			Maintenance.addError('JavaScript API Error: ' + error); 
		} catch(e) {
			alert('Error: ' + error);
		}
	}

}

var Cart = {
	
	refresh: function(callback, options) {
		API.send('cart', {}, callback, options);
	},
	
	updateFromForm: function(formID, callback, options) {
		API.send('cart', Form.serialize(formID), callback, options);
	},
				
	addItem: function(optionID, quantity, callback, options) {
		var params = {};
			params['cart[add][id]'] = optionID;
			params['cart[add][quantity]'] = quantity || 1;
		API.send('cart', params, callback, options);
	},
	
	updateItem: function(id, quantity, callback, options) {
	    var params = {};
			params['cart[update][' + id + ']'] = quantity;
		API.send('cart', params, callback, options);
	},
	
	removeItem: function(id, callback, options) {
		this.updateItem(id, 0, callback, options);
	}
		
}

var Product = {
				
	find: function(permalink, callback, options) {
		var params = { method: 'get' };
		API.send('product/' + permalink, params, callback, options);
	},
	
	findAll: function(params, callback, options) {
		params = params || {};
		params.method = 'get';
		API.send('products', params, callback, options);
	},
	
	search: function(term, params, callback, options) {
		params = params || {};
		params.search = term;
		this.findAll(params, callback, options);
	},
	
	findImage: function(url, size) {
		if(!size) return url;
		var ext = url.match(/\.(\w{2,4}$)/)[1];
		var dir = url.substr(0, url.lastIndexOf('/') + 1);
		switch(size.toLowerCase()) {
			case 'large':
				return dir + '300.' + ext;
			case 'medium':
				return dir + '175.' + ext;
			case 'thumb':
				return dir + '75.' + ext;
			default:
				return url;
		}
	}
	
}

var Format = {
	
	version: function(versionString) {
		var a = versionString.split('.');
		var v = a.shift() + '.' + a.join('');
    	return parseFloat(v);
    },
	
	money: function(n, withCommas) {
		n  = parseFloat(n);
		n -= 0;
    	n  = (Math.round(n*100))/100;
		n  = (n == Math.floor(n)) ? n + '.00' : ((n*10 == Math.floor(n*10)) ? n + '0' : n);
		return withCommas ? this.commas(n) : n;	
	},
	
	commas: function(n) {
		var x   = n.toString().split('.');
		var x1  = x[0];
		var x2  = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while(rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	},
		
	pluralize: function(count, singular, plural) {
		plural = plural || singular + 's';
		return count + ' ' + (count == 1 ? singular : plural);
	}

}

API.initialize();