$(document).ready(function(){
    
    function initFeaturedListings()
    {
    	$("#featured-listings .listing").mouseover(function(){
    		$(this).css('background', '#f2f2f2').css('cursor', 'pointer');
    	})
    	.mouseout(function(){
    		$(this).css('background', '#fff').css('cursor', 'normal');
    	})
    	.click(function(){
    		window.location = $(this).children('a').attr('href');
    	});
    	
    	$('.listing-details-item').mouseover(function(){
    		$(this).addClass('hover');
    	})
    	.mouseout(function(){
    		$(this).removeClass('hover');
    	});      
    }
    
    function initEmailLinks()
    {
        $('.email-link').click(function(e){
            var url = $(this).attr('href');
            $.post(url, {}, function(html){
                $.modal(html);
            });
            e.preventDefault();
        });
        
        $('#email-popup').click(function(e){
        	$.get('/email_results/general', function(html){
        		$.modal(html);
        	});
        	e.preventDefault();
        });
    }
    
    function initMediaLinks()
    {
		$('.media-popup').click(function(e){
			$.post($(this).attr('href'), {ajax: 'ajax'},
				function(html){
					$(html).modal({
						maxWidth:500
					});
				}
			);
			e.preventDefault();
		});    	
    }
    
    function initSortForms()
    {
    	$('.sort-form').each(function(){
    		var formObj = $(this);
    		$(this).find('select.sortBy').change(function(){
    			urlShell = formObj.find('input.urlShell').val();
    			sortBy = $(this).val();
    			window.location = urlShell.replace('*', sortBy);
    		});	
    	});
    }
    
    function init() 
    {
        initFeaturedListings();
        initEmailLinks();
        initMediaLinks();
        initSortForms();
    }
    
    init();
    
});
