﻿// jQuery mod functions
jQuery.fn.center = function() {
    this.css("position", "absolute");

    if(this.height() > $(window).height()) {
        this.css("top", $(window).scrollTop() + 50 + "px");
    } else {
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + -100 + "px");
    }
    
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

// Menu JS ---------------------
var timeout = 500;
var closetimer = 0;
var zoomenuitem = 0;
function zoo_open() {
    zoo_canceltimer();
    zoo_close();
    zoomenuitem = $(this).find('ul').eq(0).css('display', 'block');
}
function zoo_close()
{ if (zoomenuitem) zoomenuitem.css('display', 'none'); }
function zoo_timer()
{ closetimer = window.setTimeout(zoo_close, timeout); }
function zoo_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}
document.onclick = zoo_close;
// ------------------------------

//Modal Window function
function launchWindow(id) {
    
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    //transition effect
    $('#mask').fadeTo("slow", 0.6);
    $('#mask').fadeIn(1000);

    //Get the window height and width
    //var winH = $(window).height();
    //var winW = $(window).width();

    //Set the popup window to center
    //$(id).css('top', winH / 2 - $(id).height());
    //$(id).css('left', winW / 2 - $(id).width() / 2);
    $(id).center();
    
    //transition effect
    $(id).fadeIn(2000);
}

function close_div(div) {
    $("#" + div).hide();
    $('#mask, .window').hide();
    if (div == "flv_player") {
        $("#" + div).html('<div id="flv-object"></div>');
    }
}


// Change the fonts
function changeFont(size) {
    $(".content-main #content h2:not(.nochange)").css("font-size", (16 + parseInt(size)) + "px");
    $(".content-main #content h2:not(.nochange)").css("line-height", (18 + parseInt(size)) + "px");

    $(".content-main #content h3:not(.nochange)").css("font-size", (14 + parseInt(size)) + "px");
    $(".content-main #content h3:not(.nochange)").css("line-height", (16 + parseInt(size)) + "px");

    $(".content-main #content p:not(.nochange, .intro)").css("font-size", (12 + parseInt(size)) + "px");
    $(".content-main #content p:not(.nochange, .intro)").css("line-height", (18 + parseInt(size)) + "px");
    $(".ProductSection_template #content p:not(.nochange, .intro)").css("font-size", (15 + parseInt(size)) + "px");
    $(".ProductSection_template #content p:not(.nochange, .intro)").css("line-height", (18 + parseInt(size)) + "px");
    $(".content-main #content .references p:not(.nochange, .intro)").css("font-size", (12 + parseInt(size)) + "px");
    
    $(".content-main #content a:not(.nochange)").css("font-size", (12 + parseInt(size)) + "px");
    $(".content-main #content a:not(.nochange)").css("line-height", (18 + parseInt(size)) + "px");
    $(".ProductSection_template #content a:not(.nochange)").css("font-size", (15 + parseInt(size)) + "px");
    $(".ProductSection_template #content a:not(.nochange)").css("line-height", (18 + parseInt(size)) + "px");
    $(".content-main #content .references a:not(.nochange)").css("font-size", (12 + parseInt(size)) + "px");
    
    $(".content-main #content a.lg-link:not(.nochange)").css("font-size", (13 + parseInt(size)) + "px");

    $(".content-main #content li:not(.nochange)").css("font-size", (12 + parseInt(size)) + "px");
    $(".content-main #content li:not(.nochange)").css("line-height", (18 + parseInt(size)) + "px");
    $(".content-main #content .references li:not(.nochange)").css("font-size", (12 + parseInt(size)) + "px");
    
    $(".content-main #content span:not(.nochange)").css("font-size", (12 + parseInt(size)) + "px");
    $(".content-main #content span:not(.nochange)").css("line-height", (18 + parseInt(size)) + "px");
    $(".content-main #content .references span:not(.nochange)").css("font-size", (12 + parseInt(size)) + "px");

    $(".content-main #content sup:not(.nochange)").css("font-size", (9 + parseInt(size)) + "px");
    $(".content-main #content .references sup:not(.nochange)").css("font-size", (9 + parseInt(size)) + "px");
    
    $.cookie('txtSize', size);
}

// Document JS ------------------
$(document).ready(function() {

    // Check for survey - disabled for now
    /*if ($.cookie('NovoMedLinkSurvey') == null) {
        launchWindow('#dialog');
        var date = new Date();
        date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
        $.cookie('NovoMedLinkSurvey', 'true', { path: '/', expires: date });
    }*/
    

    // Nav and various dropdowns
    $('#nav-main ul.menu > li').bind('mouseover', zoo_open);
    $('#nav-main ul.menu > li').bind('mouseout', zoo_timer);
    $('#spec-menu > li').bind('mouseover', zoo_open);
    $('#spec-menu > li').bind('mouseout', zoo_timer);
    $('#pi-menu > li').bind('mouseover', zoo_open);
    $('#pi-menu > li').bind('mouseout', zoo_timer);

    // Dropdown nav
    $('#quint_dropdown').change(function() {
        if ($('#quint_dropdown option:selected[value]').attr('value') != "-- Select a Page --") {
            document.location = $('#quint_dropdown option:selected').attr('value');
        }
    });

    // HP-Accordian
    if ($("#HP-Accordian a.first").hasClass("selected")) {
        $("#HP-Accordian_top").css("background-image", "url(/Content/images/default/accordion/top_cap_on.gif)");
    }
    $("#HP-Accordian a.first").hover(function() {
        $("#HP-Accordian_top").css("background-image", "url(/Content/images/default/accordion/top_cap_on.gif)");
    });
    $("#HP-Accordian a.head:not(.first)").hover(function() {
        $("#HP-Accordian_top").css("background-image", "url(/Content/images/default/accordion/top_cap_off.gif)");
    });
    $("#HP-Accordian").accordion({
        active: '.selected',
        header: '.head',
        navigation: false,
        event: 'mouseover',
        autoheight: false,
        animated: 'easeslide'
    });

    // ORC-Drawers
    if ($("#ORC-Drawers a.first").hasClass("selected")) {
        $("#ORC-Drawers_top").addClass("top_cap_on");
    } else {
        $("#ORC-Drawers_top").addClass("top_cap_off");
    }
    $("#ORC-Drawers a.first").hover(function() {
        $("#ORC-Drawers a.head").removeClass("prev");
        $("#ORC-Drawers_top").addClass("top_cap_on");
    });
    $("#ORC-Drawers a.head:not(.first)").hover(function() {
        $("#ORC-Drawers_top").removeClass("top_cap_on");
        $("#ORC-Drawers_top").addClass("top_cap_off");
    });
    $("#ORC-Drawers a.head:not(.selected)").hover(function(e) {
        $("#ORC-Drawers a.head").removeClass("prev");
        var $liabove = $(this).parent().prev();
        $liabove.find("a.head").addClass("prev");
    });
    $("#ORC-Drawers").accordion({
        active: '.selected',
        header: '.head',
        navigation: false,
        event: 'mouseover',
        autoheight: true,
        animated: 'slide'
    });

    // Font changer
    if ($.cookie('txtSize')) {
        changeFont($.cookie('txtSize'));
    }
    $("#lg-txt").click(function() {
        changeFont(2);
    });
    $("#md-txt").click(function() {
        changeFont(1);
    });
    $("#sm-txt").click(function() {
        changeFont(0);
    });

    // Tabs
    $('#prod_tabs').tabs();


    // Helpful Links
    $('#helpful-links a.help-link').click(function() {
        $(this).siblings('ul').toggle();
        $(this).toggleClass("clicked");
        return false;
    });

    // Get return url for links
    $('a.getURL').each(function() {
        $(this).attr('href', $(this).attr('href') + window.location);
    });

    // Do Search
    $('#site-search-form').submit(function(e) {
        e.preventDefault();
        var term = $('#search-term').attr('value');
        document.location = "http://www.novomedlink.com/search/?output=xml_no_dtd&client=FrontEnd_NovoMedLink&site=NovoMedLink&q=" + term;
    });
    $('#site-search-submit').click(function(e) {
        e.preventDefault();
        var term = $('#search-term').attr('value');
        document.location = "http://www.novomedlink.com/search/?output=xml_no_dtd&client=FrontEnd_NovoMedLink&site=NovoMedLink&q=" + term;
    });

    // Turn error text red if it contains error:
    $("div.messages:contains('error')").css("color", "#F00");

    // Modal window controls
    //if close button is clicked  
    $('.window .close').click(function(e) {
        if ($(this).hasClass('go') == false) {
            //Cancel the link behavior  
            e.preventDefault();
        }
        $('#mask, .window').hide();
    });

    //if mask is clicked  
    $('#mask').click(function() {
        $(this).hide();
        $('.window').hide();
        $("#flv_player").html('<div id="flv-object"></div>');
    });

    // Flv player
    $(".swf-launcher").click(function(e) {
        e.preventDefault();
        $("#flv_player").html('<div id="flv-object"></div>');
        params = {};
        params.quality = 'high';
        params.wmode = 'opaque';
        params.salign = 't';
        vars = {};
        swfobject.embedSWF("/Content/swf/flv_player.swf?div_container=flv_player", "flv-object", "496", "300", "8", "/js/swfobject-2_2/expressInstall.swf", vars, params);
        launchWindow('#flv_player');
    });

});
