function orderByCountrySelection() {

    $("#orderByCountryList li").click( function() {
        $(this).removeClass('hover');

        if( $(this).hasClass('selected') &&  ! $("#orderByCountryList").hasClass('expand') ) {
            $("#orderByCountryList").addClass('expand');
            $("#orderByCountryList li").removeClass('hide');

            return false; //stop bubbling li click -> body click ( check below on body.click() )
        }

        $(this).children("a").click();
    });

    $("#orderByCountryList li a").click( function() {
        if( $(this).parent().hasClass('selected') &&  ! $("#orderByCountryList").hasClass('expand') ){
            $("#orderByCountryList").addClass('expand');
            $("#orderByCountryList li").removeClass('hide');
            return false;
        } else if( $(this).parent().hasClass('selected') && $("#orderByCountryList").hasClass('expand') ){
            $("#orderByCountryList").removeClass('expand');
            $("#orderByCountryList li").addClass('hide');
            return false;
        }
        document.location = $(this).attr('href');
        return false;
    });

    $("#orderByCountryList li").hover( function() {
        if( !$("#orderByCountryList").hasClass('expand') ) {
            return;
        }

        $(this).addClass('hover');
    },
    function() {
        $(this).removeClass('hover');
    });

    $("#orderByCountryList .expand-list").click( function() {
        if( $("#orderByCountryList").hasClass('expand') ) {
            $("#orderByCountryList").removeClass('expand');
            $("#orderByCountryList li").addClass('hide');
        }
        else{
            $("#orderByCountryList").addClass('expand');
            $("#orderByCountryList li").removeClass('hide');
        }

        return false; //stop bubbling
    });

    $("body").click( function() {
        if( $("#orderByCountryList").hasClass('expand') ) {
            $("#orderByCountryList").removeClass('expand');
            $("#orderByCountryList li:not(.selected)").addClass('hide');
        }
    });
}

function monthsSelection() {

    $("#months_list li").click( function() {
        $(this).removeClass('hover');

        if( $(this).hasClass('selected') &&  ! $("#months_list").hasClass('expand') ) {
            $("#months_list").addClass('expand');
            $("#months_list li").removeClass('hide');
            return false; //stop bubbling li click -> body click ( check below on body.click() )
        }else if( $(this).hasClass('selected') && $("#months_list").hasClass('expand') ){
            $("#months_list").removeClass('expand');
            $("#months_list li").addClass('hide');
            return false;
        }
        document.location = $(this).children("a").attr('href');
        return false;
    });

    $("#months_list li a").click( function() {
        if( $(this).parent().hasClass('selected') &&  ! $("#months_list").hasClass('expand') ){
            $("#months_list").addClass('expand');
            $("#months_list li").removeClass('hide');
            return false;
        } else if( $(this).parent().hasClass('selected') && $("#months_list").hasClass('expand') ){
            $("#months_list").removeClass('expand');
            $("#months_list li").addClass('hide');
            return false;
        }
        document.location = $(this).attr('href');
        return false;
    });

    $("#months_list li").hover( function() {
        if( !$("#months_list").hasClass('expand') ) {
            return;
        }

        $(this).addClass('hover');
    },
    function() {
        $(this).removeClass('hover');
    });

    $("#months_list .expand-list").click( function() {
        if( $("#months_list").hasClass('expand') ) {
            $("#months_list").removeClass('expand');
            $("#months_list li").addClass('hide');
        }
        else{
            $("#months_list").addClass('expand');
            $("#months_list li").removeClass('hide');
        }

        return false; //stop bubbling
    });

    $("body").click( function() {
        if( $("#months_list").hasClass('expand') ) {
            $("#months_list").removeClass('expand');
            $("#months_list li:not(.selected)").addClass('hide');
        }
    });
}

function tips(){
    $('div.profile_company').hover(
      function(){ //on over
        Tip( $(this).children('span.full_company_name').text() );
      },
      function(){
        UnTip();
      }
    );
}

$(document).ready( function() {
    orderByCountrySelection();
    monthsSelection();
    tips();
});
