var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}

// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 


/////////////////////////////////////
// CREATUS SLIDE MENUS
// Based on jquery
/////////////////////////////////////

// CLEAR CLASSES
function clearCSS() {
	document.getElementById('element1').className = '';	
	document.getElementById('element2').className = '';
	document.getElementById('element3').className = '';
	document.getElementById('element4').className = '';
	document.getElementById('element5').className = '';	
	document.getElementById('element6').className = '';
	document.getElementById('element7').className = '';
	document.getElementById('link1').className = '';	
	document.getElementById('link2').className = '';
	document.getElementById('link3').className = '';
	document.getElementById('link4').className = '';
	document.getElementById('link5').className = '';	
	document.getElementById('link6').className = '';
	document.getElementById('link7').className = 'last';
}

// HOME
function newHome() {	
	if ($("#newHome").is(":hidden")) {
		clearCSS();
		$("#newKontakt").slideUp("slow");		
		$("#newWebDesign").slideUp("slow");
		$("#newWebVedlikehold").slideUp("slow");
		$("#newWebPublisering").slideUp("slow");
		$("#newWebApplikasjoner").slideUp("slow");
		$("#newPortfolio").slideUp("slow");
		document.getElementById('element1').className = 'activemenu';
		document.getElementById('link1').className = 'activemenu';
		setTimeout('$("#newHome").slideToggle("slow")', 850);
	}
	else {
		$("#newHome").slideUp("slow");
		clearCSS();
	}
}

// WEB DESIGN
function newWebDesign() {	
	if ($("#newWebDesign").is(":hidden")) {	
		clearCSS();
		$("#newWebVedlikehold").slideUp("slow");
		$("#newWebPublisering").slideUp("slow");
		$("#newWebApplikasjoner").slideUp("slow");
		$("#newPortfolio").slideUp("slow");
		$("#newHome").slideUp("slow");
		$("#newKontakt").slideUp("slow");
		document.getElementById('element2').className = 'activemenu';
		document.getElementById('link2').className = 'activemenu';
		setTimeout('$("#newWebDesign").slideToggle("slow")', 850);	
	}
	else {
		$("#newWebDesign").slideUp("slow");
		clearCSS();
	}
}

// WEB VEDLIKEHOLD
function newWebVedlikehold() {
	if ($("#newWebVedlikehold").is(":hidden")) {		
		clearCSS();
		$("#newWebDesign").slideUp("slow");
		$("#newWebPublisering").slideUp("slow");
		$("#newWebApplikasjoner").slideUp("slow");
		$("#newPortfolio").slideUp("slow");
		$("#newHome").slideUp("slow");
		$("#newKontakt").slideUp("slow");
		document.getElementById('element3').className = 'activemenu';
		document.getElementById('link3').className = 'activemenu';
		setTimeout('$("#newWebVedlikehold").slideToggle("slow")', 850);
	}
	else {
		$("#newWebVedlikehold").slideUp("slow");
		clearCSS();
	}
}


// WEB PUBLISERING
function newWebPublisering() {
	if ($("#newWebPublisering").is(":hidden")) {		
		clearCSS();
		$("#newWebDesign").slideUp("slow");
		$("#newWebVedlikehold").slideUp("slow");
		$("#newWebApplikasjoner").slideUp("slow");
		$("#newPortfolio").slideUp("slow");
		$("#newKontakt").slideUp("slow");
		$("#newHome").slideUp("slow");
		document.getElementById('element4').className = 'activemenu';
		document.getElementById('link4').className = 'activemenu';
		setTimeout('$("#newWebPublisering").slideToggle("slow")', 850);
	}
	else {
		$("#newWebPublisering").slideUp("slow");
		clearCSS();
	}
}

// WEB APPLIKASJONER
function newWebApplikasjoner() {
	if ($("#newWebApplikasjoner").is(":hidden")) {		
		clearCSS();
		$("#newWebDesign").slideUp("slow");
		$("#newWebVedlikehold").slideUp("slow");
		$("#newWebPublisering").slideUp("slow");
		$("#newPortfolio").slideUp("slow");
		$("#newKontakt").slideUp("slow");
		$("#newHome").slideUp("slow");
		document.getElementById('element5').className = 'activemenu';
		document.getElementById('link5').className = 'activemenu';
		setTimeout('$("#newWebApplikasjoner").slideToggle("slow")', 850);
	}
	else {
		$("#newWebApplikasjoner").slideUp("slow");
		clearCSS();
	}
}

// PORTFOLIO
function newPortfolio() {
	if ($("#newPortfolio").is(":hidden")) {		
		clearCSS();
		$("#newWebDesign").slideUp("slow");
		$("#newWebVedlikehold").slideUp("slow");
		$("#newWebPublisering").slideUp("slow");
		$("#newWebApplikasjoner").slideUp("slow");
		$("#newKontakt").slideUp("slow");
		$("#newHome").slideUp("slow");
		document.getElementById('element6').className = 'activemenu';
		document.getElementById('link6').className = 'activemenu';
		setTimeout('$("#newPortfolio").slideToggle("slow")', 850);
	}
	else {
		$("#newPortfolio").slideUp("slow");
		clearCSS();
	}
}

// KONTAKT
function newKontakt() {
	if ($("#newKontakt").is(":hidden")) {
		clearCSS();		
		$("#newWebDesign").slideUp("slow");
		$("#newWebVedlikehold").slideUp("slow");
		$("#newWebPublisering").slideUp("slow");
		$("#newWebApplikasjoner").slideUp("slow");
		$("#newPortfolio").slideUp("slow");
		$("#newHome").slideUp("slow");
		document.getElementById('element7').className = 'activemenu-last';
		document.getElementById('link7').className = 'activemenu-last';		
		setTimeout('$("#newKontakt").slideToggle("slow")', 850);
	}
	else {
		$("#newKontakt").slideUp("slow");
		clearCSS();
	}
}	       

/////////////////////////////////////
// CREATUS FORM CSS
/////////////////////////////////////
function active(element) {
	document.getElementById(element).className = 'active';
}

function deactive(element) {
	document.getElementById(element).className = '';
}

/////////////////////////////////////
// CREATUS FORM VALIDATION
/////////////////////////////////////
function checkForm(){
	$("#formContact").validate({
		//set the rules for the field names
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			comment: {
				required: true,
				minlength: 5
			},
		},
		//set messages to appear inline
		messages: {
			name: "",
			email: "",
			comment: ""
		}
	});
}

	
			$(function checkFormLive(){
				// Grab each form element
				$("label[title]").each(function(){
					$(this).append("<div class=\"infopop\">");	
					titletext = $(this).attr("title");
					$(this).removeAttr("title");
					$(".infopop",this).css({opacity:0}).html(titletext);
					$("input",this).focus(function(){
						// Mouseover
						doFocus(this);
					}).blur(function(){
						// MouseOut
						doBlur(this);
					});
					$("textarea",this).focus(function(){
						// Mouseover
						doFocus(this);
					}).blur(function(){
						// MouseOut
						doBlur(this);
					});
				});
			});
 
			function doFocus(obj) {
				$(obj).addClass("active").parents("label").addClass("active").find(".infopop").animate({opacity:1,left:427},407);
			}
			
			function doBlur(obj) {
				if (doValidate(obj)) {
					isGood(obj);
				}
			}
			
			function reportErr(obj, message) {
				$(obj).addClass("error").parents("label").removeClass("isgood").addClass("required").addClass("error").find(".infopop").html(message).addClass("errorpop").animate({opacity:1,left:427},407);
			}
			
			function isGood(obj) {
				$(obj).removeClass("error").removeClass("active").parents("label").addClass("isgood").removeClass("error").removeClass("active").find(".infopop").removeClass("errorpop").animate({opacity:0,left:513},500);
			} 	
 
			function doValidate(obj) {
				// Extend jQuery object to include Regular expression masks assigned to properties
				mask = jQuery.extend({textfieldmask: /^[a-z\.\s-]{5,}$/i,namefieldmask: /^[a-z\.\s-]{2,}$/i, phonemask: /^[0-9\(\)\+\.\s-]{8,}$/i,passwordmask: /^\w{5,}$/, emailmask:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/});
				// Extend jQuery object to include error messages assigned to properties
				errmsg = jQuery.extend({textfielderr:"5 eller flere tegn",namefielderr:"2 eller flere tegn",phoneerr: "Include dialling code",passworderr:"Minimum 5 characters",emailerr:"Ikke gyldig epost",matcherr: "Must match"});
			
				// Set up variables to hold details of which mask to use and whether the field should match another field
				var masktouse = null;
				var mustmatch = null;
				// Determine the type of mask we're going to validate against
				switch(obj.name) {
					case "name": 		masktouse="namefieldmask"; 		errtouse="namefielderr"; 	break;
					case "email": 		masktouse="emailmask"; 			errtouse="emailerr"; 		break;
					case "comment": 	masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
				}
				// Check that the element is a required field before validating against it.
				if($(obj).parents("label").hasClass("required") && masktouse) {
					// Set up a quick way of accessing the object we're validating
					pointer = $(obj);
					// Test the value of the field against the Regular Expression
					if (mask[masktouse].test(pointer.val())) {
						// The field validated successfully!
						
						// Check to see if the field needs to match another field in the form
						if (mustmatch) {
							// It does need to match, so grab the object it needs to match
							matchobj = $("#"+mustmatch);
							if (matchobj.val()!='' && matchobj.val()!=pointer.val()) {
								// The fields don't match, so report an error on both of them
								reportErr(obj,errmsg["matcherr"]);	
								reportErr(matchobj,errmsg["matcherr"]);
							}
							else {
								// Either the fields match, or the other field hasn't been completed yet
								// If the other field has been completed, call the isGood function to clear any error message showing
								if (matchobj.val()!='') { isGood(matchobj);}
								return true;
							}
						}
						else {
							// No match is required, so return true - validation passed!
							return true;
						} 
					}
					else { 
						// The field failed to validate against the Regular Expression
						reportErr(obj,errmsg[errtouse]);
						return false; 
					}
				} 
				else {	
					// This isn't a required field, so we won't validate it against anything			
					return true;
				}
			}

	



