﻿var blnBlueOpen;
var blnGreyOpen;

// For Chrome this code hides the ugly yellow autofill bars.  i can't do this thru CSS so have to do it this way.
//if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
//    $(window).load(function () {
//        $('input:-webkit-autofill').each(function () {
//            var text = $(this).val();
//            var name = $(this).attr('id');
//            $(this).after(this.outerHTML).remove();
//            $(name).find('input').val(text);
//        });
//    });
//}

$(document).ready(function () {

    var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
    if (mobile) {
        //alert("MOBILE");
        var style = '<link href="_css/layout_mobile.css" rel="stylesheet" type="text/css">';
        //$("head").append(style);
    }

    setHeaderRadio();
    setSlideDowns();
});


function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;        
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}


var x = "Global Scope";
var myeval = eval;
function getDoucmentMode() {
    var x = "Local Scope";
    return myeval("x");
}

function setHeaderRadio() {
    // get IE version to see if we need click event
    var ver = getInternetExplorerVersion();


    $('[type = radio]').each(function () {
        var input = this;
        var label = $('label[for=' + $(input).attr('id') + ']');

        // wrap the input + label in a div 
        $('').insertBefore(input).append(input, label);

        // find all inputs in this set using the shared name attribute   
        var allInputs = $("input[name='" + $(input).attr('name') + "']");

        $(label).click(function () {
            var labelClass = label.attr('class');
            
            allInputs.each(function () {
                $('label[for=' + $(this).attr('id') + ']').removeClass('checked');
            });
            
            if (labelClass != "checked")
                label.addClass('checked');

            if (ver > -1 && ver <= 8.0)
                input.click();
            else {
                if (getDoucmentMode() == "Local Scope")
                    input.click();
            }


            // add cookie
            var name = "DocFilters";
            var values = "";
            var expires = "";

            $('.custom-radio').each(function () {
                var checked = $(this).find("label[class='" + "checked" + "']")
                var value = $("input[id='" + $(checked).attr('for') + "']").attr('value');
                if (value === undefined) {
                    value = "";
                }
                values += value + '|';
            });

            document.cookie = name + "=" + values + expires + "; path=/";
        });

    });
}




function setSlideDowns() {

    $('#greyBar').click(function (e) {
        toggleGrey();
        if (blnBlueOpen)
            toggleBlue();
    });


    $('#blueBar').click(function (e) {
        toggleBlue();
        if (blnGreyOpen)
            toggleGrey();
    });
}

function toggleGrey() {
    $('div.GreyBar').slideToggle(900, 'easeInOutBack', function () {
        var origPos = Number($('#greyBarPull').css('top').replace('px', ''));
        var boxHeight = Number($('div.GreyBar').css('height').replace('px', ''));
        var bluePos = Number($('#blueBarPull').css('top').replace('px', ''));

        if (blnGreyOpen) {
            $('#greyBarPull').css('top', (origPos - boxHeight) + "px");
            $('#blueBarPull').css('top', (bluePos - boxHeight) + "px");
            $('.GreyBarTitle').css('display', 'block');
            $('.GreyCloseBtn').css('display', 'none');
            blnGreyOpen = false;
        } else {
            $('#greyBarPull').css('top', (origPos + boxHeight) + "px");
            $('#blueBarPull').css('top', (boxHeight + bluePos) + "px");
            $('.GreyBarTitle').css('display', 'none');
            $('.GreyCloseBtn').css('display', 'block');
            blnGreyOpen = true;
        }
    });
}

function toggleBlue() {
    $('div.BlueBar').slideToggle(900, 'easeInOutBack', function () {
        var blOrigPos = Number($('#blueBarPull').css('top').replace('px', ''));
        var blBoxHeight = Number($('div.BlueBar').css('height').replace('px', ''));

        if (blnBlueOpen) {
            $('#blueBarPull').css('top', (blOrigPos - blBoxHeight) + "px");
            $('.BlueBarTitle').css('display', 'block');
            $('.BlueCloseBtn').css('display', 'none');
            blnBlueOpen = false;
        } else {
            $('#blueBarPull').css('top', (blOrigPos + blBoxHeight) + "px");
            $('.BlueBarTitle').css('display', 'none');
            $('.BlueCloseBtn').css('display', 'block');
            blnBlueOpen = true;
        }
    });
}



function RadEditorChangeBackground(editor) {
    editor.GetContentArea().style.backgroundColor = "white";
    editor.GetContentArea().style.color = 'black';
}


function ToggleAllRecipients(chk) {
    $('input[type=checkbox][id!=chk_All]').each(function () {
        this.checked = chk.checked;
    });
}

function ToggleAll(chk, poolAbbrev) {
    $('.clients').each(function () {
        var item = $(this)

        item.find('input[type=checkbox]').each(function () {
            if (item.attr('class').toString().indexOf(poolAbbrev) > -1)
                this.checked = chk.checked;
        });
    });
}
