﻿jQuery(document).ready(function() {

    //FANCYZOOM
    var isFancyZoom = false;
    isFancyZoom = (jQuery("#screenshot a").fancybox({
        'imageScale': false,
        'centerOnScroll': false,
        'zoomOpacity': true,
        'zoomSpeedIn': 500,
        'zoomSpeedOut': 500
    }).length > 0) || isFancyZoom;
    isFancyZoom = (jQuery("a.twitterUpdateNewBtn").fancybox({
        'imageScale': false,
        'centerOnScroll': false,
        'zoomOpacity': true,
        'frameWidth': 800,
        'frameHeight': 1200
    }).length > 0) || isFancyZoom;
    
    if(isFancyZoom) {
		$('head').append('<link href="/css/634157619318450000/fancybox/fancybox.css" rel="stylesheet" type="text/css"/>');
    }
    
    // FONT REPLACEMENT-- NOT JQUERY, BUT WORKS IN CONJUNCTION WITH IT.
    Cufon.replace('#registration h1', {
        fontFamily: 'VAG Rounded Bold'
    });
    Cufon.replace('.message h2', {
        fontFamily: 'VAG Rounded Bold',
        fontSize: 26,
        textShadow: '#000000 1px 1px'
    });
    Cufon.replace('#internalBillboard h1', {
        fontFamily: 'VAG Rounded Bold',
        fontSize: 27,
        textShadow: '#000 1px 1px'
    });
    Cufon.replace('h2.cufon', {
        fontFamily: 'VAG Rounded Bold'
    });
    Cufon.replace('#bodyContent h1', {
        fontFamily: 'VAG Rounded Bold',
        lineHeight: 1.5
    });
    Cufon.replace('#twitterBanner h2', {
        fontFamily: 'Museo 700',
        textShadow: '#000000 1px 1px',
        lineHeight: 1.5
    });
    Cufon.replace('h2.itsEasy', {
        fontFamily: 'Museo 700',
        lineHeight: 1.5
    });
    Cufon.replace('#twitterSteps li', {
        fontFamily: 'Museo 700'
    });

    // Block UI Settings
    jQuery.blockUI.defaults.css = {
        backgroundColor: 'transparent',
        cursor: 'wait',
        padding: 0,
        margin: 0,
        width: '300px',
        top: (screen.height) ? (screen.height - 300) / 2: '45%',
        left: (screen.width) ? (screen.width - 300) / 2 : '45%',
        centerY: false,
        centerX: false,
        textAlign: 'center',
        color: '#000'
    };
    jQuery.blockUI.defaults.overlayCSS = { opacity: '0.85', backgroundColor: '#FFF' };
    jQuery.blockUI.defaults.message = '<h2>Loading. Please wait.</h2><img src=\"/images/634157619318450000/register/spinner-barber-pole.gif\" />';
    jQuery.blockUI.defaults.baseZ = 9999;

    // default jQuery AJAX settings
    jQuery.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{}"
    });

	//JQUERY TOOLS - TABS PLUGIN    
    $("#tabs").tabs(".panes").history(); 
    
    // FACEBOOK SESSION
    fbStatus();

});  // end document ready

function fbStatus(){
    // Exempt https pages, pages without fbAppKey, and views with fbs cookie
    if (window.location.protocol == 'https:' || typeof(window['fbAppKey']) == 'undefined' || document.cookie.indexOf('fbs=') > 0)
        return;
    else if (typeof(FB) != 'object') {
        $.getScript('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php', function(){
            fbStatusGet();
        });
	}
    else
        fbStatusGet();
}

function fbStatusGet(){
	if (FB.HiddenContainer.get() == null)
		$('body').append('<div id="FB_HiddenContainer"  style="position:absolute; top:-10000px; width:0px; height:0px;" ></div>');
	FB.init(fbAppKey, '/providers/fb/xd_receiver.htm');
	FB.ensureInit(function() {
		FB.Connect.get_status().waitUntilReady(function(status){
			if (status == FB.ConnectState.connected){
				FB.Connect.requireSession();
				// Set cookie to avoid multiple loads per session
				var now = new Date();
				var pst = new Date();
				pst.setUTCHours(0, 0, 0, 0);
				pst.setTime(pst.getTime() + 8 * 60 * 60 * 1000);
				while (pst.getTime() < now.getTime())
					pst.setTime(pst.getTime() + 24 * 60 * 60 * 1000);
				document.cookie = 'fbs=1; expires=' + pst.toUTCString();
			}
		});
	});
}

function DialogOpen(formToClear, modalHolderId) {
    if (!modalHolderId) modalHolderId = "modalHolder";
    jQuery("#" + modalHolderId).dialog({
        autoOpen: true,
        closeOnEscape: true,
        width: 600,
        height: 400,
        modal: true,
        resizable: false,
        overlay: {
            background: "#69C",
            opacity: 0.8
        },
        open: function() {
            jQuery.unblockUI();
            jQuery(".closeDialog").click(function() {
                if (formToClear) jQuery('#' + formToClear).clearForm();
                jQuery("#" + modalHolderId).dialog("destroy").remove();
            }).focus();
            jQuery("#" + modalHolderId).find("input:first").focus();
        }
    });
}

function AlertDialog(type, heading, message, formToClear) {
    jQuery("body").append("<div id=\"modalHolder\" class=\"alertDialog\"></div>");
    jQuery('#modalHolder').load("/message/modal_template.aspx?divId=" + type, function(data) {
        jQuery('#modalHolder .alertHeading').html(heading);
        jQuery('#modalHolder .alertMessage').html(message);
        DialogOpen(formToClear);
    });
}

function dialogText(content, title){
	var html = '';
	var id = Math.floor(Math.random()*1000);
	if (title) html += '<div class="modalTopper"><h2 class="alertHeading">' + title + '</h2></div>';
	html += '<div class="modalBody"><p>' + content + '<p><a tabindex="101" class="closeDialog closeWindowButton clearForm" href="#" onclick="$(\'#' + id + '\').dialog(\'close\');">Close This Window</a></div>';
	$('body').append('<div class="dialog_text" id="' + id + '"></div>');
	$('#' + id).html(html).find('.modalBody').height('auto').css('min-height', '0').css('padding-bottom', 70);
	$('#' + id).dialog({modal: true});
}

function DialogShow_FailInvalid() {
    AlertDialog("alertFailInvalid", "Invalid Data Entry", "There was a problem with the information you submitted. Please verify that the information you entered was correct and then try to resubmit this form. If you continue to experience problems, please contact NutshellMail Support at <a href=\"mailto:support@nutshellmail.com\">support@nutshellmail.com</a>.");
}
function DialogShow_FailComm() {
    AlertDialog("alertFailComm", "Network Communications Error", "There was an error communicating with NutshellMail. Please try to resubmit this form. If you continue to experience problems, please send a message directly to <a href=\"mailto:support@nutshellmail.com\">support@nutshellmail.com</a>.");
}

function DialogShow_FailInvalidMAPassword() {
    AlertDialog("alertFailUser", "Invalid Username/Password", "Please be sure that the information you entered is correct and retry the connection. If the information you entered is correct, please try adding this account later.");
}

function DialogShow_FailConnMsgProvider() {
    AlertDialog("alertFailComm", "Connection Failure", "There is a temporary connection failure between the message provider and NutshellMail. Please retry the connection. If this connection continues to fail, please try again later.");
}

function DialogShow_FailConnMsgProviderCustom() {
    AlertDialog("alertFailComm", "Connection Failure", "NutshellMail was unable to connect to the message provider. There is either a temporary connection failure to the provider, or the server settings you entered are incorrect. Please check the settings and try again. If this connection continues to fail, please try again later.");
}

function DialogShow_FailInvalidNMPassword() {
    AlertDialog("alertFailUser", "Invalid Password", "The NutshellMail Password that you entered is incorrect. Please try again. If you cannot remember your password, you can <a href=\"/forgot_password/\">click here</a> to reset it.");
}

function logMetric(data) {
    jQuery.ajax({
        url: "/logMetric.aspx/logClientAction",
        data: "{data: " + JSON.stringify(data) + "}"
    });
}

function logException(accountid, message) {
    jQuery.ajax({
        url: "/logMetric.aspx/logClientException",
        data: "{accountid: " + accountid + ", message: " + JSON.stringify(message) + "}"
    });
}

function gaTrackPage(path) {
    jQuery(document).ready(function() {
        pageTracker._trackPageview(path);
    });
}

var uservoiceOptions = {
    /* required */
    key: 'nutshellmail',
    host: 'feedback.nutshellmail.com',
    forum: '36554',
    showTab: true,
    /* optional */
    alignment: 'left',
    background_color: '#FF9933',
    text_color: 'white',
    hover_color: '#336699',
    lang: 'en'
};

function initUserVoice() {
    function _loadUserVoice() {
        var s = document.createElement('script');
        s.setAttribute('type', 'text/javascript');
        s.setAttribute('src', ("https:" == document.location.protocol ? "https://" : "http://") + "cdn.uservoice.com/javascripts/widgets/tab.js");
        document.getElementsByTagName('head')[0].appendChild(s);
    }
    _loadSuper = window.onload;
    window.onload = (typeof window.onload != 'function') ? _loadUserVoice : function() { _loadSuper(); _loadUserVoice(); };
}