Ext.onReady(function() {
    if (!Ext.isIE) {
        return true;
    }

    var Pos = Ext.get('TzVideoAnchor').getXY();
    var V = Ext.get('TzVideo');

    V.setXY(Pos);
});

Tz = new function() { }

Tz.Demo = new function() {
    this.open = function() {
        Ext.get('TzCompare').hide();
        Tz.Compare.close();
        Ext.get('TzVideo').setSize(732, 412, true);
    }

    this.close = function() {
        Ext.get('TzVideo').setSize(176, 125, true);
        Ext.get('TzCompare').show();
    }
}

Tz.Compare = new function() {
    this.Extended = false;

    this.objComp = Ext.get('TzCompare');
    if (!this.objComp) {
        return;
    }

    Ext.Ajax.method = 'POST';
    Ext.Ajax.url    = '/compare_xhh.php';

    var imgCorner  = Ext.get('TzCompBoxHeadCorner');
    var imgHead    = this.objComp.select('img', true).first();

    this.height = (Ext.isIE ? -1 : 0);
    this.offset = (Ext.isGecko ? -1 : (Ext.isSafari ? 1 : 0));

    this.winContain = Ext.get('TzCompBox');
    this.winContent = Ext.get('TzCompBoxContent');
    this.boxContent = Ext.get('TzCompContain');
    this.tbl        = this.winContent.select('table', true).first();
    this.tblBody    = this.tbl.select('tbody', true).first();
    this.NotFound   = Ext.get('TzPartNotFound');

    this.NotFound.setVisibilityMode(Ext.Element.DISPLAY);
    this.tbl.setVisibilityMode(Ext.Element.DISPLAY);

    this.inpMan  = Ext.get('comp_man');
    this.inpTool = Ext.get('comp_part');

    this.run = function(e) {
        Tz.Compare.clearTable();
        Ext.Ajax.request({
            params: {action: 'compare', _competitor_id: Tz.Compare.inpMan.dom.value, search: Tz.Compare.inpTool.getValue()}
          , success: Tz.Compare.update
          , failure: Tz.Compare.ohnoes
        });
        Tz.Compare.Extended = true;
    }

    this.update = function(result, request) {
        var strClass    = 'odd';
        var blnCompDone = false;
        var objResult   = Ext.util.JSON.decode('(' + result.responseText + ')');
        var tools = objResult.tools;
        if (tools.length == 0) {
            Tz.Compare.NotFound.show();
        } else {
            Tz.Compare.tbl.show();
            for (var id in tools) {
               var type   = tools[id]['type'].charAt(0).toUpperCase() + tools[id]['type'].substring(1);

               if (!blnCompDone) {
                   strClass = (strClass == 'odd' ? 'even' : 'odd');
                   Tz.Compare.createRow([Tz.Compare.inpMan.dom.options[Tz.Compare.inpMan.dom.selectedIndex].text, request.params.search, ''], '', strClass);
                   blnCompDone = true;
               }

               strClass = (strClass == 'odd' ? 'even' : 'odd');
               Tz.Compare.createRow([type, tools[id]['code'], tools[id]['description']], tools[id]['url'], strClass);
               if (type == 'Knipex') {
                   var Row  = Tz.Compare.tblBody.dom.insertRow(-1);
                   var Cell = Row.insertCell(-1);
                   Cell.colSpan = 3;

                   Cell.className = 'TzImportant';
                   Cell.appendChild(document.createTextNode('Knipex is only available through Wera Tools in Canada.'));
               }
            }
        }

        Tz.Compare.winContent.dom.style.height = '';
        if (Tz.Compare.objComp.getHeight() > Tz.Compare.winContain.getHeight()) {
            Tz.Compare.winContent.setHeight(Tz.Compare.boxContent.getHeight() - Tz.Compare.height);
        } else {
            Tz.Compare.boxContent.setHeight(Tz.Compare.winContent.getHeight() - Tz.Compare.height, true);
        }
        Tz.Compare.winContain.slideIn('l');
    }

    this.createRow = function(arrCells, strLink, strClass) {
        if (strLink != '') {
            var objLink  = document.createElement('a');
            objLink.href = strLink;
        }
        var Row = Tz.Compare.tblBody.dom.insertRow(-1);
        Row.className = strClass;
        for (var i = 0, intLen = arrCells.length; i < intLen; i++) {
            var Cell = Row.insertCell(-1);
            if (objLink) {
                var cLink = objLink.cloneNode(true);
                cLink.onclick = function() { MM_openBrWindow(this.href, 'weralineup', 'resizable=yes,width=500,height=750'); return false; };

                cLink.appendChild(document.createTextNode(arrCells[i]));
                Cell.appendChild(cLink);
            } else {
                Cell.appendChild(document.createTextNode(arrCells[i]));
            }
        }
    }

    this.ohnoes = function() {
        alert('Unable to complete your request');
    }

    this.clearTable = function() {
        Tz.Compare.tbl.hide();
        Tz.Compare.NotFound.hide();

        for (var i = 0, intLen = Tz.Compare.tblBody.dom.rows.length; i < intLen; i++) {
            Tz.Compare.tblBody.dom.deleteRow(0);
        }
    }

    this.close = function() {
        if (!Tz.Compare.Extended) {
            return false;
        }

        Tz.Compare.winContain.slideOut('l');
        setTimeout(function() {
            if (!Ext.isGecko) {
                Tz.Compare.winContain.hide();
            }
        }, 1000);

        Tz.Compare.Extended = false;
    }

    this.winContain.alignTo(imgHead, 'tl', [imgHead.getWidth() - imgCorner.getWidth() + 2, this.offset]);
    this.winContain.on('show', function() { alert('hi'); });

    var objClose   = Ext.get('TzCompBoxClose');
    objClose.dom.onclick = function() { return false; };
    objClose.on('click', this.close);

    Ext.get('compare').dom.onclick = function() { return false; };
    Ext.get('compare').on('click', this.run);

// causing bugs in FF    Ext.get('comp_part').on('keydown', this.run);

    this.winContain.hide();
}
