// JavaScript Document

var slideshow = Class.create();

slideshow.prototype = {	

	initialize: function(container, images, path) {
	if (!$(container)) {
		throw(container+" doesn't exist!");
		return false;
	}
	this.container = container;
	this.imgNames = images;
	this.path = path;
	this.direction = 1;
	this.preloader = true;
	this.counter = 0;
	this.start_frame = -1;
	this.end_frame = images.length;
	this.delay = 5000;
	this.imgLoading = false;
	this.timer = null;
	},

	removeImage: function(waitTime){
		if(!this.imgLoading){
			this.imgLoading = true;
			$('loadingDiv').show();
			if($(this.container).childElements().length > 1)
				new Effect.Opacity($(this.container).childElements()[1], {duration:0.8, from: 1, to: 0, afterFinish: this.deleteImage.bind(this) });
			setTimeout(this.showImage.bind(this),waitTime);
		}
	},
	
	deleteImage: function(){
		if($(this.container).childElements().length > 1)
			$(this.container).childElements()[1].remove();
	},
	
	showImage: function(){
		var url = this.path + this.imgNames[this.counter];
		
		var currentImg = Builder.node('img', { className : 'slideElement', src: url });
		Element.setStyle(currentImg, {
			'opacity':'0'
		});
		$(this.container).appendChild(currentImg);
		currentImg.onload = (function(){
			this.preloadImage();
			new Effect.Opacity(currentImg, {duration:1.2, from: 0, to: 1, afterFinish: this.setImageLoaded.bind(this) });
			$('loadingDiv').hide();
		}).bind(this);
		this.timer = setTimeout(this.nextSlide.bind(this),this.delay);
	},
	
	preloadImage: function(){
		imgPreloader = new Image();
		var img_number = this.counter+this.direction;
		if(img_number > 0 && img_number < this.end_frame)
			imgPreloader.src = this.path + this.imgNames[img_number];
	},
	
	setImageLoaded: function(){
		this.imgLoading = false;
	},


	nextSlide: function() {
		this.counter++;
		if (this.counter == this.end_frame){
			this.counter = this.start_frame+1; 
			this.preloader = false;
		}
		this.removeImage(0);
	},

	start: function(init_delay) {
		if(this.end_frame!=1)
			setTimeout(this.nextSlide.bind(this),init_delay);
	},
	
	start2: function() {
		this.showImage();
	},
	
	stopShow: function(y) {
		clearTimeout(this.timer);
	},
	
	setImages: function(images) {
		this.imgNames = images;
		this.direction = 1;
		this.preloader = true;
		this.counter = 0;
		this.start_frame = -1;
		this.end_frame = images.length;
		this.imgLoading = false;
		this.nextSlide.bind(this);
	}

}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}	
