jQuery.noConflict();
jQuery(document).ready(function($) {
	$("#main-gallery").scrollable({autopause: true}).circular().mousewheel().autoscroll({
		steps: 2,
		interval: 3000
	}).find("img").each(function() {
		var t = $(this);
		t.click(function() {
			var src = t.attr("src").replace(/_(t|n)\./, '.');
			$('#lightboxImage').attr("src", src);
			popup(src, { width: 460, height: 350, centered: 1, resizable: 0 });
		});

	});


	// galleries
	var setNum = 0;
	$("table.gallery, div.gallery").each(function() {
		$(this).find("img[class!=no-link]").each(function() { 
			var $e = $(this);
			var url = $e.attr("src").replace(/(_t|_n)\./, '.');
			var alt = $e.attr("alt");
			var link = $('<a class="_prettyPhoto" href="' + url + '" rel="prettyPhoto[set' + setNum + ']" title="' + alt + '"></a>');
			$e.wrap(link);
		});
		setNum++;
	});

	$("img.gallery").each(function() {
		var $e = $(this);
		var url = $e.attr("src").replace(/(_t|_n)\./, '.');
		var alt = $e.attr("alt");
		var rel = $e.attr("rel");
		var link = $('<a class="_prettyPhoto" href="' + url + '" rel="prettyPhoto' + (rel ? '[' + rel + ']' : "") + '" title="' + alt + '"></a>');
		$e.wrap(link);
	});
	
	$("._prettyPhoto").prettyPhoto({theme: 'dark_rounded'});

});

window.onload = function() {
	//new Lightbox(); // put it here instead of lightbox.js to prevent IE6 page not found error

	/*
	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
		);
	}

	/*
	if (defined($("latest-news-list"))) {
		var _els = $$(".ln");
		setInterval(function() { gScrollable(_els); }, 7000);
	}
	*/
};

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);
		}
	}
};

function gScrollable(_els) {
        if (!_els.length) 
                return;
        for (var i = 0; i < _els.length; i++) { 
                if (_els[i].style.display != 'none') {
                        _els[i].style.display = 'none';
                        
                        // active;
                        if (i == _els.length-1) { 
                                // last. goto 1st
                                $(_els[0]).fade({ duration: 4.0, from: 0, to: 1 });
                                _els[0].style.display = 'block';
                        } else {
                                $(_els[i+1]).fade({ duration: 4.0, from: 0, to: 1 });
                                _els[i+1].style.display = 'block';
                        }
                        break;
                }
        }       
} 

