$(document).ready(function() {

    // newsletter signup input
    $("#header input").attr("value", "Search");

    // global links
    $('a[rel=external]').click(function() {
        window.open(this.href);
        return false
    });
    $('.printBtn').click(function() {
        // $('.selected .more').css({display: 'block'});
        window.print();
        return false;
    });
    $('#breadcrumb .close').click(function() {
        window.close();
        return false
    });
    $("#content form .button").hover(function () {
        $(this).css({background: "#0066cc", color: "#fff"});
    }, function () {
        $(this).css({background: "#fff", color: "#0066cc"});
    });

    // equal height for subnav dropdowns
    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }

    function setSubnavHeights() {
        equalHeight($(".sub1"));
        equalHeight($(".sub1 div"));
        equalHeight($(".sub1 h3"));
        equalHeight($(".sub2"));
        equalHeight($(".sub2 div"));
        equalHeight($(".sub2 h3"));
        equalHeight($(".sub3"));
        equalHeight($(".sub3 div"));
        equalHeight($(".sub3 h3"));
        equalHeight($(".sub4"));
        equalHeight($(".sub4 div"));
        equalHeight($(".sub4 h3"));
        equalHeight($(".sub5"));
        equalHeight($(".sub5 div"));
        equalHeight($(".sub5 h3"));
        equalHeight($(".sub6"));
        equalHeight($(".sub6 div"));
        equalHeight($(".sub6 h3"));
    }

    //setSubnavHeights();

    // Nav positioning ==========================

    // on first hover set height of all subnav cols to tallest div height
    $("#nav li.main, #nav li.last").one("mouseover", function() {
        kids = $(this).find('.subnav').children();
        equalHeight(kids);
        equalHeight($(kids).children());
        equalHeight($(kids).children('h3'));
    });

    //
    $("#nav li.main, #nav li.last").hover(function () {
        var navOffset = $("#nav ul").offset();
        $(this).addClass("over");


        // show subnav
        $("ul", this).css({left: "auto"});
        $("ul", this).hide();
        $("ul", this).fadeIn(200);
        var subOffset = this.offsetLeft;
        var subRight = $("ul", this).width() + this.offsetLeft;
        var rightEdge = $("#nav ul").width() + navOffset.left;
        if (jQuery.browser.msie) {
            var ieVersion = parseInt(jQuery.browser.version);
            if (ieVersion == 6) {
                subRight = subRight + navOffset.left - 4;
            }
            if (ieVersion == 7) {
                subRight = subRight + navOffset.left + 2;
            }
        }
        // move subnav left to right align:: if wider than nav
        if (subRight > rightEdge) {
            var moveLeft = (rightEdge - subRight) + 1;
            $("ul", this).css({marginLeft: moveLeft + "px"});
            $("ul ul", this).css({marginLeft: "auto"});
        }
    }, function () {
        $("ul", this).hide();
        $(this).removeClass("over");
    });


    // iframe open: show overlay
    $('.showHide').click(function() {
        docHeight = $(document).height();
        $('#overlay').height(docHeight);
        $('#overlay').fadeIn(500);
        $(this.hash).css({left: ''});
        $(this.hash).fadeIn(500);
        $('body').append('<div id="overlay" style="background:none;">&nbsp;</div>');
        $('#overlay').show();
        $('#overlay').click(function() {
            $('.iframe:visible').fadeOut(400);
            $('#overlay').fadeOut(500, function () {
                $('#overlay').remove();
            });
        });
        return false;
    });


    $('.showHideOverlay').click(function() {
        docHeight = $(document).height();
        $('#overlay').height(docHeight);
        $('#overlay').fadeIn(500);
        $(this.hash).fadeIn(500);
        $('body').append('<div id="overlay">&nbsp;</div>');
        $('#overlay').fadeIn(500);
        $('#overlay').click(function() {
            $('.iframe:visible').fadeOut(400);
            $('#overlay').fadeOut(500, function () {
                $('#overlay').remove();
            });
        });
        return false;
    });


    // iframe close
    $('.iframe .close').click(function() {
        $(this).parent().parent().parent().fadeOut(1000);
        $('#overlay').fadeOut(500, function () {
            $('#overlay').remove();
        });
        return false;
    });


    $('.closeThis').click(function() {
        var toggleBox = $(this).attr('href');
        $(toggleBox).fadeOut();
        return false;
    });

    // Input Button Hover
    jQuery(function($) {
        $('span.blueBdr').hover(function () {
            $(this).addClass('btnHover');
        }, function() {
            $(this).removeClass('btnHover');
        });
    });

    // Font resizing ========================
    //Create (or update) the value of a cookie to expire in 2 days:

    // COOKIES
    // User font size
    // on page load if cookie exists set font-size
    var userFontSize = $.cookie('userFontSize');
    if (userFontSize == null) {
        var currentFontSize = $('#contentWrapper').css('font-size');
    }
    else {
        var currentFontSize = $.cookie('userFontSize');
        $('#contentWrapper').css('font-size', userFontSize);
//        $('#footer').css('font-size', userFontSize);
    }

    // Increase Font Size
    $(".increaseFont").click(function() {
        // check to see if cookie exists
        var currentFontSize;
        if (userFontSize == null) {
            currentFontSize = $('#contentWrapper').css('font-size');
        }
        else {
            currentFontSize = $.cookie('userFontSize');
        }
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var largestFontSize = 18;

        if (currentFontSizeNum < largestFontSize - 1) {
            var newFontSize = Math.floor(currentFontSizeNum + 2);
            $('#contentWrapper').css('font-size', newFontSize);
//            $('#footer').css('font-size', newFontSize);
        }
        else {
            $('#contentWrapper').css('font-size', largestFontSize);
//            $('#footer').css('font-size', largestFontSize);
        }

        // set cookie
        var userFontSize = currentFontSize;
        $.cookie('userFontSize', currentFontSize, { expires: 10, path:'/' });
        return false;
    });
    // Decrease Font Size
    $(".decreaseFont").click(function() {
        // check to see if cookie exists
        var currentFontSize;
        if (userFontSize == null) {
            currentFontSize = $('#contentWrapper').css('font-size');
        }
        else {
            currentFontSize = $.cookie('userFontSize');
        }

        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var smallFontSize = 12;

        if (currentFontSizeNum > smallFontSize) {
            var newFontSize = Math.floor(currentFontSizeNum - 2);
            $('#contentWrapper').css('font-size', newFontSize);
//            $('#footer').css('font-size', newFontSize);
        }

        else {
            $('#contentWrapper').css('font-size', smallFontSize);
//            $('#footer').css('font-size', smallFontSize);
        }

        // set cookie
        var userFontSize = currentFontSize;
        $.cookie('userFontSize', currentFontSize, { expires: 10, path:'/' });
        return false;
    });


function getWindowWidth()
{
    if( typeof( window.innerWidth ) == 'number' )
    {
        myWidth = window.innerWidth;
    }
    else if( document.documentElement && document.documentElement.clientWidth )
    {
        myWidth = document.documentElement.clientWidth;
    }
    else if( document.body && document.body.clientWidth )
    {
        myWidth = document.body.clientWidth;
    }

    return myWidth;
}

    // Tool Tip/Product Info Bubble
    jQuery(function($) {
        $('a.tooltip').mouseover(function(e) {
            var popup_div = $(this).attr('rel');

            var tipPos = $(this).position();
            var popx = tipPos.left;
            var popy = tipPos.top;

            var wordWidth = $(this).width();
            var wordHeight = $(popup_div).height();

            leftVal = (e.pageX + 10);

            if( (leftVal + 350 ) > getWindowWidth())
            {
                leftVal = (e.pageX - 320);
                var el = $(popup_div).find('.tipArrow');
                el.addClass('tipArrowLeft');
                el.removeClass('tipArrow');
            }
            else
            {
                var el = $(popup_div).find('.tipArrowLeft');
                el.addClass('tipArrow');
                el.removeClass('tipArrowLeft');

                //alert('max: '  + ", width: " + getWindowWidth());
            }

            leftVal = leftVal + "px";
            topVal = (e.pageY - 28) + "px";

            $(popup_div).css({left:leftVal,top:topVal}).show().end();
            return false;
        });
        $('a.tooltip').mouseout(function() {
            var popup_div = $(this).attr('rel');
            $(popup_div).hover(function() {
                $(this).show();
            }, function() {
                $(this).hide();
            });
            $(popup_div).hide();
        });
    });

    // Product guide select
    $('#guideSelect').change(function() {
        var url = $(this).val();
        if (url != null && url != '') {
            location.href = url;
        }
    });


    // ##################################### RETRIEVER ###############################

    //alert('links: ' + $('.retrieverPopupLink'));
    jQuery(function($) {



        // iframe close
        $('.retrieverPopup .close').click(function() {
            $(this).parent().parent().parent().hide();
            return false;
        });

        $('.retrieverPopupLink').click(function() {

            var popup_div = null;

            //alert('test: ' + $('#retrieverSigninBox'));

            if ($('#retrieverSigninBox').length > 0) {
                popup_div = '#retrieverSigninBox';

                var url = window.location.href;
                if (url.indexOf('#') > 0) {
                    url = url.substring(0, url.indexOf('#'));
                }

                $('#retrieverSigninURL').attr('value', url + $(this).attr('rel'));
            }
            else {
                popup_div = $(this).attr('rel');
            }

            //alert('popup_div: ' + popup_div);
            //alert('div: ' + $(popup_div));

            var tipPos = $(this).position();
            var popx = tipPos.left;
            var popy = tipPos.top;
            var popupWidth = 300;

            leftVal = popx + 45 + "px";
            topVal = (popy - 20) + "px";

            //alert('popy: ' + popy + '\ntop: ' + topVal);

            //alert('$(popup_div): ' + $(popup_div));

            $(popup_div).css({left:leftVal,top:topVal}).show();


            return false;
        });
    });







    $('.iframe').bgiframe();
    $('#nav li.main ul').bgiframe();
    $('#nav li.last ul').bgiframe();







    // end ready function
});

// Rounded Corners
jQuery(function roundedCorners($) {
    $('.rounded').corner({
        tl: { radius: 5 },
        tr: { radius: 5 },
        bl: { radius: 5 },
        br: { radius: 5 },
        antiAlias: true
    });
    $('.roundedTop').corner({
        tl: { radius: 5 },
        tr: { radius: 5 },
        bl: { radius: 1 },
        br: { radius: 1 },
        antiAlias: true
    });
    $('.roundedBtm').corner({
        tl: { radius: 1 },
        tr: { radius: 1 },
        bl: { radius: 5 },
        br: { radius: 5 },
        antiAlias: true
    });
    
});



