var home_fade_time = 8000;
var last_image = 0;
var home_fader_timeout;

window.addEvent('domready', function() {
	initShowcase();
	initHomeFeature();
	initWysiwyg();
	$$('.lightbox').each(function(el, i) {
		new XtLightbox(el, {
			adaptorOptions: {
				Image: {
					lightboxCompat: false
				}
			},
			rendererOptions: {
				useMask: true
			}
		}); 
	});
	
});

var initWysiwyg = function() {
	var editors = [];
	$$('.basic-editor').each(function(el, i) {
		editors.push(new wysiwyg({
			textarea: el,
			buttons: ['strong','em','u',null,'ul','ol'],
			src:base_url + 'assets/js/_wysiwyg.html'
		}));
	});
	if (document.id('contact-totali')) {
		document.id('contact-totali').addEvent('submit',function(){
			for(var i = 0; i < editors.length; i++) {
				if (editors[i].open) {
					editors[i].toTextarea();
				}
			}
		});
	}
}

var initHomeFeature = function() {
	var imageList = $$('#slideshow li');
	
	if (imageList.length > 1) {		
		home_fader_timeout = setTimeout(function(){
			fadeHomeFeatureImage(imageList, 1, last_image);
		}, home_fade_time);
	}	
}

var fadeHomeFeatureImage = function(imageList, current, last) {
	$(imageList[last]).fade('out');
	$(imageList[current]).setStyle('display', 'block').fade('in');
	last = current;
	if (( current + 1 ) < imageList.length) {
		current = current + 1;
	} else {
		current = 0;
	}
	last_image = last;
	home_fader_timeout = setTimeout(function(){
		fadeHomeFeatureImage(imageList, current, last_image);
	}, home_fade_time);
}

var initShowcase = function() {
	if (document.id('showcase')) {
		
		
		var overMore = false, overButton = false;
		
		$('showcase-more').addEvents({
			'mouseover': function(ev) {
				overButton = true;
				$('more-showcases').setStyle('display', 'block');
			},
			'mouseout': function(ev) {
				overButton = false;
				setTimeout(function() {
					if ((!overMore && !overButton)) $('more-showcases').setStyle('display', 'none');
				}, 100);
			}
		});
		
		$('more-showcases').addEvents({
			'mouseout': function(ev) {
				overMore = false;
				setTimeout(function() {
					if ((!overMore && !overButton)) $('more-showcases').setStyle('display', 'none');
				}, 100);
			},
			'mouseover': function(ev) {
				overMore = true;
			},
			'mousemove': function(ev) {
				overMore = true;
			}
		});
		
		
		var showcase = document.id('showcase');
		$$('#showcase .showcase-item').each(function(ele, i) {
			var thumb = ele.getChildren('.thumb')[0], desc = ele.getChildren('.description')[0], norm = ele.getChildren('.lightbox')[0];
			thumb.addEvent('click', function(ev) {
				var curEle = $$('#showcase .showcase-item.selected')[0], curThumb = curEle.getChildren('.thumb')[0], curDesc = curEle.getChildren('.description')[0], curNorm = curEle.getChildren('.normal')[0];
				curEle.removeClass('selected');
				ele.addClass('selected');
				// reorder
				showcase.grab(ele, 'top');
				showcase.grab(curEle);
			});
		});
	}
}
