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 createAjaxObj() {
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
}

function searchClear() {
	var searchtext = document.getElementById("searchword").value;	

	if (searchtext == ' Search') {
		document.getElementById("searchword").value = '';
		$('#searchword').removeClass('inputsearchgray');
		$('#searchword').addClass('inputsearch');
	}
}

function searchCheck() {
	var searchtext = document.getElementById("searchword").value;
	var searchtext2 = ' Search';

	if (searchtext == '') {
		$('#searchword').addClass('inputsearchgray');
		$('#searchword').removeClass('inputsearch');
		document.getElementById("searchword").value = searchtext2;
	}
}

function searchSite() {
	var searchtext = document.getElementById('searchword').value;
	
	if ((searchtext != '') && (searchtext != ' Search')) {
		window.location = 'index.php?search=' + searchtext;
	}
}

function headerarrow(action, arrow) {
	if (action == 'show') {
		$('#header' + arrow + 'arr').show();
	}
	
	if (action == 'hide') {
		$('#header' + arrow + 'arr').hide();
	}
}

function openSub(subid) {
	var opensub = document.getElementById("opensub").value;
	
	if (opensub != subid) {
		$('div[id^=subcat_]').hide();
		$('#subcat_' + subid).animate({ opacity: 'show'}, 'slow' );
		
		document.getElementById("opensub").value = subid;
	} else {
		$('#subcat_' + subid).animate({ opacity: 'hide'}, 'fast' );
		$('#subcat2_' + subid).animate({ opacity: 'hide'}, 'fast' );		
		
		document.getElementById("opensub").value = '0';
	}
}

function sendEmail() {
	$("#mailmsg").html('<img src="images/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 comments = document.getElementById('comments').value;
	
	var messagesent, messagenotsent;
	
	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!';
	
	messagesent = 'Your message has been sent succesfully!<br/>Thank you, we will contact you as soon as possible.';
	messagenotsent = 'There has been a problem sending your message. Please try again or contact us using one of the other contact information.';
	
	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("sendmail.php",
		{ name: name, surname: surname, phone: phone, email: email, comments: comments },
			function(data){
				if (data == 'ok') {
					$("#mailmsg").html(messagesent);
				} else {
					$("#mailmsg").html(messagenotsent);
				}
			}
	);
}

var mainphototime, showmainphototime;

function showHeader(show) {
	window.clearTimeout(showmainphototime);
	window.clearTimeout(mainphototime);
	loadMainPhoto(show);
}

function headerPause() {
	window.clearTimeout(showmainphototime);
	window.clearTimeout(mainphototime);
	$('#headerpause').html('<a onclick="headerPlay();">PLAY</a>');
}

function headerPlay() {
	window.clearTimeout(showmainphototime);
	window.clearTimeout(mainphototime);
	var show = document.getElementById("show").value;
	var nextshow = parseInt(show) + 1;
	loadMainPhoto(nextshow);
	$('#headerpause').html('<a onclick="headerPause();">PAUSE</a>');
}

function loadMainPhoto(show) {
	var mainphotointerval = 8000;
	var mainphoto;
	var prevshow, prevshowtitle, nextshow, nextshowtitle, titlechangeline;
	var homephotoscount = document.getElementById("homephotoscount").value;
	document.getElementById("show").value = show;

	window.clearTimeout(mainphototime);
	
	mainphoto = document.getElementById("homephotosphoto_" + show).value;
	var headertitle = document.getElementById("homephotostitle_" + show).value;
	var headertext = document.getElementById("homephotostext_" + show).value;
	var headertextcolor = document.getElementById("homephotostextcolor_" + show).value;
	mainphotointerval = document.getElementById("homephotostime_" + show).value;
	
	if (show == 1) {
		prevshow = homephotoscount;
		prevshowtitle = document.getElementById("homephotostitle_" + homephotoscount).value;
		nextshow = parseInt(show) + 1;
		nextshowtitle = document.getElementById("homephotostitle_" + nextshow).value;
	} else if (show < homephotoscount) {
		prevshow = parseInt(show) - 1;
		prevshowtitle = document.getElementById("homephotostitle_" + prevshow).value;
		nextshow = parseInt(show) + 1;
		nextshowtitle = document.getElementById("homephotostitle_" + nextshow).value;
	} else if (show == homephotoscount) {
		prevshow = parseInt(show) - 1;
		prevshowtitle = document.getElementById("homephotostitle_" + prevshow).value;
		nextshow = 1;
		nextshowtitle = document.getElementById("homephotostitle_1").value;
	}
	
	var headerprevious = "<a onclick='showHeader(" + prevshow + ");'>" + prevshowtitle + "</a>";
	var headernext = "<a onclick='showHeader(" + nextshow + ");'>" + nextshowtitle + "</a>";

	$("#headerphoto").animate({ opacity: 'hide'}, 100);
	$("#headertitle").animate({ opacity: 'hide'}, 100);
	$("#headertext").animate({ opacity: 'hide'}, 100);
	$("#headerprevious").animate({ opacity: 'hide'}, 100);
	$("#headernext").animate({ opacity: 'hide'}, 100);
	showmainphototime = window.setTimeout(function(){
		switch (headertextcolor) {
			case '0':
				$('#headertitle').removeClass('headertitleblack');
				$('#headertitle').removeClass('headertitlewhite');
				$('#headertitle').addClass('headertitlewhite');
				
				$('#headertext').removeClass('headertextblack');
				$('#headertext').removeClass('headertextwhite');
				$('#headertext').addClass('headertextwhite');
				break;
			case '1':
				$('#headertitle').removeClass('headertitleblack');
				$('#headertitle').removeClass('headertitlewhite');
				$('#headertitle').addClass('headertitleblack');
				
				$('#headertext').removeClass('headertextblack');
				$('#headertext').removeClass('headertextwhite');
				$('#headertext').addClass('headertextblack');
				break;
		}
		
		$('#headerphoto').html('<img src="http://www.ipedis.org/photos/image.php/' + mainphoto + '?width=960&amp;height=360&amp;cropratio=16:6&amp;image=http://www.ipedis.org/photos/' + mainphoto + '" />');
		
		if (headertitle.length <= 25) { titlechangeline = '<br/>'; } else { titlechangeline = ''; }
		
		$('#headertitle').html('<strong>' + titlechangeline + headertitle + '</strong>');
		$('#headertext').html('<strong>' + headertext + '</strong>');
		$('#headerprevious').html(headerprevious);
		$('#headernext').html(headernext);
		}, 100);
	$("#headerphoto").animate({ opacity: 'show'}, 800);
	$("#headertitle").animate({ opacity: 'show'}, 600);
	$("#headertext ").animate({ opacity: 'show'}, 600);
	$("#headerprevious").animate({ opacity: 'show'}, 100);
	$("#headernext").animate({ opacity: 'show'}, 100);
	
	if (show == homephotoscount) { show = 1; } else { show++; };

	mainphototime = window.setTimeout(function(){loadMainPhoto(show)}, mainphotointerval);
}

function showLogo(turn, logosource) {
	var logotitle, logofile, logointerval, logotime;
	var i = turn;
	var ajaxDisplay = document.getElementById('logoscontainer');
	var logodata = logosource.split('##');
	
	window.clearTimeout(logotime);
	
	logotitle = logodata[i]; i++;
	logofile = logodata[i]; i++;
	logointerval = logodata[i]; i++;
	logolink = logodata[i]; i++;
	
	if (logodata[i] == 'thisistheendmyfriend') { i = 0; }

	$("#logoscontainer").hide();
		
	ajaxDisplay.innerHTML = '<table width="160"><tr><td width="160" align="center" valign="top"><span class="logotext">' + logotitle + '</span></td></tr><tr><td width="160" height="10"></td></tr><tr><td width="160" align="center" valign="top"><a href="' + logolink + '" target="_blank"><img src="http://www.ipedis.org/photos/image.php/' + logofile +'?width=140&amp;height=103&amp;image=http://www.ipedis.org/photos/' + logofile +'" alt="' + logotitle + '" border="0"/></a></td></tr></table>';
	
	$("#logoscontainer").animate({ opacity: 'show' }, 'slow');
	
	logotime = window.setTimeout(function(){showLogo(i, logosource)}, logointerval);
}

function loadLogos(lan) {
	var ajaxRequest = createAjaxObj();
	var queryString = '?';

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		var ajaxDisplay = document.getElementById('logoscontainer');
		ajaxDisplay.innerHTML = '<img src="../images/ajax-loader-circle.gif" />';

		if(ajaxRequest.readyState == 4){
			if (ajaxRequest.responseText != '') {
				var logodata = ajaxRequest.responseText.split('##');
				
				if (logodata[0] != 'thisistheendmyfriend') {
					showLogo(0, ajaxRequest.responseText);
				} else {
					ajaxDisplay.innerHTML = '';				
				}
			} else {
				ajaxDisplay.innerHTML = '';
			}
		}
	}

	queryString = queryString + 'm=logosload&lan=' + lan;
	
	ajaxRequest.open("GET", "../ajax/ajaxmethods.php" + queryString, true);
	ajaxRequest.send(null);	
}

function showNews(turn, newssource) {
	var newsid, newstitle, newstime;
	var i = turn;
	var ajaxDisplay = document.getElementById('newscontainer');
	var newsdata = newssource.split('##');
	var newsinterval = 5000;
	var lan = document.getElementById('lan').value;
	
	window.clearTimeout(newstime);
	
	newsid = newsdata[i]; i++;
	newstitle = newsdata[i]; i++;
	
	if (newsdata[i] == 'thisistheendmyfriend') { i = 0; }

	$("#newscontainer").hide();
		
	ajaxDisplay.innerHTML = '<a href="index.php?a=news&id=' + newsid + '&lan=' + lan + '">' + newstitle + '</a>';
	
	$("#newscontainer").animate({ opacity: 'show' }, 'slow');
	
	newstime = window.setTimeout(function(){showNews(i, newssource)}, newsinterval);
}

function loadNews(lan) {
	var ajaxRequest = createAjaxObj();
	var queryString = '?';

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if (ajaxRequest.responseText != '') {
				var logodata = ajaxRequest.responseText.split('##');
				
				if (logodata[0] != 'thisistheendmyfriend') {
					showNews(0, ajaxRequest.responseText);
				}
			}
		}
	}

	queryString = queryString + 'm=newsload&lan=' + lan;
	
	ajaxRequest.open("GET", "../ajax/ajaxmethods.php" + queryString, true);
	ajaxRequest.send(null);	
}

function showContent(pageid, lan) {
	$("#maincontenttitle").html('');
	$("#maincontenttext").html('<img src="images/ajax-loader-small.gif" />');
	
	$.post("ajax/ajaxmethods.php",
		{ m: "showcontent", pageid: pageid, lan: lan },
			function(data){
				if (data != '') {
					var contentdata = data.split('##');
					
					$("#maincontenttitle").html(contentdata[0]);
					$("#maincontenttext").html(contentdata[1]);
				} else {
					$("#maincontenttitle").html('');
					$("#maincontenttext").html('');
				}
			}
	);	
}

function pageLoad() {
	MM_preloadImages('images/aboutus.png','images/aboutus_1.png','images/aboutus_2.png','images/aboutus_3.png');
	
	var lan = document.getElementById('lan').value;
	var id = document.getElementById('id').value;
	
	loadMainPhoto(1);
	
	if (id == '0') {
		//loadLogos(lan);
		loadNews(lan);
	}
}

$(document).ready(function() {
	$("a[id^=iframe_]").fancybox({
		'width'				: 450,
		'height'			: 705,
		'autoScale'			: false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe',
		'showCloseButton'	: true
	});
});
