$(document).ready(function () {
	$(document).pngFix();
	//$('#main-content').jScrollPane({showArrows:true});

	$('ul.gallery').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			//image.attr('title','Επόμενη φωτογραφία >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
	

	// galleries h3
	$('#photos-thumbs h3').click(function() {
		$('.gallery').each(function() { 
			$(this).hide();
		});
		$(this).next("ul").show("slow");
	});

	$('.discography-years').live("click", function() {
		var e = $(this);
		var year = e.attr("id").substring(1);
		var data = { ajaxAction: "getAlbum", year: year };

		$.getJSON("/ajax.php", data, function(response) {
			$('#discography .activ').attr("src", "http://c1.hainides.gr/img/" + $('#discography .activ').attr("id").substring(1) + ".jpg").removeClass("activ");

			e.addClass("activ").attr("src", "http://c1.hainides.gr/img/" + year + "-active.jpg");
			$('#discography-thumb').hide().attr("src", "http://c2.hainides.gr/files/cats/" + response.cat + "/cd.jpg").show("slow").attr("class", "cat"+response.cat);
			$('#discography-title').hide().html(response.data).show("slow");
		});
	});

	$('#discography-thumb').livequery("click", function() {
		var e = $(this);
		var catId = e.attr("class").substring(3);
		var data = { ajaxAction: "getCatSymbol", catId: catId };
		$.post("/ajax.php", data, function(response) {
			document.location = "/discography/" + response + "?ref=ft";
		});	
	});

	$(".pretty-gallery a").each(function() {
		var e = $(this);
		e.attr("rel", "prettyPhoto").prettyPhoto({theme: 'dark_rounded'});
		e.parent().append("<span>" + e.attr("title") + "</span>");
	});

	// contact form
	$('#contact-form').jqm();
	$("#c_fullname").alphanumeric({ allow:".- " });
	$("#c_phone").numeric();
	$("#c_email").alphanumeric({ allow:".-_@" });
	
	$("#send").click(function() {
		var fullname 	= $('#c_fullname').val();
		var phone 	= $('#c_phone').val();
		var email 	= $('#c_email').val();
		var message 	= $('#c_message').val();
		var t 		= $('#t').val();

		$.ajax({
			type: "POST",
			url: "ajax.php",
			data: "ajaxAction=sendmail&fullname="+fullname+"&phone="+phone+"&email="+email+"&message="+message+"&t="+t,
			success: function(res){
				res = parseInt(res);
				var error = "";
				if (res == 0 || isNaN(res))
					error = "Υπήρξε πρόβλημα στην αποστολή των στοιχείων. Παρακαλώ προσπαθήστε ξανά";
				else if (res == -1)
					error = "Ο αριθμός τηλεφώνου είναι λάθος";
				else if (res == -2)
					error = "Το email είναι λάθος";
				else if (res == -3)
					error = "Δεν έχετε συμπληρώσει το ονοματεπώνυμο";
				else if (res == -4)
					error = "Δεν έχετε συμπληρώσει το τηλέφωνο";
				else if (res == -5)
					error = "Δεν έχετε συμπληρώσει το email";
				else if (res == -6)
					error = "Δεν έχετε συμπληρώσει το μήνυμα";
				else if (res == -7)
					error = "Υπήρξε πρόβλημα στην αποστολή των στοιχείων. Πατήστε ανανέωση σελίδας και προσπαθήστε ξανά.";

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

				alert("Ευχαριστούμε πολύ. Θα επικοινωνήσουμε σύντομα μαζί σας.");
				$("#close").click();
			}
		});
	});

});
