$(function() { 
 
    $("a[rel]").overlay({ 
    	top: 40,
        expose: 'black', 
        onBeforeLoad: function() { 
             var wrap = this.getContent().find(".contentWrap"); 
             wrap.load(this.getTrigger().attr("href"), '', function() {
             	$(this).find('form').validate({});
             }); 
        } 
    }); 
    
    // Parse the URL to see and open the popup if there is a hash tags
    switch ($(document).url().attr('hash')) {
    	case 'stay-connected':
    		$('.stayconnected-btn a').click();
    		break;
    	case 'enter-to-win':
    		$('.entertowin-btn a').click();
    		break;
    	default:
    		break;
    }
    
    // Change the document hash tag when the popup open and close buttons are clicked.
    $('.stayconnected-btn a').click(function(){
    	$(document).url().attr('hash', 'stay-connected');
    });
    $('.entertowin-btn a').click(function(){
    	$(document).url().attr('hash', 'enter-to-win');
    });
    $('.close').click(function(){
    	$(document).url().attr('hash', '');
    });
});