﻿$(function () {
    // Set custom configurations for Main Navigation
    var config = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
        interval: 1, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 0, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    $("ul#nav li .sub").css({ 'opacity': '0' });
    $("ul#nav li").hoverIntent(config);

    // Initialize Products & Services smart menu
    $.dropDownMenu();

    // use the label as default input text and hide the label
    $('#header input, #site-login input').inputSetter();
    $('#header input, #site-search input').inputSetter();

    // page print
    $('.utilities').prepend(core.printIcon);
    $('.print-this-page a').click(function () { core.print(); });
});

function CoreGlobal() {
    this.url = window.location;
    this.logo = '<img src="/App_Themes/default/img/logos/logo.png" alt="CoreLogic Logo"/>';
    this.printIcon = '<li class="print-this-page"><a href="#" title="Print This Page">print</a></li>';
    this.print = function () {
        if ($('#header .brand img').length < 1) {
            $('#header .brand').html(this.logo);
        }
        window.print();

    };
} //CoreGlobal

var core = new CoreGlobal;

// Main Nav functions
function megaHoverOver() {
    $(this).find(".sub").stop().fadeTo('fast', 1).show();

    //Calculate width of all ul's
    (function ($) {
        jQuery.fn.calcSubWidth = function () {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function () {
                rowWidth += $(this).width();
            });
        };
    })(jQuery);

    $(this).calcSubWidth();
    //Set Width
    $(this).find(".sub").css({ 'width': rowWidth });
}

function megaHoverOut() {
    $(this).find(".sub").stop().fadeTo('fast', 0, function () {
        $(this).hide();
    });
}


// Products & Services Smart Menu Function
$.dropDownMenu = function () {
    // Industries
    $(".nav-industries li").mouseover(function () {
        // Set nav select
        $(".nav-industries").find(".current").removeClass("current");
        $(this).addClass("current");
        // Set drop down contents
        $(".nav-solutions").find("div.selected").removeClass("selected").hide();
        $(".nav-solutions ." + $(this).find("a").attr("rel")).addClass("selected").show();
    });

    // Variables
    var dropdown = "";
    var dropdownTimeout = 400;
    var dropdownTimer = 0;
    var dropdownMenu = 0;

    // Open
    function dropdown_open() {
        dropdown_cancel_timer();
        dropdown_close();
        dropdownMenu = $(this).find('div.sub').show();
        $(this).addClass("hoverover");
    }
    // Close
    function dropdown_close(dropdown) {
        if (dropdownMenu) {
            dropdownMenu.hide();
        }
        $("li.nav-products").removeClass("hoverover");
    }
    // Timer
    function dropdown_timer() {
        dropdownTimer = window.setTimeout(dropdown_close, dropdownTimeout);
    }
    // Cancel Timeout
    function dropdown_cancel_timer() {
        if (dropdownTimer) {
            window.clearTimeout(dropdownTimer);
            dropdownTimer = null;
        }
    }

    // Our Action Listeners
    $("li.nav-products").bind('mouseenter', dropdown_open);
    $("li.nav-products").bind('mouseleave', dropdown_timer);
};

/*
Input Setter - pdf, 2008 isite design
pull label and insert as field. clear with click.	

ex: $("#sitesearch input").inputSetter(1); // makes default text lowercase
$("#sitesearch input").inputSetter(); // no lowercasing
*/
jQuery.fn.inputSetter = function (lower) {
    return this.each(function () {
        var $input = jQuery(this);
        var $label = jQuery("label[for='" + $input.attr("id") + "']");
        var labeltext = lower && lower == 1 ? $label.text().toLowerCase() : $label.text();
        $label.hide();
        var inputtext = $input.val();
        if (inputtext.length == 0)
            $input.val(labeltext);
        $input.focus(function () { if (this.value == labeltext) { this.value = ""; } })
			  .blur(function () { if (!this.value.length) { this.value = labeltext; } });
    });
};

/* 
share link functions
*/
function setShareLinks() {
    if (typeof(shareTitle) != 'undefined' && shareTitle != null) {
        // Email
        var emailURL = 'mailto:?subject=' + shareTitle + '&body=' + encodeURIComponent(location.href);
        if ($('#lnkEmail')) {
            $('#lnkEmail').attr('href', emailURL);
        }

        // Twitter
        var twitterURL = 'http://twitter.com/share/?url=' + encodeURIComponent(location.href) + '&text=' + shareTitle;
        if ($('#lnkTwitter')) {
            $('#lnkTwitter').attr('href', twitterURL);
            $('#lnkTwitter').attr('target', "_blank");
        }

        // Facebook
        var facebookURL = 'http://www.facebook.com/share.php?u=' + encodeURIComponent(location.href);
        if ($('#lnkFacebook')) {
            $('#lnkFacebook').attr('href', facebookURL);
            $('#lnkFacebook').attr('target', "_blank");
        }

        // linkedIn
        var linkedInURL = 'http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(location.href) + '&title=' + shareTitle + '&summary=' + shareDesc; ;
        if ($('#lnkLinkedIn')) {
            $('#lnkLinkedIn').attr('href', linkedInURL);
            $('#lnkLinkedIn').attr('target', "_blank");
        }
    }
}

// Opens new window
function openWindow(url) {
    var w = window.open(url, '', 'top=100,left=100,toolbar=0,status=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0');
    w.focus();
}

// Gets current year for footer copyright
function getYear() {
    var d = new Date();
    yr = d.getFullYear();
    document.write(yr);
}

// IE6 fixes
// make sure IE has the abbr and acronym tag
if (document.all) {
    document.createElement("abbr");
    document.createElement("acronym");
}
// prevent IE6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { }

// PNG transparency in Win IE 5.5 & 6
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
    var arVersion = navigator.appVersion.split("MSIE")
    var version = parseFloat(arVersion[1])
    if ((version >= 5.5) && (document.body.filters)) {
        for (var i = 0; i < document.images.length; i++) {
            var img = document.images[i]
            var imgName = img.src.toUpperCase()
            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                var imgID = (img.id) ? "id='" + img.id + "' " : ""
                var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                var imgStyle = "display:inline-block;" + img.style.cssText
                if (img.align == "left") imgStyle = "float:left;" + imgStyle
                if (img.align == "right") imgStyle = "float:right;" + imgStyle
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                img.outerHTML = strNewHTML
                i = i - 1
            }
        }
    }
}
//window.attachEvent("onload", correctPNG);

