var page = 1;
var width = 735;
var limit = 21;
var step = 114;
var z = 7;
var speed = 600;
var active = null;
var catalogOpened = false;
var textOpened = false;

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

	var company = $("#company").val();
	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();
	var message = $("#message").val();
	if($("#message").val().length == 0) {
		alert($("#message").attr("title"));
		$("#message").focus();
		return false;
	}
	
	$('#text-form div').fadeOut("slow");
	
	var dataString = 'fullname=' + fullname + 
					 '&company=' + company +
					 '&telephone=' + telephone + 
					 '&email=' + email + 
					 '&subject=' + subject +
					 '&message=' + message;

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

function closeText()
{
	$('#content-text').fadeOut('slow', function(){
		$(this).empty();
	});
}
function closeCatalog()
{
	$('#content-text').fadeOut("slow");
	$('#content-catalog').slideUp('slow', function(){
		$(this).empty();
		restore(active.children("a").children("img"));
		$('#dock-menu a img').mouseover(function()
		{		
			magnify($(this));
		}).mouseout(function()
		{
			restore($(this));
		});		
		
		$('#dock-menu div').click(function(){			
			animateCatalog($(this));
		});
	});
	
	catalogOpened = false;
	page = 1;	
}

function loadFile(href)
{	
	$.ajax({
		url: 'content/' + href.substring(1) + '.html',
		cache: false,
		success: function(html){
			$('#content-catalog').html(html);
			$('#content-catalog').slideDown('slow');
			
			$('.previous').click(function(){				
				if (page > 1) {	
					page--;
					var temp = page-1;
					$('.page').removeClass("selected");
					$('.page').eq(temp).addClass("selected");
					$('.title-text h4').html($('.page').eq(temp).attr("title"));
					if (true === $('.page').eq(temp).hasClass('text_only')) {
						$('.title-caption h4').html("");
					} else {
						$('.title-caption h4').html("ΣΥΣΚ/ΣΙΑ");
					}
					$('.products').animate({left: '-' + (temp * width) + 'px'}, 'slow');
				}
				return false;
			});
			
			$('.next').click(function(){				
				if (page < limit) {			
					$('.page').removeClass("selected");
					$('.page').eq(page).addClass("selected");
					$('.title-text h4').html($('.page').eq(page).attr("title"));
					if (true === $('.page').eq(page).hasClass('text_only')) {
						$('.title-caption h4').html("");
					} else {
						$('.title-caption h4').html("ΣΥΣΚ/ΣΙΑ");
					}
					$('.products').animate({left: '-' + (page * width) + 'px'}, 'slow');
					page++;
				}
				return false;
			});
			
			$('.page').click(function(){
				$('.page').removeClass("selected");
				$(this).addClass("selected");
				$('.title-text h4').html($(this).attr("title"));
				if (true === $(this).hasClass('text_only')) {
					$('.title-caption h4').html("");
				} else {
					$('.title-caption h4').html("ΣΥΣΚ/ΣΙΑ");
				}
				var href = $(this).attr("href");
				page = href.substring(2);								
				$('.products').animate({left: '-' + ((page-1) * width) + 'px'}, 'slow');						
				$('.idx').html(page);
				
				return false;
			});
			
			$('.close').click(function(){
				closeCatalog();
				return false;
			});
			
			$('.home').click(function(){
				if (true == textOpened) closeText();
				if (true == catalogOpened) closeCatalog();
				return false;
			});
		}
	});
}

function loadText(href)
{	
	$.ajax({
		url: 'content/' + href.substring(1) + '.html',
		cache: false,
		success: function(html){
			$('#content-text').html(html);
			$('#content-text').fadeIn('slow');		
			
			$('.lightbox').lightBox({
				fixedNavigation: true,
				imageLoading: '../images/lightbox/loading.gif',
				imageBtnPrev: '../images/lightbox/previous.gif',
				imageBtnNext: '../images/lightbox/next.gif',
				imageBtnClose: '../images/close.png',
				imageBlank: '../images/lightbox/blank.gif',
				txtImage: 'Εικόνα',
				txtOf: 'από'
			});
			
			$('.closeText').click(function(){
				closeText();
				return false;
			});
			
			$('.home').click(function(){
				if (true == textOpened) closeText();
				if (true == catalogOpened) closeCatalog();
				return false;
			});
			
			$('.text').click(function(){
				$('#content-text').empty();
				if (true == catalogOpened) closeCatalog();
				textOpened = true;
				loadText($(this).attr('href'));
			});
			
			$("#submit").click(function(){
				
				submitForm();
				return false;
			});	
		}
	});
}

function magnify(el)
{
	var container = el.parent().parent();
	
	var i = container.attr('id').indexOf('-');
	var index = container.attr('id').substring(i+1);
	
	container.css('z-index', z+1);
	
	var top1 = '-32px';
	var left1 = (((index-1) * step) - 32) + 'px';
	
	el.stop().animate({width: '150px', height: '200px'}, speed);
	container.stop().animate({left: left1, top: top1}, speed);
}

function restore(el)
{
	var container = el.parent().parent();
	
	var i = container.attr('id').indexOf('-');
	var index = container.attr('id').substring(i+1);
	
	var top1 = '0px';
	var left1 = ((index-1) * step) + 'px';
	
	el.stop().animate({width: '107px', height: '143px'}, speed);
	container.stop().animate({left: left1, top: top1}, speed, function(){
		container.css('z-index', z-1);
	});
}

function animateCatalog(el) 
{
	$('#dock-menu a img').unbind('mouseout').unbind('mouseover');
	$('#dock-menu div').unbind('click');
	
	if (true == textOpened) closeText();
	limit = el.attr('class').substring(1);
	catalogOpened = true;
	active = el;	
	el.animate({left: '-4px'}, 'slow', function(){
		loadFile(el.children('a').attr('href'));
	});
}

jQuery(document).ready(function()
{			
	$('ul.navigation').lavaLamp({
        fx: 'backout',
        speed: 700,
        click: function(event, menuItem) {
            //return false;
        }
    });	
	
	$('#dock-menu div').each(function (i) {
		if (i > 0) {
			$(this).stop().animate({left: (step*i)+'px'}, 1000);	
		}		
	});	
	
	$('#dock-menu a img').mouseover(function()
	{		
		magnify($(this));
	}).mouseout(function()
	{
		restore($(this));
	});
	
	$('#dock-menu div').click(function()
	{
		animateCatalog($(this));
	});
	
	$('.text').click(function(){
		$('#content-text').empty();
		if (true == catalogOpened) closeCatalog();
		textOpened = true;
		loadText($(this).attr('href'));
	});
});
