﻿/*
    TVI Framework
    Version 1.0
    Copywrite 2010 TVI Design
    Last Updated: 19/04/2010
    
    Contents:
        TVI.Components.TextArea
*/

TVI.Components.TextArea = function() {

    /* Private */

    /* Properties */

    var cmp = {};
    var componentIndex = -1;





    /* Methods */

    var init = function() {

        //add to components array
        componentIndex = TVI.Components.add({

            type: 'textArea',
            selector: '.TVI-textArea',
            setup: setup,
            create: create,
            layout: layout

        });

    };





    var setup = function(container) {

        //add data to elements
        container.find('.TVI-textArea').each(function() {
        
            //ignore if already created
            if ($(this).hasClass('TVI-created')){ return; }
            
            $(this).data('componentIndex', componentIndex);

        });

    };





    var create = function(element) {
        
        //ignore if already created
        if (element.hasClass('TVI-created')){ return; }
        
        layout(element);

        // Tell the system it doesn't need to be created again.
        element.addClass('TVI-created');

    };





    var layout = function(element) {
    
        var textAreaEl = element.find('TEXTAREA');
        var wrapperEl = textAreaEl.parent();
        var wrapperElWidth = wrapperEl.width();
        var wrapperElHeight = wrapperEl.height();
        var style = 'defaultStyle';


        // Get a list of the classes used on the main element
        var classes = element.attr('class').split(' ');
        var classesLength = classes.length;

        //loop through classes
        for (i = 0; i < classesLength; i++) {

            // Check to see if we are not using the default style
            if (classes[i].substring(0, 10) == 'TVI-style-') { style = classes[i].substring(10); }

        }

        // Create a shortcut to the button style
        style = cmp.styles[style];


        //calculate width of textbox
        var textAreaWidth = wrapperElWidth - style.leftSize - style.rightSize;
        var textAreaHeight = wrapperElHeight - style.topSize - style.bottomSize;
        
        textAreaEl.width(textAreaWidth);
        textAreaEl.height(textAreaHeight);

    };





    /* Public */

    TVI.apply(cmp, {

        /* Properties */
        
        styles: {
            defaultStyle: {
                topSize: 4,
                rightSize: 6,
                bottomSize: 4,
                leftSize: 6
            }
        },


        /* Methods */

        create: function(element) {

            create(element);

        },

        layout: function(element) {

            layout(element);

        }

    });

    TVI.ready(init);


    return cmp;

} ();
