// THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT. 
// Copyright (c) 2007 Renderspace. All rights reserved.


/************************************************/
// xaml functions
/************************************************/


var IMGSTATE_INVISIBLE = -1;
var IMGSTATE_NORMAL = 0;
var IMGSTATE_DISABLED = 1;
var IMGSTATE_HOVER = 2;
var IMGSTATE_PRESSED = 3;


var VisualElementGarbageCollector = new Array();


function visualElementsGargabeCleanup() {
    // delete objects marked as to-be-deleted
	while (VisualElementGarbageCollector.length > 0) {
		var elemData = VisualElementGarbageCollector.pop();
		if (elemData != null) {
//		    if (elemData.parent != null) elemData.parent.Children.Remove(elemData.elem);
//	        delete elemData.elem;
		}
	}
}


function createVisualElement(parent, type, name) {
	if ((rootControl == null) || (parent == null) || (type == null)) return null;
	var elem = null;
	try {
	    var xaml = '<' + type;
	    if (name != null) xaml += ' Name="' + name + '"';
	    xaml += ' />';
		elem = rootControl.content.createFromXaml(xaml);
	}
	catch (e) {}
	if (elem == null) return null;
	
	if (parent.Children == undefined) parent.SetValue("Children", elem);
	else parent.Children.Add(elem);
	return elem;
}


function createVisualElementFromXaml(parent, xaml, name) {
	if ((rootControl == null) || (parent == null) || (xaml == null)) return null;
	var elem = null;
	try {
		elem = rootControl.content.createFromXaml(xaml);
	}
	catch (e) {}
	if (elem == null) return null;
	
	if (parent.Children == undefined) parent.SetValue("Children", elem);
	else parent.Children.Add(elem);
//	if (name != null) elem.SetValue("Name", name);
	return elem;
}


function removeVisualElement(parent, elem) {
	if (elem == null) return null;
	var elemType = elem.toString();
	if (elemType == "TextBlock") elem.SetValue("Text", '');
	else if (elemType == "Image") elem.SetValue("Source", '');
    else if (elemType == "Polygon") elem.SetValue("Data", '');
    elem.SetValue("Opacity", 0);
    
    VisualElementGarbageCollector.push({parent: parent, elem: elem});
    
//	if (parent != null) parent.Children.Remove(elem);
//	delete elem;
	return null;
}


function removeVisualElementByName(parent, name) {
	if (rootCanvas == null) return null;
	var elem = rootCanvas.FindName(name);
	removeVisualElement(parent, elem);
	return null;
}


function createVisualImage(parent, name, imageFile, x, y, width, height, opacity) {
	var img = createVisualElement(parent, "Image", name);
	if (img == null) return null;
	img.SetValue("Stretch", "Fill");
	if (x != null) img.SetValue("Canvas.Left", x);
	if (y != null) img.SetValue("Canvas.Top", y);
	if (width != null) img.SetValue("Width", width);
	if (height != null) img.SetValue("Height", height);
	if (opacity != null) img.SetValue("Opacity", opacity);
	if (imageFile != null) img.SetValue("Source", imageFile);
	return img;
}


function createVisualRectangle(parent, fill) {
	var rect = createVisualElement(parent, "Rectangle", null);
	if (rect == null) return null;
	if (fill != null) rect.SetValue("Fill", fill);
	return rect;
}


function createVisualLine(parent, fill, thickness) {
	var line = createVisualElementFromXaml(parent, '<Line Stroke="' + fill + '" StrokeThickness="' + thickness + '" Opacity="0" />');
	return line;
}


function createClippedCanvas(parent) {
	return createVisualElementFromXaml(parent, '<Canvas><Canvas.Clip><RectangleGeometry /></Canvas.Clip></Canvas>');
}


function createTextElement(parent, size, color, text) {
	return createVisualElementFromXaml(parent, 
		'<TextBlock FontSize="' + size + '" Foreground="' + color + '"'
	  + ((text != null) ? ' Text="' + escapeXmlText(text) + '"' : '')
	  + '>' 
	  + '</TextBlock>');
}


function createClippedText(parent, size, color, text) {
	return createVisualElementFromXaml(parent, 
		'<TextBlock FontSize="' + size + '" Foreground="' + color + '"'
	  + ((text != null) ? ' Text="' + escapeXmlText(text) + '"' : '')
	  + '>' 
	  + '<TextBlock.Clip><RectangleGeometry /></TextBlock.Clip>'
	  + '</TextBlock>');
}
	

function createGradientRectangle(parent, name, lineColor, topColor, bottomColor) {
	return createVisualElementFromXaml(parent, 
	  '<Rectangle Stroke="' + lineColor + '" StrokeThickness="1">'+
		'<Rectangle.Fill>' +
		  '<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">' +
			'<LinearGradientBrush.GradientStops>' +
			  '<GradientStop Offset="0" Color="' + topColor + '" />' +
			  '<GradientStop Offset="1" Color="' + bottomColor + '" />' +
			'</LinearGradientBrush.GradientStops>' +
		  '</LinearGradientBrush>' +
		'</Rectangle.Fill>' +
	  '</Rectangle>', name);
}
	

function createSelectionElement(parent, name, roundnessX, roundnessY, topColor, bottomColor, lineColor) {
	if (roundnessX == null) roundnessX = 0.03;
	if (roundnessY == null) roundnessY = 0.03;
	
	var c1x = Math.floor(roundnessX * 1000) / 1000;
	var c2x = Math.floor(roundnessX * 1000 / 3) / 1000;
	var c3x = 1 - c1x;
	var c4x = 1 - c2x;
	var c1y = Math.floor(roundnessY * 1000) / 1000;
	var c2y = Math.floor(roundnessY * 1000 / 3) / 1000;
	var c3y = 1 - c1y;
	var c4y = 1 - c2y;
	var data = 'F1 ' 
	  + 'M 0.00,' + c1y + ' ' 
	  + 'L ' + c2x + ',' + c2y +' ' 
	  + 'L ' + c1x + ',0.00 ' 
	  + 'L ' + c3x + ',0.00 ' 
	  + 'L ' + c4x + ',' + c2y + ' ' 
	  + 'L 1.00,' + c1y + ' ' 
	  + 'L 1.00,' + c3y + ' ' 
	  + 'L ' + c4x + ',' + c4y + ' '
	  + 'L ' + c3x + ',1.00 '
	  + 'L ' + c1x + ',1.00 '
	  + 'L ' + c2x + ',' + c4y + ' '
	  + 'L 0.00,' + c3y + ' '
	  + 'L 0.00,' + c1y + ' '
	  + 'Z';
	  
	return createVisualElementFromXaml(parent, 
	   '<Path Opacity="0" Stroke="' + lineColor + '" StrokeThickness="1" Stretch="Fill" '
	   + 'Data="' + data + '">'
	   + '<Path.Fill>'
		 + '<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">'
		   + '<LinearGradientBrush.GradientStops>'
			 + '<GradientStop Offset="0.0" Color="' + topColor + '" />'
			 + '<GradientStop Offset="1.0" Color="' + bottomColor + '" />'
		   + '</LinearGradientBrush.GradientStops>'
		 + '</LinearGradientBrush>'
		+ '</Path.Fill>'
	  + '</Path>', name);
}


function setVisualElementSource(elem, source) {
	if (elem == null) return;
	elem.SetValue("Source", source ? source : '');
}


function setVisualElementText(elem, text) {
	if (elem == null) return;
	if (text == null) text = '';
	else if (typeof(text) != "string") text = text.toString();
	elem.SetValue("Text", text);
}


function setVisualElementPos(elem, x, y, width, height, opacity) {
	if (elem == null) return;
	if (x != null) elem.SetValue("Canvas.Left", x);
	if (y != null) elem.SetValue("Canvas.Top", y);
	if (width != null) elem.SetValue("Width", width);
	if (height != null) elem.SetValue("Height", height);
	if (opacity != null) elem.SetValue("Opacity", opacity);
}


function getVisualElementPosX(elem) {
	if (elem == null) return 0;
	return elem.GetValue("Canvas.Left");
}


function getVisualElementPosY(elem) {
	if (elem == null) return 0;
	return elem.GetValue("Canvas.Top");
}


function setLineElementPos(elem, x1, y1, x2, y2, opacity) {
	if (elem == null) return;
	if (x1 != null) elem.SetValue("X1", x1);
	if (y1 != null) elem.SetValue("Y1", y1);
	if (x2 != null) elem.SetValue("X2", x2);
	if (y2 != null) elem.SetValue("Y2", y2);
	if (opacity != null) elem.SetValue("Opacity", opacity);
}


function setVisualElementData(elem, data) {
	if (elem == null) return;
	elem.SetValue("Data", data ? data : '');
}


function getVisualElementClip(elem) {
	if (elem == null) return null;
	return elem.GetValue("Clip");
}


function setVisualElementClipRect(clipElem, x1, y1, x2, y2) {
	if (clipElem == null) return;
//    if (x2 < x1) x2 = x1;
//    if (y2 < y1) y2 = y1;
    x2 -= x1;
    y2 -= y1;
	clipElem.SetValue("Rect", Math.floor(x1) + "," + Math.floor(y1) + "," + Math.floor(x2) + "," + Math.floor(y2));
}


function setVisualElementScale(elem, scaleX, scaleY) {
	if (elem == null) return;
	elem.SetValue("ScaleX", scaleX);
	elem.SetValue("ScaleY", scaleY);
}


function setVisualElementAngle(elem, angle) {
	if (elem == null) return;
	elem.SetValue("Angle", angle);
}


function setVisualElementFillColor(elem, color) {
	if (elem == null) return;
	elem.SetValue("Fill", color ? color : '');
}


function setVisualElementForegroundColor(elem, color) {
	if (elem == null) return;
	elem.SetValue("Foreground", color ? color : '');
}


function setVisualElementStrokeColor(elem, strokeColor) {
	if (elem == null) return;
	elem.SetValue("Stroke", strokeColor ? strokeColor : '');
}


function bringVisualElementToFront(parent, elem) {
	if (elem == null) return;
	if (parent == null) parent = rootCanvas;
	parent.Children.Remove(elem);
	parent.Children.Add(elem);
}


function createStateImages(parent, name, x, y, width, height, opacity, fileNormal, fileDisabled, fileHover, filePressed) {
	return new Array(
		(fileNormal != null) ? createVisualImage(parent, null, "/" + IMAGE_DIR + fileNormal, x, y, width, height, opacity) : null, // normal
		(fileDisabled != null) ? createVisualImage(parent, null, "/" + IMAGE_DIR + fileDisabled, x, y, width, height, opacity) : null, // disabled
		(fileHover != null) ? createVisualImage(parent, null, "/" + IMAGE_DIR + fileHover, x, y, width, height, opacity) : null, // hover
		(filePressed != null) ? createVisualImage(parent, null, "/" + IMAGE_DIR + filePressed, x, y, width, height, opacity) : null // pressed
	);
}


function deleteStateImages(parent, imgList) {
	if (imgList == null) return;
	for (var i = 0; i < imgList.length; i++) {
		imgList[i] = removeVisualElement(parent, imgList[i]);
	}
}


function setStateImagePos(imgList, x, y, width, height, opacity, currentState) {
	if (imgList == null) return;
	setVisualElementPos(imgList[IMGSTATE_NORMAL], x, y, width, height, (opacity != null) ? ((currentState == IMGSTATE_NORMAL) ? opacity : 0) : null);
	setVisualElementPos(imgList[IMGSTATE_DISABLED], x, y, width, height, (opacity != null) ? ((currentState == IMGSTATE_DISABLED) ? opacity : 0) : null);
	setVisualElementPos(imgList[IMGSTATE_HOVER], x, y, width, height, (opacity != null) ? ((currentState == IMGSTATE_HOVER) ? opacity : 0) : null);
	setVisualElementPos(imgList[IMGSTATE_PRESSED], x, y, width, height, (opacity != null) ? ((currentState == IMGSTATE_PRESSED) ? opacity : 0) : null);
}


function changeStateImage(imgList, state, defaultOpacity) {
	if (imgList == null) return;
	if ((state >= 0) && (state < 4)) {
		if (imgList[state] == null) state = IMGSTATE_NORMAL;
	}
	for (var i = 0; i < 4; i++) {
		if (imgList[i] != null) imgList[i].SetValue("Opacity", (i != state) ? 0 : defaultOpacity);
	}
}


function bringStateImagesToFront(parent, imgList) {
	if (imgList == null) return;
	bringVisualElementToFront(parent, imgList[IMGSTATE_NORMAL]);
	bringVisualElementToFront(parent, imgList[IMGSTATE_DISABLED]);
	bringVisualElementToFront(parent, imgList[IMGSTATE_HOVER]);
	bringVisualElementToFront(parent, imgList[IMGSTATE_PRESSED]);
}
