
var editable = {
 	toggle: function(value) {
		$(value+'_lnk_modify', value+'_lnk_cancel', $$('#'+value+' '+eval('lnk_block.'+value))[0], $$('#'+value+' form')[0]).each(Element.toggle);
	}
};

var lnk_block = {'infos' : 'ul', 'about' : 'p', 'interests' : 'dl', 'address' : 'div'};
	
var lnk_rules = ['infos', 'about', 'interests', 'address'].fold( {}, function(result, value) {
  result['#'+value+'_lnk_modify'] = function() {
 	  $(value+'_lnk_modify').onclick = function () {
	 	  editable.toggle(value);
	    return false;
	  }
	};
  result['#'+value+'_lnk_cancel'] = function() {
	  $(value+'_lnk_cancel').onclick = function () {
		  editable.toggle(value);
	    return false;
	  }
	};
	result['#'+value+' button'] = function() {
	  $$('#'+value+' button')[0].onclick = function () {
			var v = new Ajax.Updater(value, urlAjax.user+'home/'+value+'/', {
				method: 'post',
				parameters : Form.serialize($$("#"+value+" form")[0]),
				evalScripts: true
			});	
	    return false;
	  }
	};
	return result;
});

Behaviour.register(lnk_rules);	

var Avatar = {
	modify: function() {
 	  [$$('#avatar a.photo')[0], $$('#avatar form')[0], $$('#avatar a.action')].flatten().each(Element.toggle);
	},
	
	cancel: function() {
    [$$('#avatar a.photo')[0], $$('#avatar form')[0], $$('#avatar a.action')].flatten().each(Element.toggle);
	}
};

var other_rules = {
	
	'#avatar .photo': function(el) {
	  el.onclick = function() {
		  el.href = el.href.split('#')[1];
		  return true;
		}
	},
	
	'#avatar a.modify' : function (el) {
	  el.onclick = function () {
      Avatar.modify();
			return false;
	  }	
	},
	
	'#avatar a.cancel' : function (el) {
	  el.onclick = function () {
		  Avatar.cancel();
			return false;
	  }	
	},

	'#photo_lnkedit': function(el) {
		el.onclick = function() {
			$('photo', 'photo_lnkedit').each(Element.hide);
			$('photo_edit', 'photo_lnkcancel').each(Element.show);
		};
	},
	
	'#photo_lnkcancel': function(el) {
		el.onclick = function() {
			$('photo_edit', 'photo_lnkcancel').each(Element.hide);
			$('photo', 'photo_lnkedit').each(Element.show);
		};
	},
	
	'#content #cleft textarea': function(el) {
	  Counters.bind(el, 255);
	},
	
	'#content #cleft input': function(el) {
	  Counters.bind(el, 1024);
	},
  
  '#webvoyages_invit a' : function(e) {
  	e.onclick = function() {
      var href = e.href.split('#');
      
      window.open( href[0], 'help', 'width=545,height=550' );
		  return false;
		}
	},

 '#profile #help' : function (e) {
	  e.onclick = function() {
			 var href = e.href.split('#');
 
      window.open( href[0], 'help', 'width=545,height=550' );
		  return false;
		};
	}
	
};

Behaviour.register(other_rules);
