window.addEvent('domready', function(){
// Options
var playslideshow=0; // 0=manual start, 1=automatic play
// Global Variables
if (!imageratio){var imageratio = 1.96;}
var outervpi=$('kanabos');
var scrsize;
	//Background path //
	if (resizemethod==0) {
		var scrsize = 'background/653/';
		if (window.screen.height>768){scrsize = 'background/775/';}
		if (window.screen.height>900){scrsize = 'background/955/';}
		if (window.screen.height>1080){scrsize = 'background/1315/';}
	}
	if (resizemethod==2) {
		var scrsize = 'background/maria/';
	}
	if (resizemethod==1) {		
		var scrsize = 'background/800/';
		if (window.screen.width>800){scrsize = 'background/1024/';}
		if (window.screen.width>1024){scrsize = 'background/1280/';}
		if (window.screen.width>1280){scrsize = 'background/1366/';}
		if (window.screen.width>1366){scrsize = 'background/1440/';}
		if (window.screen.width>1440){scrsize = 'background/1600/';}
		if (window.screen.width>1600){scrsize = 'background/1680/';}
		if (window.screen.width>1680){scrsize = 'background/1920/';}
		if (window.screen.width>1920){scrsize = 'background/2560/';}
	}
	// Activate Slideshow//
	if (!playslideshow) 
		{
			if (Browser.ie) {var playslideshow=1;}
			else {var playslideshow=1;}
		}
	// Fade in explorer and firefox//
		if (!allfade){
			var allfade=0;
		}
	//Select Imput//
	if (typeof thisbg == "undefined" && $('thumbsdiv')) {
		var thisthumbs;
		thisthumbs = $('thumbsdiv').getElements('img');
		thisbg = [];
		Array.each(thisthumbs, function(thumbbb, key){
			var thumburl = thumbbb.get('src');
			thisbg[key] = thumburl.substring(thumburl.lastIndexOf("/")+1);
			thumbbb.addEvents({
				click: function() {
					if (readystate==1 && bgkey!=key){
						readystate=0;						
						clearInterval(periodicalID);
						dotkey = key;
						nextimage();
						begin();
					}
				}
			});
		});
		
	};
	if (thisbg) {
		bgarray=[];
		divarray=[];
		dotdivs=[];
		var maxkey=0;
		Array.each(thisbg, function(imageurl, key){
			if (imageurl && imageurl!='') {
				bgarray[key] = scrsize+imageurl;
				maxkey++;
				divarray[key] = new Element('div', {id: 'bgnumber'+key});
				divarray[key].inject(outervpi);
				if ($('dots')){
					dotdivs[key] = new Element('div', {
						'id' : 'dotnumber'+key,
						'class' : 'mydot'
					});
					dotdivs[key].inject($('dots'));
					dotdivs[key].addEvents({
						click: function() {
							if (readystate==1 && bgkey!=key){
								readystate=0;						
								clearInterval(periodicalID);
								this.setStyle('background-image', 'url("\cktassets/img/dotactive.png")');
								dotkey = key;
								nextimage();
								begin();
							}
						}
					});
				}
			}
		});
	}
	// Fade controls if one picture//
	if (maxkey<=1) {
		if  ($('bgcontrols')) {
			var controlss = $('bgcontrols').getElements('div');
			controlss.setStyle('display', 'none')
		}
		if  ($('dots')) {
			var controlss = $('dots').getElements('div');
			controlss.setStyle('display', 'none')
		}
	}
	// Some Variables for the functions//
	var ratio;
	thisbackgrounds = [];
	var winhei;
	var winhei2;
	var winwid;
	var winwid2;
	var bgkey;
	var oldkey;
	var prevnext;
	var dotkey=100;
	var newkey;
	var maxkey;
	var readystate;
	
	// The Functions //

	function bgratio() {
		ratio = outervpi.clientWidth/outervpi.clientHeight;
		winhei = outervpi.clientHeight;
		winhei2 = outervpi.clientWidth/imageratio;
		winwid = outervpi.clientHeight*imageratio;
		winwid2 = outervpi.clientWidth;
		if (ratio < imageratio || resizemethod==0){
			he = winhei;
			wi = winwid;
		}
		else {
			he = winhei2;
			wi = winwid2;
		}
	}
	function bgresize()
	{	
		var bgimagess = outervpi.getElements('img');
		bgimagess.setStyle('height', he);
		bgimagess.setStyle('width', wi);
		bgimagess.setStyle('display', 'block');
		if ($('dots')){
			if ((wi+200)<winwid2) {
				$('dots').setStyle('right', winwid2-wi);
			}
			else {
				$('dots').setStyle('right', '205');
			}
		}
		if ($('shadow')){
			$('shadow').setStyle('right', winwid2-wi-12);
		}
	}
	function injectt(injnum) 
	{
		if (bgkey>=0) {
			oldkey = bgkey;
			thisbackgrounds[oldkey].setStyle('z-index', 3);
		}
		bgkey = injnum;
		thisbackgrounds[bgkey].setStyle('opacity', 0);
		thisbackgrounds[bgkey].setStyle('z-index', 4);
		if ($('dots')) {
			dotdivs[bgkey].setStyle('background-image', 'url("\cktassets/img/dotactive.png")');
		}
		thisbackgrounds[bgkey].inject(divarray[bgkey]);
		if (resizemethod!==2) {	bgresize(); }		
		if ((Browser.ie||Browser.firefox)&&allfade==0&&resizemethod!==2) {
			thisbackgrounds[bgkey].setStyle('opacity', 1);
			readystate=1;
			if (oldkey>=0) {
				if ($('dots')) {
					dotdivs[oldkey].setStyle('background-image', 'url("\cktassets/img/dotnormal.png")');
				}
				divarray[oldkey].set('html', '')
			}
		}
		else 
		{
			thisbackgrounds[bgkey].set('tween', {
				duration: '500',
				transition: Fx.Transitions.Quint.easeIn,
				onComplete: function() {
						readystate=1;
						if (oldkey>=0) {
							if ($('dots')) {
								dotdivs[oldkey].setStyle('background-image', 'url("\cktassets/img/dotnormal.png")');
							}
							divarray[oldkey].set('html', '')
						}
					}
				});
			thisbackgrounds[bgkey].tween('opacity', 1);
		}
	}
	function preloadd(mybgn){
		if (!thisbackgrounds[mybgn]) {
				var newasset = bgarray[mybgn];
				thisbackgrounds[mybgn] = Asset.image(newasset, {
				properties: {
					'class': 'myImage',
					'title': 'myImage'
				},
				onLoad: function(){
					if (resizemethod!==2) {	bgratio(); }
					injectt(mybgn);
				}
			});
		}
			else {
				if (resizemethod!==2) {	bgratio(); }
				injectt(mybgn);
			}
	}
	/* Periodical setup */
	function nextimage() {
			newkey=bgkey+1;
			if(prevnext==2)
			{
				newkey=bgkey-1;
				if (newkey<0) {
					newkey=maxkey-1;
				}
				prevnext=0;
			};
			if (dotkey!=100){									
				newkey = dotkey;
				dotkey = 100;
			};
			if (newkey>(maxkey-1)) {
				newkey=0;
			};
			preloadd(newkey);
	}
	var periodicalID;
	var begin = function() {
			periodicalID = nextimage.periodical(5000);
	}
	/* End of Periodical setup */
	
	//Doit//
	preloadd(0);
	/* start it! */
	if (maxkey!=0 && $('bgcontrols')) {
		/* events ("start" / "stop") */
		if ($('bgpause')) {
			$('bgpause').addEvents({
				click: function() {
					if (playslideshow==0) {
						playslideshow=1;
						nextimage();
						begin();
						$('bgpause').setStyle('background-image', 'url("\cktassets/img/bgpause.png")');
					}
					else {
						playslideshow=0;						
						clearInterval(periodicalID);
						$('bgpause').setStyle('background-image', 'url("\cktassets/img/bgplay.png")');
					}
				}
			});
		}
		$('nextbg').addEvents({
			click: function() {
				if (readystate==1){
					readystate=0;						
					playslideshow=0;
					clearInterval(periodicalID);
					nextimage();
					if ($('bgpause')) {
						$('bgpause').setStyle('background-image', 'url("\cktassets/img/bgplay.png")');
					}
				}
			}
		});
		$('prevbg').addEvents({
			click: function() {
				if (readystate==1){
					readystate=0;						
					playslideshow=0;						
					clearInterval(periodicalID);
					prevnext=2;
					nextimage();
					if ($('bgpause')) {
						$('bgpause').setStyle('background-image', 'url("\cktassets/img/bgplay.png")');
					}
				}
			}
		});
	}
	if (playslideshow==1&&maxkey>1){
		begin();
		if ($('bgcontrols')){
			if ($('bgpause')) {
				$('bgpause').setStyle('background-image', 'url("\cktassets/img/bgpause.png")');
			}
		}
	}
			//End of Doit//
	window.addEvent('resize', function(){
			var date = new Date();
			var curDate = null;
			
			do { curDate = new Date(); } 
			while(curDate-date < 50);
			if (resizemethod!==2) {	bgresize(); }
			if (resizemethod!==2) {	bgratio(); }
	});
});
