function d() {
	console.log.apply(console, arguments);
}

var Popup = {
	isOpen : false,
	isCreate : false,
	isAnim : false,
	finder : function() {
		var _self = this;

		$$('.pics a').each(function(a) {
			var img = a.getElement('img');
			// img.setStyle('cursor','pointer');
				a.addEvent('click', function(e) {
					e.stop();
					_self.show(a.get('href'));
				}.bindWithEvent(this));
			});

	},
	create : function() {
		if (!this.isCreate) {
			this.isCreate = true;
			this.dom = new Element('div', {
				'id' : 'popup'
			});
			document.body.appendChild(this.dom);
			var _s = this;
			this.tween = new Fx.Tween(this.dom, {
				duration : 300,
				// transition: Fx.Transitions.Quad.easeOut,
				onComplete : function() {

					_s.isAnim = false;
					if (!_s.isOpen) {
						_s.dom.setStyle('display', 'none');

					}
				}
			});

		}
	},
	open : function(width, height) {
		if (!this.isOpen) {
			this.create();
			this.isOpen = true;
			Mask.show();
			this.width = width;
			this.height = height;
			this.dom.setStyles( {
				'width' : this.width,
				'height' : this.height,
				'top' : -this.height,
				'margin-top' : -this.height / 2-6,
				'margin-left' : -this.width / 2-6
			});
			this.dom.setStyle('display', 'block');
			this.isAnim = true;
			this.openPos = window.getSize().y / 2 + window.getScroll().y;
			this.tween.start('top', -this.height, this.openPos);

		}
	},
	show : function(src,call) {
		var i = new Image();
		var _s = this;
		i.onload = function() {
			_s.open(this.width, this.height);
			_s.dom.innerHTML = '';
			_s.dom.appendChild(this);
			this.style.cursor = 'pointer';
			this.onclick = function() {
				_s.close();
				if(call){
					call();
				}
			};
		};

		// i.src = src.replace(/\/img\/min\//,'/img/max/');
		i.src = src;

	},
	close : function() {
		if (this.isOpen && !this.isAnim) {
			this.isOpen = false;
			this.isAnim = true;
			Mask.hide();
			this.tween.start('top', window.getSize().y / 2
					+ window.getScroll().y, -this.height);
		}
	}

};

var Gallery = new Class( {
	initialize : function(pop) {
		var days = [];
		var _self = this;
		var g = $$('.gallery');
		var dl = [];
		if (g.length) {
			var isFirst = true;
			g[0].getElements('.days').each(function(el){
				days.push(new Gallery.Days(_self,el,isFirst));
				isFirst = false;
			});
		}
		this.days = days;
	},
	toggle:function(){
		this.days.each(function(day){
			day.close();
		});
	}
});
Gallery.Days = new Class( {
	initialize : function(parent,d,isFirst) {
		this.parent = parent;
		this.isOpen = isFirst;
		
		var _self = this;
		var ps = d.getElements('.pic');
		var c = new Element('div',{
			'class':'wrap'
		});
		var cc = new Element('div',{
			'class':'wrap-body'
		});
		d.adopt(c);
		c.adopt(cc);
		//c.setStyle('overflow','hidden');
		if(!isFirst){
			c.setStyle('height',0);
		}
		var pics = [];
		ps.each(function(p) {
			cc.adopt(p);
			pics.push(new Gallery.Pic(_self,p));
		});
		this.pics = pics;
		cc.adopt(d.getElement('.clear'));
		
		c.set('tween');
		var h2 = d.getElement('h2');
		h2.addEvent('click', function(e) {
			e.stop();
			_self.onClick();
		});
		h2.addClass('over');
		this.preview = new Gallery.Preview(_self,cc);
		this._cc = cc;
		this._c = c;
	},
	onClick:function(){
		this.parent.toggle();
		if(!this.isOpen){
			this.open();
		}
	},
	open:function(){
		if(!this.isOpen){
			this._c.tween('height',this._cc.getSize().y);
			this.isOpen = true;
		}
	},
	close:function(){
		if(this.isOpen){
			this._c.tween('height',0);
			this.isOpen = false;
		}
	},
	show:function(img){
		this.preview.show(img);
	},
	toggle:function(){
		this.pics.each(function(pic){
			pic.show();
		});
	}
});
Gallery.Pic = new Class({
	initialize : function(parent,p) {
		var _self = this;
		this.isShow = true;
		this.parent = parent;
		var a = p.getElement('a');
		var l = p.getElement('.label');
		var ii = new Element('div',{
			'class':'mask'
		});
		a.set('tween',{duration:300});
		a.inject(ii);
		l.inject(ii);
		ii.inject(p,'top');
		a.addEvent('click',function(e){
			
			e.stop();
			parent.toggle();
			_self.hide();
			Popup.show(a.get('href'),function(){
				parent.toggle();
			});
		});
		this._a = a;
	},
	show:function(){
		if(!this.isShow){
			this._a.tween('top',0);
			this.isShow = true;
		}
	},
	hide:function(){
		if(this.isShow){
			this._a.tween('top',-this._a.getSize().y);
			this.isShow = false;
		}
	}
});
Gallery.Preview = new Class({
	initialize : function(parent,cc) {
		var p = new Element('div',{
			'class':'preview'
		});
		p.inject(cc,'top');
		p.set('tween');
		this._p = p;
	},
	show:function(src){
		this._p.empty();
		var img = new Image();
		img.src = src;
		img.inject(this._p);
		this._p.tween('height',200);
	}
});
var Mask = {
	isOpen : false,
	isCreate : false,
	create : function() {
		if (!this.isCreate) {
			this.isCreate = true;
			this.dom = new Element('div', {
				'id' : 'pageMask'
			});
			document.body.appendChild(this.dom);
		}
	},
	show : function() {
		if (!this.isOpen) {
			this.isOpen = true;
			this.create();

			this.dom.setStyles( {
				'height' : window.getScrollSize().y,
				'opacity' : 0
			});

			this.dom.setStyle('display', 'block');
			this.dom.fade(0.50);

		}
	},
	hide : function() {
		if (this.isOpen) {
			this.isOpen = false;
			this.dom.fade(0);
		}
	}
};
window.addEvent('domready', function() {
	Popup.finder();
	new Gallery();
});
