/* 
Bridgeline Digital

Contents
1. MINOR ENHANCEMENTS
2. SLIDESHOWS
3. MODAL WINDOWS
4. TABBED CONTENT
5. ACCORDION CONTENT

/*  1. MINOR ENHANCEMENTS
------------------------------------*/
// append hover class to list items in bad browsers
$.fn.hoverClass = function(c) {
    return this.each(function() {
        $(this).hover(
            function() { $(this).addClass(c); },
            function() { $(this).removeClass(c); }
        );
    });
};

/*  2. SLIDESHOWS
------------------------------------*/
//brandtile slideshow
$slideshow = {
    timeout: 5000,      // time before next slide appears (in ms)
    slideSpeed: 500,   // time it takes to slide in each slide (in ms)
    tabSpeed: 500,      // time it takes to slide in each slide (in ms) when clicking through tabs
    fx: 'scrollLeft',   // the slide effect to use

    init: function() {
        // set the context to help speed up selectors/improve performance
        this.context = $('.brandtile');

        // set tabs to current hard coded navigation items
        this.tabs = $('ul.nav_panel li', this.context);

        // remove hard coded navigation items from DOM 
        // because they aren't hooked up to jQuery cycle
        this.tabs.remove();

        // prepare slideshow and jQuery cycle tabs
        this.prepareSlideshow();
    },

    prepareSlideshow: function() {
        // initialize the jquery cycle plugin -
        $(".tile_wrapper_out > .tile_wrapper", $slideshow.context).cycle({
            fx: $slideshow.fx,
            timeout: $slideshow.timeout,
            speed: $slideshow.slideSpeed,
            fastOnEvent: $slideshow.tabSpeed,
            pager: $("ul.nav_panel", $slideshow.context),
            pagerAnchorBuilder: $slideshow.prepareTabs,
            before: $slideshow.activateTab,
            pauseOnPagerHover: true,
            pause: true,
            cleartypeNoBg: true
        });
    },

    prepareTabs: function(i, slide) {
        // return markup from hardcoded tabs for use as jQuery cycle tabs
        // (attaches necessary jQuery cycle events to tabs)
        return $slideshow.tabs.eq(i);
    },

    activateTab: function(currentSlide, nextSlide) {
        // get the active tab
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $slideshow.context);

        // if there is an active tab
        if (activeTab.length) {
            // remove active styling from all other tabs
            $slideshow.tabs.removeClass('on');

            // add active styling to active button
            activeTab.parent().addClass('on');
        }
        else {
            if ($slideshow.tabs[0]) {
                $($slideshow.tabs[0]).addClass('on');
            }
        }
    }
};

// iAPPS Showcase slider
$('.showcase_inner').cycle({
    fx: 'scrollHorz',
    speed: 200,
    timeout: 0,
    next: '#next',
    prev: '#prev'
});

function formLoader() {
    $('#loading').css("visibility", "visible");
}



/*  3. MODAL WINDOWS
------------------------------------*/

/* ColorBox */
$(document).ready(function() {
    $.fn.colorbox.settings.opacity = 0.5;
    $("a.modal").colorbox({ transition: "fade", speed: 400 });
    $("a.movie_modal").colorbox({ iframe: true, innerWidth: 640, innerHeight: 483, transition: "fade", speed: 600, opacity: .8 });
    $("a.slideshow").colorbox({ previous: "&laquo; Previous", next: "Next &raquo;", slideshow: true, slideshowSpeed: 5000, slideshowStart: "Start Slideshow", slideshowStop: "Stop Slideshow" });
    $("a.meet").colorbox({ iframe: true, innerWidth: 640, innerHeight: 480, transition: "fade", speed: 600, opacity: .8 });
    $("a.omgeo").colorbox({ iframe: true, innerWidth: 1010, innerHeight: 700, transition: "fade", speed: 600, opacity: .8 });
    $("a.fma").colorbox({ iframe: true, innerWidth: 880, innerHeight: 560, transition: "fade", speed: 600, opacity: .8 });
    $("a.juliachildskitchen").colorbox({ iframe: true, innerWidth: 790, innerHeight: 535, transition: "fade", speed: 600, opacity: .8 });
    $("a.smithsoniansource").colorbox({ iframe: true, innerWidth: 780, innerHeight: 700, transition: "fade", speed: 600, opacity: .8 });
    $("a.johnadamslibrary").colorbox({ iframe: true, innerWidth: 820, innerHeight: 565, transition: "fade", speed: 600, opacity: .8 });
    $("a.johnhancock").colorbox({ iframe: true, innerWidth: 785, innerHeight: 585, transition: "fade", speed: 600, opacity: .8 });
    $("a.bankofnewyork").colorbox({ iframe: true, innerWidth: 600, innerHeight: 455, transition: "fade", speed: 600, opacity: .8 });
    $("a.tour").click(function() {
        $("#colorbox").addClass("tours");
    });
    $("a.modal,a.movie_modal,a.slideshow").click(function() {
        $("#colorbox").removeClass("tours");
    });
    $("a.tour").colorbox({ iframe: true, innerWidth: "900px", innerHeight: "500px", transition: "fade", scrolling: false });

});
var popUpVisible = false;
var iframePopUp = false;
var iFrameHeight = 0;

// Opacity Focused Layer
function setWindowSize() {
    var popUpWidth = $('.popup_outer').width();
    var popUpHeight = $('.popup_outer').height();
    if (iframePopUp) {
        popUpWidth = $('.popupIframe_outer').width();
        popUpHeight = $('.popupIframe_outer').height();
    }
    var docWidth = 0;
    var docHeight = 0;
    var marginWidth = 0;
    var marginTop = 0;
    var currentScrollPos = 0;

    // returns width of window $('body').width();
    // returns width of contents minimum and widow width max $(document).width();

    if ($('body').width() > $('#site').width()) {
        docWidth = $('body').width();
    } else {
        docWidth = $('#site').width();
    }

    // get height of the document whichever is taller
    if ($(window).height() > ($('#site').height() + 70)) {
        docHeight = $(window).height()
    } else {
        docHeight = $('#site').height() + 70;
    }

    $('.overlay').width(docWidth);
    $('.overlay').height(docHeight);

    // Center window horizontally
    if (docWidth > popUpWidth) {
        marginWidth = Math.round((docWidth - popUpWidth) / 2);
    }

    // Center window vertically

    if (document.body.scrollTop > 0) {
        currentScrollPos = document.body.scrollTop;
    } else {
        currentScrollPos = document.documentElement.scrollTop;
    }

    if (($(window).height() - popUpHeight) > 0) {
        marginTop = ($(window).height() - popUpHeight) / 2 + currentScrollPos;
    } else {
        marginTop = currentScrollPos;
    }

    if (iframePopUp) {
        $('.popupIframe_outer').css("margin-left", marginWidth);
        $('.popupIframe_outer').css("margin-top", marginTop);
    } else {
        $('.popup_outer').css("margin-left", marginWidth);
        $('.popup_outer').css("margin-top", marginTop);
    }
}

//had to make a special function to specifically hide flash videos on the popup
function toggleSWF(id) {
    var swf = swfobject.getObjectById(id);
    if (null != swf) {
        if (swf.className == "hidden") {
            swf.className = "";
        } else {
            swf.className = "hidden";
        }
    }
}

$(document).ready(function() {
    // initialize the slideshow when the DOM is ready
    $slideshow.init();

    if (document.all) {
        $("#nav li").hoverClass("sfhover");
        $("div.resource_list li").hoverClass("sfhover");
        $("div.careers_list li").hoverClass("sfhover");
    }

    // Bridgeline utility nav Charlotte hover
    $('.charlotte').hover(
		function() { $('.bridgeline').toggle(); },
		function() { $('.bridgeline').toggle(); }
	)

    // Add class to final team member in accordion
    $('.team .member:last-child').addClass('final_member');


    /*  4. PopUp Links
    ------------------------------------*/
    var linkArray = $('a.btn_open');
    var popupContent = $('.showcase_content');

    $(linkArray).click(function() {
    var arrayPos = $(linkArray).index(this);
        arrayPos = Math.round(arrayPos / 2 - .5);

        $('.popup_inner').html($(popupContent[arrayPos]).html());

        $('.popup_outer').css('visibility', 'visible');
        $('.overlay').css('visibility', 'visible');
        setWindowSize();

        // onclick show layer $('.popup_outer iframe').attr('src', $(this).attr('href'));
        return false;
    });



    /*  4. TABBED CONTENT
    ------------------------------------*/
    $(".tab_content").hide();
    $(".nav_tab li:first").addClass("active").show();
    $(".tab_content:first").show();

    $(".nav_tab li").click(function() {
        $(".nav_tab li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();

        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });

    /*  5. ACCORDION CONTENT
    ------------------------------------*/
    $(".careers_list").hide();
    
    $('a.iframeBtn').click(function() {
        iframePopUp = true;
        $('div.popupIframe_outer iframe').attr('src', $(this).attr('href'));
        return false;
    });

    // close button for standard pop up
    $('.popup_outer a.btn_close').click(function() {
        $('.popup_outer').css('visibility', 'hidden');
        $('.overlay').css('visibility', 'hidden');
        toggleSWF('movie'); //have to hide any flash movies manually
        return false;
    });

    // close button for standard pop up
    $('div.popupIframe_outer a.btn_close').click(function() {
        $('div.popupIframe_outer').css('visibility', 'hidden');
        $('div.overlay').css('visibility', 'hidden');
        $('div.popupIframe_outer iframe').attr('src', '/blank.html');
        $('select').css('visibility', 'visible');
    });

    // For other good browsers.
    $('#iFrameHolder').load(function() {
        iFrameHeight = this.contentWindow.document.body.offsetHeight;
        // check if iFrame has real content inside and show
        if ($('div.popupIframe_outer iframe').attr('src') != "/blank.html") {
            //$('div.loader').css('display', 'none');
            iframePopUp = true;
            loadingPopUp = false;
            $('div.popupIframe_outer').css('visibility', 'visible');
            $('div.overlay').css('visibility', 'visible');
        } else {
            iframePopUp = false;
            $('div.popupIframe_outer').css('visibility', 'hidden');
            $('div.overlay').css('visibility', 'hidden');
        }
        resizeIframe();
    });
});

function resizeIframe() {
    //change the height of the iframe
    $('div.popupIframe_outer iframe').height(iFrameHeight);
    setWindowSize();
}

var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(setWindowSize, 50);
});
