var cs_menu = function(container){
    this.options = {
        'red':{
            'left':[],
            'right':[]
        },
        'blue':{
            'left':[],
            'right':[]
        },
        'green':{
            'left':[],
            'right':[]
        }
    };
    this.root = location.protocol+'//'+location.host+'/js/menu/';
    this.activated = false;
    this.container = container;
    this.output = '<div id="menu_buttons" class="menu_buttons">';
    this.output += '<div class="menu_btn" id="red">&nbsp;&nbsp;at Perimeter<br />';
    this.output += '<img border="0" src="'+this.root+'img/red.png" />';
    this.output += '</div>';
    this.output += '<div class="menu_btn" id="blue">&nbsp;&nbsp;Ministries<br />';
    this.output += '<img border="0" src="'+this.root+'img/blue.png" />';
    this.output += '</div>';
    this.output += '<div class="menu_btn" id="green">&nbsp;&nbsp;Worship<br />';
    this.output += '<img border="0" src="'+this.root+'img/green.png" />';
    this.output += '</div>';
    this.output += '</div>';
    this.output += '<div id="menu_container" class="menu_container">';
    /*this.output += '<div class="menu_top_bar">';
    this.output += '<div class="left">&nbsp;</div>';
    this.output += '<div class="center">&nbsp;</div>';
    this.output += '<div class="right">&nbsp;</div>';
    this.output += '</div>';*/
    this.output += '<div id="body">';
    this.output += '&nbsp;';
    this.output += '</div>';
    /*this.output += '<div class="menu_bottom_bar">';
    this.output += '<div class="left">&nbsp;</div>';
    this.output += '<div class="center">&nbsp;</div>';
    this.output += '<div class="right">&nbsp;</div>';
    this.output += '</div>';*/
    this.output += '</div>';
    this.display = function(menuitem){
        for(property in this.options){
            if(property == menuitem){
                eval('var menu_options = this.options.'+property);
                var left = '';
                var right = '';
                var leftOptions, rightOptions;
                var newlink = '';
                var newline = '';
                for(property in menu_options){
                    if(property == 'left'){
                        leftOptions = eval('menu_options.'+property);
                    }
                    if(property == 'right'){
                        rightOptions = eval('menu_options.'+property);
                    }
                }
                if(typeof(leftOptions) != 'undefined'){
                    var n = 0;
                    for(l in leftOptions){
                        n++;
                        newlink = leftOptions[l];
                        //newlink = newlink.replace(/<[^>]+>\s*<\s*\/[^>]+>/gi,'');
                        if(typeof(leftOptions[l]) == 'string'){
                            if(newlink.match(/\<a.*href/gi)){
                                newlink = this.processLink(leftOptions[l],n++);
                            }else if(newlink.match(/^[\w\s]+$/gi)){
                                newlink = '<h3 style="font-weight:normal!important;font-size:11px!important;margin:0!important">'+newlink+'</h3>';
                            }
                        }
                        newline = (typeof(newlink) == 'string' && !newlink.match(/<[\/\s]*h\d{1}[^>]*>[^<]+<[\/\s]*h\d{1}>/mi)) ? '<br />' : '';
                        left += (typeof(newlink) == 'string') ? newlink +newline: '';
                    }
                }
                if(typeof(rightOptions) != 'undefined'){
                    var nn = 0;
                    for(r in rightOptions){
                        newlink = rightOptions[r];
                        if(typeof(rightOptions[r]) == 'string'){
                            if(newlink.match(/\<a.*href/gi)){
                                newlink = this.processLink(rightOptions[r],nn++);
                            }else if(newlink.match(/^[\w\s]+$/gi)){
                                newlink = '<h3 style="font-weight:normal!important;font-size:11px!important;margin:0!important">'+newlink+'</h3>';
                            }
                        }
                        newline = (typeof(newlink) == 'string' && !newlink.match(/<[\/\s]*h\d{1}[^>]*>[^<]+<[\/\s]*h\d{1}>/mi)) ? '<br />' : '';
                        right += (typeof(newlink) == 'string') ? newlink +newline: '';
                    }
                }
                var new_html = '<div class="menuContents" style="width:48%;float:left;position:relative;background:transparent;z-index:1000">'+left.replace(/(.*)<br[^>]+>$/,'$1')+'</div>';
                new_html += '<div class="menuContents" style="width:48%;float:right;position:relative;background:transparent;z-index:1000">'+right.replace(/(.*)<br[^>]+>$/,'$1')+'</div>';
                j('.menu_container #body').html(new_html);
                //this.render();
            }
        }
        j('.linkContainer').hover(function(el){
            var cur = (typeof(el.currentTarget) != 'undefined') ? el.currentTarget : el.target;
            j(cur.childNodes[0]).css({
                color:'#636466!important'
            });
        }, function(el){
            var cur = (typeof(el.currentTarget) != 'undefined') ? el.currentTarget : el.target;
            j(cur.childNodes[0]).css({
                color:'#FFFFFF'
            });
        });
    }
    this.render = function(){
        var o = this;
        if(navigator.userAgent.match(/firefox/gi) && navigator.userAgent.match(/windows/gi)){
            j('.menuContents p').each(function(index,el){
                console.log(el);
                var txt = j(el).text();
                var imgOut = o.replace(txt,index,false);
                var imgIn = o.replace(txt,index,true);
                j(el).html(imgOut);
                j(el).hover(function(ev){
                    j(el).html(imgIn);
                },function(ev){
                    j(el).html(imgOut);
                });
            });
        }
    }
    this.processLink = function(link,ix){
        var newlink = '';
        try{
            if(typeof(link) != 'undefined'){
                if(typeof(navigator.userAgent) != 'undefined'){
                    var target = 'onclick="javascript:location.href=\''+link.replace(/.*href=[\'"]{1}([^\'"]+)[\'"]{1}.*/gi,'$1')+'\'"';
                    link = link.replace(/<[^>]+>([^<]+)<[^>]+>/gi,'$1');
                    var css = (navigator.userAgent.match(/msie/gi)) ? 'margin:0!important' : '';
                    newlink = (navigator.userAgent.match(/msie/gi)) ? '<p class="linkContainer" '+target+' style="display:inline!important;margin:0!important" id="link'+ix+'">'+link+'</p>' : '<p class="linkContainer" '+target+' style="z-index:1002;'+css+'" id="link'+ix+'">'+link+'</p>';
                }
            }
        }catch(err){}
        return newlink;
    }
    this.replace = function(text,ix,hov){
        var imgHover = new Image();
        var imgOut = new Image();
        imgHover.src = '/js/menu/txt2img.php?text='+escape(text)+'&fcolor=fff&bcolor=636466&transparency=0';
        imgOut.src = '/js/menu/txt2img.php?text='+escape(text)+'&fcolor=636466&bcolor=FFFFFF&transparency=0';
        this.images.hover[ix] = imgHover;
        this.images.out[ix] = imgOut;
        return (!hov) ? this.images.out[ix] : this.images.hover[ix];
    }
    this.images = {
        hover:[],
        out:[]
    },
    this.init = function(){
        var instance = this;
        if(typeof(instance.output) != 'undefined'){
            j(instance.container).html(instance.output);
        }
        j('.menu_buttons').hover(function(){
            // mouseover
            instance.activated = true;
        }, function(){
            // mouseout
            });
        j('.menu_btn').each(function(){
            var current = j(this);
            var close = function(){
                if(!instance.activated){
                    j('.menu_container').hide();
                }
            }
            current.hover(
                function(){
                    // mouseover
                    instance.display(current.attr('id'));
                    var bg = (navigator.userAgent.match(/msie/gi)) ? 'transparent url(\'/js/menu/img/menu_bg.gif\') no-repeat top left;' : 'transparent url(\'/js/menu/img/menu_bg.png\') no-repeat top left';
                    if(!instance.activated){
                        j('.menu_container').css({
                            top:j('.menu_buttons :first-child').offset().top+30+'px',
                            left:'0.5px',
                            height:'325px',
                            width:'324px',
                            marginLeft:menuHackMarginLeft,
                            background: bg
                        }).show();
                        instance.activated = true;
                    }
                },
                function(){
                    // mouseout
                    instance.activated = false;
                    setTimeout(close, 100);
                }
                );
            j('.menu_container').hover(
                function(){
                    // mouseover
                    instance.activated = true;
                },
                function(){
                    // mouseout
                    instance.activated = false;
                    setTimeout(close, 1000);
                }
                );
        });
    }
}
