YUI.add('connector', function (Y) {
    Y.Connector = function () {
	    this.obs = [];
	    this.events = [];
    };
    
    Y.Connector.prototype.getObj = function (ob) {
	    var i;
		for (i = 0; i < this.obs.length; i += 1) {
			if (this.obs[i] === ob) {
				return i;
			}
		}
		return -1;
    };
    
    Y.Connector.prototype.addConnection = function (ob, e, connection) {
	    var i, n;
	    n = this.getObj(ob);
	    if (n < 0) {
		    this.obs.push(ob); // myController
		    n = this.obs.length - 1;
		    this.events[n] = {};
	    }
	    
	    for (i = 0; i < e.length; i += 1) {
		    if (!this.events[n][e[i]]) {
			    this.events[n][e[i]] = []; //['all']
		    }
		
		    this.events[n][e[i]].push(connection); // myItems
	    }    
	    
    };
		
    Y.Connector.prototype.notifyConnections = function (e, caller, argv) {
	    var n = this.getObj(caller);
	    
	    if (n > -1) {
		    if (this.events[n].all) {
			    this.connectionNotify(this.events[n].all, e, argv);
		    }
		    if (this.events[n][e]) {
			    this.connectionNotify(this.events[n][e], e, argv);
		    }
	    }
    };

    Y.Connector.prototype.connectionNotify = function (connections, e, argv) {
	    var i;
	    for (i = 0; i < connections.length; i += 1) {
		    connections[i].receive(e, argv);
	    }	
    };
});

YUI().use('node', 'event', "datasource", "anim",  'connector', 'slideshow', 'slideshow-buffer', 'slideshow-display', 'slideshow-navigator', function (Y) {
    //new Y.StyleSheet(".slide .img {background: none !important;}");'stylesheet',
    Y.on('domready', function () {
	
	    
            var connector = new Y.Connector();
	    
	    var init_more = function() {
		var _handle
		,   _cont = Y.one('#content')
		,   _timeout
		,   _load = Y.one('.more .load')
		,   _source = new Y.DataSource.IO({source:loc + 'more'})
		,   _setHandle = function () {
			var o = Y.one('.more a.req');
			if(o !== null) {
			    _handle = Y.on('click',function(e) {
				e.preventDefault();
				_timeout = setTimeout(function() {
				    _load.removeClass('hide')
				}, 50);
				_source.sendRequest({request:"?"+String(e.currentTarget.getAttribute('href')).split('?')[1]});
			    },Y.one('.more a.req'));
			}
		};
		
		_source.on('data', function(e){
		    clearTimeout(_timeout);
		    Y.detach(_handle);
		    var o = Y.one('.more');
		    if(o !== null){
			o.remove();
		    }
		    _cont.append(e.data.responseText);
		    _setHandle();
		    _setSlideShow();
		});
		
		_setHandle();
	    }();
	    
	    Y.log('domready');
            
	    var _setSlideShow = function(){
		Y.all('.slideshow.ph').each(function(o){
		    var ss
		    , slides = [];
		    o.all('.slide').each(function (slide) {
			var data = slide.one('input').get('value').split('::');
    
			if(data[0] == 'img') {
			    slides.push({typ:data[0], src:data[1], w: data[2], h: data[3]});
			} else {
			    slides.push({typ:data[0], src:data[1], com: data[2]});
			}
			slide.remove();
		    });
		    o.one('.layer').remove();
		    ss = new Y.Slideshow(connector, o,  o.next('.slideshow-nav').one('.count'), '', slides.slice(),{});
		    new Y.SlideshowNavigator(connector, ss, o.next('.slideshow-nav'), 0, slides.length);
		    o.removeClass('ph');
		})
	    }
	    
	    _setSlideShow();
	    // return links
	    
	    Y.on('click',function(e){
		e.preventDefault();
		history.go(-1);
	    },Y.all('.return a'));
    });
});