var doekRatio_p1 = 1600/982;
var modelRatio_p1 = 946/982;
var doekRatio_p2 = 1600/982;
var modelRatio_p2 = 800/1312;
var doekRatio_p3 = 1600/982;
var modelRatio_p3 = 800/1312;
var doekRatio_p4 = 1600/982;
var modelRatio_p4 = 600/982;
var lampRatio = 836/858;

var bW = 0;

$(window).resize(function(){	
	buildSite();
});
$(window).load(function(){	
	c = 0;					
	wH = $("#animation").height();
	setInterval(function() {
      $("#animation").css("backgroundPosition", "0px -" + c * wH + "px");
	  if(c <= 6){
		c++;  
	  }else{
		c=0;	  
	  }
	}, 150);	
	buildSite();
});

$("#intro").click(function(){
	$(this).fadeOut(1200);					 
});

function buildSite(){
	/*
	bW = $("body").width();
	
	$('#partHolder').css("width", bW*1.5*4);
	$('#part1').css("width", bW);
	$('#part2').css("width", bW);
	$('#part3').css("width", bW);
	$('#part4').css("width", bW);
	$('#part2').css("left", bW*1.5);
	$('#part3').css("left", bW*1.5*2);
	$('#part4').css("left", bW*1.5*3);
	*/
	$("#part1 .model img").hide();
	$("#p1_1").show();
	
	$(".flash").hide();
	
	$('#cursor').css('left', ($("body").width()-$('#cursor').width())/2);
	
	$("#part1 .doek").css("width", $("#part1 .doek").height() * doekRatio_p1 );	
	$("#part1 .doek").css("margin-left", - ($("#part1 .doek").width() / 2) );	
	/*
	$("#part1 .model").css("width", $("#part1 .model").height() * modelRatio_p1 );	
	$("#part1 .model").css("margin-left", - ($("#part1 .model").width() / 2) );	
	*/
	/*
	$("#part1 .lampRechts").css("margin-left", - ($("#part1 .lampRechts").width() / 2));		
	$("#part1 .lampLinks").css("margin-left", - ($("#part1 .lampLinks").width() / 2));	
	*/
	$("#part2 .doek").css("width", $("#part2 .doek").height() * doekRatio_p2 );	
	$("#part2 .doek").css("margin-left", - ($("#part2 .doek").width() / 2) );	
	/*
	$("#part2 .model").css("width", $("#part2 .model").height() * modelRatio_p2 );	
	$("#part2 .model").css("margin-left", - ($("#part2 .model").width() / 2) );	
	
	$("#part2 .lampRechts").css("margin-left", - ($("#part2 .lampRechts").width() / 2) + ($("#part2 .model").width()));		
	$("#part2 .lampLinks").css("margin-left", - ($("#part2 .lampLinks").width() / 2) - ($("#part2 .model").width()));	
	*/
	$("#part3 .doek").css("width", $("#part3 .doek").height() * doekRatio_p3 );	
	$("#part3 .doek").css("margin-left", - ($("#part3 .doek").width() / 2) );	
	/*
	$("#part3 .model").css("width", $("#part3 .model").height() * modelRatio_p3 );	
	$("#part3 .model").css("margin-left", - ($("#part3 .model").width() / 2) );	
	
	$("#part3 .lampRechts").css("margin-left", - ($("#part3 .lampRechts").width() / 2) + ($("#part3 .model").width()));		
	$("#part3 .lampLinks").css("margin-left", - ($("#part3 .lampLinks").width() / 2) - ($("#part3 .model").width()));	
	*/
	$("#part4 .doek").css("width", $("#part4 .doek").height() * doekRatio_p4 );	
	$("#part4 .doek").css("margin-left", - ($("#part4 .doek").width() / 2) );	
	/*
	$("#part4 .model").css("width", $("#part4 .model").height() * modelRatio_p4 );	
	$("#part4 .model").css("margin-left", - ($("#part4 .model").width() / 2) );	
	
	$("#part4 .lampRechts").css("margin-left", - ($("#part4 .lampRechts").width() / 2) + ($("#part4 .model").width()));		
	$("#part4 .lampLinks").css("margin-left", - ($("#part4 .lampLinks").width() / 2) - ($("#part4 .model").width()));	
	*/
}

var activePart = 1;
var animating = false;
var ignoreCursor = false;
var nextPart = 0;

$("body").mousemove(function(e){		
	if(animating == false){							 
		$('#cursor').css('left', e.clientX - 400);
		if(e.clientX >= $("body").width()-(($("body").width())/3)){		
			if(ignoreCursor == false){
				dif = e.clientX - ($("body").width()-(($("body").width())/3));
				per =  dif / (($("body").width())/300) ;
				$("#part" + activePart).css("left", - (per / 6 )+ "%");
				$("#part" + nextPart).css("left", 150 - (per /6 ) + "%");
				$("body").css("backgroundPosition", -(dif/6) + "px 0px");		
			}
		}else if(e.clientX <= $("body").width()/3){			
			if(ignoreCursor == false){
				dif = ($("body").width()/3) - e.clientX;
				per =  dif / (($("body").width())/300) ;			
				$("#part" + activePart).css("left", (per/6)+ "%");
				$("#part" + nextPart).css("left", - 150 + (per/6) + "%");
				$("body").css("backgroundPosition", (dif/6) + "px 0px");
			}
		}else{
			$("#part" + activePart).css("left", "0%");	
			ignoreCursor = false;
		}
	}
});

function partRight(){
	if(animating == false){
		ignoreCursor = true;
		if(activePart <= 3){
			nextPart = activePart + 1;
		}else{
			nextPart = 1;
		}			
		$("#count" + activePart).hide();
		$("#title" + activePart).hide();
		$("#count" + nextPart).show();
		$("#title" + nextPart).show();
		$("body").animate({backgroundPosition: "-" + 600 * activePart + "px 0px"},500);
		$("#part" + activePart).animate({left:"-150%"},500,function(){
			animating = false;
			writeLabels();
		});	
		
		$("#part" + nextPart).css("left","150%");	
		$("#part" + nextPart).animate({left:"0%"},500);		
		activePart = nextPart;
		nextPart = 0;
	}
}
function partLeft(){
	if(animating == false){
		ignoreCursor = true;
		if(activePart >= 2){
			nextPart = activePart - 1;
		}else{
			nextPart = 4;
		}				
		$("#count" + activePart).hide();
		$("#title" + activePart).hide();
		$("#count" + nextPart).show();
		$("#title" + nextPart).show();
		$("body").animate({backgroundPosition:600 * activePart + "px 0px"},500);		
		$("#part" + activePart).animate({left:"150%"},500,function(){
			animating = false;
			writeLabels();
		});	
		$("#part" + nextPart).css("left","-150%");	
		$("#part" + nextPart).animate({left:"0%"},500);	
		activePart = nextPart;
		nextPart = 0;
	}
}
function writeLabels(){
	if(activePart == 1){
		$("#left u").html("contact");
		$("#right u").html("about");
	}else if(activePart == 2){
		$("#left u").html("collection");
		$("#right u").html("retailers");		
	}else if(activePart == 3){
		$("#left u").html("about");
		$("#right u").html("contact");		
	}else if(activePart == 4){
		$("#left u").html("retailers");
		$("#right u").html("collection");		
	}
}


$("#cursor").click(function(){
	callAS(activePart);
});

$("#fix").click(function(){
	callAS(activePart);
});


function showFull(id){
	animating = false;
	$("#page" + id).show();
	$("#pages").fadeIn(800);
}		
function closeFull(){
	document.getElementById("flash" + activePart).resetMovie(activePart);
	$("#pages").hide(0,function(){		
		$("#page" + activePart).hide();								 
	});
}
function callAS(id){
	animating = true;
	document.getElementById("flash" + id).sendToActionscript(id);
}

aP = 0;
tP = 33;
function colRight(){
	if(aP < ((tP/4)-1)){
		aP ++;	
		moveCol()
	}
}
function colLeft(){
	if(aP > 0){
		aP --;
		moveCol()
	}
}
function moveCol(){
	$("#innerCollection").animate({left:"-" + (aP * 504) +"px"},500);		
}
function showCollection(cId){
	alert(cId);	
}

aF = 1;
tF = 34;

function showCollection(i){
	aF = i;
	$("#fullview").show();
	$("#full" + aF).fadeIn(500);
}
function closeCol(){
	$("#fullview").fadeOut(500,function(){
		$("#full" + aF).hide();								
	});	
}
function fullRight(){
	$("#full" + aF).fadeOut(300);
	if(aF < tF){
		aF++;
	}else{
		aF = 1;
	}
	$("#full" + aF).fadeIn(300);
}
function fullLeft(){
	$("#full" + aF).fadeOut(300);
	if(aF > 1){
		aF--;
	}else{
		aF = tF;
	}
	$("#full" + aF).fadeIn(300);		
}

var formValid = true;
var emailValid = true;
var dataForm = "";

function submitForm(){
	formValid = true;
	var inputs = $("#contact").children("div").children("input");
	for( i=0; i < inputs.length; i++){		
		if(inputs[i].value == ""){
			formValid = false;
			document.getElementById(inputs[i].name).className = "error";
			$("#" + inputs[i].name + "-error").fadeIn();
		}else{
			dataForm += inputs[i].name + "=" + encodeURIComponent(inputs[i].value) + "&";		
		}
	}
	dataForm += "newsletter=" + $("#newsletter").is(':checked') + "&" ;
	dataForm += "brochure=" + $("#brochure").is(':checked');		
	
	if(emailValid && formValid){
		if (window.XMLHttpRequest)
		{
			xmlhttp=new XMLHttpRequest();
		}
		else
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}		
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText != ""){
					if(xmlhttp.responseText == "mail verzonden"){
						$("#contact").hide();
						$("#submitBtn").fadeOut();
						$("#contactSuccess").fadeIn();
					}else{
						alert(xmlhttp.responseText);
					}
				}
			}
		}	
		xmlhttp.open("GET","inc/functions/sendMail.php?"+dataForm,true);
		xmlhttp.send();
	}
}

function showForm(){
	$("#contactForm").fadeIn();
	$("#contactSuccess").fadeOut();
}
$("#email").blur(function(){
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!regex.test(this.value)){
		this.className = "error";
		$("#email-error").html("Invalid email<i></i>");
		$("#email-error").fadeIn();
		emailValid = false;
	}
	else{
		this.className = "";
		$("#email-error").css("display","none");
		emailValid = true;
	}
});

$("#name").blur(function(){
	if(this.value == ""){
		this.className = "error";
		$("#name-error").fadeIn();
	}else{
		this.className = "";
		$("#name-error").css("display","none");
	}
});

$("#address").blur(function(){
	if(this.value == ""){
		this.className = "error";
		$("#address-error").fadeIn();
	}else{
		this.className = "";
		$("#address-error").css("display","none");
	}
});

$("#location").blur(function(){
	if(this.value == ""){
		this.className = "error";
		$("#location-error").fadeIn();
	}else{
		this.className = "";
		$("#location-error").css("display","none");
	}
});

function getShops(){
	if($("#location").val() != ""){
		if (window.XMLHttpRequest)
		{
			xmlhttp=new XMLHttpRequest();
		}
		else
		{
			xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
		}					
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText != ''){
					$("#page3 .content").css("margin-top","-320px");
					$("#retailers").hide();
					$("#retailersBtn").hide();
					$("#results").show();
					$("#results").html(xmlhttp.responseText);
					$("#retryBtn").show();
				}
			}
		};
		var input    = encodeURIComponent($("#location").val());	
		url = 'inc/functions/getRetailers.php?input=' + input;	
		xmlhttp.open('GET',url,true);
		xmlhttp.send();
	}else{
		$("#location-error").fadeIn();
	}
}

function hideResults(){
	$("#page3 .content").css("margin-top","-100px");
	$("#retailers").show();
	$("#retailersBtn").show();
	$("#results").hide();
	$("#retryBtn").hide();
	$("#results").html("");
}

