var patt = new RegExp("[^#]+$");

function submitForm()
{
	var fullname = $("#fullname").val();
	if(fullname.length == 0) {
		alert($("#fullname").attr("title"));
		$("#fullname").focus();
		return false;
	}		

	var telephone = $("#telephone").val();
	
	var email = $("#email").val();
	if((email.length == 0) || (!email.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/))) {
		alert($("#email").attr("title"));
		$("#email").focus();
		return false;
	}
	
	var subject = $("#subject").val();
	if($("#subject").val().length == 0) {
		alert($("#subject").attr("title"));
		$("#subject").focus();
		return false;
	}

	var message = $("#message").val();
	if($("#message").val().length == 0) {
		alert($("#message").attr("title"));
		$("#message").focus();
		return false;
	}

	var dataString = 'fullname=' + fullname + 
					 '&telephone=' + telephone + 
					 '&email=' + email + 
					 '&subject=' + subject +
					 '&message=' + message;

	$.ajax({  
		type: "POST",  
		url: "scripts/contact.php",  
		data: dataString,  
		success: function(html) {  
			$('#content-text-inner').fadeOut("slow", function(){
				$(this).html(html).fadeIn("slow");
			})
		},
		error: function(){
			$('#content-text-inner').html("Error occured during Ajax request...");
		}  
	});  			
}

function ajaxMenuCall(file, href)
{
	$.ajax({
		url: file,
		cache: false,
		success: function(html){
			$("#content-title").html(html);
			
			$("ul.sub-menu a").click(function() {
				
				var target1 = "#"+patt.exec($(this).attr("href"));
				
				$("ul.sub-menu a").removeClass("selected");
				$(this).addClass("selected");
				var file = target1.substring(1);
				ajaxFileCall('content/' + file + '.html');								
			})	
			
			$("ul.sub-menu a").filter(function (index) {
										
				var target2 = "#"+patt.exec($(this).attr("href"));
                if (href == target2) {
                	$(this).addClass("selected");
                }
            })

		}
	});
}

function loadFile(href) 
{
	var file = href.substring(1);
	var i = file.indexOf("_");
	var group = file.substring(0, i);
	
	$("#title div").hide()
	$(".title-"+group).show();		
	
	ajaxMenuCall('content/' + group + '_menu.html', href);
	
	$("#foreground").fadeIn("slow", function(){
		ajaxFileCall('content/' + file + '.html');
	});				
}

function ajaxFileCall(file)
{
	$.ajax({
		url: file,
		cache: false,
		success: function(html){
			$("#content-text-inner").fadeOut("slow", function(){
				$(this).html(html);
				$(this).jScrollPane({	    
				    showArrows: false,
				    scrollbarWidth: 7,							    
				    dragMinHeight: 42,
				    dragMaxHeight: 7
				});	
				$(this).fadeIn("slow");	
				
				$('.lightbox').lightBox({
					fixedNavigation: true,
					imageLoading: '../images/lightbox/loading.gif',
					imageBtnPrev: '../images/lightbox/previous.gif',
					imageBtnNext: '../images/lightbox/next.gif',
					imageBtnClose: '../images/lightbox/close.gif',
					imageBlank: '../images/lightbox/blank.gif',
					txtImage: 'Εικόνα',
					txtOf: 'από'
				});
				
				$("#submit").click(function(){
					submitForm();
					return false;
				});
				
				$("a.content-menu").click(function() {
					var target3 = "#"+patt.exec($(this).attr("href"));
					loadFile(target3);
				})
			})						
		}
	});
}

$(function()
{			
	var target = "#"+patt.exec(location.hash);		
	
	if (target != '#null') {
		loadFile(target);
	}

	$("a.menu-item").mouseover(function(){
		$(this).css("background-position","0 -159px");
	}).mouseout(function(){
		$(this).css("background-position","0 0");
	});

	$("a.menu-item").click(function() {
		loadFile($(this).attr("href"));
	})			

	$("a.button-close").click(function() {		
		$("#foreground").fadeOut("slow", function(){
			location.hash = "";
			$("#content-title").empty();
			$("#content-text-inner").empty();
			$("#content-text-inner").hide();
			$("#content-images-inner").empty();
			$("#content-images-inner").hide();
		});
	})		
});