/*
Script Name: 	jastASlide (http://jastegg.it/jastASlide/ ) 
version: 		1.0
version date:	2008-07-30
Plugin for:		JAST ( http://jastegg.it )
--------------------------------
*/
JASTEggIt.extend('Slide', {
	_slides: null,
	currentSlide: -1,
	prevent: false,
	options: {
		tocLabel:		'Elenco delle slides',
		tocClose:		'Chiudi tabella dei contenuti',
		slidePrevious:	'Diapositiva precedente',
		slideNext:		'Diapositiva successiva',
		otherText:		'',
		syncLink:		'',
		master:			false,
		timeoutInterval:1000,
		lastTime:		''
	},
	metaParsing: function(){
		var meta = JASTEggIt._name('meta');
		for(var i = 0; i<meta.length; i++){
			var c = meta[i].content;
			var k = meta[i].httpEquiv;
			switch(k){
				 case 'label-move-next':
				 	this.options.slideNext = c;
					break;
				 case 'label-move-previous':
				 	this.options.slidePrevious = c;
					break;
				 case 'label-toc':
				 	this.options.tocLabel = c;
					break;
				 case 'label-toc-close':
				 	this.options.tocClose = c;
					break;
				case 'other-text':
					this.options.otherText = c;
					break;
				case 'sync-link':
					this.options.synkLink=c;
					break;
				case 'master':
					this.options.master = (c=='true');
					break;
				default:
					if(k.substring(0,9)=='meta-info' || k=='Author' || k=='Created'){
						if(k.substring(0,9)=='meta-info') k = k.substring(10,k.length);
						var d = JASTEggIt.DOM.createChild('DT','meta-info');
						d.innerHTML =  k.replace(/\-/g,' ') + ':';
						d.className = k;
						d = JASTEggIt.DOM.createChild('DD','meta-info');
						d.className = k;
						d.innerHTML = c;
					}
					break;
			}
			
			
			
		}
	},
	startup: function(onResize){
		if(!onResize){
			this.metaParsing();
			var allLinks = JASTEggIt._name('a');
			for (var i = 0; i < allLinks.length; i++) {
				var href = allLinks[i].href;
				if(allLinks[i].href.substring(0,6)!='mailto') 
					allLinks[i].href = 'javascript:var w=window.open(\'' + href + '\');'; 
				
			}
			
		}
		this.clientMode = (JASTEggIt.Slide.options.syncLink!='' && !JASTEggIt.Slide.options.master);
		
		var slides = JASTEggIt._class('slide');
		var body = JASTEggIt._name('body')[0];
		JASTEggIt.DOM.setStyle(body, {
			overflow: 'hidden',
			margin: '10px'
		});
		var h = this._getWindowHeight();
		var footer = JASTEggIt._id('footer');
		var header = JASTEggIt._id('header');
		
		var panel = _._id('panel'); 
		if(panel==null) panel = this._drawPanel();
		var slideTop = 10;
		if(footer) h = h- JASTEggIt.DOM.realSize(footer)['height'];
		if(header){
			header.setAttribute('role','heading');
			slideTop += JASTEggIt.DOM.realSize(header)['height'];
			h = h- slideTop;
		} 
		h = h - JASTEggIt.DOM.realSize(panel)['height'];
		
		var ancora = window.location.hash;
		var slideCorrente = 0;
		for(var i = 0; i<slides.length; i++){
			if(slides[i].id==''|| slides[i].id==null) slides[i].id = JASTEggIt.generateUniqueId('slide');
			if(ancora=='#' + slides[i].id) slideCorrente = i;

			slides[i].setAttribute('role','region');
			slides[i].setAttribute('aria-live','rude');
			
			JASTEggIt.DOM.setStyle( slides[i] , {
				height: 	(h-20) + 'px',
				width:		'100%',
				overflow: 	'auto',
				position: 	'absolute',
				top: 		slideTop + 'px',
				zIndex:		'80',
				left: '0'
			});
			if(!onResize){
				JASTEggIt.DOM.setStyle( slides[i] , {display: 	'none'} );
				JASTEggIt.fx.fadeOut(  slides[i].id , 10, 0, -10);
				slides[i].setAttribute('tabindex','-1');
				
				JASTEggIt.Events.add(slides[i],'click', function(event){
					if(JASTEggIt.Events.generator(event).tagName=='A') return false;
					
					if(JASTEggIt.Slide.clientMode) return false;
					JASTEggIt.Slide.display(JASTEggIt.Slide.currentSlide+1);
				});
				
			}
		}
		if(!onResize ){
			JASTEggIt.Events.add(document,'keyup', function(event){
				if(!event) event = window.event;
				if(JASTEggIt.Slide.clientMode) return false;
				
				kbd = JASTEggIt.kbd.getKeyPressed(event);
				if(kbd==37)	JASTEggIt.Slide.display(JASTEggIt.Slide.currentSlide-1);
				if(kbd==39)	JASTEggIt.Slide.display(JASTEggIt.Slide.currentSlide+1);
	
				if (kbd == 37 || kbd == 39) {
					if (event.stopPropagation) 
						event.stopPropagation();
					else 
						event.cancelBubble = true;
				}
			});
			
		}

		this._slides = slides;
		
		var isIE6 =(JASTEggIt.Browser.ie && JASTEggIt.Browser.version<7);
		
		if(footer){
			footer.setAttribute('role','contentinfo');
			JASTEggIt.DOM.setStyle(footer, {
				position: 'fixed',
				bottom: JASTEggIt.DOM.realSize(panel)['height'] + 'px',
				left: '0',
				width: '100%',
				zIndex: '99'
				}
			);
			
			if (isIE6) {
				JASTEggIt.DOM.setStyle(footer, {
					position: 'absolute',
					top: (this._getWindowHeight() - JASTEggIt.DOM.realSize(panel)['height']- JASTEggIt.DOM.realSize(footer)['height']) + 'px'
				});
			}
		}
		if (header) {
			JASTEggIt.DOM.setStyle(header, {
				position: 'fixed',
				top: '0',
				left: '0',
				width: '100%',
				zIndex: '99'
			});
			
			if(isIE6) JASTEggIt.DOM.setStyle(header, { position: 'absolute'});
		}
		if(!onResize){
			this.drawToc();
			JASTEggIt.Events.add(window,'resize', function(){ JASTEggIt.Slide.startup(true)});
			
			this.display(slideCorrente);
			
			if(JASTEggIt.Slide.clientMode){
				this._clientInterval = window.setInterval(JASTEggIt.Slide.synchronize,JASTEggIt.Slide.options.timeoutInterval,null);
			}
		}
	},
	synchronize: function(buffer){
		if (buffer == null) {
			if (JASTEggIt.xhttp._xhrInstances < 2){
				JASTEggIt.xhttp.get(JASTEggIt.Slide.options.syncLink, null, JASTEggIt.Slide.synchronize);
			} 
		} else {
			
			x = JASTEggIt.strings.split(buffer, ' ');
			buffer = x[0];
			if(JASTEggIt.Slide.options.lastTime<x[1]){
				JASTEggIt.Slide.options.lastTime = x[1];
				var i = parseInt(buffer);
				if (!isNaN(i)) {
					JASTEggIt.Slide.display(i, true, buffer);
				}
				
			}
		}
	},
	hideToc: function(){
		JASTEggIt.fx.queue([
			{
				id:	'toc',
				fn:	function(id, q){
					JASTEggIt.fx.fadeOut(id, 10, 0, -20,q);
				}
			},
			{
				id: 'toc',
				fn: function(id,q){
					JASTEggIt.DOM.setStyle(id, {display:'none'});
					JASTEggIt.fx.queueCommandDone(q);
				}
			}
		]);

	},
	drawToc: function(){
		var tocLink = JASTEggIt._id('toc-link');
		JASTEggIt.Accessibility.clickEvent(tocLink, function(){
			var toc =JASTEggIt._id('toc');
			if(!toc){
				toc = JASTEggIt.DOM.createOnDocument('DIV', null, null, 'toc');
				var titles = JASTEggIt._get('.slide>H2');
				var mainTitle = JASTEggIt._name('h1')[0].innerHTML;
				toc.setAttribute('role','navigation');
				var buffer = '<h3><a href="javascript:JASTEggIt.Slide.display(0, true)">' + mainTitle + '</a></h3>';
				buffer +='<ul class="toc">';
				for(var i=0; i<titles.length; i++){
					buffer +='<li><a href="javascript:JASTEggIt.Slide.display(' + (i+1) + ', true)">' + titles[i].innerHTML + '</a></li>'; 
				}
				buffer +='<li><a href="javascript:JASTEggIt.Slide.hideToc()">' + JASTEggIt.Slide.options.tocClose +'</a></li>';
				buffer +='</ul>'
				toc.innerHTML = buffer;
				document.title = mainTitle;
				
				JASTEggIt.DOM.setStyle('toc', {
					display: 'block',
					position: 'fixed',
					bottom: JASTEggIt.DOM.realSize('panel')['height'] + 'px',
					left: JASTEggIt.DOM.position('toc-link')['x'] + 'px',
					zIndex: '99',
					height: '80%',
					overflow: 'auto',
					width: '40%'
				});
				
				if(JASTEggIt.Browser.ie && JASTEggIt.Browser.version<7){
					var h = JASTEggIt.DOM.realSize('panel')['height'] +
							JASTEggIt.DOM.realSize('toc')['height']
					
					JASTEggIt.DOM.setStyle('toc', {
						position: 'absolute',
						top: (JASTEggIt.Slide._getWindowHeight() - h) + 'px'
						
					});
				}
			}
			JASTEggIt.fx.queue([
				{
					id: 'toc',
					fn: function(id,q){
						
						JASTEggIt.DOM.setStyle(id, {display:'block'});
						JASTEggIt.fx.queueCommandDone(q);
					}
				},
				{
					id:	'toc',
					fn:	function(id, q){
						JASTEggIt.fx.fadeIn(id, 10, 100, 10, q);
					}
				},
				{
					id:	'toc',
					fn: function(id, q){
						/* IE Compatibility */
						JASTEggIt.DOM.setStyle(id, {filter:''});
					}
				}
			]);
			
		});

	},
	hideAll: function(id){
		var p = JASTEggIt._class('progressive', id);
		for(var i = p.length-1; i>-1; i--){
			
			if(p[i].id==''|| p[i].id==null) p[i].setAttribute('id', id + '-' + 'progressive-' + i);
			
			if (p[i].getAttribute('altText') == null) {
				p[i].setAttribute('aria-live', 'off');
				p[i].setAttribute('altText', p[i].innerHTML);
				p[i].innerHTML = '&nbsp;';
				p[i].setAttribute('aria-live', 'rude');
				JASTEggIt.fx.fadeOut(p[i].id, 10, 0, -100);
			}
		}
		
	},
	display: function(number, force, stringNubmer){
		if (number<0) return;
		if (number>this._slides.length-1) return;
		if (JASTEggIt.Slide.prevent) {
			JASTEggIt.Slide.prevent = false;
			return;
		}
		if(JASTEggIt.Slide.clientMode && !force) return;
		
		var currentSlide = this._slides[this.currentSlide];
		if(JASTEggIt._id('toc')) JASTEggIt.Slide.hideToc();
		if(force && JASTEggIt.Slide.clientMode && number==this.currentSlide && currentSlide!=null){
			if(currentSlide.id!=null){
				if(stringNubmer+'x'!=number+'x' && stringNubmer!=null){
					var c = JASTEggIt._class('progressive', currentSlide.id);
					var j = stringNubmer.indexOf('.');
					j = stringNubmer.substring(j+1,stringNubmer.length);
					for (var i = 0; i <= j; i++) {
						var id = c[i].id;
						if( JASTEggIt.fx.effects[id]['fade'].opacity == 100 ) c[i].style.height='';
						if(c[i].getAttribute('altText')!=null){
							c[i].innerHTML = c[i].getAttribute('altText');
							c[i].removeAttribute('altText');
							JASTEggIt.fx.fadeIn(id, 10, 100, 10); 
						}
						
					}
					return;
				}
				
			}
		}
		if(number==this.currentSlide && force) return;
		if (!force) {
			if (currentSlide && number>this.currentSlide) {
				var c = JASTEggIt._class('progressive', currentSlide.id);
				for (var i = 0; i < c.length; i++) {
					var id = c[i].id;
					if(JASTEggIt.Browser.ie && JASTEggIt.fx.effects[id] &&
					JASTEggIt.fx.effects[id]['fade'] && 
						JASTEggIt.fx.effects[id]['fade'].opacity == 100 ) 
							c[i].style.height='';
					if(	JASTEggIt.fx.effects[id]['fade'].opacity < 100 ) {
						if(c[i].getAttribute('altText')!=null){
							c[i].innerHTML = c[i].getAttribute('altText');
							c[i].removeAttribute('altText');
							
							JASTEggIt.fx.fadeIn(id, 10, 100, 10); 
						}
						
						if(JASTEggIt.Slide.options.master) JASTEggIt.xhttp.get(JASTEggIt.Slide.options.syncLink,{id: this.currentSlide + '.' + i});
						return;
					}
				}
			}
		}
		if(JASTEggIt.Slide.options.master) JASTEggIt.xhttp.get(JASTEggIt.Slide.options.syncLink,{id: number});
		var nextSlide = this._slides[number];
		if (nextSlide != null){
			var h2 = JASTEggIt._name('h2', nextSlide);
			if(h2.length!=0) {
				var titolo = h2[0].innerHTML;
				titolo = titolo.replace(/<[^>]*>/g, "");
				titolo = titolo.replace(/[\n|\t]*/g,'');
				document.title = titolo;
				
			}
			JASTEggIt.DOM.setStyle(nextSlide, {
				display: 'block'
			});
		}
		if (currentSlide != null){
			JASTEggIt.fx.fadeOut(currentSlide.id, 10, 0, -10);
			JASTEggIt.DOM.setStyle(currentSlide, {zIndex: '80'});
		} 
		if (nextSlide != null) {
			this.hideAll(nextSlide.id);
			JASTEggIt.DOM.setStyle(nextSlide, {zIndex: '90'});
			JASTEggIt.fx.fadeIn(nextSlide.id, 10, 100, 10);
			nextSlide.innerHTML += '';
		}
		this.currentSlide = number;
	},
	
	_drawPanel: function(){
		var panel = JASTEggIt.DOM.createOnDocument('DIV', null, null, 'panel');
		
		var buffer = '<ul id="commands">';
		buffer +='<li><a id="toc-link" href="#toc">';
		buffer +=this.options.tocLabel;
		buffer +='</a></li>';
		buffer +='<li><a id="move-previous" href="javascript:JASTEggIt.Slide.display(JASTEggIt.Slide.currentSlide-1);">';
		buffer +=this.options.slidePrevious;
		buffer +='</a></li>';
		buffer +='<li><a id="move-next" href="javascript:JASTEggIt.Slide.display(JASTEggIt.Slide.currentSlide+1);">';
		buffer +=this.options.slideNext;
		buffer +='</a></li>';
		if(this.options.otherText!=null && this.options.otherText) buffer +='<li>' + this.options.otherText + '</li>';
		buffer += '</ul>';
		panel.innerHTML = buffer;
		if(JASTEggIt.Browser.ie && JASTEggIt.Browser.version<7){
			JASTEggIt.DOM.setStyle('panel', {position: 'absolute', top: (this._getWindowHeight() - JASTEggIt.DOM.realSize('panel')['height']) + 'px' , left:'0'});
		}else
			JASTEggIt.DOM.setStyle('panel', {position: 'fixed', bottom: '0', left:'0'});
		
		return panel;
	},
	
	_getWindowHeight: function () {
		myHeight = 0;
		if( typeof( window.innerHeight ) == 'number' ) {
			//Non-IE
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientHeight ) ) {
			//IE 4 compatible
			myHeight = document.body.clientHeight;
		}
		return myHeight;
	},
	_getWindowWidth: function () {
		myWidth = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
		} else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
			//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientWidth;
		} else if( document.body && ( document.body.clientWidth ) ) {
			//IE 4 compatible
			myHeight = document.body.clientWidth;
		}
		return myHeight;
	}
	
});


