/**
 * Pie-Class
 * 
 */
var Pie = Class.create(OFCData, {
	/* supported subtypes */
	/*public static*/ supported: [
		null
	],
	
	/**
	 * @constructor
	 * @class Pie
	 * @extends OFCData
	 * @param {String} sub subtype
	 */
	initialize: function($super, sub) {
		if (typeof sub == 'undefined') sub = null;
		$super('pie', sub);
		
		// pie style specific properties
		// alpha-value
		this.alpha = null;
		// pie line color
		
		this.colors.update({
			line: '#000000',
			labels: '#000000',
			data: OFC.defaultColors
		});
		
		this.labelstyle = '';
		this.gradientFill = null;
		// border size
		this.borderSize = null;
		// links
		this.links = [];
		this.labels = [];
	},
	
	/**
	 * Converts another OFCData-object to this one
	 * 
	 * @see OFCData#convertFrom
	 * @param {OFCData} chart Object to copy from
	 */
	convertFrom: function($super, /* OFCData */ chart) {
		var colorlist = [chart.getColors().values(), OFC.defaultColors].flatten();
		
		this.colors.set('data', colorlist);
		
		this.setKey(chart.key, chart.keySize);
		
		this.values = chart.values;
	},
	
	/**
	 * @see OFCData#toData
	 */
	toData: function() {
		var values = [];
		if (this.alpha != null) values[0] = this.alpha;
		if (typeof values[0] == 'undefined') values[0] = 60;
		if (this.colors.get('line') != null) values[1] = this.colors.get('line');
		if (typeof values[1] == 'undefined') values[1] = '#000000'; // line better gets black
		
		var style = this.labelstyle;
		var colorGrep = /color:\s*([0-9#xabcdef]+)/gi;
		style = style.replace(colorGrep, 'color:'+this.colors.get('labels'));
		
		if (this.labelstyle != '') values[2] = style;
		if (typeof values[2] == 'undefined') values[2] = 'font-size:16px; color: #000000;'; // labels have no style
		if (this.gradientFill != null) {
			if (this.gradientFill != null) values[3] = this.gradientFill ? 'true' : 'false';
		} else {
			values[3] = 'false';
		}
		if (this.borderSize != null) values[4] = this.borderSize;
		if (typeof values[4] == 'undefined') values[4] = 1;
		
		if (this.ofc != null) {
			this.ofc.set('colours', this.colors.get('data').join(','));
			this.ofc.set('pie_labels', this.labels.join(','));
		}
		
		return values;
	},
	
	/**
	 * @see OFCData#fromData
	 */
	fromData: function(info) {
		this.alpha = info.shift();
		
		if (info.length > 0)
		this.colors.set('line', '#'+info.shift().substr(-6));
		
		if (info.length > 0) {
			this.labelstyle = info.shift();
			var colorGrep = /color:\s*([0-9#xabcdef]+)/gi;
			colorGrep.exec(this.labelstyle);
			this.colors.set('labels', '#'+RegExp.$1.substr(-6));
		}
		
		if (info.length > 0) {
			var gradient = info.shift();
			if (gradient == 'true') {
				this.gradientFill = true;
			} else if (gradient == 'false') {
				this.gradientFill = false;
			}
		}
		
		if (info.length > 0) {
			this.borderSize = info.shift() * 1;
		}
		
		if (this.ofc != null) {
			this.colors.set('data', this.ofc.get('colours'));
			this.labels = this.ofc.get('pie_labels');
		}
	},
	
	/**
	 * @see OFCData#getRequiredColors
	 */
	getRequiredColors: function() {
		return new Hash({
			line:	'Outline'
		,	labels:	'Labels'
		,	data:	[]
		});
	},
	
	/**
	 * Sets all colors - use this function only in connection with
	 * getColors or getRequiredColors
	 * 
	 * Changes this objects colors
	 * @param {Hash} colors
	 */
	changeColors: function(colors) {
		this.colors.set('line', colors.get('line'));
		this.colors.set('labels', colors.get('labels'));
	},
	
	/**
	 * @see OFCData#getFullName
	 */
	getFullName: function() {
		
		var name = 'pie';
		
		if (this.subType != null) {
			name += '_'+this.subType;
		}
		
		if (this.number != 0) {
			name += '_'+this.number;
		}
		
		return name;
	},
	
	/**
	 * @see OFCData#addToOFC
	 */
	addToOFC: function($super, /*OFC*/ ofc) {
		// we have to kill all other data-elements, before calling super
		// if we are not the only one
		if (ofc.dataList.length > 1) {
			ofc.emptyDataList([this]); // all except this
		}
		$super(ofc);
		
		// convert labels
		if (ofc.get('x_labels') != null && ofc.get('x_labels').length != 0) {
			ofc.set('pie_labels', ofc.get('x_labels'));
			ofc.set('x_labels', null);
		}
		
		if (this.colors.get('data').length == 0) {
			this.colors.set('data', OFC.defaultColors);
		}
		ofc.set('colours', this.colors.get('data'));
		
		if (this.links.length > 0) {
			ofc.set('pie_links', this.links);
		}
	},
	
	/**
	 * @see OFCData#addToOFC
	 */
	removeFromOFC: function($super, /*OFC*/ ofc) {
		$super(ofc);
		ofc.set('x_labels', ofc.get('pie_labels'));
		ofc.set('pie_labels', null);
		ofc.set('colours', null);
		ofc.set('pie_links', null);
	}
});
