$(function(){
	$('li.school').css('cursor', 'pointer').click(function(){
		displayPopup('<strong>'+this.innerHTML + '</strong><ul>'+$(this).children('ul').html()+'</ul>');
	}).children('ul').hide();
});

function closePopup() {
	$('#popup').remove();
}

function displayPopup(html) {
	var cssObj = {
		backgroundColor: "#2EA443",
		border: "1px solid #000000",
		color: "#FFFFFF",
		left: "50%",
		marginLeft: "-150px",
		padding: "10px",
		position: "fixed",
		textAlign: "center",
		top: 360,
		width: "300px",
		zIndex: 9999
    }
	closePopup();
	$('<div>'+ html +'<p class="closer"><a href="javascript:closePopup()">Close</a></p></div>').attr('id', 'popup').addClass('popup').appendTo('.body.bullets');
}

$.fn.liveSelect = function() {
  
    $(this).each(function() {
       var model = $(this).attr('id');
       beforeHtml = '<div class="' + model + ' wrapper"><div class="selections"><p><strong>Current Records</strong></p><ul></ul></div><div class="options"><input type="text" class="live_search" name="' + model + '" /><ul></ul></div>';
       afterHtml = '</div>';
       $(this).before(beforeHtml);
       $(this).after(afterHtml);
       $(this).children('option:selected').each(function() {
            var id = $(this).attr('value');
            var title = $(this).html();
            var listItem = '<li>' + title + ' <a href="#" id="' + id + '">[Remove]</a></li>';
            $('.' + model + ' .selections ul').append(listItem);
       });
       $(this).hide();
       $('.' + model + ' .live_search').keyup(function() {
            var source = $('#' + model);
            var query = $(this).val();
            $('.' + model + ' .options ul li').remove();
            if(query.length > 2) {
                var results = $(source).children('option:contains(' + query + ')').not(':selected');
                $(results).each(function() {
                    var id = $(this).attr('value');
                    var title = $(this).html();
                    var listItem = '<li><a href="#" id="' + id + '">' + title + '</a></li>';
                    $('.' + model + ' .options ul').append(listItem);
                });
            }
       });
       $('.' + model + ' .options ul a').live('click',function() {
            var id = $(this).attr('id');
            var title = $(this).html();
            var listItem = '<li>' + title + ' <a href="#" id="' + id + '">[Remove]</a></li>';
            $('#' + model + ' option[value=' + id + ']').attr('selected','selected');
            $('.' + model + ' .selections ul').append(listItem);
            return false;
       });
       $('.' + model + ' .selections ul a').live('click',function() {
            var id = $(this).attr('id');
            $('#' + model + ' option[value=' + id + ']').removeAttr('selected');
            $(this).parents('li').remove();
            return false;
       });
    });
};
