﻿// JScript-Datei

function SLWrapper(objID){
    this.id = objID;
    this.currentHover = null;
}


    SLWrapper.prototype.highlight = function (id, doHighlight){
        id = "" + id;
        var element = this.control.getHost().content.findName(id);
        var extIndex, imgPath;
        
        var storyboard, animation;
        var glowImg;
        var opaq;
        if (doHighlight){
            var img = this.control.getHost().content.findName(id+'___img');
            if (img == null) return;
		    extIndex = img.Source.lastIndexOf(".");
		    if (extIndex == -1) return;
		    imgPath = img.Source.substr(0, extIndex) + "_glow" + img.Source.substr(extIndex);
		    
		    glowImg = this.control.getHost().content.findName(id+'___glow');
		    if (!glowImg){
		        var xamlString = '<Image Loaded="'+this.id+'_imgLoaded" Name="'+id+'___glow" Source="'+imgPath+'" Opacity="0" Canvas.Left="3000" Canvas.Top="3000" >';
		        xamlString += '<Image.Resources><Storyboard Name="'+id+'_animation"><DoubleAnimation Storyboard.TargetName="'+id+'___glow" Storyboard.TargetProperty="(Opacity)" To="1" Duration="0:0:0.5" /></Storyboard></Image.Resources>';
		        xamlString += '</Image>';

		        var xamlObject = this.control.getHost().content.createFromXaml(xamlString);
    	        
                this.control.getHost().content.findName(id).children.add(xamlObject);
                
                glowImg = this.control.getHost().content.findName(id+'___glow');
                
                if (element.Tag != "center"){
                    glowImg["Canvas.Left"] = -20;
                    glowImg["Canvas.Top"] = -20;
                }
                
                
            }
             
            opaq = glowImg["Opacity"];
                
            storyboard = this.control.getHost().content.findName(id+'_animation');
            if (!storyboard) return;
		    storyboard.stop();
		    animation = storyboard.children.getItem(0);
		    animation["To"] = 1;
		    animation["From"] = opaq;
		    storyboard.Begin();
            
		}else{
		    glowImg = this.control.getHost().content.findName(id+'___glow');
		    if (glowImg == null) return;
		    
		    opaq = glowImg["Opacity"];
		    
		    storyboard = this.control.getHost().content.findName(id+'_animation');
		    
            if (!storyboard) return;
		    storyboard.stop();
		    animation = storyboard.children.getItem(0);
		    animation["To"] = 0;
		    animation["From"] = opaq;
		    storyboard.Begin();
		    
		}
    
    }
    
    SLWrapper.prototype.isHighlighted = function (id){
        id = "" + id;
        var element = this.control.getHost().content.findName(id + "___glow");
        if (element.Opacity > 0) return true;
        
        return false;
        
    }
    
    SLWrapper.prototype.showElement = function (id){
    id = "" + id;
        var element = this.control.getHost().content.findName(id);
        if (element){
            element["Visibility"]="visible";
        
            if (element.Opacity != 1){
            
                var storyboard;
                var guid = id+'___showanim';//generateGuid();
                
                storyboard = this.control.getHost().content.findName(guid);
                if (storyboard){
                    //storyboard.stop();
                    element.resources.Remove(storyboard);
                }
                var xamlString = '<Storyboard Name="'+guid+'">'
                                    + '<DoubleAnimation Storyboard.TargetProperty="(Opacity)" From="0" To="1" Storyboard.TargetName="'+id+'" Duration="0:0:0.3"></DoubleAnimation>'
                                    + '</Storyboard>';

                storyboard = this.control.getHost().content.createFromXaml(xamlString);
                element.resources.add(storyboard);
                storyboard.Begin();
            }
        }
        
    }
    
    SLWrapper.prototype.hideElement = function (id){
        id = "" + id;
        var element = this.control.getHost().content.findName(id);
        //if (element)
        //element["Visibility"]="collapsed";
        
        
        if (element.Visibility != "collapsed"){
            
            var storyboard;
            var guid = id+'___hideanim';//generateGuid();
            
            storyboard = this.control.getHost().content.findName(guid);
            if (storyboard){
                //storyboard.stop();
                element.resources.Remove(storyboard);
            }
            var xamlString = '<Storyboard Name="'+guid+'">'
                                + '<DoubleAnimation Storyboard.TargetProperty="(Opacity)" From="1" To="0" Storyboard.TargetName="'+id+'" Duration="0:0:0.3"></DoubleAnimation>'
                                + '</Storyboard>';

            
            storyboard = this.control.getHost().content.createFromXaml(xamlString);
            //storyboard.AddEventListener("Completed", this.toString() + ".collapseElement");
            element.resources.add(storyboard);
            storyboard.Begin();
            
            setTimeout(this.toString() + '.collapseElement("'+id+'")', 300);
        }
    }
    
    SLWrapper.prototype.collapseElement = function (id){
        id = "" + id;
        var element = this.control.getHost().content.findName(id);
        if (element)
        element["Visibility"]="collapsed";
    }
    
    SLWrapper.prototype.hideAll = function (anim){
    
        var tagName = "Canvas";
        var parent = this.control.getHost().content.findName("rootCanvas");
        
        //var states = this.getChildNodes(parent, true);

        if (parent.toString() != "Canvas")
        {
            return null;
        }
     
        var childrenCount = parent.children.count;
        var objects = new Array();
     
        for (var i = 0; i < childrenCount; i++)
        {
            var child = parent.children.getItem(i);
            if (child.toString() == tagName) 
            {
                if (anim)
                    this.hideElement(child.Name, anim);
                else{
                    child.Opacity = 0;
                    child.Visibility = "collapsed";
                   }
                objects.push(child);
            }
        }
        return objects;
    }
    
    
    
    SLWrapper.prototype.toggleElement = function (id){
        id = "" + id;
        var element = this.control.getHost().content.findName(id);
        if (element["Visibility"]=="collapsed")
            this.showElement(element.Name);
        else
            this.hideElement(element.Name);
    }
    SLWrapper.prototype.zoomItem = function (id, zoomfactor){
	    id = "" + id;
	    if (isNaN(zoomfactor)) zoomfactor = 100;
	    else
	    zoomfactor = zoomfactor / 100;
	    var element = this.control.getHost().content.findName(id);
	    var scaleTransform = element.findName(id + "___scale");
	    if (scaleTransform == null) return;
        scaleTransform.CenterX = 0;
        scaleTransform.CenterY = 0;
        
        
        var storyboard, animation1, animation2;
        var guid = id+'___scaleanim';//generateGuid();
        
        storyboard = this.control.getHost().content.findName(guid);
        if (storyboard){
            //storyboard.stop();
            element.resources.Remove(storyboard);
        }
        
        var xamlString = '<Storyboard Name="'+guid+'">'
                            + '<DoubleAnimation Storyboard.TargetProperty="(ScaleX)" From="'+scaleTransform.ScaleX+'" To="'+zoomfactor+'" Storyboard.TargetName="'+id+'___scale" Duration="0:0:0.5"></DoubleAnimation>'
                            + '<DoubleAnimation Storyboard.TargetProperty="(ScaleY)" From="'+scaleTransform.ScaleX+'" To="'+zoomfactor+'" Storyboard.TargetName="'+id+'___scale" Duration="0:0:0.5" ></DoubleAnimation>'
                            + '</Storyboard>';

        storyboard = this.control.getHost().content.createFromXaml(xamlString);
        element.resources.add(storyboard);
        storyboard.Begin();
	}
	
	SLWrapper.prototype.setFullscreen = function (state){
	    
	    this.control.getHost().content.onFullScreenChange = Silverlight.createDelegate(this, this.onFullScreenChanged);

	
	    this.control.getHost().content.FullScreen = state;
	    
	    var base = this.control.getHost().content.findName("base");
	    var w = this.control.getHost().content.ActualWidth;
	    var h = this.control.getHost().content.ActualHeight;
	    base.Width = w;
	    base.Height = h;
	    
	    return {Width:w, Height:h}
	}
	
	SLWrapper.prototype.onFullScreenChanged = function(sender, eventArgs){
	    eval(this.id + "_onFullscreenEnter(" + this.control.getHost().content.FullScreen + ")");
	}
	//<%= this.ClientID %>_onFullscreenEnter(fullscreen)
	
	SLWrapper.prototype.getChildNodes = function(parent, recursive){
	    var children = new Array();
	    var tagName = "Canvas";
        //var parent = this.control.getHost().content.findName(parentNode);

        //if (parent.toString() != "Canvas")
        //{
        //    return null;
        //}
     
        var childrenCount = parent.children.count;
     
        for (var i = 0; i < childrenCount; i++)
        {
            var child = parent.children.getItem(i);
            if (child.toString() == tagName) 
            {
                children.push(child);
                if (recursive && child.children.count > 0){
                    children = children.concat(this.getChildNodes(child, recursive));
                }
            }
        }
        return children;
	}
	
	SLWrapper.prototype.getItemStates = function (){
	    var rootCanvas = this.control.getHost().content.findName("rootCanvas");
	    var children = this.getChildNodes(rootCanvas, true);
	    
	    var states = new Array();
	    var s = "";
	    var o;
	    var child;
	    
	    o = new Object();
	    child = this.control.getHost().content.findName('base');
	    o.Name = "base";
	    o.Background = child.background;
	    o.Width = child.Width;
	    o.Height = child.Height;
	    
	    states.push(o);
	    
	    for (var i=0; i < children.length; i++){
	        child = children[i];
	        o = new Object();
	        o.X = child["Canvas.Left"];
	        o.Y = child["Canvas.Top"];
	        o.Z = child["Canvas.ZIndex"];
	        o.Visible = child.Visibility.toLowerCase() == "visible";
	        o.Opacity = child.Opacity * 100.0;
	        o.Name = child.Name;
	        o.Scale = child.RenderTransform.ScaleX * 100;
	        
	        
	        
	        s += children[i].Name + "\n";
	        
	        states.push(o);
	    }
	    
	    return states;
	}
	
	SLWrapper.prototype.setItemStates = function (states, animate){
	    var rootCanvas = this.control.getHost().content.findName("rootCanvas");
	    var children = this.getChildNodes(rootCanvas, true);
	    
	    var s = "";
	    var o;
	    var child;
	    for (var i=0; i < states.length; i++){
	        o = states[i];
	        child = this.control.getHost().content.findName(o.Name);
	        if (o.Name == "base"){
	            if (!animate){
	                this.setBackgroundColor(o.Background);
	                child.Width = o.Width;
	                child.Height = o.Height;
	            }else{
	                this.animateBackgroundColor(o.Background);
	            }
	        }else{
	            if (!animate){
	                child["Canvas.Left"] = o.X;
	                child["Canvas.Top"] = o.Y;
	                child["Canvas.ZIndex"] = o.Z;
	                child.Visibility = o.Visible?"visible":"collapsed";
	                child.Opacity = o.Opacity / 100.0;
	                child.RenderTransform.ScaleY = 
	                child.RenderTransform.ScaleX = o.Scale / 100;
	            }else{
	                this.positionItem(o.Name, o.X, o.Y, o.Z);
	                this.zoomItem(o.Name, o.Scale);
    	            
	                if (o.Visible)
	                    this.showElement(o.Name);
	                else
	                    this.hideElement(o.Name);
    	            
	            }
	        }
	        
	        s += states[i].Name + "\n";
	    }
	}
	
	SLWrapper.prototype.setBackgroundColor = function (color){
	    if (color == null) return;
	    var base = this.control.getHost().content.findName('base');
	    base.background = color;
	    
	}
	
	SLWrapper.prototype.animateBackgroundColor = function (color){
	
	    if (color == null) return;
	    var base = this.control.getHost().content.findName('base');
	    if (!base.background)
	        base.background = "#00FFFFFF";
	        
	    if (base.background == color) return;
	    var storyboard = this.control.getHost().content.findName('AnimateBackgroundColorStoryBoard');
	    if (storyboard){
            //storyboard.stop();
	        base.resources.Remove(storyboard);
	    }
	    
        var xamlstring = '<Storyboard Name="AnimateBackgroundColorStoryBoard">'
                           + '<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="base" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">'
                           + '<SplineColorKeyFrame KeyTime="00:00:00.5" Value="'+color+'"/>'
                           + '</ColorAnimationUsingKeyFrames>'
                           //+ '<ColorAnimation BeginTime="00:00:00" Storyboard.TargetName="Page" '
                           //+ 'Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" '
                           //+ ' To="'+color+'" Duration="0:0:2" />'

                           + '</Storyboard>';
	    
        storyboard = this.control.getHost().content.createFromXaml(xamlstring);
        base.resources.Add(storyboard);
	    storyboard.Begin();
	}
	
	SLWrapper.prototype.setScale = function (id, zoomfactor){
	    id = "" + id;
	    if (isNaN(zoomfactor)) zoomfactor = 100;
	    else
	    zoomfactor = zoomfactor / 100;
	    var element = this.control.getHost().content.findName(id);
	    var scaleTransform = element.findName(id + "___scale");
        scaleTransform.CenterX = 0;
        scaleTransform.CenterY = 0;
        scaleTransform.ScaleX = 
        scaleTransform.ScaleY = zoomfactor;
	}
	
	SLWrapper.prototype.setPosition = function (id, x, y, z){
	    id = "" + id;
        var element = this.control.getHost().content.findName(id);
        
        var storyboard, animation1, animation2;
        var guid = id+'___positionanim';//generateGuid();
        
        storyboard = this.control.getHost().content.findName(guid);
        if (storyboard){
            //storyboard.stop();
            element.resources.Remove(storyboard);
        }
            
        element["Canvas.Left"] = x;
        element["Canvas.Top"] = y;
        
        if (z){
            element["Canvas.ZIndex"] = z;
        }
	}
	
	SLWrapper.prototype.animatePosition = function (id, x, y, z){
	    id = "" + id;
        var element = this.control.getHost().content.findName(id);
        
        var storyboard, animation1, animation2;
        var guid = id+'___positionanim';//generateGuid();
        
        storyboard = this.control.getHost().content.findName(guid);
        if (storyboard){
            //storyboard.stop();
            element.resources.Remove(storyboard);
        }
            
        var xamlString = '<Storyboard Name="'+guid+'">'
                            + '<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="'+element["Canvas.Left"]+'" To="'+x+'" Storyboard.TargetName="'+id+'" Duration="0:0:0.5"></DoubleAnimation>'
                            + '<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" From="'+element["Canvas.Top"]+'" To="'+y+'" Storyboard.TargetName="'+id+'" Duration="0:0:0.5" ></DoubleAnimation>'
                            + '</Storyboard>';

        storyboard = this.control.getHost().content.createFromXaml(xamlString);
        element.resources.add(storyboard);
        storyboard.Begin();
        
        if (z){
            element["Canvas.ZIndex"] = z;
        }
	}
	
	
	
	SLWrapper.prototype.getBounds = function (id){
	    id = "" + id;
	    
	    var w, h;
	    
	    // the base element should return the size of the full screen, but doesn't
	    if (id == "base"){
	        
	        w = this.control.getHost().content.ActualWidth;
	        h = this.control.getHost().content.ActualHeight;
	        
	        return { Width:w, Height:h };
	    }
	    
        var element = this.control.getHost().content.findName(id);
        
        var bounds = this.getElementBounds(element);
        if (element != null){
            bounds.Y = element["Canvas.Top"];
            bounds.X = element["Canvas.Left"];
        }else{
            bounds.Y = 0;
            bounds.X = 0;
        }
        
        return bounds;
	}
	
	SLWrapper.prototype.getElementBounds = function (element){
	    var width = 0;
        var height = 0;
	    
	    if (element != null){
            width = element.Width;
            height = element.Height;
            
            //var top = element["Canvas.Top"];
            //var left = element["Canvas.Left"];
            
            var bounds;
            if (element.toString() == "Canvas"){
                if (element.children && element.children.count > 0){
                    var top = 0;
                    var left = 0;
                    var right = 0;
                    var bottom = 0;
                    var childElement;
                    for (var i = 0; i  < element.children.count; i++){
                        childElement = element.children.GetItem(i);
                        bounds = this.getElementBounds(childElement);
                        top = Math.min(top, childElement["Canvas.Top"]);
                        left = Math.min(left, childElement["Canvas.Left"]);
                        right = Math.max(right, childElement["Canvas.Left"] + bounds.Width);
                        bottom = Math.max(bottom, childElement["Canvas.Top"] + bounds.Height);
                        
                        width = right - left;
                        height = bottom - top;
                        
                    }
                }
            }
        }
        return { Width:width, Height:height };
	}
	
	
	SLWrapper.prototype.mediaPlay = function(id, source){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    
	    var token = media.AddEventListener("MediaOpened", Silverlight.createDelegate(this, this.onMediaLoaded));
	    
	    var ext = media.Source.substr(media.Source.lastIndexOf("."));
	    
	    if (ext == "")
	        ext = ".wmv";
	    
	    var path = source + ext;
	    if (source && media.Source != path){
	        media.Source = path;
	    }
	    media.play();
	}
	
	
	SLWrapper.prototype.onMediaLoaded = function(element) {
	    eval(this.id + "_onMediaLoaded('" + element.GetParent().Name + "')");

	}



	SLWrapper.prototype.enableMediaPlayOnLoad = function(id) {
	    id = "" + id;
	    var element = this.control.getHost().content.findName(id);

	    var media = element.findName(id + "___media");

	    var token = media.AddEventListener("MediaOpened", Silverlight.createDelegate(this, this.OnMediaPlayReady));
	}

	SLWrapper.prototype.OnMediaPlayReady = function(sender) {
	    sender.play();
	}
	
	
	SLWrapper.prototype.mediaStop = function(id){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    media.stop();
	}
	
	SLWrapper.prototype.mediaPause = function(id){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    if (media.CanPause)
	        media.pause();
	}
	
	SLWrapper.prototype.mediaMute = function(id, mute){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    media.IsMuted = mute;
	}
	
	SLWrapper.prototype.getMediaPosition = function(id){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    
	    return media.Position.Seconds;
	}
	
	SLWrapper.prototype.getMediaPercentage = function(id){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    
	    var position = media.Position.Seconds;
	    var total = media.NaturalDuration.Seconds;
	    
	    return position / total;
	}
	
	SLWrapper.prototype.setMediaPercentage = function(id, percentage){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    
	    var position = percentage * media.NaturalDuration.Seconds;
	    var p = position;
	    if (typeof(position) == "number"){
	        var s = "";
	        s += Math.floor(position%60);
	        var i = s.indexOf(".");
	        if (s.substr(i).length == 1)
	            s = "0" + s;
	            
	        position = Math.floor(position / 60)%60;
	        var m = "";
	        m += position%60;
	        if (m.length == 1)
	            m = "0" + m;
	            
	        position = Math.floor(position / 60);
	        var h = "";
	        h += position;
	        if (h.length == 1)
	            h = "0" + h;
	            
	        p = h+":"+m+":"+s;

	    }
	    if (media.CanSeek)
	        media.Position = p;
	}
	
	SLWrapper.prototype.setMediaPosition = function(id, position){
        id = "" + id;
	    var element = this.control.getHost().content.findName(id);
	    
	    var media = element.findName(id +"___media");
	    
	    var p = position;
	    if (typeof(position) == "number"){
	        var s = "";
	        s += position%60;
	        if (s.length == 1)
	            s = "0" + s;
	            
	        position = Math.floor(position / 60);
	        var m = "";
	        m += position%60;
	        if (m.length == 1)
	            m = "0" + m;
	            
	        position = Math.floor(position / 60);
	        var h = "";
	        h += position;
	        if (h.length == 1)
	            h = "0" + h;
	            
	        p = h+":"+m+":"+s;

	    }
	    if (media.CanSeek)
	        media.Position = p;
	}

    SLWrapper.prototype.loadXML = function (xmlContent){
        this.control.children.clear();  
        var xamlObject = this.control.getHost().content.createFromXaml(xmlContent)
        this.control.children.add(xamlObject);
        
        eval(this.id + "_onLoad")(this.control.getHost());
    }
    
    SLWrapper.prototype.bringToFront = function (id){
        id = "" + id;
    
    }
    
    SLWrapper.prototype.toString = function (){
        return this.id + "_wrapper";
    
    }
    
    function S4() {
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }

    function generateGuid(){
            return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()).toUpperCase();
    }
