﻿// ======================
//    Glossary Tooltip
// ======================
function glossary_tooltip() {
    $(".glossary").tooltip({
        delay: 0,
        showURL: false,
        fixPNG: true,
        opacity: 0.95,
        fade: 50,
        top: -25,
        left: 20
    });
}
// ======================
//    File Icons
// ======================	
function filetype_icons() {
    /* Add .pdf icon to links */
    $("a[href$='.pdf']", "#wrap-content").addClass('pdf');
    /* Add .xls icon to links */
    $("a[href$='.xls']", "#wrap-content").addClass('xls');
    /* Add .ppt icon to links */
    $("a[href$='.ppt']", "#wrap-content").addClass('ppt');
    /* Add .doc icon to links */
    $("a[href$='.doc']", "#wrap-content").addClass('doc');
}
// =============================
//    Clear Input Pre-set Text
// =============================
function initText() {
    $('input.text').each(function() {
        var initValue = this.value;
        this.onfocus = function() { this.value = ''; }
        this.onblur = function()
		{ 
			if(this.value == "")
			{			
				this.value = initValue; 
			}
		}
    });
}

// =========================================
// Make external links open in a new window 
// =========================================
function new_window() {
    // add external icon and open in a new window for external links
    $('a[href^=http]:not([href*="' + window.location.hostname + '"]):not(:has(img))').addClass('external-link').attr('target', '_blank');
    $('a.external-link', '#content_col2').addClass('external');

    $("a[href*=.pdf]").attr({ "target": "_blank" });

}

// ================================
//    Text Resize / Print / Email
// ================================
function print_resize() {
    //js is working, so show text resize buttons
    $("#print-share").show();
    $("#footer-nav").addClass("js");

    //Small Text size
    $('li.text-small').click(function() {
    if ($('body').hasClass("font-large") || $('body').hasClass("font-med")) {
            $("body").removeClass("font-large").removeClass("font-med").addClass("font-small");
        }
        else {
            $("body").addClass("font-small");
        }

    });
    //Medium Text size
    $('li.text-med').click(function() {
        if ($('body').hasClass("font-large") || $('body').hasClass("font-small")) {
            $("body").removeClass("font-large").removeClass("font-small").addClass("font-med");
        }
        else {
            $("body").addClass("font-med");
        }

    });
    //Large Text size
    $('li.text-large').click(function() {
        if ($('body').hasClass("font-med") || $('body').hasClass("font-small")) {
            $("body").removeClass("font-med").removeClass("font-small").addClass("font-large");
        }
        else {
            $("body").addClass("font-large");
        }

    });
    
    // Add Hover Class
    $('li.text-small, li.text-med, li.text-large, li.print, li.email').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    //print button action
    $('li.print').click(function() {
        window.print();
    });
 
    //Email
    $('li.email').click(function() {

        var ThisPage = location.href;
        var subject = document.title;
        var body_message = "Thought you might like to see this page:%0D%0D" + ThisPage;

        var mailto_link = 'mailto:' + '?subject=' + subject + '&body=' + body_message;

        win = window.open(mailto_link, 'emailWindow');
        if (win && win.open && !win.closed) win.close();
    });
}
// =========================================
// Accordion
// =========================================

function accordion() {
    //hide the all of the element with class msg_body
    $(".questions > dd, .subcontent, #video-files ul").hide();
    
    //toggle the componenet with class msg_body
    $(".questions > dt, #video-files h4").click(function() {
        $(this).next(".questions > dd").slideToggle(100);
    });
    $(".toggle").click(function() {
        $(this).next(".subcontent").slideToggle(100);
    });
    $("#video-files h4").click(function() {
        $(this).next("#video-files ul").slideToggle(100);
    });
    // Add Hover Class
    $('.questions > dt, .toggle, #video-files h4').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });
}

// =========================================
// Form Validation
// =========================================

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
function formValidate(form) {
    var errors = '';
    // Loop through required fields
    $('input.required, textarea.required, select.required', form).each(function(i) {
        if($(this).val() == '') {
            errors += 'error-'+i;
        }
    });
    // Loop through email fields
    $('input.required-email', form).each(function() {
        var isValid = isValidEmailAddress($(this).val());
        if(!isValid) {
            errors = 'bad-email';
        }
    });
    // Report errors, if any
    if(errors.length) {
        if(errors == 'bad-email') {
            window.alert('You must enter a valid email address.');
        }
        else {
            window.alert('Please complete all required fields.');
        }
        return false;
    }
    else {
        return true;
    }
}

// =========================================
// Start it up
// =========================================

$(document).ready(function() {

    /* Initialize Glossary Tooltip*/
    glossary_tooltip();

    /* Initialize Icons */
    filetype_icons();

    /* Clear Forms on focus/restore on blur */
    initText();

    /* Open external links in a new window */
    new_window();

    /* Enable text resize */
    print_resize();

    /* Enable Accordion */
    accordion();
    
    /* Set video to BVD*/
    //bovine_video();
    
    /* Apply form validation */
    if($('input.required', '#wrap-content').length) {
        $('form#aspnetForm').submit(function() {
            return formValidate(this);
        });
    }

});

