jQuery.noConflict();

jQuery(document).ready(function($) {
    // Create a case-insensitive contains function
    $.expr[':'].Contains = function(a,i,m){
        var nameFull = (a.textContent || a.innerText || "").toUpperCase();
        var nameSplit = nameFull.split(" ");
        m = trim(m[3].toUpperCase());

        if (nameFull != m) {
            if (nameFull.substring(0, m.length) != m) {
                for (var j = 0; j < nameSplit.length; j++) {
                    if (nameSplit[j].length >= m.length && nameSplit[j].substring(0, m.length) == m) {
                        return true;
                    }
                }
            } else {
                return true;
            }
        } else {
            return true;
        }
        return false;
    };

    // Store original employee list
    var originalEmployeeList = $('#employeeList').html();
    var modifiedEmployeeList;

    // Bind search function to keyword field
    $('#employeeFilter').bind('keyup', function() {
        search();
    });

    // Bind search function to title field
    /*$('#employeeTitleFilter').bind('change', function() {
        search();
    });*/


    // Search function
    function search() {
        var nameFilter  = $('#employeeFilter').val();
        var titleFilter = $('#title-link').val();
        // If nameFilter and titleFilter is reset, do not run search, but restore original list, see else below.
        if (nameFilter.length > 0 || titleFilter.length > 0) {
            // If titleFilter is active, tag all elements which are NOT part of the result with a class that dims
            if (titleFilter.length > 0) {
                $('#employeeList li.notInEmployeeResult').removeClass('notInEmployeeResult');
                $('#employeeList').find('span:not(:Contains(' + titleFilter + '))').parent().addClass('notInEmployeeResult');
                modifiedEmployeeList = $('#employeeList').html();
            } else {
                // If titleFilter is reset, remove dim class form all tagged items
                $('#employeeList li.notInEmployeeResult').removeClass('notInEmployeeResult');
                modifiedEmployeeList = $('#employeeList').html();
            }

            if (nameFilter.length == 1) {
                if (titleFilter.length > 0) {
                    $('#employeeList').html(modifiedEmployeeList);
                } else {
                    $('#employeeList').html(originalEmployeeList);
                }
            }

            // If titleFilter is set, we need to run a search that takes titleFilter into account (quicker search)
            if (titleFilter.length > 0) {
                var j = 0;
                // Loop over all items which have NOT been tagged with dim class, which mean it is not part of the result
                $('#employeeList li:not(.notInEmployeeResult)').each(function(idx, element) {
                    // We need to check if we need to take nameFilter into account
                    // If nameFilter is set, we filter out all those that doesn't fit the nameFilter and tag them with the dim class
                    var move = true;
                    if (nameFilter.length > 0) {
                        if ($(element).find('a:Contains(' + nameFilter + ')').length <= 0) {
                            $(element).addClass('notInEmployeeResult');
                            move = false;
                        } else {
                            $(element).removeClass('notInEmployeeResult');
                        }
                    }
                    // All items that are part of the result is moved to the top of the list
                    if (move) {
                        var data = $(element).html();
                        $(element).remove();
                        if (j <= 0) {
                            $('#employeeList').prepend('<li id="i' + j + '">' + data + '</li>');
                        } else {
                            $('<li id="i' + j + '">' + data + '</li>').insertAfter('#i'+(j-1));
                        }
                        j++;
                    }
                });
            } else {
                // titleFilter is not selected, run a search on all items (slower search)
                // Tag all items as not part of the result,
                // then remove that tag for items that are part of the result (done in loop below)
                // The reason for this way of doing it is to achieve the dim effect on search
                $('#employeeList li').addClass('notInEmployeeResult');

                // All items that are part of the result is moved to the top of the list
                $('#employeeList').find('a:Contains(' + nameFilter + ')').each(function(idx, el) {
                    $(this).removeClass('notInEmployeeResult');
                    var data = $(this).parent().parent().parent().html();
                    $(this).parent().remove();
                    if (idx <= 0) {
                        $('#employeeList').prepend('<li id="i' + idx + '">' + data + '</li>');
                    } else {
                        $('<li id="i' + idx + '">' + data + '</li>').insertAfter('#i'+(idx-1));
                    }
                });
            }
        } else {
            $('#employeeList').html(originalEmployeeList);
        }
    }

    // Trim whitespace function
    function trim (str) {
        var	str = str.replace(/^\s\s*/, ''),
                ws = /\s/,
                i = str.length;
        while (ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }

    $('a.lightbox-flickr').lightBox({
        fixedNavigation:true,
        imageLoading: '/themes/mccann/images/jquery-lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/themes/mccann/images/jquery-lightbox/jquery-lightbox-close.jpg',
        imageBtnPrev: '/themes/mccann/images/jquery-lightbox/jquery-lightbox-previous.jpg',
        imageBtnNext: '/themes/mccann/images/jquery-lightbox/jquery-lightbox-next.jpg'
    });

    $('a.lightbox-flickr-jobs').lightBox({
        fixedNavigation:true,
        imageLoading: '/themes/mccann/images/jquery-lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/themes/mccann/images/jquery-lightbox/jquery-lightbox-close.jpg',
        imageBtnPrev: '/themes/mccann/images/jquery-lightbox/jquery-lightbox-previous.jpg',
        imageBtnNext: '/themes/mccann/images/jquery-lightbox/jquery-lightbox-next.jpg'
    });

    $('.mccann-slideshow').lbListRotator({
        activeItemClass: 'active-rotation-item',
        effectTimer: 4000
    });
});
