
var _objNum = 0;		//xNameを被らないようにするための変数。作るごとに＋１される。

/* xName生成関数 */

function setxName(type)
{
	_objNum++;
	return type + '_' + _objNum;
}

//Canvas Class

function Canvas(sender)
{
	this.sender = sender;
	this.xName = setxName("canvas");
	
	var Flagment = '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '"/>';
	    
	CreateXaml(this.sender, Flagment);
	
	this.Position = new Position(this.sender.FindName(this.xName));
}


//Mediaクラス仮
function Media(sender, other)
{

	this.sender = sender;
	//既存にも対応するため
	if(other == null){
		
		this.xName = setxName("media");

		var Flagment = '<MediaElement xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '" AutoPlay="false"/>';
		CreateXaml(this.sender, Flagment);
		
	}else{
		this.xName = other;
	}
	
	this.Media = this.sender.FindName(this.xName);
	this.Position = new Position(this.sender.FindName(this.xName));
	this.Size = new Size(this.sender.FindName(this.xName));
}

Media.prototype.SetSource = function(source)
{
	if(source != null) this.Media.Source = source;
}

Media.prototype.SetAutoPlay = function(status)
{
	if(status == 0) status = "false";
	if(status == 1) status = "true";
	if(status != null) this.Media.AutoPlay = status;
}

//日本語専用？クラス（ウソデス。マルチバイトです）Make仕様削除ｾﾘ0104

function MBText(sender)
{
	this.sender = sender;
	this.xName = setxName("mbtext");
	
	this.text = "";
	this.fontUri = "./font/ipag.ttf";
	this.fontSize = 30;
	
	
	//Create
	var Flagment = '<Glyphs xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '" Fill="#000000"';
	    Flagment += ' FontRenderingEmSize="' + this.fontSize + '" FontUri="' + this.fontUri + '"/>';

	//alert(Flagment); // チェック用
	CreateXaml(this.sender, Flagment);
	
	this.Position = new Position(this.sender.FindName(this.xName));
	this.Color = new Color(this.sender.FindName(this.xName));
	
}


//Font変更
MBText.prototype.SetFont = function(font)
{
	if(font != null) this.fontUri = font;
	this.sender.FindName(this.xName).FontUri = this.fontUri;
}

//FontSize
MBText.prototype.SetFontSize = function(size)
{
	if(size != null) this.fontSize = size;
	this.sender.FindName(this.xName).FontRenderingEmSize = this.fontSize;
}

//FontText
MBText.prototype.SetText = function(text)
{
	if(text != null) this.text = text;
	this.sender.FindName(this.xName).UnicodeString = this.text;
}

//TextBlock生成クラス 0104var適応
function TextBlock(sender, name)
{
	this.sender = sender;
	this.xName = setxName("text", name);
	
	var Flagment = '<TextBlock xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '" Text="" Foreground="#000000"/>';

	CreateXaml(this.sender, Flagment);
	
	this.Position = new Position(this.sender.FindName(this.xName));
	this.Text = new Text(this.sender.FindName(this.xName));
	
}


//Create Line 0104ver適応
function Line(sender, name)
{
	this.sender = sender;
	this.xName = setxName("text", name);
	
	this.x1 = 0;
	this.x2 = 0;
	this.y1 = 0;
	this.y2 = 0;

	var Flagment = '<Line xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '" Stroke="#000000" />';

	CreateXaml(this.sender, Flagment);
	
	this.Stroke = new Stroke(this.sender.FindName(this.xName));
	this.Position = new Position(this.sender.FindName(this.xName));
	
}


//set x1,y1,x2,y2
Line.prototype.Set = function(x1,y1,x2,y2)
{
	if(x1 != null) this.x1 = x1;
	if(y1 != null) this.y1 = y1;
	if(x2 != null) this.x2 = x2;
	if(y2 != null) this.y2 = y2;
	
	this.sender.FindName(this.xName).x1 = this.x1;
	this.sender.FindName(this.xName).y1 = this.y1;
	this.sender.FindName(this.xName).x2 = this.x2;
	this.sender.FindName(this.xName).y2 = this.y2;
}

//Create Ellipse class 0104ver適応
function Ellipse(sender, name)
{
	this.sender = sender;
	this.xName = setxName("Ellipse", name);
	
	
	var Flagment = '<Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '" Width="10" Height="10" Fill="#000000"/>';
	CreateXaml(this.sender, Flagment);
	
	this.Size = new Size(this.sender.FindName(this.xName));
	this.Stroke = new Stroke(this.sender.FindName(this.xName));
	this.Color = new Color(this.sender.FindName(this.xName));
	this.Position = new Position(this.sender.FindName(this.xName));
}

//Create Rectangle class
function Rect(sender, name)
{
	this.sender = sender;
	this.xName = setxName("Rectangle", name);
	
	//create
	var Flagment = '<Rectangle xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '" Width="10" Height="10" Fill="#000000"/>'
	CreateXaml(this.sender, Flagment);
	
	
	this.Size = new Size(this.sender.FindName(this.xName));
	this.Stroke = new Stroke(this.sender.FindName(this.xName));
	this.Color = new Color(this.sender.FindName(this.xName));
	this.Radius = new Radius(this.sender.FindName(this.xName));
	this.Position = new Position(this.sender.FindName(this.xName));
}

//Size class
function Size(sender)
{
	this.sender = sender;
	this.width = 0;
	this.height = 0;
}

Size.prototype.SetSize = function(width,height)
{
	if(width != null) this.width = width;
	if(height != null) this.height = height;
	
	this.sender.Width = this.width;
	this.sender.Height = this.height;
}

//text class
function Text(sender)
{
	this.sender = sender;
	this.color = "#000000";
	this.text = "";
	this.size = 12;
}

Text.prototype.SetText = function(text)
{
	if(text != null) this.text = text;
	this.sender.Text = this.text.toString();
}

Text.prototype.SetFontSize = function(size)
{
	if(size != null) this.size = size;
	this.sender.FontSize = this.size;
}

Text.prototype.SetColor = function(color)
{
	if(color != null) this.color = color;
	this.sender.Foreground = this.color;
}


//color class not Text 0104ver

function Color(sender)
{
	this.sender = sender;
	this.color = "#FFFFFF";

}

Color.prototype.Set = function(color)
{
	if(color != null) this.color = color;
	
	this.sender.Fill = this.color;
}

//Stroke class
function Stroke(sender)
{
	this.sender = sender;
	this.color = "#FFFFFF";
	this.size = "1";
}

//color
Stroke.prototype.SetColor = function(color)
{
	if(color != null) this.color = color;
	this.sender.Stroke = color;
}

Stroke.prototype.SetSize = function(size)
{
	if(size != null) this.size = size;
	this.sender.StrokeThickness = size;
}

//Radius
function Radius(sender)
{
	this.sender = sender;
	this.x = 0;
	this.y = 0;
}

Radius.prototype.Set = function(x,y)
{
	if(x != null) this.x = x;
	if(y != null) this.y = y;
	
	this.sender.RadiusX = this.x;
	this.sender.RadiusY = this.y;
}

//obj Position class 0104ver
function Position(sender)
{
	this.sender = sender;
	this.x = 0;
	this.y = 0;
}

Position.prototype.Set = function(x,y)
{
	if(x != null)  this.x = x;
	if(y != null)  this.y = y;

	this.sender["Canvas.Top"] = this.y;
	this.sender["Canvas.Left"] = this.x;
}


//あにめーしょん type1 = Double 2=Color 3= Point  まだできてねー
function Anime(sender, type)
{

	this.sender = sender;
	this.xName = setxName("Anime");
	
	this.animeName = this.xName + "anime";
	
	this.animexName = new Array();
	this.num = 0;
	
	this.animexName[this.num] = this.xName + "anime_" + this.num;
	
	var Flagment = '<Storyboard xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '">';
		Flagment += '<DoubleAnimation Storyboard.TargetName="' + this.sender.Name + '" Storyboard.TargetProperty="Opacity"/>'; //カラのやつを追加 ターゲットないとはじかれる
		Flagment += '</Storyboard>';

	CreateXamlAnime(this.sender, Flagment);
	this.Anime = this.sender.FindName(this.xName);

}

Anime.prototype.Add = function(type)
{
	//数字を変更＠type
	
	if(type == 2) type = "Color";
	if(type == 3) type = "Point";
	if(type == 1 || type == null) type = "Double";
	
	this.animexName[this.num] = this.xName + "_" + this.num;
	
	//たーげっとの名前こっちで指定しないとはじかれる
	var Flagment = '<'+ type +'Animation xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.animexName[this.num] + '" Storyboard.TargetName="' + this.sender.Name + '" Storyboard.TargetProperty="Opacity"';
	    Flagment += ' Duration="0:0:3" AutoReverse="True" From="1.0" To="0.0"/>';
	
	var control = this.sender.GetHost();	
	var xaml = control.content.createFromXaml(Flagment);

	this.Anime.Children.Add(xaml);
	if(this.num == 0) this.Anime.Children.RemoveAt(0); //からのやつけす
	this.num++;
}
/*
Anime.prototype.SetTime = function(time)
{
	if(time != null) this.sender.FindName(this.animeName).Duration = time;
}

Anime.prototype.SetProperty = function(target)
{
	if(target != null) this.sender.FindName(this.animeName)["Storyboard.TargetProperty"] = target;
}

Anime.prototype.SetReverse = function(status)
{
	if(status == 0) status = "False";
	if(status == 1) status = "True";
	
	if(status != null) this.sender.FindName(this.animeName).AutoReverse = status;
}

Anime.prototype.SetFromTo = function(from, to)
{
	if(from != null) this.sender.FindName(this.animeName).From = from;
	if(to != null) this.sender.FindName(this.animeName).To = to;
}

Anime.prototype.SetRepeat = function(type)
{
	if(type != null) this.sender.FindName(this.animeName).RepeatBehavior = type;
}

Anime.prototype.SetBeginTime = function(time)
{
	if(time != null) this.sender.FindName(this.animeName).BeginTime = time;
}*/

//グラーデーション仮
function Gradient(sender, type, property)
{
	this.sender = sender;
	this.xName = setxName("Gradient");
	
	this.stopxName = new Array();
	this.num = 0;
	
	if(type == null) type = "Linear";
	
	//元を生成
	var Flagment = '<' + type + 'GradientBrush xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.xName + '"/>';
	
	var control = this.sender.GetHost();	
	var xaml = control.content.createFromXaml(Flagment);
	
	//ディフォルトはFillあとはプロパティで指定
	if(property == "Stroke"){
		sender.Stroke = xaml;
	}else if(property == "Foreground"){
		sender.Foreground = xaml;
	}else{
		sender.Fill = xaml;
	}
}

//ポイントをAdd
Gradient.prototype.Add = function(color, offset)
{
	this.stopxName[this.num] = this.xName + "_stop_" + this.num; 
	var control = this.sender.GetHost();
	
	var Flagment = '<GradientStop xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="' + this.stopxName[this.num] + '" Color="' + color + '" Offset="' + offset + '"/>';
	
	var xaml = control.content.createFromXaml(Flagment);
	
	this.sender.FindName(this.xName).GradientStops.Add(xaml);
	
	this.num++;
}

//サイズマックス
function onResized(sender, e)
{
	var control = sender.getHost();
	
	sender.Width = control.content.actualWidth;
	sender.Height = control.content.actualHeight;
}

//Xaml つくるよー
function CreateXaml(sender, val)
{
	var control = sender.GetHost();	
	var xaml = control.content.createFromXaml(val);
	sender.children.add(xaml);
}

//Anime つくるよー
function CreateXamlAnime(sender, val)
{
	var control = sender.GetHost();	
	var xaml = control.content.createFromXaml(val);
	sender.resources.Add(xaml);
}

