var Client = {
	viewportWidth: function() {
		return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
	},

	viewportHeight: function() {
		return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
	},
	
	viewportSize: function() {
		return { width: this.viewportWidth(), height: this.viewportHeight() };
	}
};

fb.deleteCalendar = function(ext_id) {
	$.ajax({
		type: "POST"
		, url: '/action/calendar/delete'
		, data: 'ext_id=' + ext_id
		, success: function() {
			$('#li_' + ext_id).css('background', '#f00').fadeOut('slow');
		}
	});
}

fb.setCalType = function(type) {
	var options = new Array(
		'google'
		, 'zimbra'
		, 'boxes30'
		, 'http'
//		, 'ical'
//		, 'outlook'
//		, 'upload'
	);
	for (var i = 0; i < options.length; i++) {
		$('#cal_type_' + options[i]).attr('checked',true).parent().attr('class','');
		$('#cal_add_2').removeClass(options[i]);
	}
	$('#cal_type_' + type).attr('checked',true).parent().addClass('selected');
	$('#cal_add_2').addClass(type);
}

fb.format_time = function(time) {
	var hours = '';
	var minutes = '';
	var ampm = '';
	if (parseInt(time.substring(0, 2)) < 13) {
		ampm = 'AM';
		hours = time.substring(0, 2);
	}
	else {
		ampm = 'PM';
		hours = (parseInt(time.substring(0, 2)) - 12) + '';
	}
	if (hours.substring(0, 1) == '0') {
		hours = hours.substring(1, 2);
	}
	else {
		hours = hours.substring(0, 2);
	}
	minutes = time.substring(2, 4);
	return hours + ':' + minutes + ' ' + ampm;
}

fb.apptDecision = function(ext_id, action) {
	var params = 'ext_id=' + ext_id + '&ajax_action=' + action;
	if ($('#appt_reply_' + ext_id).attr('class').indexOf('none') == -1) {
		params += '&response=' + encodeURIComponent($('#appt_reply_' + ext_id).val());
	}
	$('#appt_' + ext_id).css({padding: '0', border: '0'}).html('<div class="loading"></div>');
	$.ajax({
		type: "POST"
		, url: '/action/appointment/ajax'
		, data: params
		, success: function(response) {
			$('#appt_' + ext_id).html(response);
		}
	});
}

fb.unblockAddress = function(a_elem) {
	$.ajax({
		type: "POST"
		, url: '/action/unblock'
		, data: 'email=' + $(a_elem).attr('rel')
		, success: function() {
			$(a_elem).parent().remove();
		}
	});
	return false;
}

fb.hideMessages = function() {
	elem = $('#messages');
	if (elem.css('display') != 'none') {
		elem.fadeOut('slow');
	}
}

fb.resetCalendar = function() {
	if (typeof fb_resetCalendar != 'undefined') {
		fb_resetCalendar();
	}
}

fb.hideInboxBanner = function() {
	elem = $('#inbox_alert_banner');
	if (elem.css('display') != 'none') {
		elem.fadeOut();
	}
}

fb.hideCalBanner = function() {
	elem = $('#cal_alert_banner');
	if (elem.css('display') != 'none') {
		elem.fadeOut();
	}
}

fb.toggleHeaderLogin = function() {
	var header_login = $('#header_login');
	if (header_login.css('display') != 'block') {
		if ($.browser.msie) {
			header_login.css('display', 'block');
			fb.resetCalendar();
			$('#hl_username').focus();
		}
		else {
			header_login.slideDown('fast', function() {
				$('#hl_username').focus();
				fb.resetCalendar();
			});
		}
	}
	else {
		header_login.slideUp('fast', fb.resetCalendar);
	}
}

fb.applyResetButtonActions = function() {
	$('#calendars .broken a.retry').unbind().click(function() {
		var ext_id = $(this).attr('rel');
		var broken = $('#broken_' + ext_id);
		broken.html('<div class="loading"></div>');
		$.post(
			'/action/calendar/clear_check_errors'
			, { 
				ext_id: ext_id
			}
			, function(response) {
				var data = eval('(' + response + ')');
				if (data.result == 'success') {
					broken.removeClass('broken').addClass('retry').html(data.html);
				}
				else {
					alert('Sorry, something unexpected happened. Please try again.');
				}
			}
		);
		return false;
	});
}

$(function() {
	$('#header_login_link').click(function() {
//		fb.toggleHeaderLogin();
//		return false;
	});
});

$(window).load(function() {
	if (typeof fb_sticky_msgs == 'undefined' || !fb_sticky_msgs) {
		setTimeout('fb.hideMessages();', 8000);
	}
});