﻿

// --------------------------------------------------------
// Initialize site scripts
// --------------------------------------------------------

$(document).ready(

	function () {

	    InterfaceManager.init();

	}

);


// --------------------------------------------------------
// Initialize interface manager
// --------------------------------------------------------

var InterfaceManager = {

    redirectUrl: '',

    UserAccount: null,

    ContentEditor: null,

    CommunityList: null,

    ModelList: null,

    QuickMoveInList: null,

    ModelHomeInvestmentList: null,

    disableForm: function (form)
    {
        $('input[type=submit]', form).attr('disabled', 'disabled');
        $('input[type=button]', form).attr('disabled', 'disabled');
        //$('select', form).attr('disabled', 'disabled');
        //$('input[type=text]', form).attr('readonly', 'readonly');
        //$('textarea', form).attr('readonly', 'readonly');        
    },
        
    enableForm: function(form)
    {
        $('input[type=submit]', form).removeAttr('disabled');
        $('input[type=button]', form).removeAttr('disabled');
        //$('select', form).removeAttr('disabled');
        //$('input[type=text]', form).removeAttr('readonly');
        //$('textarea', form).removeAttr('readonly');        
    }

};

InterfaceManager.init = function () {

    this.initGlobalControls();
    this.UserAccount.init();
    this.CommunityList.init();
    this.ModelList.init();
    this.QuickMoveInList.init();
    this.ModelHomeInvestmentList.init();
    this.ContentEditor.init();

};


// --------------------------------------------------------
// Global interface controls
// --------------------------------------------------------

InterfaceManager.initGlobalControls = function () {

    /*
    $("#contact-us-link").live("click", function () {

    if ($("input#community-id")) {
    var href = $(this).attr("href") + "&communityId=" + $("input#community-id").val();        
    $(this).attr("href", href);
    }

    });

    $("#contact-us-link").fancybox({

    'width': '75%',
    'height': '75%',
    'autoScale': false,
    'transitionIn': 'elastic',
    'transitionOut': 'elastic',
    'type': 'iframe',
    'onClosed': function () {
    if (InterfaceManager.redirectUrl.length > 0) {
    $(location).attr('href', InterfaceManager.redirectUrl);
    }
    }

    });
    $(".btn-contact-us").live('click', function (e) {

    e.preventDefault();
    $("#contact-us-link").trigger('click');
    return false;

    });
    */


    /*
    $("input#btn-saving-details").live('click', function (e) {

    e.preventDefault();
    var marketId = $("#saving-details-area :selected").val();
    $("#savings-details-link").attr('href', "/Home/SavingsDetails?MarketId=" + marketId);
    $("#savings-details-link").trigger('click');

    });

    $("#savings-details-link").fancybox({

    'width': '75%',
    'height': '75%',
    'autoScale': false,
    'transitionIn': 'elastic',
    'transitionOut': 'elastic',
    'type': 'iframe'

    });

    $(".fancybox-iframe").fancybox({

        'width': '75%',
        'height': '75%',
        'autoScale': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'type': 'iframe'

    });
    */

$('.socials>li>a.tweet').click(function(event) {
    var width  = 575,
        height = 400,
        left   = ($(window).width()  - width)  / 2,
        top    = ($(window).height() - height) / 2,
        url    = this.href,
        opts   = 'status=1' +
                 ',width='  + width  +
                 ',height=' + height +
                 ',top='    + top    +
                 ',left='   + left;
    
    window.open(url, 'twitter', opts);
 
    return false;
  });
  
};


// --------------------------------------------------------
// User account interface controls
// --------------------------------------------------------

InterfaceManager.UserAccount = {

    init: function () {

        $("a#share-content-send").live('click', InterfaceManager.UserAccount.submitShareContentSendForm);
        //$("a#contact-us-send").live('click', InterfaceManager.UserAccount.submitContactUsSendForm);
        //$("a#contact-us-create-account").live('click', InterfaceManager.UserAccount.submitContactUsCreateAccountForm);
        //$("a#contact-us-no-thanks").live('click', InterfaceManager.UserAccount.closeContactDialog);

        //$("a#register-create-account").live('click', InterfaceManager.UserAccount.submitRegisterUserForm);
        //$("a#update-account").live('click', InterfaceManager.UserAccount.submitUpdateUserForm);

        //InterfaceManager.enableForm('form#form-register-user-account');

    },

    submitShareContentSendForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/About/ShareContent");
        $(form).submit();

    },

    submitContactUsSendForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/About/ContactUs");
        $(form).submit();

    },

    submitContactUsCreateAccountForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/About/ContactUsCreateAccount");
        $(form).submit();

    },

    submitRegisterUserForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/Account/Register");
        $(form).submit();

    },

    submitUpdateUserForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/Account/UserProfile");
        $(form).submit();

    }

}



// --------------------------------------------------------
// Content editor interface controls
// --------------------------------------------------------

InterfaceManager.ContentEditor = {

    init: function () {

        //$("input#btn-content-editor-submit").live('change', InterfaceManager.CommunityList.submitCommunitySearchForm);

        /*
        $("#content-editor-link").fancybox({
            'width': '75%',
            'height': '75%',
            'autoScale': false,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'type': 'iframe',
            'onClosed': function () {
                document.location.reload(true);
            }
        });

        InterfaceManager.enableForm('form#form-content-editor');
        */
    },

    showContentEditorForm: function () {


    },

    submitContentEditorForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/Admin/ContentEditor");
        $(form).submit();

    }
}


// --------------------------------------------------------
// Community list interface controls
// --------------------------------------------------------

InterfaceManager.CommunityList = {

    init: function () {

        //$("#locations-search input#btn-community-search").live('click', InterfaceManager.CommunityList.submitCommunitySearchForm);
        //$("#locations-search select#community-distance-filter").live('change', InterfaceManager.CommunityList.submitCommunitySearchForm);
        //$("#locations-search select#community-home-type-filter").live('change', InterfaceManager.CommunityList.submitCommunitySearchForm);
        //$("#locations-search select#community-price-range-filter").live('change', InterfaceManager.CommunityList.submitCommunitySearchForm);
        //$("select#community-sort-by-filter").live('change', InterfaceManager.CommunityList.sortByCommunitySearchForm);

    },

    sortByCommunitySearchForm: function (e) {
        
        e.preventDefault();
        $('input[name="sortByOptionId"]').remove();
        $('#locations-search').append('<input type="hidden" name="sortByOptionId" value="' + $(this).val() + '" />');
        $('#locations-search').trigger('submit');
        return false;

    },

    submitCommunitySearchForm: function (e) {

        e.preventDefault();
        // Form doesn't need to be submitted when many attributes are changed
        // from other javascript method.
        if (!$('#locations-search').data("submit-locked")) {
            $('#locations-search').trigger('submit');
        }
        return false;

    }
}


// --------------------------------------------------------
// Model list interface controls
// --------------------------------------------------------

InterfaceManager.ModelList = {

    init: function () {

        //$("#form-home-type-models select#home-type-filter").live('change', InterfaceManager.ModelList.submitModelSearchForm);
        $("#form-home-type-models select#state-filter").live('change', InterfaceManager.ModelList.submitModelSearchForm);
        $("#form-home-type-models select#home-size-filter").live('change', InterfaceManager.ModelList.submitModelSearchForm);
        $("#form-home-type-models select#model-name-filter").live('change', InterfaceManager.ModelList.submitModelSearchForm);

        InterfaceManager.enableForm('form#form-home-type-models');

    },

    submitModelSearchForm: function (e) {

        e.preventDefault();
        $(this).parents('form:first').trigger('submit');

    }
}


// --------------------------------------------------------
// Quick move in list interface controls
// --------------------------------------------------------

InterfaceManager.QuickMoveInList = {

    init: function () {

        $("#form-quick-move-in select#home-type-filter").live('change', InterfaceManager.QuickMoveInList.submitQuickMoveInSearchForm);
        $("#form-quick-move-in select#state-filter").live('change', InterfaceManager.QuickMoveInList.submitQuickMoveInSearchForm);
        $("#form-quick-move-in select#home-size-filter").live('change', InterfaceManager.QuickMoveInList.submitQuickMoveInSearchForm);
        $("#form-quick-move-in select#model-name-filter").live('change', InterfaceManager.QuickMoveInList.submitQuickMoveInSearchForm);

        InterfaceManager.enableForm('form#form-quick-move-in');

    },

    submitQuickMoveInSearchForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/quick-move-in");
        $(form).submit();

    }
}


// --------------------------------------------------------
// Model Home Investment list interface controls
// --------------------------------------------------------

InterfaceManager.ModelHomeInvestmentList = {

    init: function () {

        $("#form-model-home-investment select#home-type-filter").live('change', InterfaceManager.ModelHomeInvestmentList.submitModelHomeInvestmentSearchForm);
        $("#form-model-home-investment select#state-filter").live('change', InterfaceManager.ModelHomeInvestmentList.submitModelHomeInvestmentSearchForm);
        $("#form-model-home-investment select#home-size-filter").live('change', InterfaceManager.ModelHomeInvestmentList.submitModelHomeInvestmentSearchForm);
        $("#form-model-home-investment select#model-name-filter").live('change', InterfaceManager.ModelHomeInvestmentList.submitModelHomeInvestmentSearchForm);

        InterfaceManager.enableForm('form#form-model-home-investment');

    },

    submitModelHomeInvestmentSearchForm: function (e) {

        e.preventDefault();
        var form = $(this).parents('form:first');
        InterfaceManager.disableForm(form);
        $(form).attr("action", "/model-home-investment");
        $(form).submit();

    }
}


// --------------------------------------------------------
// Misc interface controls
// --------------------------------------------------------

InterfaceManager.showDialog = function (id, url) {

    $("#" + id).fancybox({
        'href': url,
        'width': '75%',
        'height': '75%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe',
        'onClosed': function () {
            $(location).attr('href', '/About');
        }
    });

    return false;
}

InterfaceManager.closeDialogAndRedirect = function (id, isFrame, redirectUrl) {

    parent.$.fancybox.close();

    $(location).attr('href', redirectUrl);

    return false;
}



$('form[rel="external"]').live("click", function() 
{
    window.open( $(this).attr('href') );
    return false;
});

InterfaceManager.showDialog1 = function submitForm(formId) 
{

    newDialog = window.open("about:blank", "_form");
    document.forms[formId].target = "_form";
    document.forms[formId].submit();
    return false;

}


function distanceBetweenTwoPoints(lat1, lon1, lat2, lon2) 
{
    var R = 6371; // km (change this constant to get miles)
    var dLat = (lat2 - lat1) * Math.PI / 180;
    var dLon = (lon2 - lon1) * Math.PI / 180;
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
		Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
		Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    if (d > 1) return Math.round(d) + "km";
    else if (d <= 1) return Math.round(d * 1000) + "m";
    return d;
}

