$(document).ready(function() {
	// galleries
        var setNum = 0;
        $("p.gallery, 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 link = $('<a class="_prettyPhoto" href="' + url + '" rel="prettyPhoto" title="' + alt + '"></a>');
                $e.wrap(link);
        });

	/* footer scrollable */
	$('.items div img[src*="_t.jpg"]').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[footer]" title="' + alt + '"></a>');
                $e.wrap(link);
        });

        $("._prettyPhoto").prettyPhoto({theme: 'dark_rounded', show_title: false, social_tools: false });

	$("#chained").scrollable({circular: true, mousewheel: true}).navigator().autoscroll({
		interval: 5000
	});
	
	$("#sendmail").click(function() {
                var fullname    = $('#fullname').val().trim();
                var email       = $('#email').val().trim();
                var comments 	= $('#comments').val().trim();
                var token       = $('#token').val();
		var language 	= "el";

		var intl = {
			"el": {
				"empty_fullname": "Παρακαλώ συμπληρώστε το ονοματεπώνυμό σας",
				"empty_email": "Παρακαλώ συμπληρώστε το email σας",
				"empty_comments": "Παρακαλώ συμπληρώστε τα σχόλια",
				"wrong_token": "Κάντε ανανέωση της σελίδας και προσπαθήστε ξανά",
				"invalid_email": "Η μορφή του email δεν είναι σωστή. Παρακαλώ διορθώστε τον και προσπαθήστε ξανά", 
				"mx_not_found": "Δε βρέθηκε mail server για το domain του email σας",
				"mail_error": "Συνέβη ένα σφάλμα. Παρακαλώ προσπαθήστε αργότερα",
				"thank_you": "Ευχαριστούμε για το μήνυμά σας"
			}
		};


		var error = "";
		if (!fullname)
			error = intl[language]['empty_fullname'];
		else if (!email)
			error = intl[language]['empty_email'];
		else if (!comments)
			error = intl[language]['empty_comments'];

		if (error) {
			alert(error);
			return false;
		}

                $.ajax({
                        type: "POST",
                        url: "/ajax.php",
                        data: "action=contact&fullname="+fullname+"&email="+email+"&comments="+comments+"&token="+token,
                        success: function(res){
				/**
				 * errors: empty_fullname, empty_comments, empty_comments, wrong_token, invalid_email, mx_not_found, mail_error
				 * success: mail_sent
				 */
				if (res == "mail_sent") {
					alert(intl[language]['thank_you']);
					document.location = "/";
					return true;
				}

                                switch(res) {
					case "wrong_token":
					case "invalid_email":
					case "mx_not_found":
						error = intl[language][res];
						break;
					case "mail_error":
					default:
						error = error = intl[language]['mail_error'];
						break;
                                }

				alert(error);
				return false;
			}
                });
        });
});


popup = function(url, opts) {
	var title 	= opts['title']        || '';
	var width  	= opts['width']        || screen.width;
	var height 	= opts['height']       || screen.height;
	var status 	= opts['status']       || 'no';
	var toolbar 	= opts['toolbar']      || 'no';
	var menubar 	= opts['menubar']      || 'no';
	var scrollbars 	= opts['scrollbars']   || 'no';
	var centered 	= opts['centered'] ? 1 : 0;
	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+',left='+_left+',top='+_top
	);
};



