function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function searchSite() {
	var searchtext = document.getElementById('searchtext').value;
	var lan = document.getElementById('lan').value;
	
	if ((searchtext != '') && (searchtext != 'SEARCH')) {
		window.location = 'index.php?search=' + searchtext + '&lan=' + lan;
	}
}

function sendEmail() {
	$("#mailmsg").html('<img src="img/ajax-loader-small.gif" />');
	
	var name = document.getElementById('name').value;
	var surname = document.getElementById('surname').value;
	var phone = document.getElementById('phone').value;
	var email = document.getElementById('email').value;
	var message = document.getElementById('comments').value;
	var lan = document.getElementById('lan').value;
	
	var messagesent, messagenotsent;
	
	switch (lan) {
		case 'en':
			nameerror = 'Please enter your name!';
			surnameerror = 'Please enter your surname!';
			phoneerror = 'Please enter your phone number!';
			emailerror = 'Please enter your email!';
			emailformaterror = 'Please enter a email in the correct format!';
			securitycodeerror = 'Please enter the security code!';
			wrongcodeerror = 'The security code was wrong! Please try again.';
			messagesent = 'Your message has been sent succesfully!';
			messagenotsent = 'There has been a problem sending your message.';
			break;
		case 'gr':
			nameerror = 'Παρακαλώ εισάγετε το όνομα σας!';
			surnameerror = 'Παρακαλώ εισάγετε το επώνυμο σας!';
			phoneerror = 'Παρακαλώ εισάγετε το τηλέφωνο σας!';
			emailerror = 'Παρακαλώ εισάγετε το email σας!';
			emailformaterror = 'Παρακαλώ εισάγετε ένα email σε σωστή μορφή!';
			securitycodeerror = 'Παρακαλώ εισάγετε τον κωδικό ασφαλείας!';
			wrongcodeerror = 'Ο κωδικός ασφαλείας ήταν λάθος! Παρακαλώ προσπαθήστε ξανά.';
			messagesent = 'Το μήνυμα σας εστάλη επιτυχώς!';
			messagenotsent = 'Υπήρξε πρόβλημα στην αποστολή του μηνύματος.';
			break;
	}
	
	if (name == '') {
		$("#mailmsg").html('<span class="msgtext">' + nameerror + '</span>');
		return false;
	} else if (surname == '') {
		$("#mailmsg").html('<span class="msgtext">' + surnameerror + '</span>');
		return false;
	} else if (phone == '') {
		$("#mailmsg").html('<span class="msgtext">' + phoneerror + '</span>');
		return false;
	} else if (email == '') {
		$("#mailmsg").html('<span class="msgtext">' + emailerror + '</span>');
		return false;
	}
	
	var apos = email.indexOf("@");
	var dotpos = email.lastIndexOf(".");
		
	if (apos<1||dotpos-apos<2) {
		$("#mailmsg").html('<span class="msgtext">' + emailformaterror + '</span>');
		return false;
	}

	$.post("ajax/sendmail.php",
		{ name: name, surname: surname, phone: phone, email: email, message: message },
			function(data){
				if (data == 'ok') {
					$("#mailmsg").html('<span style="font-size:12px; color: #565656;">' + messagesent + '</span>');
				} else if (data == 'wrongcode') {
					$("#mailmsg").html(wrongcodeerror);
				} else {
					$("#mailmsg").html(messagenotsent);
				}
			}
	);
}

function showPhotos(photonum, type) {
	var fadeinterval = 500;
	
	var bgphoto = 'rightphoto_' + photonum + '.jpg';

	$('#rightphoto').html('<img src="photos/' + bgphoto + '" />');
	$('#rightphoto').animate({ opacity: 'show' }, fadeinterval);
}

var showtime, phototime;

function loadPhotos(nextphotonum) {
	var photointerval = 5000, fadeinterval = 500;
	
	window.clearTimeout(phototime);

	if (nextphotonum < 6) {
		nextphotonum = parseInt(nextphotonum) + 1;
	} else {
		nextphotonum = 1;
	}

	$('#rightphoto').animate({ opacity: 'hide' }, fadeinterval);	
	var showtime = window.setTimeout(function(){showPhotos(nextphotonum)}, fadeinterval);
	
	phototime = window.setTimeout(function(){loadPhotos(nextphotonum)}, photointerval);
}

function showHomePhotos(photonum, pageid, type) {
	var fadeinterval = 500;
	var prefix;

	if (type == 'page') {
		switch (pageid) {
			case '1': prefix = 'homephoto_'; break;
			case '2': prefix = 'sxedia_'; break;
			default: prefix = 'homephoto_'; break;
		}
	} else if (type == 'cat') {
		switch (pageid) {
			case '2': prefix = 'tzakia_modern_'; break;	
			default: prefix = 'homephoto_'; break;
		}
	}
	
	var bgphoto = prefix + photonum + '.jpg';
	
	$('#homephoto').html('<img src="photos/' + bgphoto + '" />');
	$('#homephoto').animate({ opacity: 'show' }, fadeinterval);
}

var showtime, phototime;

function loadHomePhotos(nextphotonum, pageid, type) {
	var photointerval = 4000, fadeinterval = 500;
	var picsnum;
	
	window.clearTimeout(phototime);
	
	if (type == 'page') {
		switch (pageid) {
			case '1': picsnum = 6; break;
			case '2': picsnum = 5; break;
			default: picsnum = 6; break;
		}
	} else if (type == 'cat') {
		switch (pageid) {
			case '2': picsnum = 7; break;
			default: picsnum = 6; break;
		}
	}

	if (nextphotonum < picsnum) {
		nextphotonum = parseInt(nextphotonum) + 1;
	} else {
		nextphotonum = 1;
	}

	$('#homephoto').animate({ opacity: 'hide' }, fadeinterval);	
	var showtime = window.setTimeout(function(){showHomePhotos(nextphotonum, pageid, type)}, fadeinterval);
	
	phototime = window.setTimeout(function(){loadHomePhotos(nextphotonum, pageid, type)}, photointerval);
}

function showSearch() {
	var opensearch = document.getElementById('opensearch').value;
	
	if (opensearch == 0) {
		document.getElementById('opensearch').value = 1;
		
		$("#lefttextcontainer").animate({ opacity: 'hide' }, 'slow')
		
		$("#bottom1").animate({ 'height':'+=150' }, 'slow');
		$("#top1").animate({ 'height':'-=150' }, 'slow');

		$("#leftbottom").animate({ 'height':'+=150' }, 'slow');
		$("#lefttop").animate({ 'height':'-=150' }, 'slow');
		
		$("#searchform").animate({ opacity: 'show' }, 'slow');
		$("#leftbg").animate({ opacity: 'show' }, 'slow');
	} else {
		document.getElementById('opensearch').value = 0;
		
		$("#searchform").animate({ opacity: 'hide' }, 'slow');
		$("#leftbg").animate({ opacity: 'hide' }, 'slow');
		
		$("#bottom1").animate({ 'height':'-=150' }, 'slow');
		$("#top1").animate({ 'height':'+=150' }, 'slow');

		$("#leftbottom").animate({ 'height':'-=150' }, 'slow');
		$("#lefttop").animate({ 'height':'+=150' }, 'slow');
		
		$("#lefttextcontainer").animate({ opacity: 'show' }, 'slow')
	}
}

function showForm() {
	var openform = document.getElementById('openform').value;
	
	if (openform == 0) {
		document.getElementById('openform').value = 1;
		
		$("#top2content").animate({ opacity: 'hide' }, 'slow')
		
		$("#bottom2").animate({ 'height':'+=600' }, 'slow');
		$("#top2").animate({ 'height':'-=600' }, 'slow');

		$("#rightbottom").animate({ 'height':'+=600' }, 'slow');
		$("#righttop").animate({ 'height':'-=600' }, 'slow');
		
		$("#insertform").animate({ opacity: 'show' }, 'slow');
		$("#rightbg").animate({ opacity: 'show' }, 'slow');
	} else {
		document.getElementById('openform').value = 0;
		
		$("#insertform").animate({ opacity: 'hide' }, 'slow');
		$("#rightbg").animate({ opacity: 'hide' }, 'slow');
		
		$("#bottom2").animate({ 'height':'-=600' }, 'slow');
		$("#top2").animate({ 'height':'+=600' }, 'slow');

		$("#rightbottom").animate({ 'height':'-=600' }, 'slow');
		$("#righttop").animate({ 'height':'+=600' }, 'slow');
		
		$("#top2content").animate({ opacity: 'show' }, 'slow')
	}
}

function showStories(stories) {
	$("#story").animate({ opacity: 'hide' }, 'fast');
	
	$("#carouselnav").animate({ opacity: 'show' }, 'fast');
	$("#carousel").animate({ opacity: 'show' }, 'fast');
}

function showStory(story) {
	$("#topmenu").show();
	
	$("#carouselnav").animate({ opacity: 'hide' }, 'fast');
	$("#carousel").animate({ opacity: 'hide' }, 'fast');
	
	$("#story").animate({ opacity: 'show' }, 'fast');
}

function closeStory() {
	$("#story").animate({ opacity: 'hide' }, 'fast');
	
	$("#carouselnav").animate({ opacity: 'show' }, 'fast');
	$("#carousel").animate({ opacity: 'show' }, 'fast');
}

$(document).ready(function() {
	MM_preloadImages('img/searchbg.png', 'img/searchleftbg.png', 'img/formbg.png');

	var pageid = document.getElementById('pageid').value;
	var catid = document.getElementById('catid').value;

//	$('#nav').droppy();
	$("a[id^=iframe_]").fancybox({
		'width'				: 720,
		'height'			: 500,
		'autoScale'			: false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe',
		'showCloseButton'	: true
	});
	
	$("a[id^=image_]").fancybox({
		'width'				: 'auto',
		'height'			: 'auto',
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'showCloseButton'	: true
	});
	
	$('#columns').columnize({
		width 				: 250,
		lastNeverTallest 	: true,
		buildOnce			: true,
		doneFunc 			: function(){ $(".column").attr('style', 'width:250px; float:left;'); },
	});
	
	
	
//	function mycarousel_initCallback(carousel) {
////		$(".jcarousel-control a").bind("click", function() {
////			carousel.scroll($.jcarousel.intval((($(this).text() - 1) * 5) + 1));
////			return false;
////		});
////		
//		jQuery("#mycarousel-next").bind("click", function() {
//			carousel.next();
//			return false;
//		});
//	
//		jQuery("#mycarousel-prev").bind("click", function() {
//			carousel.prev();
//			return false;
//		});
//	};
//	
//	$("#mycarousel").jcarousel({
//		scroll: 1,
//		initCallback: mycarousel_initCallback,
//		buttonNextHTML: null,
//		buttonPrevHTML: null
//	});
//	
//	$('.scroll-pane').jScrollPane();
});
