/* js here */
var mp3, audio;
function fixHeights() {
	 var myTable = $("gallery");
	 var rows = $T("tr", myTable);

	rows.forEachBind( function() {
			var _items = $CN( "item-content", this );
			var _max = 0;
			_items.forEachBind( function() { 
				_max = this.offsetHeight > _max ? this.offsetHeight : _max;
				}
			);
			_items.forEachBind( function() {
				this.style.height = _max + "px";
				}
			);
		}
	);

	/* add some event on mouseover titles for ie */
	if($('gallery'))	{

		Event.add( $CN( "editinline", $('gallery') ), "mouseover", function() {
			this.style.background = "#FFFFD3";
		}
		);
		Event.add( $CN( "editinline", $('gallery') ), "mouseout", function() {
			this.style.background = "#FFF";
		}
		);
	}
}
Event.add( window, "DOMContentLoaded", function() {
	if (window.mp3 && mp3.length) {
		audio = new Sound(mp3, {
		'autoPlay'	: false,
		'autoLoad'	: true
		});
	}
	
	fixHeights;
});

function init() {
	if ($('rater')) {
		var myRater = new Rater ( "rater", {
				loginUrl	: loginUrl,
				className	: "rater-my",
				
				section		: service,
				username	: parseInt(login),
				id		: parseInt(objectId),
				rate		: parseInt(myScore),
				expressions 	: [ "Η άποψη σας", "Κακή", "Μέτρια", "Καλή", "Πολύ καλή", "Εξαιρετική" ]
			}
		);
	}

	if (typeof tot != "undefined") {
		for (var i=0; i<tot; i++) {
			new Rater ( "rater-"+i, {
				loginUrl	: loginUrl,
				className	: "rater-my",
				section		: service,
				username	: parseInt(login),
				id		: parseInt(objects[i]),
				rate		: parseInt(scores[i]),
				expressions 	: [ "Η άποψη σας", "Κακή", "Μέτρια", "Καλή", "Πολύ καλή", "Εξαιρετική" ]
			}
			);
		}
	}

	if (typeof notes != "undefined") {
		for (i=0;i<notes.length;i++) {
			new Note('image', {
				content: notes[i].content,
				x 	: notes[i].x,
				y 	: notes[i].y,
				width 	: notes[i].width,
				height 	: notes[i].height,
				id 	: notes[i].id,
				readOnly: notes[i].readOnly
			});
		}
	}
}

function CheckForm(form, gb) {
	if (!form.title.value.trim().isValidEmail() ) {
                alert("Παρακαλούμε δώστε ορθό e-mail");
                form.title.focus();
                return false;
	}
	/* Perform length checkup */
	if (form.body.value.length > 2200) {
		alert ("Το σχόλιο σας δεν μπορεί να υπερβαίνει τους 2.200 χαρακτήρες!");
                form.body.focus();
		return false;
	}			

	if (!form.terms.checked) {
                alert("Πρέπει να αποδεχθείτε τους όρους χρήσης");
                form.terms.focus();
                return false;
	}
	
	/* Perform is_in_greek checkup */
	if( !gb ) {
		greek_words = form.body.value.match(/[αάΑ’βΒγΓδΔεέΕΈζΖηήΗΉθΘιίϊΙΊκΚλΛμΜνΝξΞοόΟΌπΠρΡσςΣτΤυύΎΥψφΨχΧωώΩΏ]+/g);
		if (greek_words != null)
		{
			if (greek_words.length >= 5) 
			{
				alert ("Ευχαριστούμε για τη συμμετοχή σας!");
				form.submit();
				return true;
			}
		}
		alert ("Για να προχωρήσετε πρέπει το μήνυμά σας να είναι γραμμένο με Ελληνικούς χαρακτήρες και με περισσότερες από 5 ορθά γραμμένες λέξεις.");
		return false;
	} else {
		if (form.body.value.length < 5) {
			alert ("Το σχόλιο σας είναι πολύ μικρό.");
			form.body.focus();
			return false;
		}			

	}


	return true;

}

function SendEmail(form) {
	from 	= form.from.value.trim().addSlashes();
	name 	= form.name.value.trim().addSlashes();
	subject = form.subject.value.trim().addSlashes();
	comment = form.comment.value.trim().addSlashes();
	to 	= form.to.value;

	if (!name) {
		alert("Παρακαλούμε δώστε το όνομά σας.");
		form.name.focus();
		return false;
	}
	if ( !from || !from.isValidEmail()) {
		alert("Παρκαλούμε δώστε ένα έγκυρο email.");
		form.from.focus();
		return false;			
	}
	if (!subject) {
		alert("Παρακαλούμε δώστε θέμα μηνύματος.");
		form.subject.focus();
		return false;
	}
	if (!comment) {
		alert("Παρακαλούμε δώστε κείμενο μηνύματος.");
		form.comment.focus();
		return false;
	}

	if (to)
		alert("email sending to "+ to);
	else {
		alert("No email to defined");
		return false;
	}


	body = comment;

	var method = "aux.SendMail";
	var args = "'\""+ name +"\" <"+ from +">', '"+ to +"', '"+ subject +"', '"+ body +"', 'text/plain; charset=\"iso-8859-7\"', 'aygenakis/email'";

	args = args.split(",");
	Ajax.rpc (method, args, {
		onSuccess: function(req) {
			res = req.responseJSON;
			if( res && res.faultCode ) {
				alert( res.faultString );
			}
			else
			{
				alert('Το email στάλθηκε.');
			}
		}
	});

	$('main-right').style.display = "none";
	return false;
}

function CheckContest(form)	{
	if(!form.name.value)	{
		alert("Παρακαλούμε δώστε το όνομά σας.");
		form.name.focus();
		return false;
	}
	if(!form.surname.value)	{
		alert("Παρακαλούμε δώστε το επώνυμό σας.");
		form.surname.focus();
		return false;
	}
	if(!form.email.value.trim().isValidEmail())	{
		alert("Παρακαλούμε δώστε ένα έγκυρο e-mail για να επικοινωνήσουμε μαζί σας.");
		form.email.focus();
		return false;
	}
	if(!form.terms.checked)	{
		alert("Παρακαλούμε συμφωνήστε με τους όρους συμμετοχής στον διαγωνισμό.");
		form.terms.focus();
		return false;	
	}
	if((2007 - form.birthYear.value) < 18)	{
		alert("Δεν μπορούν να συμμετάσχουν στο διαγωνισμό ανήλικα άτομα.");
		form.birthYear.focus();
		return false;
	}
	
	return true;
}

function handleFocus(el)	{
	if(!el._original && Node.hasClass( el	, "field-pale"))
		el._original = el.value;
 	else {
 		el._original = "ως";

		if( el.name == "email" )
			el._original = "γράψτε το email σας";
 	}
	
 	if( el.value == el._original ) {
 		el.value = "";
		Node.removeClass( el, "field-pale");
 	}

}

function handleBlur(el) {
	el._isOkay = true;
	if( !el.value ) {
 		el.value = el._original;
		Node.addClass( el, "field-pale");
		el._isOkay = false;
 	}
}
function signNewsletter( f ) {
	var s = "xmlrpc_struct( array())", title, section = 1378, author = 72, j=0, hashkey;

	element = f.email;
	if( element.value.length == 0 || !element.value.isValidEmail() ) {
		alert( "Πρέπει να δώσετε ένα έγκυρο e-mail για να συνεχίσετε" );
		element.focus();
		return false;
	}
	title = hashkey = element.value;
		
	data = s;
	if (hashkey) {
		/* check if exists */
		$('form-news-status').innerHTML = "Έλεγχος email...";
		Ajax.rpc( "cman.IsInserted", parseInt(section), hashkey, 0, false, {
			onSuccess: function(req) {
				res = req.responseJSON;
				if( res && res.faultCode ) {
					alert( res.faultString );
				}
				else
				{
					if (res) {
						alert('Υπάρχει ήδη αυτό το email για Newsletter.');
						$('form-news-status').innerHTML = "";
						return false;
					}
					else
					{
						// Ajax it! 
						$('form-news-status').innerHTML = "Αποθήκευση...";

						args = [section, 1, title, "", hashkey, data, 0, author, false];
						//alert(data);
						Ajax.rpc( "cman.AddItem", parseInt(section), 1, title, "", hashkey, data, 0, author, {
							onSuccess: function(req) {
								res = req.responseJSON;
								if( res && res.faultCode ) {
									alert( res.faultString );
								}
								else
								{
									alert( "Η εγγραφή πραγματοποιήθηκε επιτυχώς.")
									Node.hide($('newsletterForm'));
									$('form-news-status').innerHTML = "Σας ευχαριστούμε για την εγγραφή σας στο newsletter";
									return false;
								}
							}
						});

					}
				}
			}
		});
	}

	return false;
}

function popImg( img, width, height ) {
	if ((!window.popup)  || (window.popup && window.popup.closed) )
		window.popup = window.open( "", "popup", "width=" + width  + ", height="+(height + 30));
	else {
		window.popup.resizeTo( width  , (height + 80) );
		window.popup.focus();
	}

		
	doc = window.popup.document;
	var code = "<html>";
	code += "<head><title>" + img.title + "</title>";
	code += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-7\" />\n";
	code += "<style type=\"text/css\">";
	code += "body {margin:0; font: normal 13px arial, serif; text-align: center; background: #eee;}\nimg { margin-bottom: 5px;}\n";
	code += "</style>\n";
	code += "</head><body>\n";
	code += "<img height=\""+height+"\" width=\""+width+"\" border=\"0\" alt=\""+img.title+"\" title=\""+img.title+"\" src=\""+img.href+"\" /><br /><a href=\"#\" onclick=\"self.close();\">[x] κλείσιμο παραθύρου</a>\n";
	code += "</body></html>";
	
	doc.write( code);
	doc.close();
	return false;
}

function toggleAudio()	{
	if (audio.toggle())	{
		$('audio').setHTML("Αναπαραγωγή ήχου &raquo;");
		$('audio').removeClass("playing");
	}
	else 	{
		$('audio').setHTML("...Παύση (pause)...");
		$('audio').setClass("playing");
	}	
	
}
