/* A simple Modal Window object */

function close_modal() {
    
    //hide the mask
    $('.modal_bg').fadeOut(500);
    
    //hide modal window(s)
    $('.modal').fadeOut(500);
    
}

function populate_modal(content) {
		
		// empty the modal
		$('.modal').empty();    
		
		// fill the modal with new content
    $('.modal').append(content);
}

function show_modal() {

    //set display to block and opacity to 0 so we can use fadeTo
    $('.modal_bg').css({ 'display' : 'block', opacity : 0});
    
    //fade in the mask to opacity 0.8 
    $('.modal_bg').fadeTo(500,1);
     
     //show the modal window
    $('.modal').fadeIn(500);

    //get the height and width of the modal
    var mh = $('.modal').outerHeight();
    var mw = $('.modal').outerWidth();
    var wh = $(window).height();
    var ww = $(window).width();     
		
		//calculate top and left offset needed for centering
		var top = (wh - mh) / 2 + $(window).scrollTop();
		var left = (ww - mw) / 2;
		
		//apply new top and left css values 
		$('.modal').css({ 'top' : top, 'left' : left });
}

// this function is specifically for the colleges multipicker
$('.multipicker').live('click', function() {

		//console.log($(this));
		$(this).after('<img src="/assets/images/indicator.gif" class="load_img" alt="">');
		// when we first load the screen, we'll want to load any checked items
		// to start with
		// use the rel to get the field
		var field_name = $(this).attr('rel');
		checked_items = $('input[name="' + field_name + '"]').val();
    $('#temp_list').val(checked_items);
    
    content = $.ajax({
        //url: "/colleges/ajax_colleges_by_state",
        url: $(this).attr('href'), 
        global: false,
        type: "POST",
        data: ({ state : 'CA' }),
        dataType: "html",
        async: false,
        success: function(msg) { 
        	//alert(msg); 
        }
    }).responseText;
    
    populate_modal(content);
    
    $('.load_img').hide();
    
    $('.close_modal').click(function() {
        
        //use the function to close it
        close_modal();
        return false;
        
    });
    
    show_modal();
    return false;
        
});

$(window).load(function() {

		// this function display flash messages in the modal
		if ( ! $('.flash_message').is(':empty')) {
    		$('.modal').empty();
				$('.modal').append($('.flash_message').html());
				$('html, body').animate({ scrollTop : 0 }, 0);
				show_modal();
				$('.modal_bg').fadeTo(2000, 1).fadeOut();
		}
		
});

    

