﻿/// <reference path="http://pinnacle.localhost.com/js/jquery-1.3.2.js" />
/// <reference path="jquery.ba-url.js" />

// history and tabs ui integration
$(function() {

    // no tabs matching size
    matchHeight('#dnn_ContentPane'); // No Tabs
    matchHeight('#landingContent'); // Landing Page

    // alternating row colors
    $("#people .person:nth-child(even), #docs .document:nth-child(even), table[id*=ViewPeople]>tbody>tr:even").addClass("evenColor");
    $("#people .person:nth-child(odd), #docs .document:nth-child(odd), table[id*=ViewPeople]>tbody>tr:odd").addClass("oddColor");

    setupInteriorTabs();

    // add fixBroken code to all images on the document
    $('img').fixBroken();

    // initializes services interior pages
    //    initializeServices();

    // setup search box
    initializeSearchbox();
});

// matches height of left and right side elements of DOM
function matchHeight(id) {
    var position = $(id).offset();
    $('#line').css(position);

    $('#id1').height('100%');
    $('#id2').height('100%');

    var eli1 = $('#id1').height();
    var eli2 = $('#id2').height();

    if (eli1 < eli2) {
        $('#id1').height(eli2);
        $('#line').height(eli2);
    }

    if (eli1 > eli2) {
        $('#id1').height(eli1);
        $('#line').height(eli1);
    }
}

// creates tabbed history
function setupInteriorTabs() {

    // The "tab widgets" to handle.
    var tabs = $('#tabs'),

    // This selector will be reused when selecting actual tab widget A elements.
    tab_a_selector = 'ul.ui-tabs-nav a';

    // Enable tabs on all tab widgets. The `event` property must be overridden so
    // that the tabs aren't changed on click, and any custom event name can be
    // specified. Note that if you define a callback for the 'select' event, it
    // will be executed for the selected tab whenever the hash changes.
    tabs.tabs({
            show: function(event, ui) {
                matchHeight('#tabs');
            },
            event: 'change' 
            });

    // Define our own click handler for the tabs, overriding the default.
    tabs.find(tab_a_selector).click(function() {
        var state = {},

        // Get the id of this tab widget.
      id = $(this).closest('#tabs').attr('id'),

        // Get the index of this tab.
      idx = $(this).parent().prevAll().length;

        // Set the state!
        state[id] = idx;
        $.bbq.pushState(state);
    });

    // Bind an event to window.onhashchange that, when the history state changes,
    // iterates over all tab widgets, changing the current tab as necessary.
    $(window).bind('hashchange', function(e) {

        // Iterate over all tab widgets.
        tabs.each(function() {

            // Get the index for this tab widget from the hash, based on the
            // appropriate id property. In jQuery 1.4, you should use e.getState()
            // instead of $.bbq.getState(). The second, 'true' argument coerces the
            // string value to a number.
            var idx = $.bbq.getState(this.id, true) || 0;

            // Select the appropriate tab for this tab widget by triggering the custom
            // event specified in the .tabs() init above (you could keep track of what
            // tab each widget is on using .data, and only select a tab if it has
            // changed).
            $(this).find(tab_a_selector).eq(idx).triggerHandler('change');
        });
    })

    // Since the event is only triggered when the hash changes, we need to trigger
    // the event now, to handle the hash the page may have loaded with.
    $(window).trigger('hashchange');
}

// correct broken images on the site
$.fn.fixBroken = function() {
    return this.each(function() {
        var tag = $(this);
        var alt_img = 'images/broken-image.png';
        tag.error(function() { // this adds the onerror event to images
            tag.attr("src", alt_img); // change the src attribute of the image
            return true;
        });
    });
};

// enables/disables services based upon their content length
function ShowHideServices(id) {
    if ($('a[id=Service_' + id + '_Show]:contains("More ")').size() > 0)
    {
        $('a[id=Service_' + id + '_Show]:contains("More ")').each(function() { $(this).text($(this).text().replace("More ", "Less ")); });
        $('div[id=' + id + '].serviceBase p, div[id=' + id + '].serviceBase ul').slideDown();
    }
    else {
        $('a[id=Service_' + id + '_Show]:contains("Less ")').each(function() { $(this).text($(this).text().replace("Less ", "More ")); });
        $('div[id=' + id + '].serviceBase p:not(:first), div[id=' + id + '].serviceBase ul').slideUp();
    }

    $('div.serviceBase p:first-child').show();

    // update line height on right side of page
    matchHeight('#tabs');
}

// initializes services to a known good state
function initializeServices() {
    $('div.serviceBase p, div.serviceBase ul').hide();
    $('div.serviceBase p:first-child').show();
    //        $('a[id$=_Show]:contains("Less ")').each(function() { $(this).text($(this).text().replace("Less ", "More ")); });
}

// initialize search box
function initializeSearchbox() {
    $(document).ready(function() {
        $('#dnn_dnnSEARCH_txtSearch, #dnn_SEARCH2_txtSearch').addClass("idleField");
        $('#dnn_dnnSEARCH_txtSearch, #dnn_SEARCH2_txtSearch').focus(function() {
            $(this).removeClass("idleField").addClass("focusField");
            if (this.value == this.defaultValue) {
                this.value = '';
            }
            if (this.value != this.defaultValue) {
                this.select();
            }
        });
        $('#dnn_dnnSEARCH_txtSearch, #dnn_SEARCH2_txtSearch').blur(function() {
            $(this).removeClass("focusField").addClass("idleField");
            searchValue = $.trim(this.value);
            if (searchValue == '') {
                this.value = 'SEARCH';
            }
        });

        hrefValue = $("#dnn_dnnSEARCH_cmdSearch, #dnn_SEARCH2_cmdSearch").attr('href')
        $("#dnn_dnnSEARCH_cmdSearch, #dnn_SEARCH2_cmdSearch").replaceWith("<a href=\"" + hrefValue + "\"><img src=\"/images/icon-search.gif\" /></a>");
    });
}   

// scrolling util
(function(d) { var k = d.scrollTo = function(a, i, e) { d(window).scrollTo(a, i, e) }; k.defaults = { axis: 'xy', duration: parseFloat(d.fn.jquery) >= 1.3 ? 0 : 1 }; k.window = function(a) { return d(window)._scrollable() }; d.fn._scrollable = function() { return this.map(function() { var a = this, i = !a.nodeName || d.inArray(a.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) != -1; if (!i) return a; var e = (a.contentWindow || a).document || a.ownerDocument || a; return d.browser.safari || e.compatMode == 'BackCompat' ? e.body : e.documentElement }) }; d.fn.scrollTo = function(n, j, b) { if (typeof j == 'object') { b = j; j = 0 } if (typeof b == 'function') b = { onAfter: b }; if (n == 'max') n = 9e9; b = d.extend({}, k.defaults, b); j = j || b.speed || b.duration; b.queue = b.queue && b.axis.length > 1; if (b.queue) j /= 2; b.offset = p(b.offset); b.over = p(b.over); return this._scrollable().each(function() { var q = this, r = d(q), f = n, s, g = {}, u = r.is('html,body'); switch (typeof f) { case 'number': case 'string': if (/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)) { f = p(f); break } f = d(f, this); case 'object': if (f.is || f.style) s = (f = d(f)).offset() } d.each(b.axis.split(''), function(a, i) { var e = i == 'x' ? 'Left' : 'Top', h = e.toLowerCase(), c = 'scroll' + e, l = q[c], m = k.max(q, i); if (s) { g[c] = s[h] + (u ? 0 : l - r.offset()[h]); if (b.margin) { g[c] -= parseInt(f.css('margin' + e)) || 0; g[c] -= parseInt(f.css('border' + e + 'Width')) || 0 } g[c] += b.offset[h] || 0; if (b.over[h]) g[c] += f[i == 'x' ? 'width' : 'height']() * b.over[h] } else { var o = f[h]; g[c] = o.slice && o.slice(-1) == '%' ? parseFloat(o) / 100 * m : o } if (/^\d+$/.test(g[c])) g[c] = g[c] <= 0 ? 0 : Math.min(g[c], m); if (!a && b.queue) { if (l != g[c]) t(b.onAfterFirst); delete g[c] } }); t(b.onAfter); function t(a) { r.animate(g, j, b.easing, a && function() { a.call(this, n, b) }) } }).end() }; k.max = function(a, i) { var e = i == 'x' ? 'Width' : 'Height', h = 'scroll' + e; if (!d(a).is('html,body')) return a[h] - d(a)[e.toLowerCase()](); var c = 'client' + e, l = a.ownerDocument.documentElement, m = a.ownerDocument.body; return Math.max(l[h], m[h]) - Math.min(l[c], m[c]) }; function p(a) { return typeof a == 'object' ? a : { top: a, left: a} } })(jQuery);

// scrolling fix for IE
$(window).load(function() {
    $.scrollTo('html');
});

// url parameterizer
$.urlParam = function(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results == null ? null : results[1];
}

