function TipEditorsManager(){
	this.captchaImage = jQuery("#tipEditorsForm img");
	this.textArea = jQuery("#tipEditorsForm textarea");
	this.tipEditorsForm = jQuery("#tipEditorsForm form");
	
	var This = this;
	
	this.captchaImage.bind("click", function(e) {
		This.loadCaptchaImage(This, e);
	});

	this.textArea.focus(function(e) {
		This.textareaFocus(This);
	});

	this.tipEditorsForm.bind("submit", function(e) {
		This.tipEditorsFormSubmit(This);
		return false;
	});
};

TipEditorsManager.prototype = {
	textareaFocus : function(manager, e) {
		manager.loadCaptchaImage(manager);
		manager.textArea.unbind(e);
	},

	loadCaptchaImage : function(manager) {
		jQuery('#tipEditorsForm .captcha').show();
		randNum = Math.floor(Math.random()*1000000000);
		manager.captchaImage.attr("src", '/captcha?i=' + randNum);
		jQuery('#tipEditorsForm input[name=randomFormId]').val(randNum);
	},

	tipEditorsFormSubmit : function(manager) {
		var randomFormId = jQuery('#tipEditorsForm input[name=randomFormId]');
		var message = jQuery('#tipEditorsForm textarea');
		var email = jQuery('#tipEditorsForm input[name=email]');
		var name = jQuery('#tipEditorsForm input[name=name]');
		var captcha = jQuery('#tipEditorsForm input[name=captcha]');
		var url = jQuery('#tipEditorsForm input[name=url]');

		jQuery('p.validationerror').remove();
		jQuery('.validationerror').removeClass('validationerror');

		jQuery.post('/ajax/tipeditors', { 'json':'true', 'name': name.val(), 'randomFormId': randomFormId.val(), 
			'message': message.val(), 'replyTo': email.val(), 'captcha': captcha.val(), 'url': url.val() },

			function(data){
				if(data.status) {
					jQuery('#tipEditorsForm form').before('<p class="success">' + data.statusText + '</p>');
					jQuery('#tipEditorsForm form').hide();
				}
				else {
					if(data.field) {
						var errorField = jQuery('#tipEditorsForm *[name=' + data.field + ']');
						errorField.addClass('validationerror');
						errorField.after('<p class="hidden validationerror">' + data.statusText + '</p>');
						manager.loadCaptchaImage(manager);
						errorField.focus();
					}
					else {
						jQuery('#tipEditorsForm .form-button').after('<p class="hidden validationerror">' + data.statusText + '</p>');
					}
				}
			}, 'json');
	}
};

jQuery(document).bind("ready",function() {
	if(jQuery("#tipEditorsForm").length) {
		new TipEditorsManager();
	}
});window.onload = function() { 
   if(document.getElementById("expandWrap")){
	document.getElementById("expandWrap").style.display = "none";
	document.getElementById("expander").style.display = "block";
	document.getElementById("expander").onclick = function(){
		if(document.getElementById("expandWrap").style.display == "none"){
			document.getElementById("expandWrap").style.display = "block";
			document.getElementById("expander").innerHTML = "Vis færre";
			document.getElementById("expander").className = "expanded";
		}
		else{
			document.getElementById("expandWrap").style.display = "none";
			document.getElementById("expander").innerHTML = "Vis flere";
			document.getElementById("expander").className = "";
		}
	}
  }
};

jQuery(document).ready(function() {
	var sidebar = jQuery(".sideBar")[0];
	if(sidebar){
		var url = document.location+"";
		url = encodeURIComponent(url);
		jQuery(sidebar).prepend('<g:plusone size="medium"></g:plusone><iframe src="http://www.facebook.com/plugins/like.php?app_id=200645883318169&amp;href=' + url + '&amp;send=false&amp;layout=standard&amp;width=525&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=verdana&amp;height=24" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:525px; height:24px;" allowTransparency="true"></iframe>');
		jQuery.getScript('https://apis.google.com/js/plusone.js');
	}
});
