Ext.define('TextClass15', {
    extend: 'DisplayableAction',
    constructor: function (cfgXML, o, actionSequence) {
        this.TextShadowBox = null;
        this.TextBox = null;
        this.TextBlock = null;
        this.TextBlockShadow = null;
        this.Text = "";
        this.TextX = 0;
        this.TextY = 0;
        this.TextPosition = AuthorAlignment.MiddleCenter;
        this.FontColor = '#000000';
        this.FontFamily = 'Arial';
        this.AutoRefresh = false;
        this.FontSize = 14;
        this.FontWeight = 'Normal';
        this.FontStyle = 'Normal';
        this.TextDecoration = 'none';
        this.FontHasShadow = false;
        this.FontShadowColor = '#000000';
        this.FontShadowX = 0;
        this.FontShadowY = 0;
        this.FontShadowBlurRadius = 0;
        this.PictureBox = null;
        this.IsPictureVisible = false;
        this.PictureFileName = null;
        this.PictureWidth = 0;
        this.PictureHeight = 0;
        this.PictureX = 0;
        this.PictureY = 0;
        this.PicturePosition = AuthorAlignment.MiddleCenter;
        this.callParent([cfgXML, o, actionSequence]);
        //start self
        this.Text = XmlHelper.getString(cfgXML, "text");
        this.TextX = XmlHelper.getInteger(cfgXML, "textX");
        this.TextY = XmlHelper.getInteger(cfgXML, "textY");
        this.TextPosition = XmlHelper.getInteger(cfgXML, "textPosition");
        this.FontFamily = XmlHelper.getString(cfgXML, "fontFamily");
        this.LineHeight = XmlHelper.getString(cfgXML, "lineHeight");
        this.FontSize = XmlHelper.getFloat(cfgXML, "fontSize");
        this.TextDecoration = XmlHelper.getString(cfgXML, "textDecoration");
        this.FontStyle = XmlHelper.getString(cfgXML, "fontStyle");
        this.FontWeight = XmlHelper.getString(cfgXML, "fontWeight");
        this.FontColor = XmlHelper.getString(cfgXML, "fontColor");
        this.FontHasShadow = XmlHelper.getBoolean(cfgXML, "fontHasShadow");

        this.FontShadowX = XmlHelper.getInteger(cfgXML, "fontShadowX");
        this.FontShadowY = XmlHelper.getInteger(cfgXML, "fontShadowY");
        this.FontShadowBlurRadius = XmlHelper.getInteger(cfgXML, "fontShadowBlurRadius");
        this.FontShadowColor = XmlHelper.getString(cfgXML, "fontShadowColor");
        this.IsPictureVisible = XmlHelper.getBoolean(cfgXML, "isPictureVisible");
        this.AutoRefresh = XmlHelper.getBoolean(cfgXML, "autoRefresh");
        this.RefreshTimer = null;
        var picFileNameNode = cfgXML.getElementsByTagName("pictureFileName")[0];
        try {
            this.PictureWidth = parseInt(picFileNameNode.getAttribute('width'));
            this.PictureHeight = parseInt(picFileNameNode.getAttribute('height'));
            this.PictureFileName = picFileNameNode.childNodes[0].nodeValue;
        }
        catch (err) {
        }
        this.PictureX = XmlHelper.getInteger(cfgXML, "pictureX");
        this.PictureY = XmlHelper.getInteger(cfgXML, "pictureY");
        this.PicturePosition = XmlHelper.getInteger(cfgXML, "picturePosition");
    },
    getPictureBoxStyle: function () {
        var picStyle = "position:absolute;";
        var xR = this.ContentContainerWidth - this.PictureWidth;
        var vB = this.ContentContainerHeight - this.PictureHeight;
        var xC = xR / 2;
        var vC = vB / 2;
        var left = 0;
        var top = 0;
        switch (this.PicturePosition) {
            case AuthorAlignment.TopLeft:
                left = 0;
                top = 0;
                break;
            case AuthorAlignment.TopCenter:
                left = xC;
                top = 0;
                break;
            case AuthorAlignment.TopRight:
                left = xR;
                top = 0;
                break;
            case AuthorAlignment.MiddleLeft:
                left = 0;
                top = vC;
                break;
            case AuthorAlignment.MiddleCenter:
                left = xC;
                top = vC;
                break;
            case AuthorAlignment.MiddleRight:
                left = xR;
                top = vC;
                break;
            case AuthorAlignment.BottomLeft:
                left = 0;
                top = vB;
                break;
            case AuthorAlignment.BottomCenter:
                left = xC;
                top = vB;
                break;
            case AuthorAlignment.BottomRight:
                left = xR;
                top = vB;
                break;
        }
        picStyle += "left:" + (left + this.PictureX) + "px;top:" + (top + this.PictureY) + "px;width:" + this.PictureWidth + "px;height:" + this.PictureHeight + "px;";
        return picStyle;
    },
    getTextStyle: function () {
        var differ = Math.floor(Math.max(this.FontSize * 1.15 - this.LineHeight, 0));
        var textStyle = "font-family:" + this.FontFamily + ";";
        textStyle = textStyle + "font-size:" + this.FontSize + "px;";
        textStyle = textStyle + "line-height:" + this.LineHeight + "px;";
        textStyle = textStyle + "font-style:" + this.FontStyle + ";";
        textStyle = textStyle + "font-weight:" + this.FontWeight + ";";
        textStyle = textStyle + "text-decoration:" + this.TextDecoration + ";";
        textStyle = textStyle + "color:" + this.FontColor + ";";
        textStyle = textStyle + "overflow:visible;";
        textStyle = textStyle + "padding-bottom:" + differ + "px;";
        textStyle = textStyle + "margin-bottom:" + (-differ) + "px;";

        if (this.FontHasShadow) {
            textStyle += "text-shadow:" + (this.FontShadowX + "px " + this.FontShadowY + "px " + this.FontShadowBlurRadius + "px " + this.FontShadowColor) + ";";
        }
        textStyle += "white-space:pre-wrap;"
        return textStyle;
    },
    getTextShadowStyle: function (tLeft, tTop) {
        var str = "left:" + (tLeft - this.FontShadowBlurRadius + this.FontShadowX) + "px;top:" + (tTop - this.FontShadowBlurRadius + this.FontShadowY) + "px;color:" + this.FontShadowColor;
        str += ";filter: progid:DXImageTransform.Microsoft.Blur(pixelradius='" + this.FontShadowBlurRadius + "')";
        return str;
    },
    getTextBlockPosition: function (tbW, tbH) {
        var textPositionStyle = "left:0px;top:0px";
        var xR = this.CalculatedContentContainerWidth - tbW - 1;
        var vB = this.ContentContainerHeight - tbH;
        
        // Added -1 for 0 origin
        //   Author.log(Ext.String.format("{0}, {1}\n{2}, {3}",tbW,tbH,this.ContentContainerWidth, this.ContentContainerHeight));
        var xC = Math.ceil(xR / 2);
        var vC = Math.ceil(vB / 2);
        var left = 0;
        var top = 0;
        var align = 'left';
        switch (this.TextPosition) {
            case AuthorAlignment.TopLeft:
                left = 0;
                top = 0;
                break;
            case AuthorAlignment.TopCenter:
                left = xC;
                top = 0;
                align = 'center';
                break;
            case AuthorAlignment.TopRight:
                left = xR;
                top = 0;
                align = 'right';
                break;
            case AuthorAlignment.MiddleLeft:
                left = 0;
                top = vC;
                break;
            case AuthorAlignment.MiddleCenter:
                left = xC;
                top = vC;
                align = 'center';
                break;
            case AuthorAlignment.MiddleRight:
                left = xR;
                top = vC;
                align = 'right';
                break;
            case AuthorAlignment.BottomLeft:
                left = 0;
                top = vB;
                break;
            case AuthorAlignment.BottomCenter:
                left = xC;
                top = vB;
                align = 'center';
                break;
            case AuthorAlignment.BottomRight:
                left = xR;
                top = vB;
                align = 'right';
                break;
        }

        textPositionStyle = "left:" + left + "px;top:" + top + "px;text-align:" + align + ";";
       // alert(textPositionStyle)   ;
        return {
            'Left': left,
            'Top': top,
            'TextBlockPositionStyle': textPositionStyle
        };
    },
    doAutoRefresh: function () {
        if (this.TextBlock) {
            var replacedText = ExpressionEngine.evalExpression(ExpressionEngine.replaceVariablesWithValue(this.Text));
            this.TextBlock.setHTML(replacedText);
        }
    },
    initializeDC: function (bg) {
        this.initializeDisplayComponent(bg);
        //initialize the pictureBox first
        if (this.IsPictureVisible) {
            this.PictureBox = this.ContentContainer.createChild({
                tag: 'img',
                id: this.ObjectType + "-" + this.id + "-Picture",
                src: Author.resourcePath + this.PictureFileName,
                style: this.getPictureBoxStyle()
            })
        }
        //then the text
        this.CalculatedContentContainerWidth = this.ContentContainerWidth - this.TextX;//Using OriginalWidth because this method is called every time a text box is displayed so will get a little smaller each time
        
        this.TextBox = this.ContentContainer.createChild({
            tag: 'div',
            id: this.ObjectType + "-" + this.id + "-TextBox",
            style: 'position:absolute;left:' + this.TextX + "px;top:" + this.TextY + "px;width:" + this.CalculatedContentContainerWidth + "px;height:" + this.ContentContainerHeight + "px;"
        });
      //  alert(this.TextX + " " + this.TextY + " " + this.ContentContainerWidth + " " + this.ContentContainerHeight);
        var replacedText = ExpressionEngine.replaceVariablesWithValue(this.Text);
        if(this.AutoRefresh){
            replacedText = ExpressionEngine.evalExpression(replacedText);
        }
        this.TextBlock = this.TextBox.createChild({
            tag: "div",
            id: this.ObjectType + "-" + this.id + "-TextBlock",
            style: "position:absolute;" + this.getTextStyle(),
            html: replacedText
        });
        var tbW = this.TextBlock.getWidth();
        var tbH = this.TextBlock.getHeight();
     //   alert(Ext.browser.version.getMajor()+" " + tbW +" " + tbH);
        if (Ext.isIE && Ext.browser.version.getMajor() == 9) {
            tbW += 1;
            tbH -= 1;
        }
        var textBlockPosition = this.getTextBlockPosition(tbW, tbH);
        this.TextBlock.applyStyles(textBlockPosition.TextBlockPositionStyle);

        //Apply Text Shadow For IE
        if (Ext.isIE && this.FontHasShadow) {
            this.TextBlockShadow = this.TextBlock.createChild({
                tag: "div",
                id: this.ObjectType + "-" + this.id + "-TextBlockShadow",
                style: "position:absolute;" + this.getTextStyle(),
                html: replacedText
            }, this.TextBlock);
            var textShadowStyle = this.getTextShadowStyle(textBlockPosition.Left, textBlockPosition.Top);
            this.TextBlockShadow.applyStyles(textShadowStyle);
            this.TextBlockShadow.applyStyles(textBlockPosition.TextBlockPositionStyle);
        }
    },
    processAction: function (bg) {
        if (this.IsChangeAction) {
            Author.DisplayManager.changeAction(this);
        }
        this.initializeDC(bg);
        this.setInitialVisibility();
        this.applyEffect();
        if (this.AutoRefresh) {
            var that = this;
            this.refreshTimer = setInterval(function () {
                that.doAutoRefresh();
            }, 500);
        }
        return {MoveOn: true, Interval: 0};
    },
    remove: function () {
        if (this.refreshTimer) {
            clearInterval(this.refreshTimer);
        }
        this.callParent([]);
    }
});
