﻿//define an exists function for jquery to make life easier.
jQuery.fn.exists = function () { return jQuery(this).length > 0; }

/** ========================================================================
* SEARCHBOX
*/
function SetSearchBox(defaultSearchText) {
    var clickFunction = function () {
        if ($(this).val() == defaultSearchText) {
            $(this).val("");
        }
    };

    var blurFunction = function () {
        if ($(this).val() == "") {
            $(this).val(defaultSearchText);
        }
    }

    $('.search_text').bind('click', clickFunction);
    $('.search_text').bind('blur', blurFunction);
}

jQuery(document).ready(function () {

    //check that the ribbon bar is present. No need to resize if not.
    if ($('#RibbonBar\\.ascx_RB').exists()) {
        SetAdminSkinPosition();
        SetAdminSkinPositionUpdate();
    }

    $('.utility_print').click(function (e) {
        e.preventDefault();
        window.print();
    });
    $('.utility_large').click(function (e) {
        $('body').addClass('large-font');
        createCookie('large-font', true);
        resizeTiers();
    });
    $('.utility_small').click(function (e) {
        $('body').removeClass('large-font');
        eraseCookie('large-font');
        resizeTiers();
    });

    if (readCookie('large-font') == 'true') {
        $('body').addClass('large-font');
    }

    //hide the sub-nav if there are no child pages to display.
    if ($('div.nav_sub > div.inner > ul > li').size() == 0) {
        $('div.nav_sub').hide();
    }

    resizeTiers();
    SetBreadcrumbCurrent();

    /** ========================================================================
    * Collapseable Nav #nav_sub_special
    */
    $('.nav_sub_special .inner > ul > li > ul').hide();
    $('.nav_sub_special .inner > ul > li.open > ul').show();
    $('.nav_sub_special .inner > ul > li > a').click(function (e) {
        e.preventDefault();
        var p = $(this).parent('li');
        var l = $(this).next('ul');
        if (p.hasClass('open')) {
            l.slideUp(500, function () { p.removeClass('open'); });
        } else {
            p.addClass('open');
            l.slideDown(500);
        }
    });
    if (_isIE) {
        var dclear = $('<div style="clear:both;font-size: 0px;">&nbsp;</div>');
        $('.nav_sub_special .inner > ul').after(dclear);
    }

    //remove styles on CMS content
    //$('#dnn_ContentPane div.ModDNNHTMLC div.Normal *').removeAttr('style');


    //$("select").uniform();

    /** ========================================================================
    * HOME - ENHANCEMENTS
    */
    if ($('body').hasClass('home')) {
        match_heights();
    }

});       // end dom ready event

function LoadSlides() {
    time = 5000;

    slides = $('.slide');

    count = 0;

    //Hide all but the first slide
    $(".slide").not(":first").hide();

    //Add the dots
    $('.slide').each(function (i) {
        var pngfilter = ''
        $("#dots").append("<a href='#' id='dot_" + i + "'>" + i + "</a>");
    })

    if (_isIE6 && (typeof (DD_belatedPNG) != 'undefined')) {
        DD_belatedPNG.fix('#dots a');
    }

    //Make the first dot active
    $("#dots :first").addClass("selected");
    //Make the first Slide Active
    $(".slide:first").addClass("selected_slide");



    //Set up the trigger for the dots
    $("#dots a").click(function () {

        //Clear the timeout
        clearTimeout(slide_time_out);

        $(".selected_slide").stop().fadeOut(function () {
            $(this).removeClass("selected_slide");
        });
        $(".slide").eq($(this).index()).fadeIn().addClass("selected_slide");

        $("#dots a").removeClass("selected").eq($(this).index()).addClass("selected");

        return false;

    })

    //Start the animation

    slide_time_out = setTimeout(function () {

        clearTimeout(slide_time_out);

        change_slide();

    }, time);
}

var ribbonBarHeight = 0;

function SetAdminSkinPosition() {
    //check whether the ribbon height has changed. Otherwise DomSubTreeModified will do this buttloads of times.
    if (ribbonBarHeight != $('#RibbonBar\\.ascx_RB').height()) {
        var headerOffset = $('div.masthead').offset().top;
        $('body').css('background-position', '0px ' + headerOffset + 'px');
        ribbonHeight = 1;
        ribbonBarHeight = $('#RibbonBar\\.ascx_RB').height();
    }
}

function SetAdminSkinPositionUpdate() {
    var UpdateSkinPosition = function () {
        SetAdminSkinPosition();
    };

    $('#RibbonBar\\.ascx_RB').live('DOMSubtreeModified', UpdateSkinPosition);
}

_isIE = jQuery.browser.msie;
_isIE6 = (_isIE && parseInt(jQuery.browser.version) == 6);
/**
* IE6 PNG FIXES
*/

if (_isIE6 && (typeof (DD_belatedPNG) != 'undefined')) {
    var els = [
	           'img',
	           '.pngfix',
	           '#emergencies .tier .wrapper',
	           '#emergencies .tier .inner',
	           '#emergencies .tier .content',
	           'input.go',
	           'input.btn-view-docs'
	           ];

    DD_belatedPNG.fix(els.join(','));
}



function match_heights(c) {
    c = c || ".match_height";

    //Create an array of matched elements
    e = $.isArray(c) ? c : $(c);

    //Create an empty array to add the height of the elements to
    e_array = new Array();

    //Loop through the matched elements and add height to the array
    for (i = 0; i < e.length; i++) {
        e_array.push($(e[i]).outerHeight(true));
    }

    //Get the tallest height
    h = Math.max.apply(Math, e_array);

    //Set all the heightd
    $(c).css({
        height: h
    });
}

var slide_time_out;
function change_slide() {
    $("#dot_" + count).removeClass("selected");
    if (count == slides.length - 1) {
        count = 0;
    }
    else {
        count++;
    }


    $("#dot_" + count).addClass("selected");

    $(".slide").eq(count - 1).fadeOut("slow").removeClass("selected_slide");

    $(".slide").eq(count).fadeIn("slow", function () {


        slide_time_out = setTimeout(function () {

            clearTimeout(slide_time_out);

            change_slide();

        }, time);

    }).addClass("selected_slide");

}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

/**
* Special match height for col-2 of tiers
*/
function resizeTiers() {
    var tiers = $('.emergencies .tier');
    $.each(tiers, function (i, t) {
        var c2 = $(t).find('.col-2'),
			pt = $(c2[0]).css('padding-top').replace('px', ''),
			pb = $(c2[0]).css('padding-bottom').replace('px', ''),
			po = parseInt(pt) + parseInt(pb),
			h = $(t).height()
			;
        $(c2[0]).height(h - po);
    });
}

function SetBreadcrumbCurrent() {
    $('.breadcrumb .inner a').removeClass('nothing');
    $('.breadcrumb .inner a:last').addClass('current');
}


