﻿//************************************************************************************************************************************
// ADD NOFOLLOW TO EXTERNAL LINKS :- the following function (document.ready) will be called each time the page is loaded
//************************************************************************************************************************************

$(function() {
    $('a[href^=http://]').attr('rel', 'nofollow');
});



//************************************************************************************************************************************
// PEOPLE PAGE
//************************************************************************************************************************************

$(function() {

    $('#profiles li').click(function() {

        $li = $(this);


        $li.hide();

        if ($li.hasClass('collapse')) {

            //CLear any prev expanded items
            $('#profiles li.expand-1').switchClass('expand-1', 'collapse', 'fast');
            $('#profiles li.expand-2').switchClass('expand-2', 'collapse', 'fast');
            $('#profiles li.expand-3').switchClass('expand-3', 'collapse', 'fast');
            $('#profiles li.expand-4').switchClass('expand-4', 'collapse', 'fast');

            $li.removeClass('collapse');

            if ($('#profiles li:nth-child(4n-3)').index($li) !== -1) {
                $li.addClass('expand-1');
            }
            if ($('#profiles li:nth-child(4n-2)').index($li) !== -1) {
                $li.prev().insertAfter($li);
                $li.addClass('expand-2');
            }
            if ($('#profiles li:nth-child(4n-1)').index($li) !== -1) {
                $li.addClass('expand-3');
            }
            if ($('#profiles li:nth-child(4n)').index($li) !== -1) {
                $li.prev().insertAfter($li);
                $li.addClass('expand-4');
            }

            // height of 1 panel including margin = 160
            h = $li.height();
            h = Math.max(Math.ceil(h / 160) * 160, 320);
            h = h - 20;
            $li.height(h);

        }
        else {
            $li = $(this);
            //$li.removeClass('*');
            $li.removeClass('expand-1');
            $li.removeClass('expand-2');
            $li.removeClass('expand-3');
            $li.removeClass('expand-4');
            $li.addClass('collapse');
        }

        $li.show(1500);

    });
});



//************************************************************************************************************************************
// MAGIC LINE 
//************************************************************************************************************************************
$(function() {

    var $el, leftPos, newWidth,
        $mainNav = $('#nav'),
        pageOffset = $mainNav.offset().left;


    // Fixing the offset if the window changes size    
    $(window).resize(function() {
        pageOffset = $mainNav.offset().left;
    });

    /*
    NAV MAGIC LINE
    */
    $mainNav.append('<li id="magic-line"></li>');

    var $magicLine = $('#magic-line');

    $magicLine
        .width($('#navbar ul li a.active').width())
        .css('left', $('#navbar ul li a.active').offset().left - pageOffset)
        .data('origLeft', $magicLine.offset().left - pageOffset)
        .data('origWidth', $magicLine.width());

    $('#navbar ul li').hover(function() {
        $el = $(this);
        leftPos = $el.offset().left - pageOffset;
        newWidth = $el.width(); //.parent().width();

        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data('origLeft'),
            width: $magicLine.data('origWidth')
        });
    });

});
//*****************************************************************************************************************************



//    $(function() {

//        //-------------------------------------------------------------------------------------------------------------------------

//        //Remove the the active state underline while hovering on the ul
//        $('#navbar ul ').hover(function() {
//            $('#navbar ul li a.active').animate({
//                borderBottomColor: '#c0c0c0'
//            }, 'slow');
//        },
//        function() {
//            $('#navbar ul li a.active').animate({
//                borderBottomColor: '#260859'
//            }, 'slow');
//        });

//        //-------------------------------------------------------------------------------------------------------------------------

//        //Add the hover state underline to the a tag
//        $('#navbar ul li a').hover(function() {
//            //$(this).addClass('hover');
//            //$('#navbar ul li a').switchClass('hover', '', 300); //.removeClass('hover');
//            //$(this).switchClass('', 'hover', 300);

//            $(this).animate({
//                borderBottomColor: '#260859'
//            }, 'slow');


//        },
//        function() {
//            //$(this).removeClass('hover'); //$(this).css('border-bottom', '2px solid #c0c0c0');
//            //$('#navbar ul li a').switchClass('hover', '', 300); //.removeClass('hover');
//            $(this).animate({
//                borderBottomColor: '#c0c0c0'
//            }, 'slow');
//        });

//        //-------------------------------------------------------------------------------------------------------------------------


//    });




//*****************************************************************************************************************************

function initOverLabels() {

    if (!document.getElementById) return;

    var labels, id, field;

    // Set focus and blur handlers to hide and show 
    // LABELs with 'overlabel' class names.
    labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {

        if (labels[i].className == 'overlabel') {

            // Skip labels that do not have a named association
            // with another field.
            id = labels[i].htmlFor || labels[i].getAttribute('for');
            if (!id || !(field = document.getElementById(id))) {
                continue;
            }

            // Change the applied class to hover the label 
            // over the form field.
            labels[i].className = 'overlabel-apply';

            // Hide any fields having an initial value.
            if (field.value !== '') {
                hideLabel(field.getAttribute('id'), true);
            }

            // Set handlers to show and hide labels.
            field.onfocus = function() {
                hideLabel(this.getAttribute('id'), true);
            };
            field.onblur = function() {
                if (this.value === '') {
                    hideLabel(this.getAttribute('id'), false);
                }
            };

            // Handle clicks to LABEL elements (for Safari).
            labels[i].onclick = function() {
                var id, field;
                id = this.getAttribute('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            };

        }
    }
};

function hideLabel(field_id, hide) {

    var field_for;
    var labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {
        field_for = labels[i].htmlFor || labels[i].getAttribute('for');
        if (field_for == field_id) {
            labels[i].style.display = (hide) ? 'none' : 'block';
            return true;
        }
    }
};

window.onload = function() {
    setTimeout(initOverLabels, 50);
    addLinkTarget();
};

//Run on load to add target to all links
function addLinkTarget() {
    var url = document.URL.split('//'); // split at protocol
    var arrUrl = (url[1] ? url[1] : url[0]).split('/');
    var host = arrUrl[0];

    var links = document.getElementsByTagName('a');
    var length = links.length;
    for (i = 0; i < length; i++) {
        if (!links[i].href.match(/^javascript/i) && !links[i].href.match(host)) {
            links[i].target = '_blank';
        }
    }
}
