/**
 * $Id$
 * 
 * This file contains the javascript client code for handling cart.
 *
 * @category	Frontend
 * @author		Jari Partti <jari.partti@really.fi>
 * @copyright	Copyright (c) 2009 Really Helsinki Oy
 * @version		$Revision$
 */

function ReallyCartService() {
};

ReallyCartService.prototype = new ReallyService({
	methods:[
		{"name":"getRows","serviceURL":"\/jsonrpc\/cart\/provider","parameters":[{"name":"cartID","type":"string"}]},
		{"name":"clearRows","serviceURL":"\/jsonrpc\/cart\/provider","parameters":[{"name":"cartID","type":"string"}]},
		{"name":"addRow","serviceURL":"\/jsonrpc\/cart\/provider","parameters":[{"name":"cartID","type":"string"},{"name":"item","type":"any"},{"name":"data","type":"array"}]},
		{"name":"removeRow","serviceURL":"\/jsonrpc\/cart\/provider","parameters":[{"name":"cartID","type":"string"},{"name":"item","type":"any"},{"name":"data","type":"array"}]},
		{"name":"addItem","serviceURL":"\/jsonrpc\/cart\/provider","parameters":[{"name":"cartID","type":"string"},{"name":"item","type":"any"}]},
		{"name":"removeItem","serviceURL":"\/jsonrpc\/cart\/provider","parameters":[{"name":"cartID","type":"string"},{"name":"item","type":"any"}]}
	]
});

function ReallyCart(cartID, options) {
	this._cartID = cartID;
	if(options) {
		/*
		this._addUrl = options.addUrl;
		this._removeUrl = options.removeUrl;
		this._clearUrl = options.clearUrl;
		this._loadUrl = options.loadUrl;
		*/
		// this._element = options.element ? options.elementId : this._cartID;
		
		if(options.actionUrl) {
			this._actionUrl = options.actionUrl;
		}
		
		// if(options.element || options.element === null) {
		if(options.element) {
			this._element = ReallyHtml.byId(options.element);
			/*
			if(typeof options.element == 'string') {
				this._element = document.getElementById(options.element);
			}
			else {
				this._element = options.element;
			}
			*/
		}
		else {
			this._element = ReallyHtml.byId(this._cartID);
			// this._element = document.getElementById(this._cartID);
		}
	}
	
	this._setContent(null);

	ReallyCart._instances[cartID] = this; 
};

ReallyCart.prototype = { };

ReallyCart.prototype.getElement = function() {
	return(this._element);
};

ReallyCart.prototype._setContent = function(content) {
	var e = this.getElement();
	if(!e) {
		return;
	}

	if(content !== null)
		e.innerHTML = content;
		
	/*
	var len = e.childNodes.length;
	for(var i = 0; i < len; i++) {
		if(!ReallyHtml.hasClass(e.childNodes[i], 'cartRow')) {
			console.log("Skipping node: ", e.childNodes[i]);
			continue;
		}
		
		console.log("Processing node: ", e.childNodes[i]);
	}
	*/
};

ReallyCart.prototype.getRows = function(callback) {
	ReallyCart._service.getRows(this._cartID, callback);
};

ReallyCart.prototype.clearRows = function() {
	var e = this.getElement();
	if(e && this._actionUrl && this._actionUrl.length) {
		// If element is found and clear URL is set, then use XMLHttpRequest to
		// clear the cart.
		ReallyXhr.get({
			url: this._actionUrl,
			content: {
				method: 'clear',
				item: item,
				data: data
			},
			load: ReallyEvent.callback(this, '_setContent')
			/*
			load: function(content) {
				e.innerHTML = content;
			}
			*/
		});
	}
	else {
		var self = this;
		ReallyCart._service.clearRows(this._cartID, function(result) {
			if(e) {
				e.innerHTML = '';
			}
		});
	}
};

ReallyCart.prototype.addRow = function(item, data) {
	if(!data)
		data = null;

	var e = this.getElement();
	if(e && this._actionUrl && this._actionUrl.length) {
		// If element is found and add URL is set, then use XMLHttpRequest to
		// add the item and to fetch the new content of cart.
		ReallyXhr.get({
			url: this._actionUrl,
			content: {
				method: 'add',
				item: item,
				data: data
			},
			/*
			load: function(content) {
				e.innerHTML = content;
			}
			*/
			load: ReallyEvent.callback(this, '_setContent')
		});
	}
	else {
		// If element is not found or add URL is not set, then use JSON-RPC
		// to call the server side business logic and use reload() method to
		// refresh cart content.
		var self = this;
		ReallyCart._service.addRow(this._cartID, item, data, function(result) {
			self.reload();
		});
	}
};

ReallyCart.prototype.removeRow = function(item, data) {
	if(!data)
		data = null;
	
	var e = this.getElement();
	if(e && this._actionUrl && this._actionUrl.length) {
		// If element is found and remvoe URL is set, then use XMLHttpRequest to
		// remove the item and to fetch the new content of cart.
		ReallyXhr.get({
			url: this._actionUrl,
			content: {
				method: 'remove',
				item: item,
				data: data
			},
			load: ReallyEvent.callback(this, '_setContent')
		});
	}
	else {
		// If element is not found or remove URL is not set, then use JSON-RPC
		// to call the server side business logic and use reload() method to
		// refresh cart content.
		var self = this;
		ReallyCart._service.removeRow(this._cartID, item, data, function(result) {
			self.reload();
		});
	}

};

ReallyCart.prototype.reload = function() {
	if(!this._actionUrl || !this._actionUrl.length) {
		return;
	}
	
	var e = this.getElement();
	if(!e) {
		return;
	}
	
	ReallyXhr.get({
		url: this._actionUrl,
		content: {
			method: 'load'
		},
		load: ReallyEvent.callback(this, '_setContent')
	});
};

ReallyCart._service = new ReallyCartService();

ReallyCart._instances = { };

ReallyCart.getCart = function(cartID) {
	if(!ReallyCart._instances[cartID]) {
		new ReallyCart(cartID);
	}
	
	return(ReallyCart._instances[cartID]);
};

