var SlideShow = function(divNameOrig) {	
	var state = 0;
	var stateStarted = 1;
	var stateStopped = 0;
	
	var currentPhoto = 0;
	
	var timer = false;
	
	var photos = [];
	
	var divName = divNameOrig;

	var showing = false;
	var dur = 1;
	
	this.add = function(url) {
		photos.push(url);
	}
	
	this.start = function(redraw) {
		state = stateStarted;
		timer = setTimeout('sldShow.next()', 4000);
		if (redraw) {
			this.redrawStartStop();
		}
	}
	
	this.stop = function() {
		state = stateStopped;
		clearTimeout(timer);
		timer = false;
		this.redrawStartStop();
	}
	
	this.prev = function(doStop) {
		currentPhoto--;
		if (currentPhoto < 0) {
			currentPhoto = photos.length -1;
		}
		this.showPicture();

		if (doStop) {
			this.stop();
		}
	}
	
	this.next = function(doStop) {
		currentPhoto++;
		if (photos.length <= currentPhoto) {
			currentPhoto = 0;
		}
		
		this.showPicture();
		
		if (doStop) {
			this.stop();
		} else {
			this.start(false);
		}
	}
	
	this.redrawStartStop = function()	{
		var nav = this.startStopBtn();
		document.getElementById('sshow_navbttns').innerHTML = '<a href="javascript:sldShow.next(true);" class="nav_next">Next</a>' +
					'<a href="javascript:sldShow.prev(true);" class="nav_prev">Previous</a>' +
					nav; 

	}
	
	this.showPicture = function() {
		var url = photos[currentPhoto];

		var image = $('sshow_pic');
		var div = $('sshow_img');

		if (showing) {
			div.style.background = '#000 url(\''+  url  +'\') no-repeat center';
			new Effect.Opacity(image, {duration: dur, from: 1.0, to: 0.0});
			showing = false;
		} else {
			image.src = url;
			new Effect.Opacity(image, {duration: dur, from: 0.0, to: 1.0});
			showing = true;
		}
		//document.getElementById('sshow_img').innerHTML = '<img src="' + url + '" width="416" height="276" />';
	}

	this.startStopBtn = function() {
		if (state) {
			return '<a href="javascript:sldShow.stop();" class="nav_stop">Stop</a>';
		} else {
			return '<a href="javascript:sldShow.start(true);" class="nav_play">Play</a>';
		}
	}

	this.show = function() {
		document.getElementById(divName).innerHTML = '<div id="sshow_img" style="width:416; height:276; padding: 0px;"><img width="416" height="276" id="sshow_pic"/></div><div id="sshow_nav"><div id="sshow_navbttns"></div><div id="sshow_preloads" style="visibility: hidden; position: absolute; top: -1; left: -1; width: 1px; height: 1px; overflow: hidden;"></div></div>';
		this.redrawStartStop();
		this.preloads();
		this.showPicture();
	}

	this.preloads = function() {
		//fix of numerous JS requests
		for (var i = 0; i < photos.length; i++) {
		//for (var i in photos) {
			var url = photos[i];
			document.getElementById('sshow_preloads').innerHTML = document.getElementById('sshow_preloads').innerHTML + '<img src="' + url + '" width="416" height="276" />';
		}
	}
};

function boxesSwitch(boxId) {
	var open = (document.getElementById(boxId).className == 'policy_text_hided');
	boxesCloseAll();
	if (open) {
		document.getElementById(boxId).className = 'policy_text';
		document.getElementById(boxId + 'Ctrl').className = 'details_slctd';
	}
}
function boxesClose(boxId) {
	document.getElementById(boxId).className = 'policy_text_hided';
	document.getElementById(boxId + 'Ctrl').className = 'details';
}
function boxesCloseAll() {
	boxesClose('boxInclExcl');
	boxesClose('boxPricing');
	boxesClose('boxCancel');
}