/*extern $, $$, Event, Fx, named, sIFR, dnn, $G */

/* Preserve Moo's version of $() */
(function()
{
    var old$ = $;
    window.addEvent("domready", function()
    {
        if($ !== old$)
        {
            $ = old$;
        }
    });
})()

var $G = {};//obsolete;

var com =
{
    phn:
    {
       setActiveStyleSheet: function(title)
       {
            $$('link').filterByAttribute("rel","~=","stylesheet").each(function(el)
            {
                el.disabled = (el.title.toLowerCase() !== title.toLowerCase())
            });
            switch (title)
            {
                case "Large text":
                    if ($("FontSizePlus"))
                    {
                        $("FontSizePlus").parentNode.style.display = "none";
                        $("FontSizeMinus").parentNode.style.display = "block";
                    }
                    if ($("navAdmin"))
                    {
                        $$(".areapane .listLinks")[0].style.paddingTop = "0";
                        $("navArea").style.height = "60px";
                        $("navHeader").style.height = "59px";
                        $$(".pagemaster")[0].style.backgroundPosition = "0px 119px";
                    }
                break;
                default:
                    if ($("FontSizeMinus"))
                    {
                        $("FontSizePlus").parentNode.style.display = "block";
                        $("FontSizeMinus").parentNode.style.display = "none";
                    }
                break;
            }
            Cookie.set("style", title, {duration: 365});
        },
        getActiveStyleSheet: function()
        {
            var links = $$('link').filterByAttribute("rel","~=","stylesheet");
            var link;
            for(var i = 0; link = links[i]; i++)
            {
                if(!link.disabled)
                {
                    return link.title;
                }
            }
            return false;
        },
        getPreferredStyleSheet: function()
        {
            var links = $$('link').filterByAttribute("rel","~=","stylesheet");
            var link;
            for(var i = 0; link = links[i]; i++)
            {
                if(!link.rel.contains("alt"))
                {
                    return link.title;
                }
            }
            return false;
        },
        saveStyleSheetCookie: function()
        {
            Cookie.set("style", this.getActiveStyleSheet(), {duration: 365});
        },
        initStyleSheet: function()
        {
            var cookie = Cookie.get("style");
            var title = cookie ? cookie : this.getPreferredStyleSheet();
            this.setActiveStyleSheet(title);
            
            
            if($("FontSizePlus"))
            {
                $("FontSizePlus").addEvent("click", function(e)
                {
                    e = new Event(e).stop;
                    this.blur();
                    com.phn.setActiveStyleSheet("Large text");
                });
            }
            if($("FontSizeMinus"))
            {
                $("FontSizeMinus").addEvent("click", function(e)
                {
                    e = new Event(e).stop;
                    this.blur();
                    com.phn.setActiveStyleSheet("Default");
                });
            }
        }
    }
}

window.addEvent("domready", com.phn.initStyleSheet.bind(com.phn));
//window.addEvent("unload", com.phn.saveStyleSheetCookie.bind(com.phn));

window.addEvent("domready", function()
{
    var ta = $('toggleAdmin');
    if(ta)
    {
        var fx = new Fx.Slide("navAdmin", {duration: 650, wait: false}).hide();
        ta.addEvent("click", function(e)
        {
            e = new Event(e).stop();
            fx.toggle();
            this.blur();
        }).addEvent("mouseenter", function()
        {
            this.setStyle("text-decoration", "underline");
        }).addEvent("mouseleave", function()
        {
            this.setStyle("text-decoration", null);
        });
    }
    
    var ha = $('toggleHost');
    if(ha)
    {
        var hx = new Fx.Slide("navHost", {duration: 650, wait: false}).hide();
        ha.addEvent("click", function(e)
        {
            e = new Event(e).stop();
            hx.toggle();
            this.blur();
        }).addEvent("mouseenter", function()
        {
            this.setStyle("text-decoration", "underline");
        }).addEvent("mouseleave", function()
        {
            this.setStyle("text-decoration", null);
        });
    }
});

window.addEvent("domready", function()
{
    var formSearch = $("formSearch");
    
    if (formSearch)
    {
    
        var fx = new Fx.Style(formSearch, 'height',
        {
            duration: 500,
            wait: false
        });

        formSearch.setStyles({
            display: "block",
            overflow: "hidden"
        });
        
        fx.set(0);
        var menu;
        
        $$("#navHeader .listLinks a").each(function(el)
        {
            if (el.innerHTML === "Search")
            {
                menu = el;
                el.addClass("toggleSearch").addClass("stMenu");
            }
        });
        
        var rm = function()
        {
            var n = $('navHeader').getCoordinates();
            
            formSearch.setStyles(
            {
                left: n.right - formSearch.getStyle("width").toInt() + 6,
                top: n.bottom
            });
        };
        window.addEvent("resize", rm);
        rm();

        $$(".toggleSearch").each(function(el)
        {
            el.addEvent("click", function(e)
            {
                e = new Event(e).stop();
                this.blur();
                if(fx.element.getStyle(fx.property).toInt() === 0)
                {
                    menu.addClass("toggleOn");
                    try
                    {
                        $(menu.nextSibling.nextSibling).addClass("toggleOn");
                    }catch(ex1){}
                    fx.start(0,115);
                }
                else
                {
                    menu.removeClass("toggleOn");
                    try
                    {
                        $(menu.nextSibling.nextSibling).removeClass("toggleOn");
                    }catch(ex2){}
                    fx.start(115,0);
                }
            });
        });
    }
});

window.addEvent("domready", function()
{
    try
    {
        //fix rounded corner of secondary navigation
	    $$(".headerpane a").getLast().addClass("last");
    }
    catch(ex){}
});

window.addEvent("domready", function()
{
    // HACK - add support PNG alpha support for IE 6
    var replace = function(s, filename)
    {
        $$(s).each(function (el)
        {
            //console.log(el);
            if (!el.hasClass("disabled"))
            {
                // remove existing background
                el.style.backgroundImage = "none";
                // apply PNG background with alpha transparency filtering
                el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $G.VirtualRoot + "/Portals/_default/Skins/BonaVista/_images/templates/" + filename + "')";
            }
        });
    };
    
    // if IE 5.5 or IE 6 windows
    if(window.ie && !window.ie7)
    {
        // all button elements
        replace("button", "bg_btnSmall.png");

        // specific button sizes...
        replace(".btnExtraSmall", "bg_btnExtraSmall.png");
        replace(".btnSmall", "bg_btnSmall.png");
        replace(".btnLarge", "bg_btnLarge.png");
        replace(".btnExtraLarge", "bg_btnExtraLarge.png");

        // open account button
        replace(".btnOpenAccount", "bg_btnLarge.png");
        var els = $$(".btnOpenAccount").each(function (el)
        {
            // correct positioning
            el.style.paddingTop = "3px";
            el.style.marginTop = "-2px";
        });

        // search form background replacement
        var el = $("fieldsetSearch");
        if (el)
        {
            el.style.backgroundImage = "none";
            el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $G.VirtualRoot + "/Portals/_default/Skins/BonaVista/_images/templates/bg_fieldsetSearch.png')";
        }
    }
});

window.addEvent('domready', function()
{
    var font = $G.VirtualRoot + "/Portals/_default/Skins/BonaVista/_images/templates/georgia.swf";
    if(sIFR)
    {
            sIFR.replaceElement(named({sSelector:"h1",                              sFlashSrc:font, sColor:"#003300", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".homepage #formLogin h2",         sFlashSrc:font, sColor:"#FFFFFF", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".promo h2",                       sFlashSrc:font, sColor:"#0099CC", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".contentpane h2",                 sFlashSrc:font, sColor:"#003300", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".alert h3",                       sFlashSrc:font, sColor:"#FF9900", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".homepage #formLogin .error h3",  sFlashSrc:font, sColor:"#CC0000", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".error h3",                       sFlashSrc:font, sColor:"#FFFFFF", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".success h3",                     sFlashSrc:font, sColor:"#FFFFFF", sWmode:"transparent", sFlashVars:"underline=true"}));
            sIFR.replaceElement(named({sSelector:".column h3",                      sFlashSrc:font, sColor:"#003300", sWmode:"transparent", sFlashVars:"underline=true"}));
            if (window.ie)
            {
                sIFR.replaceElement(named({sSelector:".wizard h3",                  sFlashSrc:font, sColor:"#003300", sWmode:"transparent", sFlashVars:"underline=true"}));
            }
            else
            {
                sIFR.replaceElement(named({sSelector:".wizard h3",                  sFlashSrc:font, sColor:"#003300", sWmode:"transparent", nPaddingTop:"10", sFlashVars:"underline=true"}));
            }
            sIFR.replaceElement(named({sSelector:".contentpane h3",                 sFlashSrc:font, sColor:"#003300", sWmode:"transparent", sFlashVars:"underline=true"}));
        }
});



/* dnn admin */

function cmdGo_OnClick(o)
{
	if (o.selectedIndex > 0)
	{
		var sVal = dnn.getVar('__dnn_CSAction_' + o.id + '_' + o.options[o.selectedIndex].value);
		if (sVal !== null)
		{
			return eval('var bRet = ' + sVal);
		}
	}
    else
    {
        return false;
	}
	return true;
}

window.addEvent("domready", function()
{
    try
    {
        $$(".tabs a.selected").each(function (el)
        {
            el.parentNode.className = "selected";
        });
    }
    catch(ex){}
});

/*snaps

$$('.moduleAdmin select').each(function(el)
{
    el.getChildren().each(function(ch)
    {
        if(ch.text === "_Edit Text")
        {
            var i = el.id.substr(7);
            i = i.substr(0, i.indexOf('_'));
            var module = el.getParent().getParent().getParent();

            var ed = new Element("div",
            {
                styles:
                {
                    position: 'absolute',
                    float: 'left',
                    border: '1px solid red',
                    cursor: 'pointer'
                }
            }).setText("Edit Text").addEvent("click", function(e)
            {
                new Event(e).stop();
                window.location.href = "/Default.aspx?ctl=Edit&mid="+i;
            }).addEvent("mouseenter", function(e)
            {
                module.getLast().setStyle("border", "1px dashed #ccc");
            }).addEvent("mouseleave", function(e)
            {
                module.getLast().setStyle("border", "0px");
            });
            ed.injectAfter(module.getFirst());
        }
    });
})

*/