﻿/*
SAF.js
Slideshow A Folder
Creator: Johan Stroem.
© swedish boy productions 2009
*/
// 	SAF_DATA array 0 = delay, 1 = amount imgs, 2=tile bg:bool, 3=width, 4=height, 5=speed, 6=click next, 7=click hover text, 8=mobile?

// initialize
safC=10;
safOne=1;
safTwo=2;
SAF_keptSlides=Array();
var SAFt;
SAFpos=0;
SAF_stopped=false;

IE=document.all?true:false;

var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

function nextSlide() {
	if(SAFpos==SAF_slides.length) SAFpos=0;
	slide=SAF_slides[SAFpos];
	//SAF_keptSlides.push(slide);
	if(SAF_data[2]==false) bg_repeat='no-repeat';
	else bg_repeat='repeat';			
	slide="black url('"+SAF_dir+''+slide+"') center center "+bg_repeat;
	SAFpos++;
	return slide;
}

function SAF_create_layer(num) {
	safL = document.createElement('div');
	slide = nextSlide();
	safL.style.background=slide;
	safL.style.width=SAF_data[3];
	safL.style.height=SAF_data[4];
	safL.style.zIndex=2-num;
	safL.style.position='absolute';
	safL.style.top=0;
	safL.style.left=0;
	safL.id='saf_'+num;
	//safL.onclick=Function();
	safL.className='saf';
	return safL;
}

function createButton(type) {
	safL = document.createElement('div');
	safL.id='SAF_'+type;
	safL.title=SAF_data[7];
	return safL;
}

function SAF_init(element) {
//	SAFpos = Math.floor(Math.random()*SAF_data[1])
	element = document.getElementById(element);
	if(element && SAF_dir!=false && SAF_slides[1]) {
		// first of all we move all childs on top of slideshow
		childs = element.childNodes;
		for (i=0; i<childs.length; i++) { 
			if(childs[i].id) {
				named=document.getElementById(childs[i].id);
				if(!named.style.position) {
					named.style.position='absolute';
					named.style.zIndex=named.style.zIndex>2? named.style.zIndex : 5;
				}
			}
		} 

		element.style.zIndex=0;
		element.style.position='relative';
		layer1 = SAF_create_layer(1);
		element.appendChild(layer1);
		layer2 = SAF_create_layer(2);
		element.appendChild(layer2);
	
		///*
		if(SAF_data[6]==1 && (iPhone==false || SAF_data[9]=='gallery')) {
		nextB = createButton('next');
		nextB.onclick=SAF_SlideShow;
		element.appendChild(nextB);
		}

		//prevB = createButton('prev');
		//prevB.onclick=prevSlide;
		//element.parentNode.appendChild(prevB);

		// launch it.
	
		if(SAF_data[0]!==0) SAFt = setTimeout(SAF_SlideShow,SAF_data[0]);
		else {
			SAF_stopped=true;
		//if(SAF_fallback) element.innerHTML=SAF_fallback;
		//window.status='SAF_error';
		}

	}
}

function SAF_toggle() {
	
	if(SAF_stopped) {
		SAF_stopped=false;
		_e('pp').className='pause';
		SAF_data[0]=SAF_data[10];
		SAF_SlideShow();
	}else{
		if(safC<=0 || safC==10) clearTimeout(SAFt);
		_e('pp').className='play';
		SAF_stopped=true;
		SAF_data[10]=SAF_data[0];
		SAF_data[0]=0;
	}
}

function SAF_SlideShow() {
	// are we there yet?
	clearTimeout(SAFt);

	
	if(safC>0) {
		topSlide=eval('layer'+safOne);
		backSlide=eval('layer'+safTwo);

		if(SAF_data[8]=='mobile--') {
			topSlide.style.webkitTransform = "rotate(deg30)";
			window.status='iphone trans?';
			safC=0;
		}else{
			if(IE) 	{
				if(Browser.Version() >= 7) topSlide.style.filter='alpha(opacity='+(safC*10)+')';
			}else 	topSlide.style.opacity=0.1*safC;
		
		safC=safC-SAF_data[5]; 		
		}
		//else safC=safC-1;
		
		setTimeout("SAF_SlideShow()",45);
	}else{
	// time to flip slide
		topSlide.style.zIndex=1;
		backSlide.style.zIndex=2;
		//reseting stuff
		if(IE) 	{
			if(Browser.Version() >= 7) 	topSlide.style.filter='alpha(opacity=100)';
		}else 	topSlide.style.opacity=1;
		// next slide please
		slide=nextSlide();
		// put in on the back layer
		topSlide.style.background=slide;
		//what's up, what's next?
		safOne = safOne==1 ? 2 : 1;
		safTwo = safTwo==2 ? 1 : 2;
		//ok, let's run it again...
		safC=10;
		if(SAF_data[0]!=0)	SAFt = setTimeout(SAF_SlideShow,SAF_data[0]);		
	}
}

