/*
 * 
 *  Lazzo.nl
 *  
 */
var Lazzo = [];

document.observe('dom:loaded', function(){
	
	
var docBody = $(document.body);
var content = $('content');
var showcasehome = $('showcase-home');

/*
 * Showcase
 * 
 */
if (docBody.hasClassName('home') && content && showcasehome) {

Lazzo.ShowCase = Class.create({
	
	initialize: function() {
		this.delay = 5;
		this.duration = 0.8;
		this.fadeEffect = null;
		this.showcases = content.select('.post.category-showcase');
		this.showcaseSize = this.showcases.size();
		this.currentCase = this.showcaseSize-1;
		this.createNavs();
		this.start();
	},

	select: function(i) {
		while (this.currentCase != i) {
			var node = this.showcases[this.currentCase];
			content.insert({top:node});
			this.nextCase();
		}
		this.selectNav();
	},
	
	nextCase: function() {
		this.currentCase--;
		if (this.currentCase <= -1) {
			this.currentCase = this.showcaseSize-1;
		}
	},
	
	createNavs: function() {
		this.navs = [];
		this.showcases.each(function(showcase, i) {
			var element = new Element('div', {className:'showcaseNumber	case' + i}).update(i+1).setStyle({left:(i*24) + 48 + 'px', top:content.down('.post img').getHeight()-30+'px'});
			$('home-columns').insert({before:element});
			this.navs.push(element);
			element.observe('mouseenter', this.pause.bind(this));
			element.observe('mouseleave', this.start.bind(this));
			element.observe('click', this.select.bind(this, this.showcaseSize - i - 1));
		}.bind(this));
		this.navs.reverse();
		docBody.addClassName('navigational-elements-inserted');
	},
	
	selectNav: function() {
		this.navs.invoke('removeClassName', 'active');
		this.navs[this.currentCase].addClassName('active');
	},
	
	start: function() {
		var node = this.showcases[this.currentCase];
		this.selectNav();
		this.fadeEffect = new Effect.Fade(node, {
			delay:this.delay,
			duration:this.duration,
			afterFinish:function() {
				this.nextCase();
				showcasehome.insert({top:node});
				node.setOpacity(1);
				node.show(); 
				this.start();
			}.bind(this)
		});
		
	},
	
	pause: function(){
		if (this.fadeEffect)
			this.fadeEffect.cancel();
	},
	stopAndHide: function() {
		this.pause();
		this.showcases.invoke('hide');
	},
	startAndShow: function() {
		this.start();
		this.showcases.invoke('show');
	}
	
});

Lazzo.showCase = new Lazzo.ShowCase();
	


/*
 * Video showcase
 * 
 * 
 */
var videoshowcase = $('video-showcase');
if (videoshowcase) {
	
	var myPlayer = VideoJS.setup("home-showcase-video");
	
	var fx = Class.create(Effect.Base, {
		  initialize: function(element) {
		    this.element = $(element);
		    if (!this.element) throw(Effect._elementDoesNotExistError);
		    this.baseHeight = 245;
		    this.extendedHeight = 524;
		    var options = Object.extend({
		      mode: 'open'
		    }, arguments[1] || { });
		    this.start(options);
		  },
		  update: function(position) {
			  var h;
			 if (this.options.mode == 'open')
				 h = this.baseHeight + (position*280);
			 else 
				 h = this.extendedHeight - (position*280);
		    this.element.setStyle({height:h+'px'});
		  }
	});
	
	
	var open = function(evt) {
		if (evt) 
			evt.stop();
		Lazzo.showCase.stopAndHide();
		docBody.addClassName('video-playing');
		$('showcase-home').insert(videoshowcase);
		new fx($('showcase-home'), {delay:0.1, duration:0.4, mode:'open'});
		$('news-ticker').hide();
		videoshowcase.show();
		try {myPlayer.play();} catch(e) { };
	};
	var frontposts = $$('.entry a');
	var frontpost = frontposts[frontposts.size()-1];
	if (frontpost) {
		frontpost.observe('click', open);	
	}

	var introVidLink = $$('a[title=Intro video]');
	introVidLink.each(function(link){	link.observe('click', open); });
	if (location.search == '?play-video') {
		open();
	}


	var close = function() {

		new fx($('showcase-home'), {duration:0.4, mode:'close', afterFinish: function(){
			$('showcase-home').setStyle({height:'245px'});
			docBody.removeClassName('video-playing');
			$('news-ticker').show();
			try {myPlayer.pause();} catch(e){};
			videoshowcase.remove();			
			Lazzo.showCase.startAndShow();
		}});

	};
	
	
	$('video-close').observe('click', close);
    
	
}



} else {		// not home


	var introVidLink = $$('a[title=Intro video]');
	introVidLink.each(function(link){	link.observe('click', function(evt){evt.stop(); location.href = 'http://www.livewords.com/?play-video';}); });
	
			
	
			
	
}


/*
 * Hot news ticker
 * 
 */
var newsTicker = $('news-ticker');
if (newsTicker) {
	var currentItem = newsTicker.down('.news-item');
	
	var rotateNews = function() {
		
		currentItem = (currentItem.next('.news-item'))? currentItem.next('.news-item') : newsTicker.down('.news-item');
		currentItem.appear({duration:0.5, afterFinish:function(){
			currentItem.fade({duration:0.5, delay:3, afterFinish:rotateNews});	
		}});
	};
	rotateNews();
}

/*
 * Footer height
 * 
 */
var footer = $('footer');
if (footer) {
	var footerheight = footer.getHeight() - 28;
	footer.down('ul > li > ul').childElements().invoke('setStyle', {height:footerheight+'px'})
}

/*
 * Portfolio
 * 
 */
if (docBody.hasClassName('page-template-portfolio-php') || docBody.hasClassName('page-template-partner-php') || docBody.hasClassName('page-template-template_clients-php')) {
	var Collapse = Class.create(Effect.Base, {
		  initialize: function(element) {
		    this.element = $(element);
		    if (!this.element) throw(Effect._elementDoesNotExistError);
		    var options = Object.extend({
		    	fromHeight: 0,
		    	toHeight:    0
		    }, arguments[1] || { });
		    this.start(options);
		  },
		  setup: function() {
		    //this.startHeight = this.element.getHeight();
		  },
		  update: function(position) {
		    this.element.setStyle({
		      height: (this.options.fromHeight + (this.options.toHeight * position)).round() + 'px'
		    });
		  }
		});
	
	var effectInProgress	= false;
	var openNode			= null;
	var mouseenterTimer		= null;

	var doCollapse = function(node) {
		
		if(!effectInProgress && node != openNode) {
			effectInProgress = true;
			
			if (openNode) {
				openNode.removeClassName('active');
				new Collapse(openNode, {
					fromHeight:130,
					toHeight:-110,
					duration:0.3,
					afterFinish: function(){
						//effectInProgress = false;
					}
				});
			}
			
			openNode = node;
			node.addClassName('active');
			new Collapse(node, {
				fromHeight:20,
				toHeight:130,
				duration:0.3,
				afterFinish: function(){
					effectInProgress = false;
				}
			});
		}
	}
	
	
	
	var posts = content.select('.post').each(function(node) {
		
		var mousenter = function(){if (!effectInProgress) {mouseenterTimer = setTimeout(function(){doCollapse(node)}, 750)}};
		node.observe('mouseenter', mousenter).observe('custom:enter', mousenter);
		node.observe('mouseleave', function(){if (!effectInProgress) clearTimeout(mouseenterTimer)});
				
	});
	content.down('.post').fire('custom:enter');
}



/*
 * Faq
 * 
 */
if (content.hasClassName('faq')) {
	
	content.select('p.faq-question').each(function(node, i) {
		
		node.observe('click', function() {
			var opened = (node.next('div').hasClassName('active'))? true:false;
			content.select('div.faq-answer').invoke('removeClassName', 'active');
			if (opened) {
				node.next('div').removeClassName('active');
			} else {
				node.next('div').addClassName('active');	
			}
		});
		
	});

}

/*
 * Newsletter
 * 
 */
var newsletter = $('newsletter');
if (newsletter && newsletter.down('form')) {
	var empty = false;
	var form = newsletter.down('form');
	var input = newsletter.down('input[type=text]');
	if (input.value !== '') {
		form.addClassName('active');
	} else {
		empty = true;
		input.value = 'e-mail';
	}
	
	newsletter.down('input[type=text]').observe('focus', function() {
		newsletter.down('form').addClassName('active');
		if (empty) {
			input.value = '';
		}
	});
	
}


/*
 * call me
 * 
 */
var callme = $('callme');
if (callme) {
	
	var open = false;
	var running = false;
	var drawer = callme.down('div');
	
	var Dropout = Class.create(Effect.Base, {
		  initialize: function(element) {
		    this.element = $(element);
		    if (!this.element) throw(Effect._elementDoesNotExistError);
		    var options = Object.extend({}, arguments[1] || { });
		    this.start(options);
		  },
		  update: function(position) {
		    this.element.setStyle({height:122*position+'px'});
		  }
		});
	
	var finish = function() {
		open = (open)? false: true;
		running = false;
	};
	
	callme.down('h3').observe('click', function() {
		if (running) {return;}
		running = true;
		var transition = (open)? Effect.Transitions.reverse : Effect.Transitions.linear;
		new Dropout(drawer, {duration:0.3, afterFinish:finish, transition: transition});
	});
		

}




});















