document.observe('dom:loaded', function() {
	$$('.navlink[rel]').invoke('observe', 'click', function(event) {
		event.element().blur();
		event.stop();
		if ($(this.rel)) $(this.rel).scrollTo();
	});
	
	if (window.Projects && $('ideas')) Projects.createIdeas('Pushup');
	
	$('browsers').observe('click', function(event) {
	  var element = event.findElement('li[class]');
	  if (element && element.className) Pushup.show(element.className, { ignoreReminder: true });
	});
	
	$('download').down('.downloadLink').observe('click', function(event) {
	  event.stop();
	  window.location.href = this.rel;
	});

	/* character limit */
	var _charlimit = 100;
	$('message')._charlimit = _charlimit;
	$('charsleft').value = _charlimit;
	function limitChars() {
		if(this.value.length > this._charlimit)
		  this.value = this.value.substr(0, this._charlimit);
		else
		  $('charsleft').value = this._charlimit - this.value.length;
	}
	$('message').observe('keydown', limitChars).observe('keyup', limitChars);

	/* preview */
	$('createPreview').observe('click', function() {
		var domainText = $F('domain').strip();
		if(!domainText || !domainText.include('.')) {
			$('domain').addClassName('bad');
			return;
		}
		else
			$('domain').removeClassName('bad');
		
		// clear tags
		$('message').value = $('message').value.stripTags();

		// prepare domain
		$('domain').value = $('domain').value.strip().toLowerCase();
		// clear http://
		if ($F('domain').startsWith('http://'))
		  $('domain').value = $('domain').value.substr(7);
		// clear /
		if ($F('domain').endsWith('/'))
		  $('domain').value = $('domain').value.substr(0, $F('domain').length - 1);

		new Ajax.Request('include/uniquedomain/', {
		  parameters: 'domain=' + $F('domain'),
		  onComplete: function(t) {
			if (t.responseText == 'NOTUNIQUE') {
				$('domain').prototip.show();
				return;
			}
			
			$w('domain yourname email country message').each(function(field) {
				var value = $F(field),
				 target = $('step2').down('.' + field);
				if (field == 'country' && value == 'select')
				  value = null;
				
				if (value)
				 target.show().down('.right').update(value);
				else
				 target.hide();
			});
			
			hideSteps();
			$('step2').show();
		  }
       });
	});
	
	function resetDomainForm() {
		$('domainForm').reset();
		hideSteps();
		$('step1').show();
	}
	
	function hideSteps() {
		$R(1,3)._each(function(s) { Element.hide('step' + s) });
	}
	function closeDomainForm(event) {
		event.stop();
		if ($('domain').prototip) $('domain').prototip.hide();
		$('addYourDomain').hide();
		hideSteps();
		$('step1').show();
		resetDomainForm();
	}
	
	function modifyDomain() {
		hideSteps();
		$('step1').show();
	}
	function toggleDomainForm(event) {
		$('domainForm').enable();
		event.stop();
		resetDomainForm();
		if ($('addYourDomain').visible() && $('domain').prototip) $('domain').prototip.hide();
		$('addYourDomain').toggle();
	}

	function submitDomain() {
		new Ajax.Request('include/adddomain/', {
			parameters: $('domainForm').serialize(),
			onComplete: function() {
				$('addYourDomainLink').remove();
				
				var countSpan = $('currentPushups').down('.count');
				  countSpan.update(parseInt(countSpan.innerHTML) + 1);
				
				$('step3').down('.domain').update($F('domain'));
				$('step2').hide();
				$('step3').show();		
			}
		});
		$('domainForm').disable();
	}

	$('addYourDomain').down('.close').observe('click', closeDomainForm);
	$('addYourDomainLink').observe('click', toggleDomainForm);
	$('modifyDomain').observe('click', modifyDomain);
	$('submitDomain').observe('click', submitDomain);
	
  if ($('domain' && !!window.Tip)) {
	new Tip('domain', 'This domain is already listed', {
		style: 'protoblue',
		stem: 'leftBottom',
		hook: { target: 'rightMiddle', tip: 'leftBottom' },
		offset: { x: 3, y: 6 },
		hideOn: { element: 'target', event: 'focus' },
		showOn: false,
		hideAfter: 4,
		width: 'auto'
	});
	}
});

