Dialog.prototype.RootName = "Dialog";
Dialog.prototype.titleText = "";
Dialog.prototype.bodyText = "";
Dialog.prototype.defaultX = 250;
Dialog.prototype.defaultY = 170;
Dialog.prototype.revealOrientation = 0;

function Dialog(s, rootName, revealOrientation)
{
    this.RootName = rootName;
    this.s = s;
    this.plugin = s.getHost();
	this.MainCanvas = this.s.findname("MainCanvas");
    this.DebugBox = this.s.findname("DebugBox");
    this.xamlObject = this.s.findname(this.RootName);
    this.imageObject = this.s.findname(this.RootName + "Image");
    this.revealOrientation = revealOrientation;
    
    this.BuildRoll();
}

Dialog.prototype.rollObj = null;
Dialog.prototype.pathObj = null;

Dialog.prototype.BuildRoll = function()
{
    var t = '';
    
    var ww = 20;
    var hh = this.xamlObject["Height"];
    
    if (this.revealOrientation == 1)
    {
        ww = this.xamlObject["Width"];
        hh = 20;
    }
    
    var gx0 = 1;
    var gx1 = 0;
    var gy0 = 0.5;
    var gy1 = 0.5;
    
    if (this.revealOrientation == 1)
    {
        gx0 = 0.5;
        gx1 = 0.5;
        gy0 = 1;
        gy1 = 0;
    }
    
	t += '<Path Width="' + ww + '" Height="' + hh + '" Canvas.Left="0" Canvas.Top="0" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Data="F1 M 63.5556,563.111L 63.5556,257.333L 125.778,255.556L 180,257.333L 180,563.111L 122.222,564.889L 63.5556,563.111 Z ">';
		t += '<Path.Fill>';
			t += '<LinearGradientBrush StartPoint="' + gx0 + ',' + gy0 + '" EndPoint="' + gx1 + ',' + gy1 + '">';
				t += '<LinearGradientBrush.GradientStops>';
					t += '<GradientStop Color="#FF777777" Offset="0"/>';
					t += '<GradientStop Color="#FFFFFFFF" Offset="0.436441"/>';
					t += '<GradientStop Color="#FFAFBCC3" Offset="0.872881"/>';
					t += '<GradientStop Color="#FF404F5A" Offset="1"/>';
				t += '</LinearGradientBrush.GradientStops>';
			t += '</LinearGradientBrush>';
		t += '</Path.Fill>';
	t += '</Path>';
	
	this.pathObj = this.plugin.content.createFromXaml(t);
	
	t = '<Canvas></Canvas>'
	
	this.rollObj = this.plugin.content.createFromXaml(t);
	this.rollObj.children.add(this.pathObj);
	
	this.xamlObject.children.add(this.rollObj);
}

Dialog.prototype.textCanvas = null;
Dialog.prototype.textClipRect = null;

Dialog.prototype.BuildText = function()
{
    var t = '';
    t += '<Canvas Canvas.Left="9" Canvas.Top="10">';
        t += '<Canvas.Clip>';
            t += '<RectangleGeometry Rect="0,0,0,115">';
            t += '</RectangleGeometry>';
        t += '</Canvas.Clip>';  
        t += '<TextBlock Foreground="#ffffffff" FontFamily="Verdana" FontSize="12" FontWeight="Bold">' + this.titleText + '</TextBlock>'
        t += '<Canvas Canvas.Top="19">';
            t += '<TextBlock Foreground="#ffffffff" FontSize="9.5">' + this.bodyText + '</TextBlock>';
        t += '</Canvas>';
    t += '</Canvas>';
    
    this.textCanvas = this.plugin.content.createFromXaml(t);
    this.textClipRect = this.textCanvas.Clip;
    //alert("textClipRect = " + this.textClipRect);
    this.xamlObject.children.add(this.textCanvas);
}

Dialog.prototype.RollVert = function(direction, duration)
{
    var tt = 0;
    
    var topEdge = tt;
    var hh = this.xamlObject["Height"];
    var bottomEdge = tt + hh;
        
    var rollAnim = new Animation("rollAnim", 
        this.rollObj, "Canvas.Top", topEdge, bottomEdge, duration);
   
    var clipAnim = new Animation("clipAnim", 
        this, "ClipY", 0, 1, duration);
        
    
    var clipAnim2 = new Animation("clipAnim2", 
        this, "RollHeight", 20, 0, duration);
        
    clipAnim2.easeFunction = 
        function (inVal) { 
            var delta = 1; 
            var stepp = (Math.pow((inVal), 9999999999999)); 
            return Math.ceil(stepp) 
            } 
   
    var comp = new CompoundAnimation();
    comp.baseAnimation.ease = false;
    comp.baseAnimation.duration = duration;
    comp.baseAnimation.easeFunction =
        function(_inVal)
        {
            var inVal = _inVal;
            
            if (direction == -1)
                inVal = 1 - inVal;
            var rc = 0;
            
            var wallFrac = 0.5;
            var bounceDepth = 0.3;
            
            if ( (inVal < wallFrac))
            {
                rc = 1 - Math.cos(inVal / wallFrac * Math.PI/2);
            }
            else
            {
                var ff = (inVal - wallFrac)/ (1 - wallFrac);
                
                rc = 1 - bounceDepth * Math.abs(Math.sin(ff * 2 * Math.PI)) * (1 - ff);
            }
            
            return rc;
        }
        
    comp.Add(rollAnim);
    comp.Add(clipAnim);
    comp.Add(clipAnim2);
    
    clipAnim.OnFractionScript = Silverlight.createDelegate(this, this.UpdateVert);

    return comp;
}

Dialog.prototype.Roll = function(direction, duration)
{
    var ll = 0;//this.xamlObject["Canvas.Left"];
    //Debug("ll = " + ll);
    
    var leftEdge = ll;
    var ww = this.xamlObject["Width"];
    var rightEdge = ll + ww;
        
    var rollAnim = new Animation("rollAnim", 
        this.rollObj, "Canvas.Left", leftEdge, rightEdge, duration);
    
//    if (direction == -1)
//    {
//        var skoot = new Animation("skoot", this.rollObj, "Canvas.Left",
//                    rightEdge, 0 - ww, 3.4);
//        rollAnim.OnStopAnimation = skoot;
//    }
   
    var clipAnim = new Animation("clipAnim", 
        this, "ClipX", 0, 1, duration);
        
    
    var clipAnim2 = new Animation("clipAnim2", 
        this, "RollWidth", 20, 0, duration);
        
    clipAnim2.easeFunction = 
        function (inVal) { 
            var delta = 1; 
            var stepp = (Math.pow((inVal), 9999999999999)); 
            return Math.ceil(stepp) 
            } 
   
    var comp = new CompoundAnimation();
    comp.baseAnimation.ease = false;
    comp.baseAnimation.duration = duration;
    comp.baseAnimation.easeFunction =
        function(_inVal)
        {
            var inVal = _inVal;
            
            if (direction == -1)
                inVal = 1 - inVal;
            var rc = 0;
            
            var wallFrac = 0.5;
            var bounceDepth = 0.3;
            
            if ( (inVal < wallFrac))// && direction == 1) )|| (inVal > wallFrac && direction == -1) )
            {
                rc = 1 - Math.cos(inVal / wallFrac * Math.PI/2);
            }
            else
            {
                var ff = (inVal - wallFrac)/ (1 - wallFrac);
                
                rc = 1 - bounceDepth * Math.abs(Math.sin(ff * 2 * Math.PI)) * (1 - ff);
            }
            
            return rc;
        }
        
    comp.Add(rollAnim);
    comp.Add(clipAnim);
    comp.Add(clipAnim2);
    
    clipAnim.OnFractionScript = Silverlight.createDelegate(this, this.Update);
    //comp.Play();

    return comp;
}


Dialog.prototype.ClipY = 0;
Dialog.prototype.RollHeight = 20;

Dialog.prototype.UpdateVert = function(anim)
{
    //Debug("this = " + this + "        this.currentValue = " + this.currentValue);
    this.SetClipHeight(anim.currentValue * this.xamlObject["Height"]);
    this.SetRollHeight(20 - anim.currentValue * 20);
}

Dialog.prototype.ClipX = 0;
Dialog.prototype.RollWidth = 20;

Dialog.prototype.Update = function(anim)
{
    //Debug("this = " + this + "        this.currentValue = " + this.currentValue);
    this.SetClipWidth(anim.currentValue * this.xamlObject["Width"]);
    this.SetRollWidth(20 - anim.currentValue * 20);
}

Dialog.prototype.SetClipWidth = function(val)
{
    var rr = "0,0," + val + "," + this.xamlObject["Height"];
    //Debug("rr = " + rr);
    mainWindow.xamlObject.findname(this.RootName + "ClipRect")["Rect"] = rr;
    this.textClipRect["Rect"] = rr;
}

Dialog.prototype.SetRollHeight = function(val)
{
    this.pathObj["Height"] = val;
}

Dialog.prototype.SetClipHeight = function(val)
{
    var rr = "0,0," + this.xamlObject["Width"] + "," + val;
    //Debug("rr = " + rr);
    mainWindow.xamlObject.findname(this.RootName + "ClipRect")["Rect"] = rr;
    this.textClipRect["Rect"] = rr;
}

Dialog.prototype.SetRollWidth = function(val)
{
    this.pathObj["Width"] = val;
}

Dialog.prototype.SetRollHeight = function(val)
{
    this.pathObj["Height"] = val;
}

Dialog.prototype.OnSized = function()
{
	//this.rollObj["Canvas.Top"] = this.xamlObject["Canvas.Top"];
}

Dialog.prototype.Show = function(anObj, val)
{
    var obj = anObj;
    if (obj == null) obj = this.s.findname(this.RootName);
    
    if (val == true)
    {
        
        visibleDialog = this;
    }
    else
    {
        obj["Visibility"] = "Collapsed";
    }
}

Dialog.prototype.OnStopped = function()
{
    this.runningAnimation = null;
}

Dialog.prototype.runningAnimation = null;

Dialog.prototype.Reveal = function(val, duration, posX, posY)
{   
    if (this.runningAnimation != null)
    {
        this.runningAnimation.Stop();
        this.runningAnimation = null;
    }
        
    var startVal = 0;
    var endVal = 1;
    if (!val)
    {
        startVal = 1;
        endVal = 0;
    }
    
    var ll = posX;
    
    if (this.revealOrientation == 1)
        ll = posY;
        

    var approachDur = 0.3;
    
    var revealAnim = new Animation("fade", this.xamlObject, 
        "Opacity", startVal, endVal,
         duration);
         
    revealAnim.startVal = startVal;
    revealAnim.endVal = endVal;
    revealAnim.duration = approachDur;
    
    var actualWidth = this.plugin.content.actualWidth;
    var actualHeight = this.plugin.content.actualHeight;
    var propName = "Canvas.Left";
    var wigglePropName = "Canvas.Top";
    
    var r2Start = val ? actualWidth : ll;
    var r2End = val? ll : -1 * this.rollObj["Width"];
    
    if (this.revealOrientation == 1)
    {
        r2Start = val ? actualHeight : ll;
        r2End = val ? ll : -1 * this.rollObj["Height"]
        propName = "Canvas.Top";
        wigglePropName = "Canvas.Left";
    }
    
    
    var revealAnim2 = new Animation("revealAnim2", this.xamlObject, 
        propName, r2Start, r2End,
         duration);
         
    revealAnim2.duration = approachDur;
    revealAnim.OnPlayAnimation = revealAnim2;
    var dir = 1;
    if (!val) dir = -1;
    
    var roll = null;
    
    if (this.revealOrientation == 0)
    {
        roll = this.Roll(dir, duration);
    }
    else
    {
        //alert("vert = " + this.revealOrientation);
        roll = this.RollVert(dir, duration);
    }
    
    var wiggleStartVal = this.xamlObject[wigglePropName];   
    
    var wiggleAmount = 0;
    
    if (dir == 1)
    {
        revealAnim.OnStopAnimation = roll;
                        
        var wiggleAnim = new Animation("wiggle", this.xamlObject, wigglePropName,
            wiggleStartVal, wiggleStartVal + wiggleAmount, duration); 
            
        roll.baseAnimation.OnStopAnimation = wiggleAnim;
        wiggleAnim.OnStopScript = Silverlight.createDelegate(this, this.OnStopped);
        revealAnim.Play();
        
        this.runningAnimation = wiggleAnim;
    }
    else if (dir == -1)
    {
        var wiggleAnim = new Animation("wiggle", this.xamlObject, wigglePropName,
            wiggleStartVal, wiggleStartVal - wiggleAmount, duration * 0.15); 
        wiggleAnim.OnStopAnimation = roll.baseAnimation;
        roll.baseAnimation.OnStopAnimation = revealAnim;
        roll.baseAnimation.OnStopScript = Silverlight.createDelegate(this, this.OnStopped);
        wiggleAnim.Play();
        
        this.runningAnimation = roll.baseAnimation;
    }
}

Dialog.prototype.IsVisible = function()
{
    return (this.MainCanvas.findname(this.RootName)["Opacity"] > 0);
}

Dialog.prototype.ShowHide = function(posX, posY, _dur)
{
    var dur = _dur;
    
    if (dur == null || dur == "undefined") dur = 0.5;
    
    if (posX == -1 && posY == -1)
    {
        posX = this.defaultX;
        posY = this.defaultY;
    }
    if (this.MainCanvas.findname(this.RootName)["Opacity"] == 0)
    {
        if (visibleDialog != null && visibleDialog.IsVisible())
        {
            visibleDialog.ShowHide(visibleDialog.xamlObject["Canvas.Left"], visibleDialog.xamlObject["Canvas.Top"]);
        }
    
        this.xamlObject["Canvas.Left"] = posX;
        this.xamlObject["Canvas.Top"] = posY;
        this.OnSized();
        //Debug("\n\n\n\n\n\n\nJust showing dialog for this room.");
        //var dialogFade = navBar.GetSliderFadeAnim("Dialog", 0, 1, 1);
        //dialogFade.Play();
        
        this.Reveal(true, dur, posX, posY);
        
        visibleDialog = this;
    }
    else
    {
        Debug("");
        this.Reveal(false, dur, posX, posY);
    }
    
    //toggleLayer(this.RootName + "Shown");
}

