jQuery ( function ($) {
    /* Global Variables */
    var continueRegistration = $('#registerForm input[id=continue]').val();
    var url = window.location.href.replace('http://','');

    /* Filtering */
    $("#numberperpage").show();
    $("#accounttype",".filter").change(function(){
        var selected = $("#accounttype").val();
        if (selected == 'cd'){
            $("#term").show();
        }
        else{
            $("#term").hide();
        }
    });
    $("#locationtype",".filter").change(function(){
        var selected = $("#locationtype").val();
        if (selected == 'local'){
            $("#zip").show();
        }
        else{
            $("#zip").hide();
        }
    });
    $("#zip",".filter").focus(function(){
        var defaultZip = '-Enter Zip-';
        if (defaultZip == $("#zip",".filter").val()){
            $("#zip",".filter").val('');
        }
    });
    $("#zip",".filter").blur(function(){
        var zip = $("#zip",".filter").val();
        zip = zip.replace(/^\s*/, "");
        zip = zip.replace(/\s*$/, "");
        if (zip.length == 0){
            $("#zip",".filter").val('-Enter Zip-');
        }
    });
    $("#numberperpage").change(function(){
        var selected = $("#numberperpage").val();
        var url = new String(window.location);

        $.get(url,'cpp='+selected,function(response){
            //if (response == '1'){
            if (response.length > 0){
                window.location.reload();
            }
        },'text');
    });

    /* Small Rate Table */
    $("#recent",".smallRateTable").click(function(){
        $("#tabHighest",".smallRateTable").hide();
        $("#tabRecent",".smallRateTable").show();
        $("#highest",".smallRateTable").removeClass();
        $("#recent",".smallRateTable").addClass("selected");
        return false;
    });
    $("#highest",".smallRateTable").click(function(){
        $("#tabRecent",".smallRateTable").hide();
        $("#tabHighest",".smallRateTable").show();
        $("#recent",".smallRateTable").removeClass();
        $("#highest",".smallRateTable").addClass("selected");
        return false;
    });

    /* Main Rate Table - Overlays */
    $("#loginFormClose").click(function(){
        $("#loginForm").hide('slow');
        return false;
    });

    $("#hideForm").click(function(){
        $("#mainRateTable").hide('slow');
        $("#showMainRateTable").show('slow');
        return false;
    });

    $("#hideErrors").click(function(){
        $("#rateTableErrors").hide('slow');
        return false;
    });

    $("#showMainRateTable").click(function(){
        $("#mainRateTable").show('slow');
        $("#showMainRateTable").hide('slow');
        return false;
    });

    $('#registerForm').submit(function(){
        if ($('#mailinglist:checked').val() !== '1' && continueRegistration == 'false'){
            $('#optoutModal').jqmShow();
            return false;
        }
        return true;
    });

    $('#optoutModal #optoutPrefButton').click(function(){
        continueRegistration = true;
        if ($('#optoutModal input[name=optoutpref]:checked').val() == 'yes') { $('#registerForm input[id=mailinglist]').attr('checked', true); }
        $('#registerForm input[id=continue]').val("true");
        $('#optoutModal').jqmHide();
        $('#registerForm #submit').trigger('click');
    });

    $('#emailverificationModal #closeVerificationButton').click(function() {
        $('#emailverificationModal').jqmHide();
    });

    /* JQ Modal */
    var openSlow=function(hash) { hash.w.fadeIn('850'); };
    var closeSlow=function(hash) { hash.w.fadeOut('850',function(){ hash.o.remove(); }); };
    var locationUrl = '/location/' + $('#bankSlug').val() + '/';
    var ratecomparisonUrl = '/ratecomparison/' + $('#bankSlug').val() + '/' + $('#locationType').val() + '/' +$('#accountType').val() + '/';
    if ($('#term').val() != undefined && $('#term').val().length > 0){
        ratecomparisonUrl += $('#term').val() + '/';
    }
    var signupUrl = '/signup/register/';
    if($('#referrerModal').length > 0) {
        signupUrl += $('#referrerModal').attr('ref');
    }
    signupUrl += "?bg="+$('#signupBg').attr('ref');
    $('#signupModal').jqm({
        ajax: signupUrl, 
        trigger: 'a.showSignup',
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });
    $('#loginModal').jqm({
        ajax: '/signup/login' + $('#referrer').val(), 
        trigger: 'a.loginLink',
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });
    $('#optoutModal').jqm({
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });
     $('#seeTermsModal').jqm({
        trigger: 'a.seeTerms',
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });
    $('#locationModal').jqm({
        ajax: locationUrl , 
        trigger: 'a#locationLink',
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });
    $('#ratecomparisonModal').jqm({
        ajax: ratecomparisonUrl , 
        trigger: 'a#ratecomparisonLink',
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });
    $('#emailverificationModal').jqm({
        overlay: 80,
        onShow: openSlow,
        onHide: closeSlow
    });

    if($('#emailverificationModal').length > 0) {
        $('#emailverificationModal').jqmShow();
    }

    if($('#referrerModal').length > 0) {
        $('#signupModal').jqmShow();
    }

    $('a.socialmedia').click( function (e) { 
        e.preventDefault();
        if (!($(this).is('[tooltype]') && $(this).is('[url]') && $(this).is('[message]'))) {
            // do nothing if the above attributes are not present
            return false;
        }
        var type = $(this).attr('tooltype');
        var url  = $(this).attr('url');
        var message = $(this).attr('message');
        var destination = '';
        jQuery.ajax({
            url: '/social-tools/'+message+'/'+url,
            async: false,
            cache: false,
            success: function(data){ 
                switch (type) { 
                    case 'tw':
                        destination = 'http://twitter.com/?status=' + escape(data);
                    break;
                 }
             }
        });
        window.open(destination);
        return false;
    });

    $('#resendVerification','#rateTableErrors').click( function () {
        $('#registerForm').append('<input type="hidden" name="resend" value="true"/>');
        $('#registerForm #submit').trigger('click');
    });
});
