var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}

// social buttons handler
var SocialButtons = Class.create({
	initialize: function() {
		var btns = "#fb-btn, #de-btn, #tw-btn, #rs-btn";
		$$(btns).invoke('observe', 'mouseover', this.onHover.bindAsEventListener(this));
		$$(btns).invoke('observe', 'mouseout', this.onLeave.bindAsEventListener(this));
		$$(btns).invoke('observe', 'mousedown', this.onMousePressed.bindAsEventListener(this));
		$$(btns).invoke('observe', 'mouseup', this.onHover.bindAsEventListener(this));
		$$(btns).invoke('observe', 'click', this.onClicked.bindAsEventListener(this));
	},
	onHover: function(event) {
		var item = event.element();
		item.style.backgroundPosition = "0 -20px";
	},
	onLeave: function(event) {
		var item = event.element();
		item.style.backgroundPosition = "0 0";
	},
	onMousePressed: function(event) {
		var item = event.element();
		item.style.backgroundPosition = "0 -40px";
	},
	onClicked: function(event) {
		var item = event.element();
		if(item.id.startsWith('fb')) {
			item.href = "http://www.facebook.com/sharer.php?u=###URL###&t=###TITLE###";
		} else if (item.id.startsWith('de')) {
			item.href = "http://delicious.com/save?v=5&noui&jump=close&url=###URL###&title=###TITLE###";
		} else if (item.id.startsWith('tw')) {
			item.href = "http://twitter.com/home?status=Reading ###TITLE### - "+ encodeURIComponent("http://www.club-tourismus.org");
		}
		
		item.href = item.href.sub('###URL###', encodeURIComponent(location.href));
		item.href = item.href.sub('###TITLE###', encodeURIComponent(document.title.split("•",1)[0].strip()));
		
		//open dialog in a popup
		Popup.open({url: item.href, width: 800});
		
		if (event) event.stop();
	}
});

document.observe("dom:loaded", function() {
	new SocialButtons();
});