/*
 * Copyright (C) 2009 Experiencias Ocentu. All rights reserved.
 *
 * Proprietary code. No modification, distribution or reproduction without
 * written permission.
 * 
 * @author: jesus.rodriguez
 * $Id$
 */

(function($) {

var _order_form = '';
var _baseurl = 'http://api.disfrutalia.com/';

$.widget("ui.orderable", {

	_init: function() {
		var self = this.element;

		// Customizable message
		this.message = $('<div>')
			.attr('class', 'message')
			.html('Has añadido <span></span> a tu cesta:');
		// Customizable image
		this.image = $('<div>')
			.attr('class', 'image')
			.html('<img src""/>');
		
		// Bind click in the element
		self.bind('click.orderable', function() { self.orderable('startorder'); });
		
		// Processing layer
		this.processing = $('<div>')
			.attr({'class':'processing'})
			.html('Procesando ..');
	},

	startorder: function() {
		var self = this.element;
		
		this.orderdialog = $('<div>');
		this.orderdialog
			.attr('id', 'popup_window')
			.dialog({
				autoOpen:false,
				modal:true,
				bgiframe: true,
				width: 550,
				resizable: false,
				buttons: {
					'Finalizar compra': function() { self.orderable('purchase'); },
					'Seguir navegando': function() {self.orderable('close'); }
				}})
			//.html('<div id="popup_window">'+this.processing.text()+'</div>')
			.dialog('open');
		
		var popup = $('<div>').attr('id','popup_window');
		this.processing.appendTo(popup);
		popup.appendTo(this.orderdialog);
		
		//var image = this.image;
		var message = this.message;
		var url = _baseurl + 'order/set/id/' + this.options.value + '/?callback=?';
		$.getJSON(url, function(data) {
			if(data.status == 1){
				message.html('Has añadido <span>"' + data.name + '"</span> a tu cesta');
				self.orderable('confirmorder');
			} else {
				// @TODO!!
				back.html('Error, por favor, vuelva a intentarlo');
			}
		});
	},
	
	confirmorder: function() {
		//_order_form = this.message.text();
		//_order_form += this.image.text();
		_order_form = $('<div>').attr('class','submessage').html('¿Qué deseas hacer ahora?');

        this.orderdialog.html('');
        this.orderdialog.append(this.message);
		this.orderdialog.append(_order_form);
		
		/**
		 * @TODO: Get cart data from this response and update content
		 * instead orders. Use _setData();
		 */
		$('#user_menu').userbox('updateorders');
	},
	
	purchase: function() {
		this.orderdialog.dialog('close');
		window.location = 'http://www.disfrutalia.com/purchase/order/';
	},
	
	close: function() {
		this.orderdialog.dialog('close');
	},
	
	destroy: function() {
		this.element.unbind('.orderable');
		$.widget.prototype.destroy.apply(this, arguments);
	},

	value: function(newValue) {
		if (newValue === undefined) {
			return this._value();
		}
		
		this._setData('value', newValue);
		return this;
	},

	_setData: function(key, value) {

		switch (key) {
			case 'value':
				this.options.value = value;
				break;
		}

		$.widget.prototype._setData.apply(this, arguments);

	},

	_value: function() {

		var val = this.options.value;
		return val;

	}
});

$.extend($.ui.orderable, {
	version: "1.7.2",
	defaults: {
		value: 0
	}
});

})(jQuery);



/*
 * Cancel Order Plugin
 */
(function($) {

	var _order_form = '';
	var _baseurl = 'http://api.disfrutalia.com/';

	$.widget("ui.cancelorder", {

		_init: function() {
			var self = this.element;
			
			// Bind click in the element
			self.bind('click.unorderable', function() { self.cancelorder('cancelorder'); });
		},

		cancelorder: function() {
			var self = this.element;
			var url = _baseurl + 'order/delete/id/' + this.options.value + '/?callback=?';
			$.getJSON(url, function(data) {
				if(data.status == 1){
					// @TODO
					$('#user_menu').userbox('updateorders');
				} else {
					// @TODO!!
				}
			});
		},
		
		destroy: function() {
			this.element.unbind('.unorderable');
			$.widget.prototype.destroy.apply(this, arguments);
		},

		value: function(newValue) {
			if (newValue === undefined) {
				return this._value();
			}
			
			this._setData('value', newValue);
			return this;
		},

		_setData: function(key, value) {

			switch (key) {
				case 'value':
					this.options.value = value;
					break;
			}

			$.widget.prototype._setData.apply(this, arguments);

		},

		_value: function() {

			var val = this.options.value;
			return val;

		}
	});

	$.extend($.ui.cancelorder, {
		version: "1.7.2",
		defaults: {
			value: 0
		}
	});

})(jQuery);
