/**
 * @author chrisjones@twofour.co.uk
 */
if(!window.Twofour)
	window.Twofour = {};
if(!Twofour.Xaml)
	Twofour.Xaml = {};

Twofour.Xaml.XamlFactory = function(version){
	this.version = !version ? Twofour.Xaml.XamlFactory.supportedVersions.v1_0RC : version;
}

Twofour.Xaml.XamlFactory.supportedVersions = {
	v1_0RC : '1.0RC',
	v1_1Alpha: '1.1Alpha'
};

Twofour.Xaml.XamlFactory.prototype = {
	typeName : 'Twofour.Xaml.XamlFactory',
	
	createXamlElement : function(tagName, xName, attributes, content, includeNamespace) {
		var xAttributes = [];
		var xElements = [];
		attributes = attributes || {};
		if(includeNamespace){
			attributes['xmlns']="http://schemas.microsoft.com/client/2007";
			attributes['xmlns:x'] ="http://schemas.microsoft.com/winfx/2006/xaml";
		}
		var accept = {'string':true,'number':true,'boolean':true};
		for(var propName in attributes){
			if(accept[typeof attributes[propName]]===true)
				xAttributes.push(new Twofour.Xaml.XamlAttribute(propName,attributes[propName]))
		} 
		if(content){
			if(content.length && content.length > 0)
				xElements=content;	
			else
				xElements.push(content);
		}
		return new Twofour.Xaml.XamlElement(tagName,xName,xAttributes,xElements);
	},

	createCanvas : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('Canvas',name,attributes,content,includeNamespace);
	},
	
	createImage : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('Image',name,attributes,content,includeNamespace);
	},
	
	createTextBlock : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('TextBlock',name,attributes,content,includeNamespace);	
	},
	
	createLine : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('Line',name,attributes,content,includeNamespace);	
	},
	
	createMediaElement : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('MediaElement',name,attributes,content,includeNamespace);	
	},
	
	createMediaAttribute : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('MediaAttribute',name,attributes,content,includeNamespace);	
	},
	
	createPath : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('Path',name,attributes,content,includeNamespace);	
	},
	
	createStoryboard : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('Storyboard',name,attributes,content,includeNamespace);	
	},
	
	createDoubleAnimation : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('DoubleAnimation',name,attributes,content,includeNamespace);	
	},
	
	createEllipseGeometry : function(name, attributes, content, includeNamespace){
		return this.createXamlElement('EllipseGeometry',name,attributes,content,includeNamespace);	
	},
	
	createGradientStop: function(colour, offset){
		return this.createXamlElement('GradientStop',{Color:colour,Offset:offset});
	},
	
	createLinearGradientBrush: function(startPoint, endPoint, contents){
		return this.createXamlElement('LinearGradientBrush',{StartPoint:startPoint,EndPoint:endPoint},contents);
	}
}

Twofour.Xaml.XamlColour = function(alpha,red,green,blue){
	this.alpha = alpha || 'ff';
	this.red = red || '00';
	this.green = green || '00';
	this.blue = blue || '00';
}
Twofour.Xaml.XamlColour.prototype = {
	typeName: 'Twofour.Xaml.XamlColour',
	
	toString: function (){
		return '#'+this.alpha+this.red+this.green+this.blue;
	}
}

Twofour.Xaml.XamlAttribute = function(name, value){
	this.name = name;
	this.value = value;
}
Twofour.Xaml.XamlAttribute.prototype = {
	typeName : 'Twofour.XamlFactory.XamlAttribute',
	
	toString : function(){
		return  ' '+this.name + '="'+this.value+'"';
	}
}

Twofour.Xaml.XamlElement = function(tagname, name, attributes, content){
	this.tagname = tagname;
	this.attributes = attributes || [];
	if(name)
		this.attributes.push(new Twofour.Xaml.XamlAttribute('x:Name',name)); 
	this.content = content || [];
} 
Twofour.Xaml.XamlElement.prototype = {
	typeName : 'Twofour.XamlFactory.XamlElement',
	
	hasContent : function (){
		return this.content.length >=1;
	},
	
	toString : function(){
		var xaml = null;
		var serialise = function(iterable){
			var out = '';
			if(iterable)
				for(var i=iterable.length-1;i>-1;i--)
					out += iterable[i].toString();
			return out;
		};
		xaml = '<'+this.tagname+serialise(this.attributes);
		if(this.hasContent())
			xaml+= '>'+serialise(this.content)+'</'+this.tagname+'>';
		else
			xaml += '/>'; 
		return xaml;
	}
}

