function Ajax() {
	this.failed = false;
	this.xmlhttp = null;
	this.method = 'GET';
	this.URLString = null;
	this.timer = null;
	this.nocache = 0;

	this.resetFunction();
}

Ajax.prototype = {
	resetFunction: function() {
		this.onloading = function() { return false; };
		this.onsuccess = function() { return false; };
		this.createAJAX();
	},

	createAJAX: function() {
		this.failed = false;
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}
		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	},

	timerRequest: function(file, time, options) {
		var _this = this;
		this.timer = setInterval(function() { _this.request(file, options); }, time);
	},

	stopTimer: function() {
		clearInterval(this.timer);
	},

	request: function(file, options) {
		if(options) {
			this.onsuccess = options.onsuccess || this.onsuccess;
			this.method = options.method || this.method;
			this.nocache = options.nocache || this.nocache;
		}
		if(!this.failed) {
			if (this.xmlhttp) {
				var self = this;
				if (this.nocache) file += ((file.indexOf('?')==-1)?'?':'&')+"rnd=" + Math.random();
				if (this.method == "GET") {
					this.xmlhttp.open(this.method, file, true);
				} else {
					this.xmlhttp.open(this.method, file, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}
				
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
						case 2:
						case 3:
							self.onloading();
							break;
						case 4:
							self.failed = false;
							self.responseText = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.onsuccess(self.xmlhttp);
					}
				};
				this.failed = true;
				this.xmlhttp.send(this.URLString);
			}
		}

	}
};

var plWebcam = {
	baseUrl: 'http://www.konatraveler.com/',
	webcamUrl: 'http://www.konatraveler.com/uploads/images/webcam/',
	nbimg: 60,
	changepic_timeout: 2000,
	reload_timeout: 60000,
	changepic_timer: false,
	reload_timer: false,
	div: false,
	picbox: false,
	controlbox: false,
	next: false,
	previous: false,
	start_stop: false,
	images: new Array(),
	curimg: false,
	current_pic: false,
	diapo_on: false,
	ajaxLoader: false,

	init: function(divid, changetimeout, reloadtimeout) {
		if(changetimeout && changetimeout > 0)	this.changepic_timeout = changetimeout;
		if(reloadtimeout && reloadtimeout > 0)	this.reload_timeout = reloadtimeout;
		
		this.div = document.getElementById(divid);
		if(!this.div)	return false;
		
		this.picbox = document.createElement('div');
		this.picbox.id = 'webcam_pic';
		this.div.appendChild(this.picbox);

		this.ajaxLoader = new Image();
		this.ajaxLoader.src = this.baseUrl + "images/ajax-loader.gif";
		this.picbox.appendChild(this.ajaxLoader);

		this.controlbox = document.createElement('div');
		this.controlbox.id = 'webcam_controls';
		this.div.appendChild(this.controlbox);
	
		this.previous = document.createElement('a');
		this.previous.innerHTML = 'Prev';
		this.previous.id = 'webcam_btn_previous';
		this.previous.onclick = function() { plWebcam.previouspic(); return false; };
		this.controlbox.appendChild(this.previous);

		this.next = document.createElement('a');
		this.next.innerHTML = 'Next';
		this.next.id = 'webcam_btn_next';
		this.next.onclick = function() { plWebcam.nextpic(); return false; };
		this.controlbox.appendChild(this.next);

		this.start_stop = document.createElement('a');
		this.start_stop.innerHTML = 'Stop';
		this.start_stop.id = 'webcam_btn_startstop';
		this.start_stop.onclick = function() { plWebcam.togglediapo(); return false; };
		this.controlbox.appendChild(this.start_stop);
		
		this.curimg = -1;
		this.auto_reload();
	},
	auto_reload: function() {
		this.call();
		this.reload_timer = setTimeout("plWebcam.auto_reload();",this.reload_timeout);
	},
	call: function() {
		var xmlhttp = new Ajax();
		xmlhttp.onsuccess = function(o) { plWebcam.receive(o); };
		xmlhttp.URLString = 'nbimg='+this.nbimg;
		xmlhttp.request(this.baseUrl+'webcam.php', {method: 'POST'});
	},
	receive: function(o) {
		if(this.ajaxLoader){
			this.picbox.removeChild(this.ajaxLoader);
			this.ajaxLoader = false;
		}
		var tmpimages = o.responseText.replace(/^\s+|\s+$/g,"").split('|');
		if(tmpimages.length >= this.images.length){
			var start_diapo = (this.images.length == 0 || this.curimg == (this.images.length -1));
			if(this.curimg > 0){
				// if possible, we try to keep the same picture
				var i = 0;
				while(this.images[i] && this.images[i] != tmpimages[0]){
					i++;
				}
				this.curimg = this.curimg - i;
				if(this.curimg < 0)	this.curimg = -1;
			}
			this.images = tmpimages;
			this.changepic(this.curimg);
			if(start_diapo)	this.start_diapo();
		}
	},
	changepic: function(newpic) {
		if(this.images[newpic]){
			this.curimg = newpic;
			if(!this.current_img){
				this.current_img = new Image();
				this.picbox.appendChild(this.current_img);
			}
			this.current_img.src = this.webcamUrl + this.images[newpic];
			
			if(this.images[newpic + 1]){
				this.preload_pic = new Image();
				this.preload_pic.src = this.webcamUrl + this.images[newpic + 1];
			}
			return true;
		}
		return false;
	},
	autochangepic: function() {
		if(!this.diapo_on)	return false;
		this.changepic(this.curimg + 1)
		if(this.images[this.curimg + 1]){
			this.changepic_timer = setTimeout("plWebcam.autochangepic();",this.changepic_timeout);
		}else{
			this.stop_diapo();
		}
	},
	nextpic: function() {
		this.stop_diapo();
		this.changepic(this.curimg + 1);
	},
	previouspic: function() {
		this.stop_diapo();
		this.changepic(this.curimg - 1);
	},
	stop_diapo: function() {
		this.diapo_on = false;
		this.start_stop.innerHTML = 'Play';
	},
	start_diapo: function() {
		this.diapo_on = true;
		this.start_stop.innerHTML = 'Stop';
		this.autochangepic();
	},
	togglediapo: function() {
		if(this.diapo_on){
			this.stop_diapo();
		}else{
			if(this.curimg == (this.images.length - 1))	this.curimg = -1;
			this.start_diapo();
		}
	}
}


var plCamShot = {
	baseUrl: 'http://www.konatraveler.com/',
	webcamUrl: 'http://www.konatraveler.com/uploads/images/webcam/',
	reload_timeout: 60000,
	reload_timer: false,
	div: false,
	picbox: false,
	title: false,
	current_pic: false,

	init: function(divid, reloadtimeout) {
		if(reloadtimeout && reloadtimeout > 0)	this.reload_timeout = reloadtimeout;
		
		this.div = document.getElementById(divid);
		if(!this.div)	return false;
		
		this.picbox = document.createElement('div');
		this.picbox.id = 'webcam_shot';
		this.div.appendChild(this.picbox);

		this.current_pic = new Image();
		this.div.appendChild(this.current_pic);

		this.title = document.createElement('div');
		this.title.id = 'webcam_shottitle';
		this.div.appendChild(this.title);
			
		this.auto_reload();
	},
	auto_reload: function() {
		this.call();
		this.reload_timer = setTimeout("plCamShot.auto_reload();",this.reload_timeout);
	},
	call: function() {
		var xmlhttp = new Ajax();
		xmlhttp.onsuccess = function(o) { plCamShot.receive(o); };
		xmlhttp.URLString = 'nbimg=1';
		xmlhttp.request(this.baseUrl + 'webcam.php', {method: 'POST'});
	},
	receive: function(o) {
		var tmpimages = o.responseText.replace(/^\s+|\s+$/g,"").split('|');
		if(tmpimages[0] && this.current_pic.src != tmpimages[0]){
			this.current_pic.src = this.webcamUrl + tmpimages[0];
			this.title.innerHTML = tmpimages[0].substr(0,tmpimages[0].length-4).replace("_"," ");
		}
	}
}


