var Communicator = {
	status: 2,
	gstatus: 0,
	mstatus: 0,
	initWidth: 52,
	maxwidth: 150,
	msgWidth: 250,
	widget: null,
	menu: null,
	gaduGadget: null,
	msgGadget: null,
	duration: 500,
	
	init: function(event) {
		if(Communicator.status == 2 && Communicator.gstatus == 0 && Communicator.mstatus == 0)
			Communicator.arrowClicked(event);
	},
	
	arrowClicked: function(event) {
		if(Communicator.status == 0) Communicator.show();
		else Communicator.hide(event != null);
	},
	
	show: function() {
		Communicator.status = 1;
		$(Communicator.menu).stop().animate(
				{width: (Communicator.initWidth + Communicator.maxwidth)}, 
				{duration: Communicator.duration }
				);
		$(Communicator.menu).find("div.content").stop().animate(
				{width: Communicator.maxwidth}, 
				{duration: Communicator.duration}
				);
	},
	
	hide: function(closeOther) {
		Communicator.status = 0;
		$(Communicator.menu).stop().animate(
				{width: Communicator.initWidth}, 
				{duration: Communicator.duration}
				);
		$(Communicator.menu).find("div.content").stop().animate(
				{width: 0}, 
				{duration: Communicator.duration }
				);
		if(closeOther) {
			Communicator.ghide(Communicator.msgGadget, false);
			Communicator.ghide(Communicator.gaduGadget, false);
		}
	},
	
	gadu: function(event) {
		Communicator.gstatus =  (Communicator.gstatus + 1) % 2;
		 
		if(Communicator.gstatus == 1) Communicator.gshow(Communicator.gaduGadget);
		else Communicator.ghide(Communicator.gaduGadget, event != null);
	},
	
	msg: function(event) {
		Communicator.mstatus =  (Communicator.mstatus + 1) % 2;
		 
		if(Communicator.mstatus == 1) Communicator.gshow(Communicator.msgGadget);
		else Communicator.ghide(Communicator.msgGadget, event != null);
	},
	

	gshow: function(gadget) {
		$(gadget).stop().animate(
				{left: 0}, 
				{duration: Communicator.duration }
				);
		if(gadget == Communicator.msgGadget) {
			Communicator.gstatus =  0;
			Communicator.ghide(Communicator.gaduGadget, false);
		} else {
			Communicator.mstatus = 0;
			Communicator.ghide(Communicator.msgGadget, false);
		}
	},
	
	ghide: function(gadget, closeOther) {
		$(gadget).stop().animate(
				{left: -Communicator.msgWidth}, 
				{duration: Communicator.duration}
				);
		if(closeOther) {
			Communicator.arrowClicked();
		}
	},
	
	sendMsg: function() {
		var form = $(Communicator.widget).find('form').get(0);
		var emailInput = $(form).find('input[name="email"]');
		var email = $(emailInput).val();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   		
		if(reg.test(email) == false) {
			$(emailInput).addClass("italic").addClass("red").val("Nieprawidłowy email");
		   	} else form.submit();
			
		},
		
	focus: function(event) {
		if($(event.target).hasClass("italic"))$(event.target).removeClass("italic").removeClass("red").val("");
	}

}

$(document).ready(function() {
	Communicator.widget = $("div.communicator").get(0);
	Communicator.menu = $("div.communicator div.menu").get(0);
	Communicator.gaduGadget = $("div.communicator div.gadugadu").get(0);
	Communicator.msgGadget = $("div.communicator div.message").get(0);
	$(Communicator.menu).find("div.arrow").bind("click", null, Communicator.arrowClicked);
	$(Communicator.widget).find("input").bind("focus", null, Communicator.focus);
	$(Communicator.widget).find("textarea").bind("focus", null, Communicator.focus);
	});
