if (!com) var com = {};
if (!com.betterbricks) com.betterbricks = {};

com.betterbricks.www = {

    menuFadeTime: 250,
    menuHoldTime: 10,
    
    onReady: function() {
        this.applyClasses();
        this.initMenu();
        
        //
        // Clear submit button text (for IE).
        //
        $("#search input[type='submit']").val("");

        //
        // Add additional classes for browsers lacking CSS attribute selectors (e.g. IE6).
        //
        $("input[type='text']").addClass("text");
        
        //
        // Dim inactive elements.
        //
//$("#rotator").animate({
//           width: "70%"
//        }, 1500 );
       // $("#rotator").hide();
        
//          $(document.body).click(function () {
//      if ($("div:first").is(":hidden")) {
//        $("#rotator").slideDown("slow");
//      } else {
//        $("#rotator").hide();
//      }
//    });

        
        this.formatAmpBreaking();
        
        this.styleLinks();
        
        $("#logo").click(function(){
            window.location = "./";
        }).css({
            cursor: "pointer"
        });


        //
        // Handle IE6 PNG issues.
        //
        this.fixPNG("body.home .panel img");
        
        this.formatSubHome();
    },

    fixPNG: function(selector) {

        if (!$.browser.msie || parseInt($.browser.version) > 6) return;

        var $selection = $(selector);

        if (!$selection.length) return;

        $selection.each(function() {
            var $this = $(this);
            if (this.tagName.toLowerCase() == "img") {
                this.src = this.src.replace(/\.png$/, ".gif");
            }
        });
    },
    
    formatSubHome: function(){
        $(".sub-home .teaser a").each(function(){
            var $this = $(this);
            $this.text($this.text()+">");
        });
        $("#left-menu blockquote").each(function(){
            var $this = $(this);
            var $label = $this.find("label");
            
            $label.find("i").remove();
            var label = $label.text();

            var $item = $this.parent().find("> p em");
            
            if ($item.length) {
                $label.remove();
                
                $submenu = $("<span/>");//.addClass("submenu");
                $this.contents().appendTo($submenu);
                
                $item.append($submenu);
            }
        });
    },
    
    formatAmpBreaking: function(){
        $("#MainMenu *:contains('&')").each(function(){
            alert(this.tagName);
        });
    },
        
    styleLinks: function(){
        $("a.more").each(function(){
            var $this = $(this);
            $this.text($this.text() + " »");
        });
    },
    
    applyClasses: function() {
        var tag = this.menuHeadingTag;
        
        //
        // Assert: Second-level child is heading.
        //
        $("#menu > * > :contains('Practices')").parent().addClass("practices");
        $("#menu > * > :contains('Services')").parent().addClass("services");
        $("#menu > * > :contains('Other')").parent().addClass("other");
        
        $("#menu a:contains('About Us')").addClass("about");
        $("#menu a:contains('Blog')").addClass("blog");
    },
    
    initMenu: function() {
        var www = this;
        
        $("#menu a").each(function(){
            var href = $(this).attr("href");
            if (href) {
                var $submenu = $("#navigation a[href='"+href+"'] ~ ul").clone();
                if ($submenu.length) {
                    
                    //
                    // Add formatting to submenu.
                    //
                    $submenu.addClass($(this).parents("#menu > *").attr("class") + "-submenu");
                    $submenu.addClass($(this).attr("class") + "-submenu");
                    $submenu.addClass("submenu");

                    //
                    // Add submenu to document.
                    //
                    $("body").append($submenu.hide());
                    
                    //
                    // Link menu item and submenu to each other.
                    //
                    this.submenu = $submenu.get(0);
                    this.submenu.upmenu = this;
                    
                    //
                    // Wire show and hide for submenu.
                    //
                    this.submenu.doShow = function(){
                        $(this.upmenu).addClass("hover");
                        $(this).slideDown(www.menuFadeTime);
                    };
                    this.submenu.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.submenu).appendTo($("body")).css({
                            position: "absolute",
                            left: $this.offset().left - 1,  // 1 for border
                            top: $this.offset().top + this.offsetHeight
                        });
                        
                        this.submenu.doShow();
                        
                    },function(){                 
                        //
                        // Instead of hiding submenu, allow brief interval
                        // to let mouse pass to submenu (which then clears
                        // this hide call if hover is detected.)
                        //       
                        var submenu = this.submenu;
                        this.hider = setTimeout(function(){submenu.doHide();}, www.menuHoldTime);                        
                    });
                    
                    //
                    // Wire hover for submenu.
                    //
                    $submenu.hover(function(){
                        clearTimeout(this.upmenu.hider);    // clear previously initiated hide
                    },function(){
                        var submenu = this;
                        this.upmenu.hider = setTimeout(function(){submenu.doHide();}, www.menuHoldTime);                        
                    });
                }
            }
        });
    }
    
};
$(document).ready(function(){com.betterbricks.www.onReady();});

//home page rotation scripts

function openbox(title,content)
{

 Shadowbox.open({
        content:    content,
        player:     "html",        
        height:     550,
        width:      450        
    });
    };
 var interval = "";
 function startrotation()
{
    $("#myimdg1").css({left: "0px"});
    for (j=2;j<=recordcount;j++)
            $("#myimdg" + j).css({left: "601px"});
    interval = window.setInterval('slidesForward(1)',9000);    
}    
function slidesForward(shiftCopy)
{
    counter++;
    if (counter > recordcount)
    {    
        if (shiftCopy == 1)
            $("#box" + recordcount).fadeOut(500, fadeBoxIn(1));            
        for (j=1;j<recordcount;j++)
            $("#myimdg" + j).css({left: "601px"});
            
        $("#myimdg1").animate({ left: "0px" }, 0);   
        $("#myimdg" + recordcount).animate({ left: "-601px"},0, setLast());
        $("#myimdg1").fadeIn(500, fadeBoxIn(1));
        counter = 1;
    }
    else
    {
        if (shiftCopy == 1)
            $("#box" + (counter-1)).fadeOut(500, fadeBoxIn(counter));
            $("#box" + (counter+1)).fadeOut(500, fadeBoxIn(counter));
        $("#myimdg" + counter).css({left: "601px"});
        $("#myimdg" + (counter-1)).fadeOut(500, fadeBoxIn(counter));
        $("#myimdg" + (counter)).animate({ left: "0px" },0);
        $("#myimdg" + (counter)).fadeIn(500, fadeBoxIn(counter));
        if (counter == 2)        
            $("#myimdg" + recordcount).css({left: "601px"});
    }
}
function slidesBack(shiftCopy)
{
    counter--;
    if (counter == 0)
    {    
        if (shiftCopy == 1)
            $("#box1").fadeOut(500, fadeBoxIn(recordcount));
        $("#myimdg" + recordcount).css({left: "-601px"});
        
        for (j=2;j<recordcount;j++)
            $("#myimdg" + j).css({left: "-601px"});
            
         $("#myimdg1").fadeOut(500, fadeBoxIn(recordcount));
        $("#myimdg1").animate({ left: "601px"}, 0);       
         $("#myimdg" + recordcount).animate({ left: "0px"}, 0, setFirst()); 
         $("#myimdg" + recordcount).fadeIn(500, fadeBoxIn(recordcount)); 
      counter = recordcount;
    }
    else
    {
        if (shiftCopy == 1)
         $("#box" + (counter+1)).fadeOut(500, fadeBoxIn(counter));
        $("#myimdg" + (counter+1)).fadeOut(500, fadeBoxIn(counter));
        $("#myimdg" + counter).animate({ left: "0px" }, 0);
        $("#myimdg" + (counter)).fadeIn(500, fadeBoxIn(counter));
    }
}
function fadeBoxIn(Num)
{
    $("#box" + Num).fadeIn(500);
    for (j=1;j<= recordcount;j++)
    {
        document.getElementById('number' + j).className='homeRotationBoxes buttonoff';
    }
        document.getElementById('number' + Num).className='homeRotationBoxes buttonon';
}
function setLast()
{   
        document.getElementById('number' + recordcount).className='homeRotationBoxes buttonon';
        document.getElementById('number1').className='homeRotationBoxes buttonoff';
}
function setFirst()
{
        document.getElementById('number' + recordcount).className='homeRotationBoxes buttonoff';
        document.getElementById('number1').className='homeRotationBoxes buttonon';
}

function slideIt(Increment)
{
    window.clearInterval(interval);
    if (Increment == 1)
        slidesForward(1);
    else
        slidesBack(1);
}
function goToBox(BoxNum)
{
    window.clearInterval(interval);
    if (BoxNum != counter)
        $("#box" + counter).fadeOut(500, fadeBoxIn(BoxNum));
    if (BoxNum > counter)
    {
         $("#myimdg" + counter).fadeOut(500, fadeBoxIn(BoxNum));
       $("#myimdg" + BoxNum).animate({left: "0px"}, 0);
        $("#myimdg" + BoxNum).fadeIn(500, fadeBoxIn(BoxNum));
        
    }
    else if (BoxNum < counter)
    {
         $("#myimdg" + counter).fadeOut(500, fadeBoxIn(BoxNum));
       $("#myimdg" + BoxNum).animate({left: "0px"}, 0);
        $("#myimdg" + BoxNum).fadeIn(500, fadeBoxIn(BoxNum));
   }
    counter = BoxNum;
}


