$(document).ready(function () {
    $('select').select2({
        minimumResultsForSearch: Infinity
    });
    $('.status-img').select2({
        minimumResultsForSearch: Infinity,
        templateResult: addUserPic,
        templateSelection: addUserPic
    });
    $('.navbar-toggle').click(function () {
        $('.main-menu ul').slideToggle();
    });
    $('.expand-box span').click(function () {
        var $el = $(this);
        $el.parent().parent().next().slideToggle();
    });

    if ($(window).width() <= '767') {
        $('.section-btn .box-btn').attr('value', 'Proceed');
        $(".app-out-btn .box-btn").text('Verify and proceed')
    }
    
    $('.errorField input, .errorField select, .errorField textarea, .errorField .assocObjDesc').eq(0).focus();
    
    /*Set sidebar height*/
    $(window).load(function() {
      var sdh = $('.company-content-area').outerHeight();
      $('.tabing-sidebar').css('min-height',sdh);
    });
    $(window).resize(function(){
      var sdh = $('.company-content-area').outerHeight();
      $('.tabing-sidebar').css('min-height',sdh);
    });
});

function addUserPic(opt) {
    if (!opt.id) {
        return opt.text;
    }
    var optimage = $(opt.element).data('image');
    if (!optimage) {
        var $opt = $(
                '<span class="userName"><img src="images/' + $(opt.element).text().toLowerCase() + '.png"   class="userPic" /> ' + $(opt.element).text() + '</span>'
                );
        return $opt;
        
    } else {
        var $opt = $(
                '<span class="userName"><img src="' + optimage + '" class="userPic" /> ' + $(opt.element).text() + '</span>'
                );
        return $opt;
    }
};

equalheight = function (container) {

    var currentTallest = 0,
            currentRowStart = 0,
            rowDivs = new Array(),
            $el,
            topPosition = 0;
    $(container).each(function () {

        $el = $(this);
        $($el).height('auto')
        topPostion = $el.position().top;

        if (currentRowStart != topPostion) {
            for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                rowDivs[currentDiv].height(currentTallest);
            }
            rowDivs.length = 0; // empty the array
            currentRowStart = topPostion;
            currentTallest = $el.height();
            rowDivs.push($el);
        } else {
            rowDivs.push($el);
            currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
        }
        for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
            rowDivs[currentDiv].height(currentTallest);
        }
    });
}

$(window).load(function () {
    equalheight('.eq-height');
    equalheight('.eq-second-height');
});


$(window).resize(function () {
    equalheight('.eq-height');
    equalheight('.eq-second-height');
});

function readURL(input) {
    if (input.files && input.files[0]) 
    {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#client-logo').attr('src', e.target.result);
            $('#client-logo').removeAttr("width");
            $('#client-logo').attr("height", "90");
            $('input[name$=IsLogoDeleted]').val('false').change();
            $('.u-img-tag').show();
            $('#replace-btn').text('Replace');
        }
        reader.readAsDataURL(input.files[0]);
    }
}

function removeLogo() {
    $('#client-logo').removeAttr("src");
    $('input[name$=IsLogoDeleted]').val('true').change();
    $('.u-img-tag').hide();
    $('#replace-btn').text('Upload');
}