$(document).ready(function() {

    // Set up the top hat
    topHat.init();
    topHat.reposition();

    // Add :external express to jQuery
    $.expr[':'].external = function(obj){
        return !obj.href.match(/^mailto\:/)
                && (obj.hostname != location.hostname);
    };

    // Bind external link checks to all external links
    $('a:external').click(function() {
        $.ajax({
            url: window.location.protocol + '//' + window.location.host + '/check_link/',
            data: {url: $.URLEncode($(this).attr('href'))},
            type: "POST"
        });
    });
    
    // Check the referer on page load
    if (document.referrer !== "")
    {
        $.ajax({
            url: window.location.protocol + '//' + window.location.host + '/ajax/referrer/',
            data: {ref: document.referrer, path:window.location.pathname},
            type: "POST"
        });
    }
    
});

$(window).resize(function() {
    topHat.reposition();
});


/**
 * A closure to contain all the topHat functionality
 */
var topHat = (function() {
    var pageTitle = $('#sectionInfo h3').text();
    var $topHat = $('#dating');
    var $datingText = $('.datingText')
    var $pageContent = $('#pageContent');

    /**
     * Repositions the top hat to be at the bottom of the page
     */
    function positionTopHat() {
        var footerHeight = $('#footerMain').outerHeight(true);
        var topHatHeight = $topHat.outerHeight(true);
        var docHeight = $(document).height();
            
        var elemTop = docHeight - footerHeight - topHatHeight;
        
        if ($topHat.length > 0)
        {
            $topHat.css('position', 'absolute')
                .css('top', elemTop + 'px')
                .css('left', ($pageContent.offset().left-10) + 'px')
                .css('color', '#003366')
                .css('width', $pageContent.outerWidth())
                .css('height', 'auto')
                .css('overflow', 'visible');
                
            $pageContent.css('paddingBottom', $topHat.outerHeight(true) );
        }

    }
    
    /**
     * Adds the div which is clicked on to toggle the display
     */
    function insertControl() {
        $topHat.prepend('<div id="datingControl">Click here to see more ' + pageTitle + ' information!</div>');
        this.$topHatControl = $('#datingControl');
    }
    
    /**
     * Changes the control text
     */
    function changeControlText() {
        if ( $datingText.is(':visible') ) {
            $topHatControl.text('Click here to see less ' + pageTitle + ' information');
        } else {
            $topHatControl.text('Click here to see more ' + pageTitle + ' information!');           
        }           
    }

    /**
     * Binds the functionality to the control
     */
    function bindControlFunction() {
        $topHatControl.click(function() {
            $datingText.slideToggle('slow', function() {
    
                $('#pageContent').css('paddingBottom', ($topHat.outerHeight(true)) );
                changeControlText() 
    
            });
        });
    }
    
    /**
     * A misnomer, but adds padding to #pageContent for proper display
     */
    function resizeTophat() {
        $pageContent.css('paddingBottom', $topHat.outerHeight(true) );
    }

    /* Return these functions as public, the rest as private */
    return {
        init: function() {
            positionTopHat();
            insertControl();
            bindControlFunction();
            resizeTophat();
            changeControlText();
        },
        reposition: function() {
            positionTopHat();
        }
    }
})();


$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});