jQuery(function(){
	jQuery('input.hint').bind('focus', inputHintFocus).bind('blur', inputHintBlur).blur()
	jQuery('#tryform').bind('submit', tryformClick)
})

function inputHintFocus(ev) {
	var input = jQuery(ev.target)
	if (input.val()=="" || input.val()==input.attr('title')) {
		input.removeClass('inactive').addClass('active').val("")
	}
}

function inputHintBlur(ev) {
	var input = jQuery(ev.target)
	if (input.val()=="" || input.val()==input.attr('title')) {
		input.removeClass('active').addClass('inactive').val(input.attr('title'))
	}
}

function tryformClick(ev) {

	try {
			pageTracker._trackEvent('Index', 'Try now!')
	} catch (ex) {}

	
	var jabberId = jQuery('#jabberId').val()
	if (jabberId=="" || jabberId==jQuery('#jabberId').attr('title'))
		return false;
	
	hideError('#trymessage')
	if (jabberId.indexOf('@') < 0)
		return showError('Incorrect JabberID. Try smth like "steve@apple.com"', '#trymessage')
	
	jQuery.getJSON('/api/Jabber2Twitter/inviteUser?jabberId='+jabberId, function(data){
		if (data.isError)
			return showError(data.errorMessage, '#trymessage')
		showError('Nice! Now look to your Jabber client', '#trymessage')
	})
	
	return false;
}

function showError(mesg, obj) {
	jQuery(obj).attr('class', 'error').text(mesg).show()
	return false
}

function hideError(obj) {
	jQuery(obj).hide()
}
