﻿var cornerAlertTemplate = "";
var allowCornerAlert = true;
var isCornerAlertHidden = false;
var numContentPages = 0;
var articleAllowExecutePaging = false;
var prevClickedPage = -1;
var lastScrollTop = 0;
var isScrollDown = false;
var hasCornerAlertBeenTracked = false;

$(document).ready(function() {

    initMenus();
    initInPageLinks();

    var videoModal = $('#OverlayContainer').overlay({
        expose: {
            color: 'black',
            opacity: 0.9
        },
        effect: 'apple',
        onClose: function() {
            $("#exposeMask").fadeOut(); $("#Player").html("");
        },
        closeOnClick: true,
        api: true
    });


    $('a.youtube').click(function() {

        var videoUrl = $(this).attr('href');
        var videoId = $(this).attr('title');
        var atts = $(this).attr('title');
        var flashvars = {};
        var params = {
            allowFullScreen: "true",
            allowscriptaccess: "always"
        };

        var html = "<iframe type='text/html' width='640' height='385' src='http://www.youtube.com/embed/" + videoId + "?autoplay=1&fs=1&color1=0x3a3a3a&color2=0x999999&rel=0' frameborder='0'></iframe>";
        html += "<a href='http://www.youtube.com/NebraskaMedCenter#p/a/f/" + videoId + "'>Visit The Nebraska Medical Center YouTube channel to watch or comment on our other videos</a>";
        $("#Player").html(html);

        videoModal.load();

        try { _gaq.push(['_trackEvent', 'YouTubeOverlay', videoId]); } catch (e) { }

        return false;
    });

    $("a.flowplayer").click(function() {

        var href = $(this).attr('href');
        var title = $(this).attr('title');
        var rel = $(this).attr('rel');

        var vid = $f("Player", VirtualDirectory + "/App_Files/js/jquery/flowplayer/flowplayer-3.2.5.swf",
        {
            clip: {
                url: href,
                autoPlay: true,
                onStart: function() { try { _gaq.push(['_trackEvent', 'FlowPlayerOverlay', href]); } catch (e) { } },
                title: title
            }
        });

        vid.load();
        videoModal.load();

        return false;
    });

    $("div.hero-content a").click(function() { var title = $(this).attr('title'); try { _gaq.push(['_trackEvent', 'Hero-Link', title]); } catch (e) { } });

    $(window).scroll(function() {
        try {
            var windowHeight = $(window).height(),
            windowWidth = $(window).width(),
            documentHeight = $(document).height(),
            documentWidth = $(document).width(),
            scroll = $(window).scrollTop();

            if (scroll > lastScrollTop) {
                isScrollDown = true;
            } else {
                isScrollDown = false;
            }
            lastScrollTop = scroll;

            if (isScrollDown && allowCornerAlert && (windowHeight + scroll + 600 > documentHeight)) {
                $("#Corner-Alert").show("slow");
                isCornerAlertHidden = false;

                if (!hasCornerAlertBeenTracked) {
                    hasCornerAlertBeenTracked = true;
                    try { _gaq.push(['_trackEvent', 'CornerAlert', 'Show', cornerAlertTemplate]); } catch (e) { }
                }
            }
            else if (!isScrollDown && !isCornerAlertHidden && (windowHeight + scroll + 600 < documentHeight)) {
                $("#Corner-Alert").hide("slow");
                isCornerAlertHidden = true;
            }
        }
        catch (e)
        { }
    });

    $("#Corner-Alert button").click(function() {
        $("#Corner-Alert").hide("fast");
        _gaq.push(['_trackEvent', 'CornerAlert', 'Close by Click', cornerAlertTemplate]);
        allowCornerAlert = false;
    });

    $("#Corner-Alert a.next-article").click(function() { _gaq.push(['_trackEvent', 'CornerAlert', 'Click Next Article', cornerAlertTemplate]); });
    $("#Corner-Alert a.read-more").click(function() { _gaq.push(['_trackEvent', 'CornerAlert', 'Click Read More', cornerAlertTemplate]); });
    $("#Corner-Alert a.our-services").click(function() { _gaq.push(['_trackEvent', 'CornerAlert', 'Click Our Service', cornerAlertTemplate]); });
    $("#Corner-Alert a.find-physician").click(function() { _gaq.push(['_trackEvent', 'CornerAlert', 'Click Find Physician', cornerAlertTemplate]); });

    $("div#Accordion h3 a").click(function() { try { var url = window.location.pathname + '/' + $(this).text().replace(' ', '-'); _gaq.push(['_trackPageview', url]); } catch (e) { } });
});

function initInPageLinks() {
    $("div.skipnav > a[href^='#']")
    .click(function(evt) {
        var j = $(evt.currentTarget);
        var anchorTarget = j.attr("href");
        $("body")
            .scrollTo(anchorTarget, 500, {
                onAfter: function() {
                    window.location.hash = anchorTarget.substr(1);
                    $(anchorTarget).attr("tabindex", -1).focus();
                }
            });

        evt.preventDefault();
    });
}

function initMenus() {
    var side_config = {
        over: showMenu, // function that happens when you mouse over the item
        timeout: 200, // the amount of time it takes before the out function is called
        interval: 200, // the amount of time it takes to run the over function based on how long the mouse is over the target
        sensitivity: 1, // how sensitive the hover is to mouse movement
        out: hideMenu // function that happens when you move the mouse off the item
    };
    $('ul.menu li.parent:not(.current) a.parent-arrow').click(function() {
        var parentListItem = $(this).parent('li');
        $(parentListItem).toggleClass("selected unselected");
        return false;
    });
}

function showMenu() {$(this).removeClass('unselected').addClass('selected'); }
function hideMenu() { $(this).removeClass('selected').addClass('unselected'); }

function createYouTubePlayer(videoId) {
    params = { allowScriptAccess: "always" };
    atts = { id: videoId };
    swfobject.embedSWF("http://www.youtube.com/v/" + videoId + "?enablejsapi=1&autoplay=1&hl=en_US&fs=1&color1=0x3a3a3a&color2=0x999999", "Player", "640", "385", "8", null, null, params, atts);
}

function getAccSlide(){
    var active = new Number();
    active = 1;
    try { var hash = window.location.hash; active = parseInt(hash.replace("#acc", "")); } catch (e) { }
    return (isNaN(active)) ? false : active;
}

function Acc(){
    $("#Accordion").accordion({ header: 'h3', collapsible: true, autoHeight: false, active: getAccSlide() });
    $("#Accordion a").click(function() { window.location.hash = this.hash; });
}

var iframeHeight = function(data) {
    //these two lines of code should reference the ID of the iframe that points to the Stelter site
    var heightData = parseInt(data);
    if (heightData == NaN) {
        heightData = 1000;
    }
    $("iframe#stelter-iframe").height(heightData);
    //var oldIframe = $("iframe#stelter-iframe");
    //var newIframe = $(oldIframe).clone().height(heightData).attr('scrolling', 'no');
    //oldIframe.replaceWith(newIframe);
};

function articlePaginationClick(new_page_index, pagination_container) {

    //DUE TO WINDOW ONCHANGE CALL, THIS CALLBACK IS EXECUTED TWICE. SET VARIABLE SO THAT THE CODE IS ONLY EXECUTED
    //THE FIRST TIME (THE FIRST TIME THE CURRENT PAGE NUMBER CHANGES)
    var currentPage = articleCurrentPage();
    if (currentPage.toString() != prevClickedPage.toString()) {
        var runScroll = (prevClickedPage > -1); //DON'T RUN SCROLLER IF FIRST VISIT TO PAGE

        prevClickedPage = currentPage;

        $(".pub-detail #Pagination").show();
        //DON'T SHOW ARTICLE PAGER OR CORNER ALERT IF NOT ON LAST PAGE OF ARTICLE
        if (new_page_index < numContentPages - 1) {
            allowCornerAlert = false;
            $(".pub-detail #Issue-Pager").hide();
        }
        else {
            allowCornerAlert = true;
            $(".pub-detail #Issue-Pager").show();
        }

        //SHOW MAIN IMAGE ONLY ON FIRST PAGE
        if (new_page_index == 0) {
            $(".pub-detail #Main-Image").show();
        }
        else {
            $(".pub-detail #Main-Image").hide();
        }

        $(".pub-detail .page").hide();
        $(".pub-detail #Page-" + new_page_index.toString()).show();

        if (currentPage > 0) {
            $(".page-intro").text("(Page " + (currentPage + 1).toString() + " of " + numContentPages.toString() + ")");
            $(".page-intro").show();
        }
        else {
            $(".page-intro").hide();
        }
        
        if (runScroll) {
            $.scrollTo('.pub-detail #Top', 800);
        }
        
        trackPageView('/Page-' + (new_page_index+1).toString());
    }
    return true;
}

function articleCurrentPage() {
    var pgStr = window.location.hash.replace("#", "");
    var pgNum = new Number(-1);

    if (!isNaN(pgStr) || pgStr.length > 0) {
        pgNum = new Number(pgStr);
    }

    return pgNum;
}

function viewFullArticle(){
    $(".pub-detail .detail .page").show();
    allowCornerAlert = true;
    $(".pub-detail #Issue-Pager").show();
    $(".pub-detail #Pagination").hide();
    $(".page-intro").hide();
    $("a#Full-Article").hide();
    $.scrollTo('.pub-detail #Top', 800);
    trackPageView('/Full-Article');
    return false;
}

function trackPageView(suffix) {
    try {
        var url = window.location.pathname + suffix;
        _gaq.push(['_trackPageview', url]);
    }
    catch (e)
    { }
}

function isApple() {
    if (/iPad;.U/.test(navigator.userAgent) || /iPod;.U/.test(navigator.userAgent) || /iPhone;.U/.test(navigator.userAgent)) return true;
}

