﻿function CanvasSize(){
    this.Width = 0;
    this.Height = 0;
}

CanvasSize.prototype.isValid = function(){
    return ((this.Width != NaN) && (this.Height != NaN) && (this.Width > 0) && (this.Height > 0));
}

function CanvasSizeSelection()
{
    this.CustomSizeOptionControlId = null;
    this.CustomLengthDropDownControlId = null;
    this.CustomWidthDropDownControlId = null;
    this.StandardSizeDropDownControlId = null;
}

CanvasSizeSelection.prototype.Debug = function(msg){
    alert(msg);
}
CanvasSizeSelection.prototype.CustomSizeSelected= function(){
    //try{
        var _CustomSizeSelected = false;
        if (this.CustomSizeOptionControlId != null){
            $('#'+this.CustomSizeOptionControlId).each(function() {if ($(this).is(':checked')) {_CustomSizeSelected = true;}});
        }
        return _CustomSizeSelected;
    //} catch(ex){
        //this.Debug(ex);
    //}
}


CanvasSizeSelection.prototype.getSize = function() {
    var selectedSize = new CanvasSize();

    if (this.CustomSizeSelected()) {
        if ((this.CustomLengthDropDownControlId != null) && (this.CustomWidthDropDownControlId != null)) {
            selectedSize.Width = parseInt($('#' + this.CustomLengthDropDownControlId).val());
            selectedSize.Height = parseInt($('#' + this.CustomWidthDropDownControlId).val());
        }
    } else {
        if (this.StandardSizeDropDownControlId != null) {
            // parse standard size value
            var unparsedDimensions = $('#' + this.StandardSizeDropDownControlId).find('option:selected').text();

            var widthPosStart = 0;
            var widthPosEnd = unparsedDimensions.toLowerCase().indexOf(' x ')
            selectedSize.Width = parseInt(unparsedDimensions.substr(widthPosStart, widthPosEnd));


            if (unparsedDimensions.toLowerCase().indexOf(' x ') > -1) {
                var heightPosStart = widthPosEnd + 3
                var heightAndRemainder = unparsedDimensions.substr(heightPosStart)
                if (heightAndRemainder.indexOf(' ') > -1) {
                    var heightPosEnd = heightAndRemainder.indexOf(' ');
                    selectedSize.Height = parseInt(heightAndRemainder.substr(0, heightPosEnd));
                } else {
                    selectedSize.Height = parseInt(heightAndRemainder);
                }
            }
        }
    }
    return selectedSize;
}




function CanvasSizePreview (){
    this.animate = false;
    this.scale = 0;
    this.maxX = 100;
    this.maxY = 100;
    this.x = 50;
    this.y = 50;
    this.CanvasFrame = null;
}

CanvasSizePreview.prototype.ResizeCanvasFrame = function(newSize) {
    if (!newSize.isValid()) {
        return;
    }
    /**
    x: canvas Length
    y: canvas Width;
    scale: scale size, if 0 will scale to fit the wrapper
    animate: if true will animate the object to new values
    **/

    //case:5637
    x = newSize.Height;
    y = newSize.Width;


    var outX = x;
    var outY = y;



    var marginTop = 0;

    if (this.scale <= 0) {
        var r1 = this.maxX / x;
        var r2 = this.maxY / y;
        var r = Math.min(r1, r2)

        outX = Math.round(r * x);
        outY = Math.round(r * y);
    } else {
        outX = Math.round(x * this.scale);
        outY = Math.round(y * this.scale);
    }




    if (this.scale > 0) {
        // this is not working because the maximum width and height passed for the wall preview are calculated as 0, which is obviously not correct, fix it in size modifier/view.aspx
        marginTop = Math.round(this.maxY - outY) / 2; // 8 is for the two borders on top and bottom
    } else if (outX > outY) {
        marginTop = Math.round(this.maxY - outY) / 2; // 8 is for the two borders on top and bottom
    } else {
        marginTop = 0;
    }

    var marginTopStr = '0px';
    if (marginTop > 0) {
        marginTopStr = marginTop + 'px';
    }

    if (false /*this.animate*/) {
        try {
            this.CanvasFrame.animate({
                width: outX + 'px',
                height: outY + 'px',
                margin: 'auto',
                marginTop: marginTopStr
            }, 1500);

        } catch (ex) {
            // ie doesnt like animations,
            this.CanvasFrame.css('width', outX + 'px');
            this.CanvasFrame.css('height', outY + 'px');
            this.CanvasFrame.css('margin', 'auto');
            this.CanvasFrame.css('margin-top', marginTopStr);
        }
    } else {
        this.CanvasFrame.css('width', outX + 'px');
        this.CanvasFrame.css('height', outY + 'px');
        this.CanvasFrame.css('margin', 'auto');
        this.CanvasFrame.css('margin-top', marginTopStr);
    }
};