/**
 * @author Bartek G
 */
var Bookmarks = new Class({
	Implements: Options,
    options: {
        selector: 'tools',
        main: ['wykop','twitter','facebook'],
		more: ['wykop','twitter','facebook','separator','live','friendfeed','delicious','digg','google','technorati','gwar','bebopl','linkologia','zakladka'],
		text: 'Share with friends',
		textMore: 'More',
		path: 'bookmarks/',
		id: 'bookmarks',
		link: document.URL,
		title: document.title,
		description: -1,
		target:0,
		where: 'bottom'
    },
	sitelist: null,
	more: null,
	initialize: function(options){
		var bind = this;
		this.setOptions(options);
		var controller = new Element('div',{
			'id': this.options.id,
			'class': 'socialbookmarks',
			'html': '<span>' + this.options.text + '</span><ul><li class="' + this.options.id + 'more"><a href="#"><strong>' + this.options.textMore + '</strong></a></li></ul>'
		});
		if ($(this.options.selector)) {
			controller.inject($(this.options.selector),this.options.where);
			this.more = controller.getElement('li.' + this.options.id + 'more');
			if (!this.options.more || !this.options.more.length) 
				this.more.destroy();
			if (this.options.description < 0) {
				$$('meta').each(function(el){
					if (el.httpEquiv.toLowerCase() == 'description') 
						bind.options.description = el.get('content');
				});
			}
			if (bind.options.description < 1) bind.options.description = '';
			this.load();
		}
		if(this.options.target) {
			this.options.target = ' target="'+ this.options.target + '"';
		}
	},
	proc: function() {
		var bind = this;
		var list = $(bind.options.id).getElement('ul');
        if(this.options.main) this.options.main.each(function(el){
			var json = eval('bind.sitelist.sites.' + el);
            var li = new Element('li',{
				'class':el,
				'html':'<a href="' + json.url.replace('{title}',bind.options.title).replace('{description}',bind.options.description).replace('{link}',bind.options.link) + '" title="' + json.name + '"' + bind.options.target + '><img src="' + bind.options.path + 'icons/' + json.img + '" alt="' + json.name + '" /><span>' + json.name + '</span></a>'
			});
			li.inject(list);
        });
		bind.more.inject(list);
		if (this.options.more) {
			bind.more.fade('show');
			new Element('div',{'html':'<ul></ul>'}).inject(bind.more);
			var moreList = bind.more.getElement('ul');
			moreList.fade('hide');
			bind.more.getElement('a').addEvent('click', function(e) {
	            var e = new Event(e);
	            e.preventDefault();
            });
			bind.more.addEvents({
				'mouseenter':function(){
					moreList.fade(1);
				},
				'mouseleave':function(){
                    moreList.fade(0);
                }
			});
	        this.options.more.each(function(el){
				if(el=='separator') {
					var li = new Element('li',{
                        'class':el,
                        'html':'<span>&nbsp;</span>'
                    });
				}
				else {
					var json = eval('bind.sitelist.sites.' + el);
                    var li = new Element('li',{
	                    'class':el,
	                    'html':'<a href="' + json.url.replace('{title}',bind.options.title).replace('{description}',bind.options.description).replace('{link}',bind.options.link) + '" title="' + json.name + '"' + bind.options.target + '><img src="' + bind.options.path + 'icons/' + json.img + '" alt="' + json.name + '"/><span>  ' + json.name + '</span></a>'
	                });
				}
	            li.inject(moreList);
	        });
		}
    },
	load: function() {
		var bind = this;
		new Request.JSON({
            url: this.options.path + 'sites.json',
			onRequest: function(){
				bind.more.fade('hide');
				bind.more.getParent('div').addClass('loading');
			},
	        onSuccess:function(responseJSON) {
	            bind.sitelist = responseJSON;
				bind.proc();
				bind.more.getParent('div').removeClass('loading');
	        }
        }).send();
	}
});
