﻿function displayWindow()
{
    this.item = this.control.content.findName(this.name);
    this.openAnim = this.control.content.findName("open_CM"+this.id);
    this.closeAnim = this.control.content.findName("close_CM"+this.id);
    this.title = this.control.content.findName("title_CM"+this.id);
    this.body = this.control.content.findName("body_CM"+this.id);
    this.quote = this.control.content.findName("quote_CM"+this.id);
    this.reference = this.control.content.findName("reference_CM"+this.id);
    this.overviewTabs = this.control.content.findName("overviewTabs_CM"+this.id);
    this.overviewHit = this.control.content.findName("overviewHit_CM"+this.id);
    this.videoTabs = this.control.content.findName("videoTabs_CM"+this.id);
    this.videoHit = this.control.content.findName("videoHit_CM"+this.id);
    this.viewStudy = this.control.content.findName("viewStudy_CM"+this.id);
    this.content = this.control.content.findName("content_CM"+this.id);
    this.videoHolder = this.control.content.findName("videoHolder");
    this.video = this.control.content.findName("video");
    this.trackBack = this.control.content.findName("trackBack");
    this.bar = this.control.content.findName("bar");
    this.handle = this.control.content.findName("handle");
    this.playIcon = this.control.content.findName("playIcon");
    this.pauseIcon = this.control.content.findName("pauseIcon");
    this.preloader = this.control.content.findName("preloader");
    this.VideoLoaded = false;
    this.link = null;
    this.firstLoaded = true;
    
    
    this.item.Visibility = "Collapsed";
    this._parent = this.params;
    
    this.open = function()
    {
        this.item.Visibility = "Visible";
        this.openAnim.Begin();
    }
    
    this.close = function(sender, args)
    {
        if(!this._parent.selecting)
        {
            this._parent.closing = true;
            this.video.Stop();
            this.videoHolder.Visibility = "Collapsed";
            this.closeAnim.Begin();
            this._parent.spinning = true;
            this._parent.arrowHolder.Visibility = "Collapsed";
            this._parent.start();
        }
    }
    
    this.closeComplete = function(sender, args)
    {
        this.item.Visibility = "Collapsed";
        this._parent.closing = false;

    }
    
    this.openComplete = function(sender, args)
    {   
        if(this.firstLoaded)
        {
            this.viewVideo();
        }

    }
    
    this.setDisplay = function(title, body, quote, reference, link, video, firstView)
    {   
        this.title.Text = title;
        this.body.Text = body;
        this.firstLoaded = firstView;
        if(quote != null) 
        {   
            this.quote["Canvas.Top"]= this.body.ActualHeight+this.body["Canvas.Top"]+4;
            this.quote.Text = quote;
        }
        else
        {
            this.quote.Text ="";
        }
        if(reference != null)
        {
            this.reference["Canvas.Top"]= this.quote.ActualHeight+this.quote["Canvas.Top"]+4;
            this.reference.Text = reference;
            this.reference["Canvas.Left"]= 320-this.reference.ActualWidth;
            
        }
        else
        {
            this.reference.Text ="";
        }
        this.VideoLoaded = false;
        this.video.Stop();
        if(video != null)
        {   
            this.video.Source = video;
            this.preloader.Visibility = "Visible";
            this.resetVideo();
            if(this.firstLoaded)
            {
                this.overviewTabs.Visibility = "Collapsed";
                this.videoTabs.Visibility = "Visible";
                this.content.Visibility = "Collapsed";
            }
            else
            {
                this.content.Visibility = "Visible";
                this.videoTabs.Visibility = "Collapsed";
                this.overviewTabs.Visibility = "Visible";
                this.videoHolder.Visibility = "Collapsed";
            }
            
        }
        else
        {
            this.overviewTabs.Visibility = "Collapsed";
            this.content.Visibility = "Visible";
            this.videoTabs.Visibility = "Collapsed";
            this.preloader.Visibility = "Collapsed";
            this.videoHolder.Visibility = "Collapsed";
        }
        if(link != null)
        {
            this.viewStudy.Visibility = "Visible";
            this.link = link;
        }
        else
        {
            this.viewStudy.Visibility = "Collapsed";
            this.link = null;
        }
        
    }
    
    this.resetVideo = function()
    {
        this.handle["Canvas.Left"] = this.trackBack["Canvas.Left"];
        this.bar.Width = this.handle.Width/2;
        this.pauseIcon.Visibility = "Visible";
        this.playIcon.Visibility = "Collapsed";
    }
    
    this.videoOpened = function(sender, args)
    {
        this.VideoLoaded = true;
        this.preloader.Visibility = "Collapsed";
        if(this.videoTabs.Visibility == "Visible")
        {
            this.video.Play();
            this._parent.spinInterval = setInterval(preserveScope(this._parent.videoPlayer, this._parent.videoPlayer.track), 10);
        }
    }
    
    this.viewVideo = function(sender, args)
    {
        this.overviewTabs.Visibility = "Collapsed"; 
        this.videoTabs.Visibility = "Visible";
        this.videoHolder.Visibility = "Visible";
        this.content.Visibility = "Collapsed";
        if(this.VideoLoaded)
        {
            if(this.pauseIcon.Visibility == "Visible")
            {
                this.video.Play();
            }
            this._parent.spinInterval = setInterval(preserveScope(this._parent.videoPlayer, this._parent.videoPlayer.track), 10);
        }
    }
    
    this.viewOverview = function(sender, args)
    {   
        this.overviewTabs.Visibility = "Visible"; 
        this.videoTabs.Visibility = "Collapsed";
        this.videoHolder.Visibility = "Collapsed";
        this.content.Visibility = "Visible";
        if(this.VideoLoaded)
        {
            this.video.Pause();
        }
    }
    
    this.openLink = function(sender, args)
    {
        window.open(this.link, "_top");
    }
    
    this.closeBTN = this.control.content.findName("closeBTN_CM"+this.id);
    this.closeBTN.cursor = "Hand";
    this.closeBTN.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.close));
    this.closeAnim.addEventListener("Completed", Silverlight.createDelegate(this, this.closeComplete));
    this.openAnim.addEventListener("Completed", Silverlight.createDelegate(this, this.openComplete));
    
    this.overviewHit.cursor = "Hand";
    this.overviewHit.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.viewVideo));
    
    this.videoHit.cursor = "Hand";
    this.videoHit.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.viewOverview));
    
    this.viewStudy.cursor = "Hand";
    this.viewStudy.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.openLink));
    
        
    this.video.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.videoOpened));
    this._parent.addWindow(this);
}