Date.now = Date.now || function() { return new this().getTime(); };

Modal = {
	confirm: function(callback, contents, bypass) { if (bypass || confirm(contents || "Virkelig?")) return callback(); },
	present: function(contents, callback) {
		var element = $('<div id="modal"/>').append($('<div id="modal-content"/>').append(contents)).hide();
		var wrapper = $('<div id="modal-wrapper"/>').append(element).prependTo($('body'));
		wrapper.click(function(e) { if (e.target == this) $(this).fadeOut(function() { $(this).detach(); }); });
		return element.fadeIn(function() { $(this).find('input:not(:submit)').first().focus(); }); } };

$.fn.extend({
	editable: function(callback, plain) {
		return $(this).each(function() {
			$(this).get(0).contentEditable = true;
			$(this).blur(function() { if (callback) callback.call($(this), $(this)[plain ? 'text' : 'html']()); });
		}); } });

$(function() {
	$('#administration .access:not(.logout)').click(function() {
		var modal = Modal.present($('<form action="/administration" method="post" id="authorization"/>').append(
			$('<fieldset/>').append(
				$('<input type="password" name="passphrase" placeholder="Adgangskode"/>'),
				$('<input type="submit" value="Log Ind"/>'))).ajaxForm({
					beforeSubmit: function() { },
					 success: function() { modal.find('form').addClass('throbbing').find('fieldset').fadeOut(150, function() { $(this).css('visibility', 'hidden').show(); window.location.reload(); }); },
					   error: function() { modal.effect('shake', {}, 60, function() { modal.find('[value=""], :password').first().focus(); }); } })); return false; });
	
	var modelBackShown = false;
	$('#splash .which-model').click(function() {
		$(this).prev('#devices').each(function() {
			if (modelBackShown) {
				$(this).find('img.back').fadeOut();
				$(this).find('img.front').fadeIn();
			} else {
				$(this).find('img.front').fadeOut();
				$(this).find('img.back').fadeIn();
			} modelBackShown = !modelBackShown; }); return false; });
	
	$('#contact-form').each(function() {
		var form = $(this).ajaxForm({
			beforeSubmit: function() {
				form.find(':input, :submit').attr('disabled', true);
				form.find('label.reply').fadeOut(function() { $(this).detach(); }); },
			success: function(r) {
				form.find(':input, :submit').attr('disabled', false)
				form.find(':input:not(:submit)').val('');
				form.find(':submit').after($('<label class="reply"/>').text("Afsendt").fadeIn()); },
			error: function() {
				form.find(':input, :submit').attr('disabled', false); 
				form.find(':submit').effect('shake', {}, 60, function() {
					form.find(':input[value=""]').first().focus(); }); } }); });
	
	if ($.browser.msie || $.browser.mozilla)
		$('[placeholder]').each(function() {
			// var element = $('<span class="input-placeholder"/>').text($(this).attr('placeholder')).insertAfter($(this));
			var text = $(this).attr('placeholder');
			
			$(this).focus(function() {
				if ($(this).val() == text)
					$(this).val('').css('color', null);
			}).blur(function() {
				if ($(this).val() == text || $(this).val().length == 0)
					$(this).val(text).css('color', 'gray');
			}).blur();
		}); });

