window.onload = function() {
	var galleries = $$("table.gallery", "div.gallery");
	if (galleries.length) {
		var imagesetNum = 0;
		galleries.each(function(gallery) {
			var images = $A(gallery.getElementsByTagName("img"));
			images.each(function(img) {
				if (!Element.hasClassName($(img), "no-link")) {
					var lightboxLink = document.createElement("a");
					lightboxLink.setAttribute("rel", "lightbox[set" + imagesetNum + "]");

					if ($(img).getAttribute("title"))
						lightboxLink.setAttribute("title", $(img).getAttribute("title"));
					else if ($(img).getAttribute("alt"))
						lightboxLink.setAttribute("title", $(img).getAttribute("alt"));
					else 
						lightboxLink.setAttribute("title", " ");

					lightboxLink.href = $(img).src.replace(/(_t|_n)\./, '.');
					$(img).parentNode.insertBefore(lightboxLink, $(img));
					lightboxLink.appendChild($(img));
				}	
			});
			imagesetNum++;
		});
	}


	if (defined($("insert-user"))) {
		Event.observe
		(
			$("username"), 
			"keyup", 
			function() {
				$("username").value = $("username").value.toLowerCase().replace(/[^a-z0-9._\-]+/, '');
								
				if (!$F("username").strip()) {
					$("username_exists").innerHTML = "";	
					return;
				}

				var url = "ajax.php";
				var params = "ajaxAction=usernameExists&username=" + $F("username").strip();
				var req = new Ajax.Request(
					url,
					{
						method: 'post',
						parameters: params,
						onComplete: function(req) {
							if (parseInt(req.responseText)) {
								with ($("username_exists")) {
									className = "check error";
									innerHTML = "Το όνομα χρήστη χρησιμοποιείται.";
								}
								$("submit").disabled = "disabled";
								$("submit").style.backgroundColor = "#ccc";
							} else {
								with ($("username_exists")) {
									className = "check correct";
									innerHTML = "Το όνομα χρήστη δε χρησιμοποιείται.";
								}	
								$("submit").disabled = "";
								$("submit").style.backgroundColor = submitBackgroundColor;
							}
						},
						onFailure: function() {
							alert("An error occured. Please, try again later.");
						}
					}
				);

			},
			false
		);

		Event.observe
		(
			$("pass1"),
			"keyup",
			function() { 
				$("pass1").value = $("pass1").value.toLowerCase().replace(/[^A-Za-z0-9\-!@$.]+/, '');
				if (!$F("pass1")) {
					$("check_passwd").innerHTML = "";
					return;
				}

				if ($F("pass1").length < 4) {
					$("check_passwd").innerHTML = "Απαιτούνται τουλάχιστον 4 χαρακτήρες";
					$("check_passwd").className = "check error";
				} else {
					$("check_passwd").innerHTML = "";
				}
			},
			false
		);
		
		Event.observe
		(
			$("pass2"),
			"keyup",
			function() { 
				$("pass2").value = $("pass2").value.toLowerCase().replace(/[^A-Za-z0-9\-!@$.]+/, '');

				var pass1 = $F("pass1");
				var pass2 = $F("pass2");
				if (!pass2) {
					$("check_passwd2").innerHTML = "";
					return;
				}	

				if (pass2.length < 4) {
					$("check_passwd2").innerHTML = "Απαιτούνται τουλάχιστον 4 χαρακτήρες";
					$("check_passwd2").className = "check error";
				} else if (pass1 != pass2) {
					$("check_passwd2").innerHTML = "Οι κωδικοί δεν ταιριάζουν";	
					$("check_passwd2").className = "check error";
				} else if (pass1.length == pass2.length && pass1 == pass2) {
					$("check_passwd2").innerHTML = "Οι κωδικοί ταιριάζουν";
					$("check_passwd2").className = "check correct";
				} else {
					$("check_passwd2").innerHTML = "";
				}
			},
			false
		);

		
		Event.observe
		(
			$("email"), 
			"keyup", 
			function() {
				$("email").value = $("email").value.toLowerCase().replace(/[^a-z0-9@\._\-]+/, '');

				if (!$F("email") || (defined($("current_email")) && $F("email") == $F("current_email"))) {
					$("email_exists").innerHTML = "";	
					return;
				}

				var url = "ajax.php";
				var params = "ajaxAction=emailExists&email=" + $F("email");
				if (defined($("current_user_id")))
					params += "&user_id=" + $F("current_user_id");	

				var req = new Ajax.Request(
					url,
					{
						method: 'post',
						parameters: params,
						onComplete: function(req) {
							if (parseInt(req.responseText)) {
								with ($("email_exists")) {
									className = "check error";
									innerHTML = "Το email χρησιμοποιείται.";
								}
								$("submit").disabled = "disabled";
								$("submit").style.backgroundColor = "#ccc";
							} else {
								with ($("email_exists")) {
									className = "check correct";
									innerHTML = "Το email δε χρησιμοποιείται.";
								}	
								$("submit").disabled = "";
								$("submit").style.backgroundColor = submitBackgroundColor;
							}
						},
						onFailure: function() {
							alert("An error occured. Please, try again later.");
						}
					}
				);

			},
			false
		);


		Event.observe
		(
			$("mobile"), 
			"keyup", 
			function() {
				$("mobile").value = $("mobile").value.toLowerCase().replace(/[^0-9\+]+/, '');
								
				if (!$F("mobile") || ($F("mobile") == $F("current_mobile"))) {
					$("mobile_exists").innerHTML = "";	
					return;
				}

				var url = "ajax.php";
				var params = "ajaxAction=mobileExists&mobile=" + $F("mobile");
				if ($F("current_user_id"))
					params += "&user_id=" + $F("current_user_id");

				var req = new Ajax.Request(
					url,
					{
						method: 'post',
						parameters: params,
						onComplete: function(req) {
							if (parseInt(req.responseText)) {
								with ($("mobile_exists")) {
									className = "check error";
									innerHTML = "Το κινητό χρησιμοποιείται.";
								}
								$("submit").disabled = "disabled";
								$("submit").style.backgroundColor = "#ccc";
							} else {
								with ($("mobile_exists")) {
									className = "check correct";
									innerHTML = "Το κινητό δε χρησιμοποιείται.";
								}	
								$("submit").disabled = "";
								$("submit").style.backgroundColor = submitBackgroundColor;
							}
						},
						onFailure: function() {
							alert("An error occured. Please, try again later.");
						}
					}
				);

			},
			false
		);
	}

};

var defined = function(A){if(typeof A=="string"){A=$(A)}return !((A==undefined&&typeof A=="undefined")||A===null)};

var popup = function(url, opts) {
	var title 	= opts['title']        	|| '';
	var width  	= opts['width']        	|| screen.width;
	var height 	= opts['height']       	|| screen.height;
	var top 	= opts['top']		|| 0;
	var left  	= (opts['left'] && !opts['centered']) ? opts['left'] : 0;
	var status 	= opts['status']       	|| 'no';
	var toolbar 	= opts['toolbar']      	|| 'no';
	var menubar 	= opts['menubar']      	|| 'yes';
	var scrollbars 	= opts['scrollbars']   	|| 'no';
	var centered 	= opts['centered']      ? opts['centered'] : 1;
	var resizable 	= opts['resizable']    	|| 'yes';
	var location 	= opts['location']    	|| 'yes';

	if (centered) {
		_left = (screen.width  - width)  / 2;
		_top  = (screen.height - height) / 2;
	} else {
		_left = _top = 0;
	}


	window.open(
		url, 
		title,
		'width=' + width + ', height=' + height + ', status=' + status +', toolbar='+toolbar+', menubar='+menubar+', scrollbars='+scrollbars+',location=' + location + ',left='+_left+',resizable=' + resizable + ',top='+_top
	);
};


getVar = function(variable) {
	var location = document.location.toString();
	if ((qmPos = location.indexOf("?")) != -1) {
		location = location.substring(qmPos+1);
		if (location.indexOf("&") != -1) {
			// found many vars
			location = location.split("&");
			for (var i = 0; i < location.length; i++) {
				if ((eqPos = location[i].indexOf("=")) != -1) {
					if (location[i].substring(0, eqPos) == variable)
						return location[i].substring(eqPos + 1);
				}
			}	
			return;
		} else {
			// url has only one var
			if ((eqPos = location.indexOf("=")) == -1)
				return; // not found = in url
			return location.substring(eqPos + 1);
		}
	}
};

