var d=document;
$('#logo').ready(function() {
  preloading();
});
$(document).ready(function(){
    $('#toppic').cycle({
		fx: 'fade',
		timeout: 4000,
		speed: 1000
	});
	$('#enewsemail').focus(function() {
		$('#enewsemail').css("background-color","#ffffff");
		$(this).select();
	});
	$('#signupemail').focus(function() {
		$('#signupemail').css("background-color","#ffffff");
		$(this).select();
	});
	if (location.pathname.indexOf("index.php")!=-1) {
		$("#sitemap").css( { "left": 4 + "px", "top":  - 288 + "px" });
	} else {
		$("#sitemap").css( { "left": 11 + "px", "top":  - 276 + "px" });
	}
	//alert(location.pathname);
	//alert($("#sitemap").css('top')+":"+$("#bottom").position().top);
	//$('#content').pngFix();
}); 
function showSiteMap() {
	$("#sitemap").fadeIn('fast');
}
function hideSiteMap() {
	//alert($("#sitemap"));
	$("#sitemap").fadeOut('fast');
	/*$("#sitemap").slideDown('slow', function() {
    // Animation complete.
	});*/
}

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_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_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 preloading() {
	MM_preloadImages('images/menu/dot.gif','images/menu/dot-ov.gif','images/menu/bg-01.png','images/menu/bg-02.png','images/menu/bg-03.png','images/menu/bg-04.png');
}
function showPulldown(b,f,e) {
	//var w=d.getElementById(f);
	var c=d.getElementById(f+"Pulldown");
	a(1);
	if(e==2){
		b.onclick=function(){
					c.hit=false;
					a(0)
				  }
	} else {
		b.onmouseover=function(){c.hit=true;a(1)};
		c.onmouseover=function(){c.hit=true;a(1)};
		b.onmouseout=function(){c.hit=false;a(0)};
		c.onmouseout=function(){c.hit=false;a(0)}
	}
	function a(g) {
		if(g==1){
			//if(b.id!=levelArray[0]&&b.className.indexOf("active")<0){
			if(b.className.indexOf("active")<0){
				b.className+=" active"
			}
			
			//alert($("#logo").offset().left);
			//$("#"+f+"Pulldown").style.left = $("#"+f).offset().left
			//w.style.left = c.style.left;
			if (f=="efforts") {
				$("#"+f+"Pulldown").css( { "left": $("#about").offset().left - $("#logo").offset().left + "px" } );
			} else if (f=="support") {
				$("#"+f+"Pulldown").css( { "left": $("#"+f).offset().left - $("#logo").offset().left + 6 + "px" } );
			} else {
				$("#"+f+"Pulldown").css( { "left": $("#"+f).offset().left - $("#logo").offset().left + "px" } );
			}
			$("#"+f+"Pulldown").fadeIn("fast");

			//c.style.display="block";
		} else {
			//if(b.id!=levelArray[0]) {
				b.className=b.className.replace("active","")
			//}
			setTimeout(function(){if(!c.hit){$("#"+f+"Pulldown").fadeOut("fast");if(e==2){b.onclick=function(){showPulldown(b,"route",2)}}}},100)
			//setTimeout(function(){if(!c.hit){c.style.display="none";if(e==2){b.onclick=function(){showPulldown(b,"route",2)}}}},100)
		}
	}
}

function changeLang(slang) {
	var locArray = location.href.split("/");
	location.href="../"+slang+"/"+locArray[locArray.length-1];
	return true;
}

function enewsBeforeSubmit() {
	//checking();
	
	if (isEmpty(document.enewsf.enewsemail.value)) {
		//document.getElementById("eemail").innerHTML="*";
		$('#enewsemail').css("background-color","#ffcccc");
		return false;
	} else if (!isEmail(document.enewsf.enewsemail.value)) {
		//document.getElementById("eemail").innerHTML="*";
		
		$('#enewsemail').css("background-color","#ffcccc");
		return false;
	} else {
		//emailAction();
		//alert("false");
		document.e-newsf.submit();
		//return true;
	}
}

function emailBeforeSubmit() {
	//checking();
	
	if (isEmpty(document.enewsform.signupemail.value)) {
		//document.getElementById("eemail").innerHTML="*";
		$('#signupemail').css("background-color","#ffcccc");
		return false;
	} else if (!isEmail(document.enewsform.signupemail.value)) {
		//document.getElementById("eemail").innerHTML="*";
		
		$('#signupemail').css("background-color","#ffcccc");
		return false;
	} else {
		//emailAction();
		//alert("false");
		document.enewsform.submit();
		//return true;
	}
}

function isEmpty(sText) {
  var spaceChars = " "
  var Char;
  for (i = 0; i < sText.length ; i++) {
    Char = sText.charAt(i);
    if (spaceChars.indexOf(Char) == -1) {
      return false;
    }
  }
  return true;	
}

function isEmail(emailStr) {
	if (!isEmailCompatibleCharacters(emailStr)) {
		return false;
	} 
	var emailPat		= /^(.+)@(.+)$/

	testString = "\\?"; //"
	var specialChars="";
	if( testString.charCodeAt(1)!=163 ) {
		specialChars	= "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]\\!\\$\\%\\^\\&\\*\\+\\{\\}\\=\\#"
	} else {
		specialChars	= "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]\\!\\?\\$\\%\\^\\&\\*\\+\\{\\}\\=\\#"
	}
	var validChars		= "\[^\\s" + specialChars + "\]"
	var firstChars		= validChars
	var quotedUser		= "(\"[^\"]*\")"
	var ipDomainPat		= /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom			= "(" + firstChars + validChars + "*" + ")"
	var word			= "(" + atom + "|" + quotedUser + ")"
	var userPat			= new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat		= new RegExp("^" + atom + "(\\." + atom +")*$")

	var matchArray = emailStr.match(emailPat)
	if (matchArray == null) {
		return false;
	}

	var user = matchArray[1]
	var domain = matchArray[2]

	if (user.match(userPat) == null) {
		return false;
	}

	if (domain.match(domainPat) == null) {
			return false;
	}

	var IPArray = domain.match(ipDomainPat)
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
			}
		}
		return true;
	}

	var atomPat = new RegExp(atom, "g")
	var domArr	= domain.match(atomPat)
	var len		= domArr.length

	if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 5) {
		return false;
	}

	if (domArr[domArr.length - 1].length == 2 && len < 2) {
		return false;
	}

	if (domArr[domArr.length - 1].length == 3 && len < 2 ) {
		return false;
	}

	return true;
}
  
function isEmailCompatibleCharacters(aString) {
	for (var i = 0; i != aString.length; i++) {
		aChar = aString.substring(i, i + 1);
		// check for valid character
		if (aChar == '@' || aChar == '.' || aChar == '_' || aChar == '-' || (aChar >= 'a' && aChar <= 'z') || (aChar >= 'A' && aChar <= 'Z') || (aChar >= '0' && aChar <= '9')) {
			continue;
		} 
		else { 
			return false;
		}
	}
	return true;
}

