﻿/*
SAF.js
Script for Slideshow a Folder WP-plugin
Creator: Johan Stroem.
© swedish boy productions 2009
*/
safC=10;
safOne=1;
safTwo=2;
SAF_keptSlides=Array();
var safTimer;
SAFpos=0;

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]==0) 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;
	return safL;
}
function createButton(type) {
	safL = document.createElement('div');
	safL.id='SAF_'+type;
	safL.title=SAF_data[7];
	return safL;
}

function SAF_init(element) {
	el = document.getElementById(element);

	if(el && SAF_dir!=false && SAF_slides[1]) {

		// first of all we move all childs on top of slideshow
		childs = el.childNodes;
		for (i=0; i<childs.length; i++) { 
			if(childs[i].id) {
				tc=document.getElementById(childs[i].id);
				if(!tc.style.position) {
					tc.style.position='absolute';
					tc.style.zIndex= tc.style.zIndex >2 ? tc.style.zIndex : 5;
				}
			}
		} 

		el.style.zIndex=0;
		el.style.position='relative';
		layer1 = SAF_create_layer(1);
		el.appendChild(layer1);
		layer2 = SAF_create_layer(2);
		el.appendChild(layer2);
		
		if(SAF_data[6]==1) {
			nextB = createButton('next');
			nextB.onclick=SAF_SlideShow;
			el.appendChild(nextB);
		}

		/*
		prevB = createButton('prev');
		prevB.onclick=prevSlide;
		el.parentNode.appendChild(prevB);
		*/
		
		// launch it.
		if(SAF_data[0]!=0) {
			safTimer = setTimeout(SAF_SlideShow,SAF_data[0]);
		}

	}else{
		//SAF_fallback();		
		window.status=SAF_error;
	}
}

// 	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?


function SAF_SlideShow() {

	// are we there yet?
	clearTimeout(safTimer);
	if(safC>0) {
		topSlide=eval('layer'+safOne);
		backSlide=eval('layer'+safTwo);

		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]; 		
		
		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)	safTimer = setTimeout(SAF_SlideShow,SAF_data[0]);		
	}
}


