function mss(){}


mss.control=null;
mss.content=null;

mss.OnLoad=function(silverlightControl)
{
    mss.control=silverlightControl;
    mss.content=mss.control.content;
}
    
mss.Animate=function(animationName) 
{
    var animation = mss.FindName(animationName);
	
    if (animation!=null)
    {
        try 
        {
            animation.begin();
        }
        catch(err)
        {
                txt = "Animation problem with the animation: "+animationName;
                alert(txt);

        }
        return true;
    }
    return false;
}

mss.StopAnimate = function(animationName)
{
    var animation = mss.FindName(animationName);

    if (animation != null)
    {
        try
        {
            animation.stop();
        }
        catch (err)
        {
            txt = "Stop animation problem with the animation: " + animationName;
            alert(txt);

        }
        return true;
    }
    return false;
}
mss.FindName=function(name) 
{
    return mss.content.findName(name);
}

mss.CreateFromXaml=function(xaml)
{
    return mss.content.CreateFromXaml(xaml);
}

mss.CreateClipRect=function(left,top,width,height)
{
    var clippingString = '<RectangleGeometry Rect="left,top,width,height"/>';
    clippingString = clippingString.replace("left", left);
    clippingString = clippingString.replace("top", top);
    clippingString = clippingString.replace("width", width);
    clippingString = clippingString.replace("height", height);

    return clippingGeometry = mss.content.CreateFromXaml(clippingString);
}

mss.AddResource = function(obj)
{
    var root = mss.FindName("root");
    root.Resources.Add(obj);
}

mss.RemoveResource = function(obj)
{
    var root = mss.FindName("root");
    root.Resources.Remove(obj);
}

mss.AddStoryBoard = function(xaml)
{
    var obj = mss.CreateFromXaml(xaml);
    mss.AddResource(obj);
    return obj;
}

mss.SetVisible = function(canvasName,show) 
{
    var panel = mss.FindName(canvasName);
    
    if (panel!=null)
    {
        if (show)
        {
           panel.SetValue("Visibility","Visible");
        }
        else
        {
           panel.SetValue("Visibility","Collapsed");
        }
    }
}

mss.AnimateMouseLeave=function(sender, eventArgs) 
{
    mss.Animate(sender.Name+"_MouseLeave");
}

mss.AnimateMouseEnter=function(sender, eventArgs) 
{
    mss.Animate(sender.Name+"_MouseEnter");
}
	
mss.AnimateMouseDown=function(sender, eventArgs) 
{
    mss.Animate(sender.Name+"_MouseDown");
}
	
mss.AnimateMouseUp=function(sender, eventArgs) 
{
	mss.Animate(sender.Name+"_MouseUp");
}

mss.AddAnimationMouseEventListeners=function(panelName,clickAction)
{

    var panel = mss.FindName(panelName);
    
	if (panel!=null)
    {
        panel.addEventListener("MouseEnter", Event.createDelegate(mss, mss.AnimateMouseEnter));
        panel.addEventListener("MouseLeftButtonDown", Event.createDelegate(mss, mss.AnimateMouseDown));
	    panel.addEventListener("MouseLeave", Event.createDelegate(mss, mss.AnimateMouseLeave));
	    
	    if (clickAction!=null)
	    {
	        panel.addEventListener("MouseLeftButtonUp", clickAction);
	    }
    }
}

function TextAlign(){}
TextAlign.CenterVertical=1;
TextAlign.CenterHorizontal=2;

mss.AlignText=function(textBlock,textAlign)
{

//        while(textBlock.ActualWidth > textBlock.Width) 
//        { 
//            textBlock.FontSize -= 0.5; 
//        } 

        if ((textAlign & TextAlign.CenterHorizontal)==TextAlign.CenterHorizontal)
        {
            textBlock.SetValue("Canvas.Left", 0);
            var newLeft = ((textBlock.Width - textBlock.ActualWidth) / 2)+ textBlock.GetValue("Canvas.Left"); 
            
            textBlock.SetValue("Canvas.Left", newLeft); 
        }
 

        if ((textAlign & TextAlign.CenterVertical)==TextAlign.CenterVertical)
        {
            textBlock.SetValue("Canvas.Top", 0);
            var newTop = ((textBlock.Height - textBlock.ActualHeight) / 2) + textBlock.GetValue("Canvas.Top");
                 
            textBlock.SetValue("Canvas.Top", newTop);
        }
}
mss.SetText = function(textBlock, text)
{
    if (textBlock)
    {
        textBlock.Text = text;
        var wrappedText = false;

        if ("Wrap" == textBlock.GetValue("TextWrapping"))
        {
            wrappedText = true;
            if (textBlock.Height >= textBlock.ActualHeight)
            {
                return;
            }
        }
        else
        {
            if (textBlock.Width >= textBlock.ActualWidth)
            {
                return;
            }
        }



        var bottom = 0;
        var top = text.length;
        var newIdx = top;
        var length = 0;
        var actualLength = 0;
        var workingText = "";

        while ((bottom + 1) < top)
        {
            workingText = text.substring(0, newIdx);
            textBlock.Text = workingText + "...";

            if (wrappedText)
            {
                length = textBlock.Height;
                actualLength = textBlock.ActualHeight;
            }
            else
            {
                length = textBlock.Width;
                actualLength = textBlock.ActualWidth;
            }

            if (length < actualLength)
            {
                top = newIdx;
            }
            else
            {
                bottom = newIdx;
            }

            newIdx = bottom + Math.floor((top - bottom) / 2);
        }
        
        
        while (newIdx < text.length - 1)
        {
            newIdx++;
            workingText = text.substring(0, newIdx);
            textBlock.Text = workingText + "...";

            if (wrappedText)
            {
                length = textBlock.Height;
                actualLength = textBlock.ActualHeight;
            }
            else
            {
                length = textBlock.Width;
                actualLength = textBlock.ActualWidth;
            }

            if (actualLength > length)
            {
                newIdx--;
                workingText = text.substring(0, newIdx);
                textBlock.Text = workingText + "...";
                return;
            }
        }

    }
}

mss.FormatTimeHHMMSS=function(duration)
{
    var whole = duration/1000;
    
    hours = Math.floor(whole/3600);
    whole = whole- (hours*3600);
    if (hours<10)
    {
        hours="0"+hours;
    }
    
    return hours+":"+mss.FormatTimeMMSS(whole*1000);
}

mss.FormatTimeMMSS=function(duration)
{
    var whole = duration/1000;
    
    minutes = Math.floor(whole/60);
    whole = whole- (minutes*60);
    seconds = Math.floor(whole);
    
    whole = whole-seconds;
    
    if (minutes<10)
    {
        minutes="0"+minutes;
    }
    if (seconds<10)
    {
        seconds="0"+seconds;
    }
    return ( minutes+":"+seconds);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////

function Size(width,height)
{
    this.Width=width;
    this.Height=height;
}

function Rectangle(left,top,width,height)
{
    this.Left=left;
    this.Top=top;
    this.Width=width;
    this.Height=height;
}

Rectangle.prototype =
{
    SetCanvasRect: function(canvas)
    {
        if (canvas)
        {
            canvas.SetValue("Canvas.Left", this.Left);
            canvas.SetValue("Canvas.Top", this.Top);
            canvas.width = this.Width;
            canvas.height = this.Height;
        }

    },

    SetCanvasSize: function(canvas)
    {
        if (canvas)
        {
            canvas.width = this.Width;
            canvas.height = this.Height;
        }
    },

    GetCanvasRect: function(canvas)
    {
        this.Width = canvas.width;
        this.Height = canvas.height;
        this.Left = canvas.GetValue("Canvas.Left");
        this.Top = canvas.GetValue("Canvas.Top");
    },

    SetClipRect: function(rectangleGeometry)
    {
        var rectDimensions = this.Left + "," + this.Top + "," + this.Width + "," + this.Height;
        rectangleGeometry["Rect"] = rectDimensions;
    },

    Clone: function()
    {
        return new Rectangle(this.Left, this.Top, this.Width, this.Height);
    },

    ToString: function()
    {
        return this.Left + "," + this.Top + "," + this.Width + "," + this.Height;
    }
}


////////////////////////////////////////////

function Event(name)
{
    this.Name=name;
    this.delegateList=new Array(0);
}

Event.createDelegate = function(instance, method) 
{
	return function() {
        return method.apply(instance, arguments);
    }
}

Event.prototype=
{

    Invoke:function(objParameters)
	{
	    for (var i=0;i<this.delegateList.length;i++)
	    {
	        if (this.delegateList[i])
	        {
	            this.delegateList[i](objParameters);
	        }
	    }

    },
    
    addListener:function(delegate)
    {
    
        for(var i=0;i<this.delegateList.length;i++)
        {
            if (this.delegateList[i]==null)
            {
                this.delegateList[i]=delegate;
                return i;
            }
        }
        
        var newList = new Array(this.delegateList.length+1);
        
        for(var i=0;i<this.delegateList.length;i++)
        {
            newList[i]=this.delegateList[i];
        }
        
        newList[this.delegateList.length]=delegate;
        
        this.delegateList=newList;
        
        return (this.delegateList.length-1);
    },
    
    removeListener:function(token)
    {
        if (this.delegateList.length>token)
        {
            this.delegateList[token]=null;
        }
    }
}

function EventList() 
{
    this.namedEvents=new Array(arguments.length);
    
    for(var i=0;i<arguments.length;i++)
    {
        this.namedEvents[i]=new Event(arguments[i]);
    }
}

EventList.prototype=
{

    Invoke:function(name,objParameters)
	{
	    var event = this.findEvent(name);
	    
	    if (event)
	    {
	        event.Invoke(objParameters);
	    }

    },
    
    addListener:function(name,delegate)
    {
        var event = this.findEvent(name);
	    
	    if (event)
	    {
	        return event.addListener(delegate);
	    }
	    
	    return null;
    },
    removeListener:function(name,token)
    {
        var event = this.findEvent(name);
	    
	    if (event)
	    {
	        event.removeListener(token);
	    }
    },
    findEvent:function(name)
    {
        for(var i=0;i<this.namedEvents.length;i++)
        {
            if (this.namedEvents[i].Name==name)
            {
                return this.namedEvents[i];
            }
        }
        
        return null;
    }
}



function DynamicImageLoader(imageControl)
{
    this.imageControl = imageControl;

    this.downloader = mss.control.createObject("downloader");
    this.downloader.addEventListener("completed", Event.createDelegate(this, this.handleCompleted));
}

DynamicImageLoader.prototype=
{
    GetImage:function(url)
	{
        this.downloader.open("GET", url);
        this.downloader.send();
    },
    
        
    handleCompleted:function(sender, eventArgs)
    {
        this.imageControl.setSource(sender,"");
    }

}
        


