/**
 * Post class
 *
 * @author Martin Bartels <martin@apollomedia.nl>
 *
 */
 
var Post = new Class({
	Implements 						: Item,
	
	options: {
		content 					: $empty,
		contentHeight 				: 0,
		excerpt 					: $empty,
		excerptHeight 				: 0,
		contentContainer			: $empty,
		gallery 					: $empty,
		selectedWidth				: 976,
		itemImageSelectedHeight		: 300,
		itemImageHeight 			: 150,
		galleryNavCSSClass 			: '',
		galleryNextCSSClass			: '',
		galleryPreviousCSSClass		: '',
		itemImageCSSClass 			: '',
		useGallery					: true
	}, 
	
	imageHeight : 0,
	titleHeight : 0,
	galleryIndex : 0,
	
	initialize : function(element, options) {
		this.initItem(element, options);
		
		if(this.options.useGallery) {
			this.initGallery();
		} else {
			//this.hideGalleryNav();
		}
	},
	
	initGallery : function() {	
		
		var next = this.element.getElement(this.options.galleryNextCSSClass);
		//console.log(this.options.galleryNextCSSClass);
		if(next!=null) {
			next.addEvent('click', this.showNextGalleryItem.bind(this));
		}
		
		var prev = this.element.getElement(this.options.galleryPreviousCSSClass);
		if(prev!=null) {
			prev.addEvent('click', this.showPreviousGalleryItem.bind(this));
		}
		
		var img = this.element.getElement(this.options.itemImageCSSClass);
		if(img==null) {
			return;
		}
		
		/*
		var s = img.getStyle('background');
		if(s=="") return;

		if(s.indexOf('"')>=0) {
			s = s.split('"')[1];
		} else {
			s = s.split('(')[1];
			s = s.split(')')[0];
		}
	
		this.options.gallery.splice(0,0,s);
		*/
		
		var cont = new Element('div');
		var me = this;
	
		for(var i=0; i<this.options.gallery.length; i++) {
			var l = new Element('a');
			l.addEvent('click', function(e){
				e.stop();
				me.showGalleryItem(this.retrieve('photo'));
				
			});
			l.store('photo', i);
			l.set('text', (i+1)+" ");
			l.addClass('gallery-index');
			l.addClass((i+1));
			
			if(i==0) {
				l.addClass('nav-selected');
			}
			
			cont.adopt(l);
		}
		
		if(this.element.getElement('.gallery-nav-num')!=null)
			this.element.getElement('.gallery-nav-num').adopt(cont);
		
		this.showGalleryItem(0);
	},
	
	showGalleryNav : function() {
		if(this.options.gallery.length==1) return;
		this.galleryIndex = 0;
		$$('.gallery-index').each(function(o,i){
			if(o.hasClass(this.galleryIndex+1)) {
				o.addClass('nav-selected');
			} else {
				o.removeClass('nav-selected');
			}
		}.bind(this));	
		this.element.getElement(this.options.galleryNavCSSClass).reveal();
	},
	
	hideGalleryNav : function() {
		this.element.getElement(this.options.galleryNavCSSClass).hide();
	},
	
	hideContent : function() {
		this.options.contentContainer.hide();
	},
	
	showContent : function() {
		this.options.contentContainer.reveal();
	},
	
	showGalleryItem : function(iIndex) {
		this.galleryIndex = iIndex;
		
		this.element.getElement(this.options.itemImageCSSClass).setStyle('background', 
			'url('+this.options.gallery[this.galleryIndex]+') center center no-repeat #fff');
		
		$$('.gallery-index').each(function(o,i){
			if(o.hasClass(this.galleryIndex+1)) {
				o.addClass('nav-selected');
			} else {
				o.removeClass('nav-selected');
			}
		}.bind(this));	
	},
	
	showNextGalleryItem : function(e) {
		e.stop();
		
		this.galleryIndex++;
		if(this.galleryIndex==this.options.gallery.length) {
			this.galleryIndex = 0;
		}	
	
		this.element.getElement(this.options.itemImageCSSClass).setStyle('background', 
			'url('+this.options.gallery[this.galleryIndex]+') center center no-repeat #fff');
		
		$$('.gallery-index').each(function(o,i){
			if(o.hasClass(this.galleryIndex+1)) {
				o.addClass('nav-selected');
			} else {
				o.removeClass('nav-selected');
			}
		}.bind(this));	
	},
	
	showPreviousGalleryItem : function(e) {
		e.stop();
		
		this.galleryIndex--;
		if(this.galleryIndex==-1) {
			this.galleryIndex = this.options.gallery.length-1;
		}
		
		this.element.getElement(this.options.itemImageCSSClass).setStyle('background', 
			'url('+this.options.gallery[this.galleryIndex]+') center center no-repeat #fff');
		
		$$('.gallery-index').each(function(o,i){
			if(o.hasClass(this.galleryIndex+1)) {
				o.addClass('nav-selected');
			} else {
				o.removeClass('nav-selected');
			}
		}.bind(this));
			
		
		
		this.element
	},
	
	getContent : function() {
		return this.options.content;
	},
	
	setContent : function(sContent) {
		this.element.getElement('.post-excerpt').set('html', sContent);
		this.options.content = sContent;
	},
	
	getExcerpt : function() {
		return this.options.excerpt;
	},
	
	setExcerpt : function(sExcerpt) {
		this.element.getElement('.post-excerpt').set('html', sExcerpt);
		this.options.excerpt = sExcerpt;
	}
});


/**
 * Chocolate & Foam Animated Grid class
 *
 * @author Martin Bartels <martin@apollomedia.nl>
 *
 */
 
var CAFAG = new Class({

	Implements : AnimatedGrid,
	
	selected		 : null,
	lastSelected	 : null,
	selectedHeight 	 : 0,
	spacer 			 : null,
	isTweening		 : false,
	
	initialize : function(options) {
		this.initAnimatedGrid(options);
		this.addEventListeners();
	},
	
	addEventListeners : function() {
		window.addEvent('resize', this.resize.bind(this));
		
		var me = this;
		this.getItems().each(function(item){
			item.element.addEvent('click', function(){
				me.onItemClick.bind(this)(me);
			});
		});
	},
	
	addRowAt : function(row, i) {
		this.rows.splice(i,0,row);
	},
	
	showItem : function(oItem, iWidth, iHeight, oCallback) {
		if(!this.spacer) {
			this.spacer = new HBox({
				width: 10,
				height: 10
			});
		}

		if(this.isTweening) return;
		this.isTweening = true;
		
		if(this.selected!=null) {
			
			this.lastSelected = this.selected;
			this.selectedHeight = this.selected.tweenProps.height;

			this.selected.morph({
				width:this.selected.width,
				height: this.selected.height
			});
			this.selected.move({
				relativeTo: this.options.container, 
				position: {x:'left', y:'top'},
				offset: {x:0,y:0}
			});
			
			this.selected.row = this.rows[0];
			this.rows[0].addAt(this.selected,0);
		}
		
		this.selected = oItem;
		oItem.row.remove(oItem);
		
		this.vbox.remove(this.spacer);
		this.spacer.height = iHeight;
		this.vbox.addAt(this.spacer, this.rows.indexOf(this.selected.row)+1);
		
		oItem.hideContent();
		
		this.sort();
		
		oItem.morph({
			width: iWidth,
			height: iHeight
		});
		
		oItem.morpher.addEvent('complete', oCallback);
		
		oItem.move({
			relativeTo:this.options.container, 
			position: {
				x: 'left', 
				y: 'top'
			},
			offset: {
				x: 38,
				y: oItem.row.options.y+oItem.row.height + this.options.rowMargin
			}
		});

		this.selected.showGalleryNav();
		this.row = this.selected.row;
		this.vbox.position();
	},
	
	sort : function() {
		this.rows.each(function(oRow,i){
			if(oRow.items==undefined) return;
			
			if(oRow.items.length>0) {
				if(oRow.getLength() > this.options.cols) {
					
					if(this.rows[i+2]!=undefined) {
						this.rows[i+2].addAt(oRow.items[0].instance(),0);
						oRow.removeAt(0);
						this.rows[i+2].setRowOfItem(0);
					}
					
				} else if(oRow.getLength() < this.options.cols) {
				
					if(i!=this.rows.length-1) {
						
						if(this.rows[i+2]!=undefined) {
							oRow.addAt(this.rows[i+2].items[0].instance(), this.options.cols-1);
							this.rows[i+2].removeAt(0);
							oRow.setRowOfItem(this.options.cols-1);
						}
						
					}

				}
			}
		}.bind(this));
		
	},
	
	resize : function() {
		this.vbox.position(false);
		
		if(this.selected!=null) {
			this.selected.position({
				relativeTo: this.options.container, 
				position: {x:'left', y:'top'},
				offset: {x:40,y:this.selected.element.getPosition(this.options.container).y}
			})
		}
	},
	
	getContainerHeight : function() {
		return this.container.getSize().y;
	},
	
	onItemClick : function(ag) {
	
		if(ag.selected == this.instance) return;
		
		var eh = this.instance.options.excerptHeight;
		var ch = this.instance.options.contentHeight;
		
		var h = this.instance.height + (ch-eh);
		h += this.instance.options.itemImageSelectedHeight-this.instance.options.itemImageHeight;
			
		
		if(ag.selected) {
			ag.selected.setExcerpt(ag.selected.getExcerpt());
			ag.selected.hideGalleryNav();
		}
		
		var c = function(){
			this.instance.morpher.removeEvent('complete', c);
			this.instance.setContent(this.instance.getContent());
			this.instance.showContent();
			
			var scrollfx = new Fx.Scroll(window, {
				transition: Fx.Transitions.Sine.easeInOut,
				duration: 500
			}).start(0, ag.selected.element.getPosition().y-40);

			ag.resize();
			
			ag.isTweening = false;
			
		}.bind(this);
		
		ag.showItem(this.instance, this.instance.options.selectedWidth, h, c);
	}
});


