// MS 11/22/2011, added OuterHTML (used by AutoSort)
// OuterHTML v1.0.0 (Release version)
// Examples and documentation at: http://darlesson.com/jquery/outerhtml/
(function(jQuery){jQuery.fn.extend({outerHTML:function(value){if(typeof value==="string"){var $this=jQuery(this),$parent=$this.parent();var replaceElements=function(){var $img=$this.find("img");if($img.length>0){$img.remove();}
var element;jQuery(value).map(function(){element=jQuery(this);$this.replaceWith(element);})
return element;}
return replaceElements();}else{return jQuery("<div />").append(jQuery(this).clone()).html();}}});})(jQuery);

$(document).ready(function(){
    // MS 11/22/2011, added AutoSort feature
    jQuery("[autoSortIdsBy]").each(function() {
        var $container = jQuery(this);
        // if there is only 1 (or 0), then no sorting is needed
        if ($container.find("[autoSortId]").length > 1) {
            var sContainerInnerHtml = "";
            var sDelim = $container.attr("autoSortDelim");
            if (typeof(sDelim) == "undefined") sDelim = "";
            jQuery.each($container.attr("autoSortIdsBy").split("|"), function() {
                sContainerInnerHtml += $container.find("[autoSortId='"+ this +"']").outerHTML() + sDelim;
            });

            if (sDelim.length > 0)
                $container.html(sContainerInnerHtml.slice(0,(-1 * sDelim.length)));
            else
                $container.html(sContainerInnerHtml);
        }
    });
    
    // this is on the home page ONLY
    //$("#sliderContainer img").hide();
    /*$('#sliderContainer').bxSlider({
        mode: 'fade',
        auto: true,
        controls: true,
        pager: false,
        speed: 1000,
        pause: 8000
    });
    /*$("#sliderContainer .image").fadeIn();
    $(".sliderContainer").fadeIn();*/


if($(".slider").length > 0){
$('#nivoSlider').nivoSlider({
pauseTime: 9000,
randomStart: true,
directionNavHide: false
});

$(".sliderContainer").fadeIn(function(){

var dotPosition = $(".nivo-controlNav").position();


$(".nivo-prevNav").css("left", dotPosition.left - 12);
$(".nivo-nextNav").css("left", dotPosition.left + $(".nivo-controlNav").width() + 8);

});

$("#nivoSlider").css("height", "310px");
}




    // Detect if menu needs to expand to left if out of room
    $(".navigationContainer li").hover(function () {
        $(this).children("ul").css("left", "auto");
        if (($(this).children("ul").offset().left + $(this).children("ul").outerWidth()) > $(window).width()) {
            var leftOffset = $(this).children("ul").outerWidth(true);
            $(this).children("ul").css("margin-left", -leftOffset+($(this).width()+24));
        }
        else {
            $(this).children("ul").css("left", "auto");
        }
    }, function () {
        $(this).children("ul").css("left", "-999em");
    });
    //--/ End Detect if menu needs to expand to left if out of room

    
    
    //MS 04/12/2011
    // home page modifications
    var $homeNav = $("#navigationZone.home");
    if ($homeNav.length > 0) {
        // change text on Industries and Services (first one)
        $homeNav.find("li.CMSListMenuLI:first a").text("Practice Areas");

        // change the padding on blogs (last one)
        $homeNav.find("li.CMSListMenuLI:last a").css("padding", "0 3px 0 3px");
        
        $homeNav.find("ul").show();
        
        $homeNav.find(".search input[type=text]").show();
    }
    
    
    // Retain hover background on navigation
    $("#navigationZone li ul").hover(function(){
        $(this).prev("a").addClass("highlight");
    },function(){
        $(this).prev("a").removeClass("highlight");
    });
    
    
      
    $("#navigationZone li").hover(function(){
        $(this).addClass("sfHover");
        $(this).children("ul").css("left", "auto");
        $(this).children("ul").css("z-index", "999");
    }, function(){
        //alert("out");
        $(this).removeClass("sfHover");
        $(this).children("ul").css("left", "-999em");
    });

    $(document).ready(function() {
        
        //setup top-right search
        var runSearch = function() {
            location.href = "/Search-Results.aspx"
                +"?searchtext="+    $("#txtSiteSearch").val()
                +"&searchsection="+ $("#ddSiteSection").val();
        }

        // setup search button
        $("#btnSiteSearch").click(function() {
            runSearch();
        });
        
        $("#txtSiteSearch").keydown(function (e) {
            if ((e.keyCode && e.keyCode == 13) || (e.which && e.which == 13)) {
                e.preventDefault();
                runSearch();
            }
        });

        var SEARCH_DEFAULT = "Search";
        $("input[name$='Search$txtWord'],#txtSiteSearch").val(SEARCH_DEFAULT);
        $("input[name$='Search$txtWord'],#txtSiteSearch").focus(function() {
            var $this = $(this);
            if ($this.val() == SEARCH_DEFAULT) {
                $this.val("");
            }
        });
        
        // if has a queryString
        if (window.location.href.indexOf("?") > 0 && window.location.href.indexOf("searchtext") > 0) {
            $("#txtSiteSearch").val(getParameterByName( "searchtext" ));
            $("#ddSiteSection").val(getParameterByName( "searchsection" ));
        }
        
    });
    

    $("#toolbarEmail").click(function(){
        $(this).attr("href", 'mailto:?subject=Information%20from%20www.wnj.com&body=Please%20visit%20this%20page%20on%20the%20Warner%20Norcross%20%26%20Judd%20web%20site:' + document.location.href);
    });

	// MS 02/01/2012 - new, pdf functionality
    $("#toolbarPrint")
        .attr("href", "#")
        .click(function() {
            location.href = "/WNJ/framework/CMSPages/SaveAsPDF.aspx?NodeAliasPath="+ escape( location.pathname + location.search );
        });

    $("#toolbarBookmark").click(function(){
        if(window.sidebar){
              window.sidebar.addPanel(window.title, self.location.href,"");  
        }
        else if(window.external) {
            window.external.AddFavorite(self.location.href,window.title)
        }
        else if(window.print && window.opera) {
            var opbook = document.createElement('a');
            opbook.setAttribute('rel','sidebar');
            opbook.setAttribute('href',self.location.href);
            opbook.setAttribute('title',window.title);
            mbm.click();
        }
    });
    
    
    // fix all mailto's
    $(function() {
        var $mailtos = $("a[href^='mailto']");
        $mailtos.each(function() { $(this).data("orig-href", $(this).attr("href")); });
        $mailtos.attr("href", "#");
        $mailtos.click(function() {
            $('#basic-modal-content').modal();
            
            var sMailTo = $(this).data("orig-href");
            
            $(".accept").click(function(){
                location.href = sMailTo;
                $.modal.close();
            });
        
        });
    });
    //---/ End MailTos

    
    
});
