﻿// This is just here to tell JS lint that we need to use a global variable
/*global TVI, loadFirebugConsole, document, alert, console, window $ */

/*
    TVI Framework
    Version 1.0
    Copywrite 2009 TVI Design
    Last Updated: 10/11/2009
    
    Contents:
        TVI.Components
*/

TVI.Components = function() {

    /* Private */

    /* Properties */

    var cmp = {};
    var components = [];
    var selector = null;





    /* Methods */

    var init = function() {
    
    };
    
    
    
    
    
    var add = function(params){
    
        /* adds a component to this collection */
                
        //build selector for all components
        if (selector !== null){   
                 
            selector += ', ';     
                   
        }
        else{
            selector = '';
        }
        
        selector += params.selector;
    
        //return the added index
        return components.push(params) - 1;
    
    };
    
    
    
    
    
    var initialize = function(container) {
    
        /* initializes each component */

        if (container === undefined){ container = $('BODY'); }
        
        //call each component's create function
        var componentCount = components.length;
    
        for (var i=0; i<componentCount; i++){
        
            components[i].setup.call(cmp, container);
        
        }
                        
        //loop through elements calling their create function
        container.find(selector).each(function(){
                
            var componentIndex = $(this).data('componentIndex');
            
            if (componentIndex === undefined){ return; }
                                    
            components[componentIndex].create.call(cmp, $(this));
                    
        });
        
        zIndexWorkaround();
                
    };
    
    
    
    
    
    var layout = function(container) {
    
        /* resets the component's layout */
                
        container.find(selector).each(function(){
        
            var componentIndex = $(this).data('componentIndex');
            
            if (componentIndex === undefined){ return; }
                                    
            components[componentIndex].layout.call(cmp, $(this));
        
        });
        
        zIndexWorkaround();
                
    };





    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        /* Methods */
        
        add: function(params){
        
            return add(params);
        
        },
        
        initialize: function(container){
        
            initialize(container);
        
        },
        
        layout: function(container){
        
            layout(container);
        
        }

    });
    

    TVI.ready(init);


    return cmp;

} ();



// zIndexWorkaround() is used to make sure custom drop downs etc. work in IE7
function isIE() {
    if (navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    return false;
}

function zIndexWorkaround() {
    if (isIE()) {
        var zi = 10000; 
        $(".TVI-onTop").each(function(block) {
            $(this).css({ 'zIndex': zi });
            zi--;
        });
    }
}