window.addEvent('domready', function() {
	
	Element.alias('addEvent','on');
  Element.implement({
  	reload: function() {
    	this.load(this.get('data-url'));
    }
  });
  
  Element.Events.outerClick = {
  	base : 'click',
    condition : function(event){
    	event.stopPropagation();
      return false;
    },
    onAdd : function(fn){
    	this.getDocument().addEvent('click', fn);
    },
    onRemove : function(fn){
    	this.getDocument().removeEvent('click', fn);
    }
  };
  
  $('allmenu').addEvent('mouseleave', function() {
  	if ($('allmenu').getStyle('display') == 'block') {
    	$('allmenu').setStyle('display', 'none');
    }
  });
  
  /*$('friendlist').addEvent('mouseleave', function() {
  	if ($('friendlist').getStyle('display') == 'block') {
    	$('friendlist').setStyle('display', 'none');
    	$('friendsmore').setStyle('background', '#092F4B');
    }
  });*/
  
  new SmoothScroll({ duration: 1000 }, window);
  var roar = new Roar({ position: 'upperRight' });
  SqueezeBox.initialize();
  SqueezeBox.assign($$('a[rel=boxed]'), {
  	size: {x: 400, y: 210}
  });
  
  $$('a').each(function(link) {
  	if(link.hostname != window.location.host && link.hostname != 'zapoj.me' && link.hostname != 'lokali.info' && link.hostname != 'twitter.com' && link.hostname != 'facebook.com') {
    	var loc = link.get('href');
      link.set('href', './out.php?url=' + encodeURIComponent(loc));
    }
  });
  
  $$('.note').makeDraggable ({
		onStart: function(el) {
	  	el.setStyle('opacity','.5');
	  },
	  onComplete: function(el) {
	  	divX = this.element.getLeft(); 
	    divY = this.element.getTop(); 
	    note_id = this.element.id.replace("note_","");
	    el.setStyle('opacity','.7');
	    var updatexy = new Request({
	    	url:'ajax.php?what=note&id=' + note_id + '&x=' + divX + '&y=' + divY,
	      method:'get'
	    }).send();
	 	}
	});
	    
	$$('.editable').each(function(el) {
	  el.addEvent('dblclick',function() {
	    var before = el.get('html').trim();
	    el.set('html','');
	    if (el.hasClass('textarea')) {
	    	var input = new Element('textarea', { 'class':'box', 'text':before });
	    } else {
	      var input = new Element('input', { 'class':'box', 'value':before });
	      input.addEvent('keydown', function(e) { if(e.key == 'enter') { this.fireEvent('blur'); } });
	    }
	    input.inject(el).select();
	    input.addEvent('blur', function() {
	      val = input.get('value').trim();
	      el.set('text',val).addClass(val != '' ? '' : 'editable-empty');
	      if (el.get('class') == 'editable textarea') {
	      	var field = 'content';
	      } else {
	      	var field = 'title';
	      }
	      var url = 'edit-note.php?id=' + el.get('rel') + '&update=' + field + '&value=' + el.get('text').replace(/\n/g, '<br/>');
	      var request = new Request({
	      	url: url,
	        method: 'post',
	        onSuccess: function() {
	        	roar.alert('Uspeh!','Spremembe uspešno shranjene.');
	        }
	     	}).send();
	    });
	  });
	});
	
});
