var cam_count = 2;
var light_count = 1;
var ptz_count = 2;

var update_time = 1000;
var update_time_slow = 5000;
var max_load_time = 3000;

var stats_time = 60000;

// YOU DONT HAVE TO CHANGE THE NEXT LINES - THEY WORK FINE!

/* ------------------------------------------------------------------ */

var allow_check = false;
var webcam_error = true;
var admin_mode = false;
var light_mode = false;

var last_ptz_pos = new Array(ptz_count);
for(var a = 0; a < ptz_count; a++)
	last_ptz_pos[a] = -1;

var image_current = new Array(cam_count);
for(var a = 0; a < cam_count; a++)
	image_current[a] = 0;

var image_lock = new Array(cam_count);
for(var a = 0; a < cam_count; a++)
	image_lock[a] = 0;

var opacity_hide = 0.0;
var opacity_norm = 0.4;
var opacity_info = 0.7;

/* ------------------------------------------------------------------ */

var comment_slide;
var bestof_slide;
var bestof_morph;
var bestof_current = 0;

window.addEvent("domready", function()
{
	$("pass").addEvents
	(
		{
			keyup: function()
			{
				var url_parameter = "action=check_user";
				var url = "command.php";

				new Request
				(
					{
						url: url,
						data: url_parameter + "&user=" + $("user").value + "&pass=" + $("pass").value,
						onSuccess:function(str)
						{
							var light = $('lightbar');
							if(str > 0)
							{
								$('lock_state').src = 'images/lock.png';
								light.addClass("button");
								light_mode = true;
								if(str > 1)
								{
									admin_mode = true;
								}
								else
									admin_mode = false;
							}
							else
							{
								$('lock_state').src = 'images/unlock.png';
								light.removeClass("button");
								light_mode = false;
								admin_mode = false;
							}
						},
						method:"get"
					}
				).send();
			}
		}
	);

	$("walk2").addEvent("click", function(e) {
		e.stop();
		nS1.walk(1);
	});

	$("save1").addEvent("click", function(e) {
		e.stop();
		save_img(1);
	});
	$("save2").addEvent("click", function(e) {
		e.stop();
		save_img(2);
	});

	$("light1").addEvent("click", function(e) {
		e.stop();
		flip_light(1);
	});

	$("ptz1_1").addEvent("click", function(e) {
		e.stop();
		move_ptz(1, 1);
	});
	$("ptz1_2").addEvent("click", function(e) {
		e.stop();
		move_ptz(1, 2);
	});
	$("ptz1_3").addEvent("click", function(e) {
		e.stop();
		move_ptz(1, 3);
	});

	$("ptz2_1").addEvent("click", function(e) {
		e.stop();
		move_ptz(2, 1);
	});
	$("ptz2_2").addEvent("click", function(e) {
		e.stop();
		move_ptz(2, 2);
	});
	$("ptz2_3").addEvent("click", function(e) {
		e.stop();
		move_ptz(2, 3);
	});

	$("check").addEvent("click", function(e) {
		e.stop();
		flip_check();
	});

	$("sfun1").addEvent("click", function(e) {
		e.stop();
		fun1();
	});
	$("sfun2").addEvent("click", function(e) {
		e.stop();
		fun2();
	});

	bestof_loaded = false;
	var nS1 = new noobSlide
	(
		{
			box: $("box"),
			items: $$("#box div"),
			size: 740,
			handles: $$("#handles span"),
			onWalk: function(currentItem,currentHandle){
				if(currentHandle.innerHTML == "BestOf Bilder" && !bestof_loaded)
				{
					bestof_loaded = true;
					get_bestof_links(0);
				}
				$$(this.handles).removeClass("active");
				$$(currentHandle).addClass("active");
			}
		}
	);

	$("barbestof").set("opacity", opacity_hide);

	$('form_com').addEvent('submit', function(e)
	{
		e.stop();
		if($('com_id').value == "" ||  $('com_name').value == "" || $('com_text').value == "")
			return;

		var img = $('spinner_com');
		img.src = "images/reload.png";

		var url_parameter = "action=insert_comment&id=" + $('com_id').value + "&com_name=" + $('com_name').value + "&com_text=" + $('com_text').value;
		var url = "command.php";

		new Request
		(
			{
				url: url,
				data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
				onSuccess: function(str)
				{
					img.src = "images/empty.png";
					get_comment($('com_id').value);

					//$('com_name').value = "";
					$('com_text').value = "";
				},
				method:'post'
			}
		).send();
	});

	comment_slide = new Fx.Slide('bestof_comments', {duration: 'short'});
	bestof_slide = new Fx.Slide('bestof_images', {duration: 'short'});

	// start em
	setTimeout("flip_check()", 500);
	setTimeout("check()", 1000);
	update_stats();
});

/* ------------------------------------------------------------------ */

function check()
{
	if(allow_check)
	{
		var url_parameter = "action=check";
		for(var a = 0; a < cam_count; a++)
			url_parameter += "&img_now_" + (a + 1) + "=" + image_current[a];
		var url = "command.php";

		var status_img = $("status_img");
		var status_text = $("status_text");

		new Request.JSON
		(
			{
				url: url,
				data: url_parameter,
				onComplete: function(jsonObj)
				{
					if(jsonObj != null)
					{
						/*
						 * $json['server_state']
						 *		 0 = webcam is up and working
						 *		-1 = we are connecting to the webcam, or the webcam is idle
						 *		-2 = the webcam is offline or dead
						 * $json['cam'] - array
						 *			['id']		id of the cam starting at 1
						 *			['img']		0 = no new image
						 *						>0 = new image
						 * $json['light'] - array
						 *			['id']		id of the light starting at 1
						 *			['state']	0 = light is off
						 *						1 = light is on
						 * $json['ptz'] - array
						 *			['id']		id of the ptz starting at 1
						 *			['state']	0 = ptz is moving
						 *						>0 = ptz position
						 * $json['user_online']	holds the actual user which are online
						 */

						jsonObj.lights.each(
							function(light)
							{
								update_light(light.id, light.state);
							}
						);
						jsonObj.ptzs.each(
							function(ptz)
							{
								update_ptz(ptz.id, ptz.state);
							}
						);
						$('user_online').innerHTML = jsonObj.user_online;

						switch(jsonObj.server_state)
						{
							case 0:
								if(webcam_error)
								{
									webcam_error = false;
									status_text.innerHTML = "";
									status_img.src = "images/empty.png";
								}
								jsonObj.cams.each(
									function(cam)
									{
										if(cam.img != 0)
											get_img(cam.id, cam.img);
										// save is ok
										$("bar" + cam.id).fade(opacity_norm);
									}
								);

								// recheck
								setTimeout("check()", update_time);
								break;

							case -1:
								webcam_error = true;
								status_text.innerHTML = "Verbinde zu Webcam";
								status_img.src = "images/warning.png";

								// dont hammer
								setTimeout("check()", update_time_slow);
								break;

							case -2:
								webcam_error = true;
								status_text.innerHTML = "Webcam ist offline";
								status_img.src = "images/error.png";

								// dont hammer
								setTimeout("check()", update_time_slow);
								break;
						}
					}
					else
					{
						// dont hammer
						setTimeout("check()", update_time_slow);
					}
				},
				method:'get'
			}
		).send();
	}
	else
	{
		// dont hammer
		setTimeout("check()", update_time_slow);
	}

	return true;
}

/* ------------------------------------------------------------------ */

function delete_img(img)
{
	var url_parameter = "action=delete&img=" + img;
	var url = "command.php";

	var bar = $("barbestof");
	var status_img = $("status_img");
	var status_text = $("status_text");

	status_text.innerHTML = "L&ouml;sche Bild";
	status_img.src = "images/info.png";
	bar.fade(opacity_info);

	new Request
	(
		{
			url: url,
			data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
			onSuccess:function(str)
			{
				if(str == 1)
				{
					status_text.innerHTML = "Bild gel&ouml;scht";
					status_img.src = "images/info.png";
					get_bestof_links(bestof_current);
					$('bestof_comments').innerHTML = "";

					var img = $("image_bestof");
					img.src = "shake.gif";
					img.title = "shake your head";
				}
				else
				{
					status_text.innerHTML = "Bild nicht gel&ouml;scht";
					status_img.src = "images/error.png";
				}
				bar.fade(opacity_hide);
			},
			onFailure: function()
			{
				status_text.innerHTML = "Bild nicht gel&ouml;scht";
				status_img.src = "images/error.png";
				bar.fade(opacity_hide);
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function flip_check()
{
	var status_img = $("check");

	allow_check = !allow_check;
	if(allow_check)
	{
		status_img.src = "images/stop.png";
		status_img.title = "Webcam stoppen";
	}
	else
	{
		status_img.src = "images/start.png";
		status_img.title = "Webcam starten";
	}
}

/* ------------------------------------------------------------------ */

function flip_light(id)
{
	var url_parameter = "action=light_flip&id=" + id;
	var url = "command.php";

	var status_img = $("status_img");
	var status_text = $("status_text");

	new Request
	(
		{
			url: url,
			data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
			onSuccess: function(str)
			{
				if(str == 1)
				{
					status_text.innerHTML = "Licht umgeschaltet";
					status_img.src = "images/info.png";
				}
				else
				{
					status_text.innerHTML = "Licht nicht umgeschaltet";
					status_img.src = "images/error.png";
				}
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function fun1()
{
	var src = "http://bimmel-bommel.de/fun/flop_" + $("tfun1").value + ".gif";
	if($("ifun1").src != src)
		$("ifun1").src = src;
}

/* ------------------------------------------------------------------ */

function fun2()
{
	var src = "http://bimmel-bommel.de/fun/flip_" + $("tfun2").value + ".gif";
	if($("ifun2").src != src)
		$("ifun2").src = src;
}

/* ------------------------------------------------------------------ */

function get_img(cam, img)
{
	var time = new Date().getTime() + max_load_time;

	if(image_lock[cam - 1] > time)
		return;
	image_lock[cam - 1] = time;

	var url_parameter = "action=image&img_num=" + img + "&img_cam=" + cam;
	var url = "command.php";

	$("spinner" + cam).src = "images/reload.png";

	var images = [url + "?" + url_parameter];
	new Asset.images
	(
		images,
		{
			onComplete: function()
			{
				$("spinner" + cam).src = "images/empty.png";
				image_lock[cam - 1] = 0;
				image_current[cam - 1] = img;
				images.each(
					function(image, i)
					{
						$("image" + cam).src = url + "?" + url_parameter;
					}
				);
			}
		}
	);
}

/* ------------------------------------------------------------------ */

function move_ptz(id, pos)
{
	var url_parameter = "action=move_ptz&id=" + id + "&pos=" + pos;
	var url = "command.php";

	var ptz = $("ptz" + id + "_" + pos);
	ptz.addClass("waiting");

	var status_img = $("status_img");
	var status_text = $("status_text");

	new Request
	(
		{
			url: url,
			data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
			onSuccess: function(str)
			{
				if(str == 1)
				{
					status_text.innerHTML = "Position gesendet";
					status_img.src = "images/info.png";
				}
				else if(str == 0)
				{
					status_text.innerHTML = "Kamera bewegt sich bereits";
					status_img.src = "images/warning.png";
				}
				else
				{
					status_text.innerHTML = "Position nicht umgeschaltet";
					status_img.src = "images/error.png";
				}
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function save_img(cam)
{
	var url_parameter = "action=save&img_num=" + image_current[cam - 1] + "&img_cam=" + cam;
	var url = "command.php";

	var bar = $("bar" + cam);
	var status_img = $("status_img");
	var status_text = $("status_text");

	status_text.innerHTML = "Speichere Bild";
	status_img.src = "images/info.png";
	bar.fade(opacity_info);

	new Request
	(
		{
			url: url,
			data: url_parameter,
			onSuccess:function(str)
			{
				if(str == 1)
				{
					var frame = $("frame" + cam);
					var img = new Element("img", {"src": $("image" + cam).src}).setStyle("position", "absolute").inject(frame, "top");
					new Fx.Morph(img).start({marginTop:"-260px",width:"0px",height:"0px"});

					status_text.innerHTML = "Bild gespeichert";
					status_img.src = "images/info.png";
					get_bestof_links(bestof_current);
				}
				else
				{
					status_text.innerHTML = "Bild nicht gespeichert";
					status_img.src = "images/error.png";
				}
				bar.fade(opacity_norm);
			},
			onFailure: function()
			{
				status_text.innerHTML = "Bild nicht gespeichert";
				status_img.src = "images/error.png";
				bar.fade(opacity_norm);
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function show_img(src)
{
	var bar = $("barbestof");
	var img = $("image_bestof");

	img.src = src.replace(/thumb\//g, "");
	img.title = src;

	bar.empty();
	if(admin_mode)
	{
		bar.set("opacity", opacity_info);
		var e = new Element("img", {"src": "images/delete.png"}).inject(bar);
		e.addEvent("click", function(e)
		{
			e.stop();
			delete_img(src.substring(src.lastIndexOf("/") + 1));
		});
	}
	else
	{
		bar.set("opacity", opacity_hide);
	}
}

/* ------------------------------------------------------------------ */

function update_bestof(id)
{
	//bestof_slide.hide();
	bestof_current = id;

	var url_parameter = "action=get_bestof&id=" + id;
	var url = "command.php";

	new Request.HTML
	(
		{
			url: url,
			data: url_parameter,
			onSuccess: function(html)
			{
				$('box2').set('text', '');
				$('box2').adopt(html);

				var images = $$("#box2 img");
				images.each
				(
					function(image, i)
					{
						image.addEvent("click", function(e) {
							e.stop();
							show_img(image.alt);
							get_comment(image.alt);
						});
					}
				);

				//bestof_slide.slideIn();
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function update_light(id, state)
{
	var status_img = $("light" + id);

	switch(state)
	{
		case '-1':
		case '0':
			status_img.src = "images/light_off.png";
			status_img.title = "Licht ist aus";
			break;
		case '1':
			status_img.src = "images/light_on.png";
			status_img.title = "Licht ist an";
			break;
	}
}

/* ------------------------------------------------------------------ */

function update_ptz(id, state)
{
	if(last_ptz_pos[id - 1] != state)
	{
		var status_img = $("status_img");
		var status_text = $("status_text");

		status_text.innerHTML = "Position umgeschaltet";
		status_img.src = "images/info.png";

		last_ptz_pos[id - 1] = state;
		for(var i = 1; i <= 3; i++)
		{
			var ptz = $("ptz" + id + "_" + i);
			if(i == state)
				ptz.addClass("active");
			else
				ptz.removeClass("active");
			ptz.removeClass("waiting");
		}
	}
}

/* ------------------------------------------------------------------ */

function get_comment(id)
{
	comment_slide.hide();

	id = id.replace(/.*\//g, "");
	$('com_id').value = id;

	var url_parameter = "action=get_comment&id=" + id;
	var url = "command.php";

	new Request.HTML
	(
		{
			url: url,
			data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
			onSuccess: function(html)
			{
				$('bestof_comments').set('text', '');
				$('bestof_comments').adopt(html);
				var images = $$("#bestof_comments img");
				images.each
				(
					function(image, i)
					{
						if(image.id > 0)
						{
							image.addEvent("click", function(e) {
								e.stop();
								delete_comment(image.id);
							});
						}
					}
				);

				comment_slide.slideIn();
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function delete_comment(id)
{
	var url_parameter = "action=delete_comment&id=" + id;
	var url = "command.php";

	new Request
	(
		{
			url: url,
			data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
			onSuccess: function(str)
			{
				if(str != "0")
					get_comment(str);
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function get_bestof_links(id)
{
	var url_parameter = "action=get_bestof_links";
	var url = "command.php";

	new Request
	(
		{
			url: url,
			data: url_parameter + "&user=" + $('user').value + "&pass=" + $('pass').value,
			onSuccess: function(str)
			{
				$('handles_bestof').innerHTML = str;

				var spans = $$("#handles_bestof span");
				spans.each
				(
					function(span, i)
					{
						span.addEvent("click", function(e) {
							e.stop();
							$$(spans).removeClass("active");
							$$(span).addClass("active");
							update_bestof(i);
						});
						if(i == id)
						{
							$$(span).addClass("active");
							update_bestof(id);
						}
					}
				);
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */

function update_stats()
{
	var url_parameter = "action=get_stats";
	var url = "command.php";

	new Request.HTML
	(
		{
			url: url,
			data: url_parameter,
			onSuccess: function(html)
			{
				$('stats').set('text', '');
				$('stats').adopt(html);

				setTimeout("update_stats()", stats_time);
			},
			method:'get'
		}
	).send();
}

/* ------------------------------------------------------------------ */
