;(function($){

    $.localhost.master.define({

        fontFamily: 'AmazoneBT',

        debug: {
	        height: false,
	        headings: false
        },

        isOldIE: $.browser.msie && parseInt($.browser.version) < 7,

        ready: function(){
        
            this.init();
            
            this.page.init();
            
            $(this.controls).each(function(){
                this.init();
            });
            
        },
        
        init: function(){	    
            //alert('master init');
    	
            //
            // Add "home" to start of main menu.
            //
            $("#menu ul").prepend($('<li class="first"><a href="/">Home</a></li>'));

            //
            // Wire print button.
            //
            if (window.print) {
                $("#print a").click(function(){
                    window.print();
                });
            } else {
                $("#print").hide();
            }
            
            //
            // Detect active industry.
            //
            $("#industry-links a").each(function(){
                var $this = $(this);
                var uri = $this.attr("href");
                if (window.location.pathname.indexOf(uri, 0) == 0) {
                    $this.addClass("active");
                }
            });
            
            //
            // Post-process submenus
            //
            $(".submenu a").each(function(){
                var $this = $(this);
                if (window.location.pathname.indexOf($this.attr("href")) == 0) {
                    $this.replaceWith($("<span/>").text($this.text()));
                }
            });
            $("#tertiary-menu ul").appendTo($("#submenu ul ul li:has(span)").eq(0));
            $("#submenu span").eq(0).each(function(){
                var $span = $(this);
                $span.replaceWith($("<h3/>").text($span.text()));
            });
            
	        //
	        // IE Bug : Absolutely-positioned footer disappears when
	        //	#header and #body are 100% width.
	        //
	        if (false && $.browser.msie) {
		        var $footer = $("#footer");
		        $footer.css({
			        width: $footer.width()
		        });
	        }
	        
	        //
	        // Add classes for current page
	        //
	        var $body = $("body");
	        var url = window.location.pathname;
	        $(url.split('/')).each(function(i,v){
	            if (v) $body.addClass(v);
	        });

    		
	        /*
	        this.replaceHeading(1, {
		        size:64,
		        color:"white"
	        });
	        */

            //
            // Tag unformatted (generic) lists.
            //
	        $("ul:not(ul[class])").addClass("unformatted");

	        /*
	        if (!this.isOldIE) {
		        this.replaceHeading(2, {
			        fontFamily: "Optima",
			        fontSize:14
		        });
		        this.replaceHeading(3, {
			        fontFamily:"Optima",
			        textTransform:"uppercase"
		        });
	        }
    */
    		
	        //
	        // Fix PNGs for IE < 7
	        //
	        /*
	        $.ifixpng('/images/pixel.gif');
	        $('img[src$=.png]').ifixpng();
            */
            
	        /*
	        $(window).resize(this.$f("fillHeight"));
	        this.fillHeight();
	        */

	        //this.formatNav();
	        //this.addInners();

	        $("h1 img").click(function(){
		        location="/";
	        }).css({
		        cursor:"pointer"
	        });

	        $("#industry-links li").each(function(){
		        var $this = $(this);
		        $this.addClass($this.text().toLowerCase());
	        });
	        $("#industry-links a").each(function(){
		        $(this).wrapInner("<span/>");
	        });

	        this.initMenu();
    		
        },
        
        defineControl: function(object){
            this.controlIDs.push(id);
            this.controlObjects[id] = object;
        },
        
        addInners: function() {
	        $(".panel, .section, .tabs > .head").each(function() {
		        var $this = $(this);
		        if (!$this.children(".inner").length) {
			        $this.wrapInner($("<div/>").addClass("inner"));
		        }
	        });
	        $(".tabs > .head a").each(function() {
		        var $this = $(this);
		        if (!$this.children(".inner").length) {
			        $this.wrapInner($("<span/>").addClass("inner"));
		        }
	        });
        },

        fillHeight: function() {
	        //return;

	        var winHeight = $(window).height();
	        var myHeight = $("#layout").get(0).offsetHeight;
	        var bodyHeight = $("#body").get(0).offsetHeight;
	        var mainHeight = $("#main").get(0).offsetHeight;
	        var moreHeight = $("#more").get(0).offsetHeight;
	        var footHeight = $("#foot").get(0).offsetHeight;
	        var origHeight = $("#body").get(0).origHeight || bodyHeight;

	        var flexHeight = $("#flex").stop().get(0).offsetHeight;
	        var contHeight = $("#layout").get(0).offsetHeight - flexHeight;

	        //var minHeight = Math.max(mainHeight, $("#more").get(0).offsetHeight) + $("#foot").get(0).offsetHeight;
	        var minHeight = Math.max(mainHeight, $("#more").get(0).offsetHeight);

	        var origMinHeight = $("#main").get(0).origMinHeight || minHeight;

	        $("#body").get(0).origHeight = bodyHeight;
	        $("#main").get(0).origMinHeight = origMinHeight;

	        var duration = this.debug.height ? 3000 : 1;

	        //return;

	        //var newHeight = bodyHeight + (winHeight - myHeight);
	        var newHeight = minHeight + (winHeight - myHeight);

	        var newFlexHeight = Math.max(0, winHeight - contHeight);

	        //alert(vapi.sprintf("%s:%s", [winHeight,myHeight]));
	        //alert(vapi.sprintf("%s:%s", [mainHeight,newHeight]));
	        //alert(newFlexHeight);

	        var $flex = $("#flex");
	        $("#flex").animate({
		        height: newFlexHeight + "px"
	        }, duration);

	        if (newHeight >= minHeight || newHeight > origMinHeight) {
        /*
		        $("#foot").css({
			        position: "absolute",
			        left: 0,
			        bottom: 0
		        });
		        */
        /*
		        var $main = $("#main");
		        if (this.debug.height) {
			        $main.css({
				        border: "2px dotted fuchsia"
			        });
		        }
		        $("#main").animate({
    //                height: "+=" + newHeight + "px"
			        //height: newHeight + "px",
			        //minHeight: newHeight + "px"
		        }, duration);
		        */
        }


        },

        replaceHeading: function(level, options) {
	        var debug = this.debug.headings;

	        /*
	        alert($("body").css("font-size"));
	        alert($("#body").css("font-size"));
	        alert($("#main").css("font-size"));
	        alert($("#more").css("font-size"));
	        */

	        $("h" + level).each(function(){
		        var $this = $(this);

		        //
		        // Get heading text.
		        //
		        var heading = $this.text().replace(/^\s*/, '').replace(/\s*$/, '').replace(/\s*\n\s*/, ' ');

		        //
		        // Copy options.
		        //
		        var opts = [];
		        for (var i in options) {
			        opts[i] = options[i];
		        }

		        //
		        var size = $this.css("font-size");
		        var fontSize = Math.floor(parseFloat($this.css("font-size").replace("px", "")));
		        var color = $this.css("color").replace("#", "");
		        var borderColor = ($this.css("background-color") || "").replace(/(?:transparent|rgba).*/, "").replace("#", "");

		        //fontSize = $.browser.msie ? Math.floor(fontSize * 1.25) : fontSize;

		        opts.fontSize = fontSize;
		        opts.color = color;
		        opts.borderColor = borderColor || options.borderColor;
		        opts.type = "png";
		        opts.backgroundColor = "transparent";

		        opts.content = debug ? heading + " (" + size + ")" : heading;

		        if ($.browser.msie && parseInt($.browser.version) < 7) {
			        opts.type = "gif";
		        //$this.empty().text(uri);
		        //return;
		        }

		        var uri = vapi.util.makeURL("http://tools.vezea.net/textimage", opts);

		        $this.wrapInner('<span class="text-image"/>');

		        //
		        // Create new image to fade-in replace heading upon complete download.
		        //
		        var image = new Image();
		        image.onload = function() {
			        var $span = $this.find("span");
			        $span.fadeOut({
				        duration: 1,
				        complete: function() {
					        var $img = $("<img/>");
					        $img.attr({
						        "src": image.src,
						        "alt": $span.text()
					        });
					        $span.empty().append($img);
					        $span.fadeIn();
				        //$span.parent().height(fontSize);
				        }
			        });
		        };
		        //
		        // Initiate image download by setting "src" attribute.
		        //
		        image.src = uri;
	        });
        },

        initMenu: function() {
	        var www = this;

	        $("#menu a").each(function(){
		        var href = $(this).attr("href");
		        if (href) {
			        var $dropmenu = $("#navigation a[href='"+href+"'] ~ ul").clone();
			        if ($dropmenu.length) {

				        //
				        // Add formatting to dropmenu.
				        //
				        $dropmenu.addClass($(this).parents("#menu > *").attr("class") + "-dropmenu");
				        $dropmenu.addClass($(this).attr("class") + "-dropmenu");
				        $dropmenu.addClass("dropmenu");

				        //
				        // Fix IE.
				        //
				        if ($.browser.msie) {
					        $dropmenu.find("a").width("200");
				        }

				        //
				        // Add dropmenu to document.
				        //
				        $("body").append($dropmenu.hide());

				        //
				        // Link menu item and dropmenu to each other.
				        //
				        this.dropmenu = $dropmenu.get(0);
				        this.dropmenu.upmenu = this;

				        //
				        // Wire show and hide for dropmenu.
				        //
				        this.dropmenu.doShow = function(){
					        $(this.upmenu).addClass("hover");
					        $(this).slideDown(www.menuFadeTime);
				        };
				        this.dropmenu.doHide = function(){
					        $(this.upmenu).removeClass("hover");
					        $(this).slideUp(www.menuFadeTime);
				        };

				        //
				        // Wire hover for menu item.
				        //
				        $(this).hover(function(){
					        var $this = $(this);

					        clearTimeout(this.hider);    // clear previously initiated hide

					        $(this.dropmenu).appendTo($("body")).css({
						        position: "absolute",
						        left: $this.offset().left - 1,  // 1 for border
						        top: $this.offset().top + this.offsetHeight
					        });

					        this.dropmenu.doShow();

				        },function(){
					        //
					        // Instead of hiding dropmenu, allow brief interval
					        // to let mouse pass to dropmenu (which then clears
					        // this hide call if hover is detected.)
					        //
					        var dropmenu = this.dropmenu;
					        this.hider = setTimeout(function(){
						        dropmenu.doHide();
					        }, www.menuHoldTime);
				        });

				        //
				        // Wire hover for dropmenu.
				        //
				        $dropmenu.hover(function(){
					        clearTimeout(this.upmenu.hider);    // clear previously initiated hide
				        },function(){
					        var dropmenu = this;
					        this.upmenu.hider = setTimeout(function(){
						        dropmenu.doHide();
					        }, www.menuHoldTime);
				        });
			        }
		        }
	        });
        }

    });
    
})(jQuery);
