﻿// On page load functions

$(document).ready(function() {
    /***********************
    Collapsible panels 
    ***********************/
    $('#sitemap_cols').masonry({
        singleMode: true,
        columnWidth: 185,
        itemSelector: '.box'
    });

    //Hide (Collapse) the toggle containers on load
    $(".toggle_container").hide();

    $('h2.trigger').collapser({
        target: 'next',
        targetOnly: '.toggle_container',
        changeText: false,
        effect: 'fade'
    });

    //make external sites open up in a new window
    $('a[rel="external"]').attr({
        target: "_blank",
        title: "Opens in a new window"
    });

    /***********************
    Equal Height Divs 
    ***********************/

    var currentTallest = 0,
                    currentRowStart = 0,
                    rowDivs = new Array();

    function setConformingHeight(el, newHeight) {
        // set the height to something new, but remember the original height in case things change
        el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")));
        el.height(newHeight);
    }

    function getOriginalHeight(el) {
        // if the height has changed, send the originalHeight
        return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"));
    }

    function columnConform() {

        // find the tallest DIV in the row, and set the heights of all of the DIVs to match it.
        $('.sectionBox').each(function() {

            // "caching"
            var $el = $(this);

            var topPosition = $el.position().top;

            if (currentRowStart != topPosition) {

                // we just came to a new row.  Set all the heights on the completed row
                for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);

                // set the variables for the new row
                rowDivs.length = 0; // empty the array
                currentRowStart = topPosition;
                currentTallest = getOriginalHeight($el);
                rowDivs.push($el);

            } else {

                // another div on the current row.  Add it to the list and check if it's taller
                rowDivs.push($el);
                currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest);

            }
            // do the last row
            for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);

        });

    }

    $(window).resize(function() {
        columnConform();
    });

    function doSlash() {

        var i = 0;
        $('.otherNewsDataPagerPortfolio span, .otherNewsDataPager span').children('a, span').each(function() {
            var n = $('.otherNewsDataPagerPortfolio span a, .otherNewsDataPager span a').length;

            if (i < n) {
                $(this).after('<span class="gold_text pageslash">/</span>');
                i++
            }
        });

    };

    $(function() {

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            doSlash();
        };
    });


    // Dom Ready
    $(window).load(function() {
        columnConform();
        doSlash();
        $('.right_column > :last-child').css('border-bottom', 'none')
    });

});
