if (!Function.prototype.bind) {
  Function.prototype.bind = function (obj)
  {
    var func = this;
    return function () {return func.apply(obj, arguments);};
  };
}


function PushFriendBubble() {
}

PushFriendBubble.prototype.setHandlers = function() {
	$('#push_your_friend').submit(this.onSubmit.bind(this));
}

PushFriendBubble.prototype.onSubmit = function() {
	var nickname = jQuery.trim($('#push_your_friend_nick').val());
	if (nickname != '') {
		document.location = '/'+nickname+'/';
	}
	return false;
}

function PushmeForm() {
	this.uploader = null;
}

PushmeForm.prototype.setUploader = function(uploader) {
	this.uploader = uploader;
}

PushmeForm.prototype.clearValidation = function() {
	$('#push_form_too_long_message').hide();
	$('#push_form_cannot_store_attachment').hide();
	$('#push_form_no_message').hide();
	$('#push_form_no_signature').hide();
	if ($('#push_form_wrong_captcha').length > 0) {
		$('#push_form_wrong_captcha').hide();
	}
}

PushmeForm.prototype.validate = function() {
	var isFormValid = true;
	if (jQuery.trim($('#message').val())=='') {
		$('#push_form_no_message').show();
		isFormValid = false;
	}
	
	if (jQuery.trim($('#signature').val())=='') {
		$('#push_form_no_signature').show();
		isFormValid = false;
	}

	if ($('#captcha').length > 0 && jQuery.trim($('#captcha').val())=='') {
		$('#push_form_wrong_captcha').show();
		isFormValid = false;
	}
	
	return isFormValid;
}

PushmeForm.prototype.enableSubmitButton = function(doEnable) {
	if (doEnable) {
		$('#send_message_submit').removeAttr('disabled');
		$('#pushme_iphone_submit').removeAttr('disabled');
	} else {
		$('#send_message_submit').attr('disabled','disabled');
		$('#pushme_iphone_submit').attr('disabled','disabled');
	}
}

PushmeForm.prototype.showInProgress = function(isInProgress) { 
	if (isInProgress) {
		$('#push_in_progress').css('visibility','visible');
		this.enableSubmitButton(false);
	} else { 
		$('#push_in_progress').css('visibility','hidden');
		this.enableSubmitButton(true);
	}
}

PushmeForm.prototype.showSentSuccess = function()  {
	$('#send_result').show();
	$('#send_message_form').hide();
	$('#pushme_iphone').hide();
}

PushmeForm.prototype.onSuccess = function(data, status) {
	eval("var obj="+data); // FIXME

	this.showInProgress(false);

	if ('success' == obj.status) {
		this.showSentSuccess();
	} else if ('error' == obj.status) {
		var errorShown=false;
		if (obj.error == 'noMessage') {
			errorShown=true;
			$('#push_form_no_message').show();
		}
		if (obj.error == 'noSignature') {
			errorShown=true;
			$('#push_form_no_signature').show();
		}
		if (obj.error == 'tooLong') {
			errorShown=true;
			$('#push_form_too_long_message').show();
		}
		if (obj.error == 'wrongCaptcha') {
			errorShown=true;
			$('#push_form_wrong_captcha').show();
			$('#captchaImg').attr('src', '/captcha/?id='+obj.captchaId);
			$('#captchaId').attr('value', obj.captchaId);
		}
		if (obj.error == 'cannotStoreAttachment') {
			errorShown=true;
			$('#push_form_cannot_store_attachment').show();
		}

		if (!errorShown) {
			this.showError("Unknown error happened. Try again later.");
		}		
	}
}

PushmeForm.prototype.clearError = function() {
	$('#send_message_error_result').hide();
}

PushmeForm.prototype.showError = function(errorString) {
	$('#send_message_error_result').html(errorString);
	$('#send_message_error_result').show();
}

PushmeForm.prototype.onFailure = function() {
	this.showInProgress(false);
	this.showError("Error happened. Try again later.");
}

PushmeForm.prototype.setHandlers = function() {
	this.clearValidation();
	this.showInProgress(false);
	this.clearError();

	$('#send_message_form').submit(this.onSubmit.bind(this));
	// FIXME  onsubmit for the form
	$('#pushme_iphone').submit(this.onSubmit.bind(this));

	$('#message').bind("change keyup", this.updateLength.bind(this));
	$('#signature').bind("change keyup", this.updateLength.bind(this));
	this.updateLength();

	$('#message').focus();
}

PushmeForm.prototype.onSubmit = function() {
	this.clearValidation();
	this.clearError();

	if (!this.validate()) {
		//return false;
	}

	this.showInProgress(true);
	var postArguments = {
		'message':$('#message').val(),
		'signature':$('#signature').attr('value'),
		'nickname':$('#nickname').val(),
		'captcha[input]':$('#captcha').val(),
		'captcha[id]':$('#captchaId').val()
	};
	if (this.uploader) {
		var uploadedFilenames = this.uploader.uploadedFilenames();
		if (uploadedFilenames.length>0) {
			postArguments.filenames = uploadedFilenames.join(',');
		}
	}

	$.ajax({
		'type':'POST',
		'url': '/z/ajax/pushme/',
		'data': postArguments,
		'error' : this.onFailure.bind(this),
		'success':this.onSuccess.bind(this)
	});
	return false;
}


PushmeForm.prototype.updateLength = function() {
	var maxLength = 500;

	var charsLeft = maxLength - $('#message').val().length - $('#signature').val().length;
	
	if (charsLeft < 0) {
		this.enableSubmitButton(false);
		charsLeft = 0;
	} else {
		this.enableSubmitButton(true);
	}
	
	$('#chars_left').html(charsLeft);

	if (charsLeft == 0) {
		$('#send_message_characters > span').attr('id','red');
	} else if (charsLeft < 50) {
		$('#send_message_characters > span').attr('id','orange');
	} else {
		$('#send_message_characters > span').attr('id','gray');
	}

	var percent = 100-(charsLeft/500*100);
	if (percent < 8) {
		percent = 8;
	}
	$('#send_message_characters > span > span').css('width',percent+'%');
}

function PushmeWidgetExportForm() {
	this.kind = 'iframe';
	this.style = 'default';
}

PushmeWidgetExportForm.prototype.styleClicked = function(style) {
	this.setStyle(style);
	this.render();
	this.updateFrames();
	return false;
}

PushmeWidgetExportForm.prototype.kindClicked = function(kind) {
	this.setKind(kind);
	this.render();
	this.updateFrames();
	return false;
}

PushmeWidgetExportForm.prototype.onFailure = function() {
	alert("Oops. Try again.");
}

PushmeWidgetExportForm.prototype.onSuccess = function(data, status) {
	eval("var obj="+data); // FIXME
	this.indeedUpdateFrames(obj.widgetHash);
}

PushmeWidgetExportForm.prototype.indeedUpdateFrames = function(hash) {
	$('#pushmeWidgetPreviewIframe').attr('src', '/z/widget/export/?hash='+hash+'&previewMode=1');
	$('#pushmeWidgetCodeIframe').attr('src', '/z/widget/code/?kind='+this.kind+'&colorScheme='+this.style);
}

PushmeWidgetExportForm.prototype.updateFrames = function() {
	$.ajax({
		'type':'POST',
		'url': '/z/ajax/widget/',
		'data':{
			'kind': this.kind,
			'colorScheme':this.style
		},

		'error' : this.onFailure.bind(this),
		'success':this.onSuccess.bind(this)
	});
}

PushmeWidgetExportForm.prototype.setHandlers = function() {
	// FIXME - to bind!!
	$('#pushmeWidgetExportForm .kind').each(function(i) {
		$(this).bind('click', function() {pushmeWidgetExportForm.kindClicked(this.getAttribute('name'))})
	});
	$('#pushmeWidgetExportForm .style').each(function(i) {
		$(this).bind('click', function() {pushmeWidgetExportForm.styleClicked(this.getAttribute('name'))})
	});
}

PushmeWidgetExportForm.prototype.setKind = function(kind) {
	this.kind = kind;
	this.style='default';
}

PushmeWidgetExportForm.prototype.setStyle = function(style) {
	this.style = style;
}

PushmeWidgetExportForm.prototype.clearStyles = function() {
	$('#pushmeWidgetExportForm  #styles_iframe').hide();
	$('#pushmeWidgetExportForm  #styles_html').hide();
	$('#pushmeWidgetExportForm .active').each(function(i) {
		$(this).removeClass('active');
	});
}

PushmeWidgetExportForm.prototype.render = function() {
	this.clearStyles();
	$('#pushmeWidgetExportForm  #styles_'+this.kind).show();
	$('#pushmeWidgetExportForm .kind[name="'+this.kind+'"]').addClass('active');
	$('#pushmeWidgetExportForm .style[name="'+this.style+'"]').addClass('active');
}

PushmeCheckboxes = function() {
}

PushmeCheckboxes.prototype.isAnyOneEnabled = function() {
	var anyOne = false;
	$("#last_messages  dl  dt input").each(function(i,element){
		if (element.checked) {
			anyOne = true;
		}
	});

	return anyOne;
}

PushmeCheckboxes.prototype.tick = function() {
	this.enableDelete(this.isAnyOneEnabled());
}

PushmeCheckboxes.prototype.checkboxElements = function() {
	return $("#last_messages  dl  dt input");
}

PushmeCheckboxes.prototype.deleteLinkElement = function() {
	return $('#last_messages #bottom_links #delete_link');
}

PushmeCheckboxes.prototype.enableDelete = function(enable) {
	if (enable) {
		this.deleteLinkElement().addClass("active");
	} else {
		this.deleteLinkElement().removeClass("active");
	}
}

PushmeCheckboxes.prototype.selectAllClicked = function() {
	var iMetSomeone = false;
	this.checkboxElements().each(function(i,element){
		element.checked = "checked";
		iMetSomeone = true;
	});

	this.enableDelete(iMetSomeone);
	return false;
}

PushmeCheckboxes.prototype.deleteClicked = function() {
	var ids = [];
	if (this.deleteLinkElement().hasClass('active')) {
		this.checkboxElements().each(function(i, element){
			if (element.checked) {
				ids.push(element.name);
			}
		});

		$.ajax({
			'type':'POST',
			'url': '/z/ajax/delete/',
			'data':{
				'ids':ids.join(',')
			},
			'error' : this.onFailure.bind(this),
			'success':this.onSuccess.bind(this)
		});
	}
	return false;
}

PushmeCheckboxes.prototype.onFailure = function() {
	alert("Oops. Error happened. Try again later pls.")
}

PushmeCheckboxes.prototype.onSuccess = function(data, status) {
	document.location=document.location;
}





PushmeMessages = function () {
}

PushmeMessages.prototype.showOlder = function(lastShownMessageId) {
	$('#old_messages_loading_in_progress').css('visibility','visible');
	$('#older_messages').load(
		'/z/index/older-messages/?lastShownMessageId='+lastShownMessageId,
		function(responseText, textStatus, xhr){
			if (xhr.status == 200) {
				$('#show_older_messages').hide();
			} else {
				$('#old_messages_loading_in_progress').css('visibility','hidden');
			}
		}
	);
	return false;
}



