(function($){
    $.fn.mainmenu = function(options){
    
        var defaults = {
            sLanguage: 'eng',
            //[ eng | esp ]
            sType: 'mainmenu',
            //[ mainmenu | secondarymenu ]
            iSelectedMenuIndex: 0,
            bDisplayServiceDeskLogo: false
        
        };
        
        var options = $.extend(defaults, options);
        
        
        return this.each(function(){
        
            obj = $(this);
            
            var menuoptions = getMenuOptions();
            var strHtml = '';
            if (options.sType == 'mainmenu') {
                strHtml = renderMenuOptions(menuoptions, options);
            }
            else {
            
                strHtml = renderSecondaryMenu(menuoptions[options.iSelectedMenuIndex].subMenus, options);
                /*				for (var i = 0; i < menuoptions.length; i++) {
                
                 
                
                 
                
                 
                
                 strHtml += test('', menuoptions[i]);
                
                 
                
                 
                
                 
                
                 }
                
                 
                
                 
                
                 
                
                 */
                
            }
            
            obj.html(strHtml);
            
        });
    };
    function test(indent, obj){
        var result = '';
        
        if (hasSubMenus(obj)) {
            result += '<br>' + indent + '&nbsp;-&nbsp;' + obj.text;
            for (var i = 0; i < obj.subMenus.length; i++) {
                var subobj = obj.subMenus[i];
                result += '<br>' + test(indent + '&nbsp;&nbsp;', obj.subMenus[i]);
            }
        }
        else {
            result += '<br>' + indent + ' - ' + obj.url;
        }
        
        return result;
    }
    
    function hasSubMenus(menuoption){
        if (menuoption.subMenus != undefined && menuoption.subMenus != null && menuoption.subMenus.length > 0) {
            return true;
        }
        else {
            return false;
        }
    }
    
    function getText(obj, options){
    
        if (options.sLanguage == 'eng') {
            return obj.text;
        }
        else {
            return obj.text_spanish;
        }
    }
    
    function getUrl(obj, options){
        var url = obj.url;
        if (url != undefined) 
            return url;
        else 
            return '';
    }
    
    function isMenuOptionCurrentPage(menuoption){
        var isCurrentPage = (location.pathname.indexOf(menuoption.url) > 0);
        
        if (!isCurrentPage) {
            if (hasSubMenus(menuoption)) {
                for (var i = 0; i < menuoption.subMenus.length; i++) {
                    if (isMenuOptionCurrentPage(menuoption.subMenus[i])) {
                        return true;
                    }
                }
            }
        }
        
        return isCurrentPage;
    }
    function renderSecondaryMenu(submenus, options){
        var result = '';
        var isCurrentPage = false;
        if (submenus[0].type == 1) {
            for (var i = 0; i < submenus.length; i++) {
            
                result += '<table border="0" cellspacing="0" cellpadding="0" >';
                result += ' <caption>' + getText(submenus[i], options) + '</caption>';
                result += '   <thead>';
                result += '     <tr>';
                result += '       <td valign="top" class="sm_tl"><img src="images/spacer.gif" width="4" height="4" /></td>';
                result += '       <td valign="top" class="sm_tm"><img src="images/spacer.gif" width="5" height="4" /></td>';
                result += '       <td valign="top" class="sm_tr"><img src="images/spacer.gif" width="4" height="4" /></td>';
                result += '     </tr>';
                result += '   </thead>';
                result += '   <tbody>';
                if (hasSubMenus(submenus[i])) {
                    for (var x = 0; x < submenus[i].subMenus.length; x++) {
                        isCurrentPage = isMenuOptionCurrentPage(submenus[i].subMenus[x]);
                        
                        result += '     <tr>';
                        result += '       <td valign="top" class="sm_ml"><img src="images/spacer.gif"  width="4" height="5" /></td>';
                        if (isCurrentPage) {
                            result += '       <td valign="top" class="sm_bc sm_sub_lnk_hover"' + (x == submenus[i].subMenus.length - 1 ? ' style="border-bottom:none"' : '') + '><div>' + getText(submenus[i].subMenus[x], options) + '</div>';
                        }
                        else {
                            result += '       <td valign="top" class="sm_bc sm_sub_lnk" onmouseover="this.className=\'sm_bc sm_sub_lnk_hover\'" onmouseout="this.className=\'sm_bc sm_sub_lnk\'" ' + (!hasSubMenus(submenus[i].subMenus[x]) ? 'onclick="goTo(\'' + getUrl(submenus[i].subMenus[x], options) + '\')"' : '') + (x == submenus[i].subMenus.length - 1 ? ' style="border-bottom:none"' : '') + '><div>' + getText(submenus[i].subMenus[x], options) + '</div>';
                        }
                        if (hasSubMenus(submenus[i].subMenus[x])) {
                            result += '<dl>';
                            for (var y = 0; y < submenus[i].subMenus[x].subMenus.length; y++) {
                                isCurrentPage = isMenuOptionCurrentPage(submenus[i].subMenus[x].subMenus[y]);
                                if (isCurrentPage) {
                                    result += '<dd class="hover">' + getText(submenus[i].subMenus[x].subMenus[y], options) + '</dd>';
                                }
                                else {
                                    result += '<dd onmouseover="this.className=\'hover\'" onmouseout="this.className=\'\'" onclick="goTo(\'' + getUrl(submenus[i].subMenus[x].subMenus[y], options) + '\')">' + getText(submenus[i].subMenus[x].subMenus[y], options) + '</dd>';
                                }
                            }
                            result += '</dl>';
                        }
                        result += '          </td>';
                        result += '       <td valign="top" class="sm_mr"><img src="images/spacer.gif" width="4" height="5" /></td>';
                        result += '     </tr>';
                        
                    }
                    
                }
                //<dl><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('financial_services.html')">Financial Services Applications</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('hr_solutions.html')">Human Resources Management Solutions</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('card_processing.html')">Card Products Processing</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('voip.html')">VoIP Telephony</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('it_management.html')">Management Information Systems</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('ebt.html')">Electronic Benefit Transfer (EBT)</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('call_center_services.html')">Call Center</dd><dd onmouseover="this.className='hover'" onmouseout="this.className=''" onclick="goTo('electronic_ticketing.html')">Ticketpop</dd></dl>
                result += '   </tbody>';
                result += '   <tfoot>';
                result += '     <tr>';
                result += '       <td valign="top" class="sm_bl"><img src="images/spacer.gif" width="4" height="5" /></td>';
                result += '       <td valign="top" class="sm_bm"><img src="images/spacer.gif" width="5" height="5" /></td>';
                result += '       <td valign="top" class="sm_br"><img src="images/spacer.gif" width="4" height="5" /></td>';
                result += '   </tr></tfoot>';
                result += ' </table>';
                result += ' <div class="st"></div>';
                
            }
        }
        else {
        
            result += '<table border="0" cellspacing="0" cellpadding="0" >';
            result += '   <thead>';
            result += '     <tr>';
            result += '       <td valign="top" class="sm_tl"><img src="images/spacer.gif" width="4" height="4" /></td>';
            result += '       <td valign="top" class="sm_tm"><img src="images/spacer.gif" width="5" height="4" /></td>';
            result += '       <td valign="top" class="sm_tr"><img src="images/spacer.gif" width="4" height="4" /></td>';
            result += '     </tr>';
            result += '   </thead>';
            result += '   <tbody>';
            
            for (var j = 0; j < submenus.length; j++) {
            
                if (isMenuOptionCurrentPage(submenus[j])) {
                    result += '     <tr>';
                    result += '       <td valign="top" class="sm_ml"><img src="images/spacer.gif"  width="4" height="5" /></td>';
                    result += '       <td valign="top" class="sm_bc sm_sub_lnk_hover"' + (j == submenus.length - 1 ? ' style="border-bottom:none"' : '') + '><div>' + getText(submenus[j], options) + '</div></td>';
                    result += '       <td valign="top" class="sm_mr"><img src="images/spacer.gif" width="4" height="5" /></td>';
                    result += '     </tr>';
                }
                else {
                    result += '     <tr>';
                    result += '       <td valign="top" class="sm_ml"><img src="images/spacer.gif"  width="4" height="5" /></td>';
                    result += '       <td valign="top" class="sm_bc sm_sub_lnk" onmouseover="this.className=\'sm_bc sm_sub_lnk_hover\'" onmouseout="this.className=\'sm_bc sm_sub_lnk\'" onclick="goTo(\'' + getUrl(submenus[j], options) + '\')"' + (j == submenus.length - 1 ? ' style="border-bottom:none"' : '') + '><div>' + getText(submenus[j], options) + '</div></td>';
                    result += '       <td valign="top" class="sm_mr"><img src="images/spacer.gif" width="4" height="5" /></td>';
                    result += '     </tr>';
                }
            }
            
            
            result += '   </tbody>';
            result += '   <tfoot>';
            result += '     <tr>';
            result += '       <td valign="top" class="sm_bl"><img src="images/spacer.gif" width="4" height="5" /></td>';
            result += '       <td valign="top" class="sm_bm"><img src="images/spacer.gif" width="5" height="5" /></td>';
            result += '       <td valign="top" class="sm_br"><img src="images/spacer.gif" width="4" height="5" /></td>';
            result += '   </tr></tfoot>';
            result += ' </table>';
            result += ' <div class="st"></div>';
        }
        result += '<div class="banners"><a href="http://www.ath.com" target="_blank"><img src="' + (options.sLanguage == 'esp' ? '../' : '') + 'images/banner.jpg" border="0" class="st" title="www.ath.com" alt="www.ath.com" /></a>';
        if (options.bDisplayServiceDeskLogo == true) {
            result += '<img src="' + (options.sLanguage == 'esp' ? '../' : '') + 'images/logo_service_desk.gif" alt="Service Desk" width="184" height="58" class="st" style="margin-top:25px" />';
        }
        result += '</div>';
        
        return result;
    }
    function renderMenuOptions(menuOptions, options){
    
        var result = '';
        result += '<div id="main_menu"><div id="tab_container">';
        result += '<div class="r fr"></div>';
        result += '<div class="l fl"></div>';
        for (var i = 0; i < menuOptions.length; i++) {
        
        
            result += '<!-- Link -->';
            if (i == options.iSelectedMenuIndex) {
                result += '<div id="menu_' + (i + 1) + '" class="tab" ' + (menuOptions[i].url != '' ? 'onclick="goTo(\'' + getUrl(menuOptions[i], options) + '\')"' : '') + '>';
            }
            else {
                result += '<div id="menu_' + (i + 1) + '" class="lnk cursor no_cursor" onmouseover="this.className=\'tab cursor\';showSub(this.id)" onmouseout="this.className=\'lnk cursor\';hideSub(this.id)" ' + (menuOptions[i].url != '' ? 'onclick="goTo(\'' + getUrl(menuOptions[i], options) + '\')"' : '') + '>';
            }
            result += '  <div class="l fl"></div>';
            result += '  <div class="label fl">' + getText(menuOptions[i], options) + '</div>';
            result += '  <div class="r fl"></div>';
            if (i != options.iSelectedMenuIndex && hasSubMenus(menuOptions[i])) {
            
                result += renderSubMenus(menuOptions[i].subMenus, i, options);
                
            }
            result += '</div>';
            result += '<!-- End Link -->';
            if (i < menuOptions.length - 1) {
                result += '<div class="divider"></div>';
            }
            
        }
        result += '</div></div>';
        
        return result;
    }
    function renderSubMenus(submenus, menuIndex, options){
    
        var result = '';
        result += '  <!--SUBMENU-->';
        result += '<div id="menu_' + (menuIndex + 1) + '_sub" class="submenu" style="display:none">';
        result += '            <table border="0" cellspacing="0" cellpadding="0"  style="width:260px">';
        result += '              <thead>';
        result += '                <tr>';
        result += '                  <td valign="top" class="bc" style="width:6px;height:4px"><img src="images/spacer.gif" width="6" height="4" /></td>';
        result += '                  <td valign="top" class="bc" style="width:13px;height:4px;width:100%"><img src="images/spacer.gif" width="13" height="4" /></td>';
        result += '                  <td valign="top" class="tr" style="width:13px;height:4px"><img src="images/spacer.gif" width="13" height="4" /></td>';
        result += '                </tr>';
        result += '              </thead>';
        result += '              <tbody>';
        result += renderSubMenuItem(submenus, options);
        result += '              </tbody>';
        result += '              <tfoot>';
        result += '                <tr>';
        result += '                  <td valign="top" class="bl" style="width:6px;height:13px"><img src="images/spacer.gif" width="6" height="13" /></td>';
        result += '                  <td valign="top" class="bm" style="width:6px;height:13px"><img src="images/spacer.gif" width="6" height="13" /></td>';
        result += '                  <td valign="top" class="br" style="width:13px;height:13px"><img src="images/spacer.gif" width="13" height="13" /></td>';
        result += '              </tr></tfoot>';
        result += '            </table>';
        result += '          </div>';
        result += '  <!--END SUBMENU-->';
        return result;
    }
    function renderSubMenuItem(submenus, options){
    
        var result = '';
        var item = null;
        var subitem = null;
        var subsubitem = null;
        for (var x = 0; x < submenus.length; x++) {
            item = submenus[x];
            if (item.type == 1) { //Section Item
                result += '                <tr>';
                result += '                  <td valign="top" class="bc"><img src="images/spacer.gif"  width="6" height="4" /></td>';
                result += '                  <td valign="top" class="bc" style="border-bottom:none;width:100%;padding:12px 0px 6px 12px"><div><b>' + getText(item, options) + '</b></div></td>';
                result += '                  <td valign="top" class="mc"><img src="images/spacer.gif" width="13" height="4" /></td>';
                result += '                </tr>';
                var hassubmenu = hasSubMenus(item);
                if (hassubmenu) {
                    for (var i = 0; i < item.subMenus.length; i++) {						
                        subitem = item.subMenus[i];
						hassubmenu = hasSubMenus(subitem);
                        result += '                <tr>';
                        result += '                  <td valign="top" class="bc"><img src="images/spacer.gif"  width="6" height="4" /></td>';
                        result += '                  <td valign="top" class="bc sub_lnk" onmouseover="this.className=\'bc sub_lnk_hover\'" onmouseout="this.className=\'bc sub_lnk\'" ' + (!hassubmenu ? 'onclick="goTo(\'' + getUrl(subitem, options) + '\')" ' : '') + (x == submenus.length - 1 && i == item.subMenus.length - 1 ? 'style="border-bottom:none;"' : '') + '><div>' + getText(subitem, options) + '</div>';
                        if (hassubmenu) {
                            result += '<div style="font-size:100%;padding-left:2px" class="st">';
                            for (var y = 0; y < subitem.subMenus.length; y++) {
                                subsubitem = subitem.subMenus[y];
                                result += '<div onclick="goTo(\'' + getUrl(subsubitem, options) + '\')" onmouseover="this.className=\'white\'" onmouseout="this.className=\'\'" >- ' + getText(subsubitem, options) + '</div>';
                            }
                            result += '</div>';
                        }
                        result += '</td>';
                        result += '                  <td valign="top" class="mc"><img src="images/spacer.gif" width="13" height="4" /></td>';
                        result += '                </tr>';
                    }
                }
                
            }
            else 
                if (item.type == 2) { //SubMenu Item
                    result += '                <tr>';
                    result += '                  <td valign="top" class="bc"><img src="images/spacer.gif"  width="6" height="4" /></td>';
                    result += '                  <td valign="top" class="bc sub_lnk" onmouseover="this.className=\'bc sub_lnk_hover\'" onmouseout="this.className=\'bc sub_lnk\'" onclick="goTo(\'' + getUrl(item, options) + '\')" ' + (x == submenus.length - 1 ? 'style="border-bottom:none;"' : '') + '><div>' + getText(item, options) + '</div>';
                    result += '</td>';
                    result += '                  <td valign="top" class="mc"><img src="images/spacer.gif" width="13" height="4" /></td>';
                    result += '                </tr>';
                }
            
        };
        
        return result;
        
        
    }
    function getMenuOptions(){
        return [{
            'text': 'Products &amp; Services',
            'text_spanish': 'Productos y Servicios',
            'url': '',
            'type': 0,
            'subMenus': [{
                'text': 'Transaction Processing',
                'text_spanish': 'Procesamiento de Transacciones',
                'url': '',
                'type': 1,
                'subMenus': [{
                    'text': 'ATH<span class="reg">&reg;</span> Network',
                    'text_spanish': 'Red ATH<span class="reg">&reg;</span>',
                    'url': 'ath_pos.html',
                    'type': 2
                }, {
                    'text': 'Payment Processing',
                    'text_spanish': 'Procesamiento de Pagos',
                    'url': 'payment_processing.html',
                    'type': 2
                }, {
                    'text': 'Card Products Processing',
                    'text_spanish': 'Procesamiento de Productos de Tarjetas',
                    'url': 'card_processing.html',
                    'type': 2
                }, {
                        'text': 'Electronic Benefit Transfer (EBT)',
                        'text_spanish': 'Sistema de Distribuci&oacute;n de Beneficios (EBT)',
                        'url': 'ebt.html',
                        'type': 2
                    }]
            }, {
                'text': 'Business Solutions',
                'text_spanish': 'Soluciones de Negocio',
                'url': '',
                'type': 1,
                'subMenus': [{
                    'text': 'IT Consulting',
                    'text_spanish': 'Servicios Profesionales de TI',
                    'url': 'it_professional_services.html',
                    'type': 2
                }, {
                    'text': 'Network Services',
                    'text_spanish': 'Servicios de Redes',
                    'url': '',
                    'type': 2,
                    subMenus: [{
                        'text': 'Network Services',
                        'text_spanish': 'Servicios de Redes',
                        'url': 'network_services.html',
                        'type': 2
                    }, {
                        'text': 'VoIP Telephony',
                        'text_spanish': 'Telefon&iacute;a de Voz Sobre Protocolo de Internet (VoIP)',
                        'url': 'voip.html',
                        'type': 2
                    }, {
                        'text': 'Call Center',
                        'text_spanish': 'Manejo de Centros de Contacto',
                        'url': 'call_center_services.html',
                        'type': 2
                    }]
                }, {
                    'text': 'Printing &amp; Distribution',
                    'text_spanish': 'Impresi&oacute;n y Distribuci&oacute;n',
                    'url': 'printing_distribution.html',
                    'type': 2
                }, {
                    'text': 'Cash Processing',
                    'text_spanish': 'Procesamiento de Efectivo',
                    'url': 'cash_processing.html',
                    'type': 2
                }, {
                    'text': 'Item Processing',
                    'text_spanish': 'Procesamiento de Efectos',
                    'url': 'item_processing.html',
                    'type': 2
                }, {
                    'text': 'Business Processes Outsourcing',
                    'text_spanish': 'Outsourcing de Procesos Operacionales',
                    'url': '',
                    'type': 2,
                    'subMenus': [{
                        'text': 'Business Recovery &amp; Continuity',
                        'text_spanish': 'Resguardo y Continuidad de Negocio',
                        'url': 'business_continuity.html',
                        'type': 2
                    }, {
                        'text': 'Financial Services Applications',
                        'text_spanish': 'Aplicaciones para Servicios Financieros',
                        'url': 'financial_services.html',
                        'type': 2
                    }, {
                        'text': 'Human Resources Management Solutions',
                        'text_spanish': 'Soluciones para el Manejo de Recursos Humanos',
                        'url': 'hr_solutions.html',
                        'type': 2
                    }, {
                        'text': 'Management Information Systems',
                        'text_spanish': 'Manejo de Sistemas de Informaci&oacute;n',
                        'url': 'it_management.html',
                        'type': 2
                    },  {
                        'text': 'Electronic Ticketing',
                        'text_spanish': 'Boleter&iacute;a Electr&oacute;nica',
                        'url': 'electronic_ticketing.html',
                        'type': 2
                    }]
                }]
            }, {
                'text': 'Merchant Acquiring Solutions',
                'text_spanish': 'Soluciones para el Comerciante (Merchant POS)',
                'url': '',
                'type': 1,
                'subMenus': [{
                    'text': 'Merchant Acquiring Solutions',
                    'text_spanish': 'Soluciones para el Comerciante (Merchant POS)',
                    'url': 'merchant_acquiring.html',
                    'type': 2
                }, {
                    'text': 'Sales Tax Collection',
                    'text_spanish': 'Recaudación del Impuesto de Ventas',
                    'url': 'ivuloto.html',
                    'type': 2
                }]
            }]
        }, {
            'text': 'Support',
            'text_spanish': 'Apoyo',
            'url': '',
            'type': 0,
            'subMenus': [{
                'text': 'Service Desk Request',
                'text_spanish': 'Service Desk Request',
                'url': 'eservice_tool.html',
                'type': 2
            }]
        }, {
            'text': 'About Us',
            'text_spanish': 'Sobre Nosotros',
            'url': '',
            'type': 0,
            'subMenus': [{
                'text': 'Corporate Profile',
                'text_spanish': 'Perfil Corporativo',
                'url': 'profile.html',
                'type': 2
            }, {
                'text': 'Our History',
                'text_spanish': 'Nuestra Historia',
                'url': 'our_history.html',
                'type': 2
            }, {
                'text': 'Mission &amp; Values',
                'text_spanish': 'Misi&oacute;n y Valores',
                'url': 'mission_vision.html',
                'type': 2
            }, {
                'text': 'Community',
                'text_spanish': 'Comunidad',
                'url': 'revolucion_naranja.html',
                'type': 2
            }, {
                'text': 'Executive Team',
                'text_spanish': 'Equipo Ejecutivo',
                'url': 'eslc_members.html',
                'type': 2
            }, {
                'text': 'News & Articles',
                'text_spanish': 'Noticias y Artículos',
                'url': 'noticia_ccpr.html',
                'type': 2
            }, {
                'text': 'Infrastructure',
                'text_spanish': 'Infraestructura',
                'url': 'infrastructure.html',
                'type': 2
            }]
        }, {
            'text': 'Investor Relations',
            'text_spanish': 'Relaciones con los Inversionistas',
            'url': 'http://www.snl.com/IRWebLinkX/corporateprofile.aspx?iid=4261617',
            'type': 0
        }, {
            'text': 'Contact Us',
            'text_spanish': 'Cont&aacute;ctenos',
            'url': 'contactus.html',
            'type': 0
        }];
    }
})(jQuery);



