var newwindow;
function poptastic(url){
	newwindow=window.open(url,'name','height=900,width=700,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}

//activate imagetrail
function activateimgtrail(){
	// var description = this.alt + " <span>Klik voor meer details</span>";
	var description = this.readAttribute('rel');
	var header 		= this.title;
	var bar 		= this.src;
	var myclass 	= this.readAttribute('class');
	var image 		= bar.replace("normal.jpg", "big.jpg")
	showtrail(image,header,description,				
								myclass,						
								'5',							
								'5',							
								492,							
								1);
}

//ajax form contact
function sendForm(e) { 
	//validate
	var valid = new Validation(this, {onSubmit:false});
	var result = valid.validate();
	if (result==true) {
		// submit the form using Ajax 
		new Ajax.Request("../../includes/processform.php", { 
			parameters : Form.serialize(this), 
			onSuccess : updateForm 
		}); 
	}
	Event.stop(e); 
} 
function updateForm(req){
	// alert(req.responseText);
	// resp.responseText ;
	// alert(resp);
	$('contactform').fade({duration:1.5});
	$('responseform').appear({ delay:2,duration: 1.5 });
	$('responseform').update(req.responseText);
	//timedRedirect();
}

//ajax form comment
function sendComment(e) { 
	//validate
	var valid = new Validation(this, {onSubmit:false});
	var result = valid.validate();
	if (result==true) {
		// submit the form using Ajax 
		new Ajax.Request("../../includes/commentform.php", { 
			parameters : Form.serialize(this), 
			onSuccess : updateComment 
		}); 
	}
	Event.stop(e); 
}
function updateComment(req){
	// alert(req.responseText);
	// resp.responseText ;
	// alert(resp);
	$('commentform').fade({duration:1.5});
	$('responseform').appear({ delay:2,duration: 1.5 });
	$('responseform').update(req.responseText);
	//timedRedirect();
}

redirectTime = "9000";
redirectURL = "../../";
function timedRedirect() {
	setTimeout("location.href = redirectURL;",redirectTime);
}
function formCallback(result, form) {
	window.status = "validation callback for form '" + form.id + "': result = " + result;
}

// slide 
function slideme() {
		var containerbigimg = $('imgbig');
		var newimg			= this;
}
// end slide

//suckerfish hover
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	sfEls += document.getElementById("nav2").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
//end suckerfish hover

// cleartitle
function cleartitle(){
	$(this).down().title='';
}
// end cleartitle

// toggeldiv
function togglediv(){
	var el= this.next();
	Effect.toggle(el, 'slide',{ duration: .8 });
}
//end toggeldiv	

// submitform
function submitform(theform){ 
 	document.theform.submit(); 
} 
// end submitform

//cookies
function delete_cookie(cookie_name){
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}
//end cookies

//slidefooter
function slidefooter(el){
	new Effect.toggle(el, 'slide',{duration:0.8})
	setTimeout(function(){scrolltofooter(el)}, 500);
}
// end slidefooter

//scrolltofooter
function scrolltofooter(el){ 
	new Effect.ScrollTo(el);
}
//end scrolltofooter

//projecten
function submitselect(){ //sorteer de projecten
	var selectbox1 = $('selectprovincie').value;
	var selectbox2 = $('selectprojecttype').value;
	var selectbox3 = $('selectcontractvorm').value;	
	var allprojects = $$('#productoverzicht li');
	for (var i = allprojects.length; i--; ) {
		allprojects[i].hide();
	}
	var url = "../includes/sortprojects.php";
	new Ajax.Request(url, {parameters:{provincie: selectbox1, projecttype: selectbox2, contractvorm:selectbox3},
		onCreate: function start(){
			$('productoverzicht').setStyle({backgroundPosition: '320px 155px'});
		},
		onSuccess: function(transport) {
			var showthese 	= new Array();
			var showthese 	= transport.responseText.split("|");
			var totalItems 	= showthese.length;
			if (totalItems<5) {
				currentItemsTo = totalItems;
			}else{
				currentItemsTo = 5;			
			}
			if (showthese=="") {//leeg
				$('productoverzicht').setStyle({backgroundPosition: '320px -155px'});
				var currentItemFrom = 0;
				var currentItemsTo = 0;
				var totalItems = 0;
				movedtolast=1;
			}else{
				$('productoverzicht').setStyle({backgroundPosition: '320px -355px'})
				for (var i = showthese.length; i--; ) {
					$(showthese[i]).appear()
				}
				var currentItemFrom = 1;
				movedtolast=0;
			}		
			//goto startpos
			moveToFirstAfterSelect();
			// var currentItem = 1;
			updatecounters(currentItemFrom,currentItemsTo,totalItems);
		}
		// onFinish:function(){;}	
	});
}

function moveToFirstAfterSelect(){ //naar het eerste project na selectie
	var currentpos = $('productoverzicht').getStyle('left');
	if (currentpos!=undefined) {
	var currentpos = currentpos.replace('px','')
	var currentpos = currentpos.replace('-','')

	new Effect.Move('productoverzicht', { x: currentpos, y: 0, transition: Effect.Transitions.sinoidal,afterFinish: function(){getcounters()} });
	}
	var currentItem = 0;
	var currentItemFrom = 1;
	var currentItemTo = 5;
	if (currentItemTo>totalItems) {
		currentItemTo = totalItems;
	}
	movedtolast++;
	movedtolast=0;
	return false;
}

function isfirst(){ //checked of we bij het begin zijn
	//check current position
	currentpos = $('productoverzicht').getStyle('left');
	if (currentpos==undefined) {
		return true;
	}else{
		var currentpos = currentpos.replace('px','');
		var currentpos = currentpos.replace('-','');
		if (currentpos == 0) {
			return true;
		}else{
			return false;
		}
	}	
}

function islast(){ //checked of we bij het eind zijn
	// console.log("------------------------------------");
	//check current position
	currentpos = $('productoverzicht').getStyle('left');
	if (currentpos!=undefined) {
		var currentpos = currentpos.replace('px','')
		var currentpos = currentpos.replace('-','')

		// --
		//totaal aantal items
		var totalItemz = 0;
		var allprojects = $$('#productoverzicht li');
		for (var i = allprojects.length; i--; ) {
			if (allprojects[i].visible()) {
				totalItemz++;
			};
		}
		// --
		//bij welk item zijn we nu
		var currentItemb4 = $('productoverzicht').getStyle('left');
		if (currentItemb4!=undefined) {
			var currentItemb4 = currentItemb4.replace('px','');
			var currentItemb4 = currentItemb4.replace('-','');
			var currentItemb4 = currentItemb4/178;
			var currentItemb4 = currentItemb4+1;
		}else{
			var currentItemb4 = 1;
		}
		// --
		//tot welk item
		var currentItemToz = (totalItemz-(totalItemz-currentItemb4))+4;
		if (currentItemToz>totalItemz) {
			currentItemToz = totalItemz;
		}
		// --
		// console.log("totalItemz: "+totalItemz+"\ncurrentItemb4:"+currentItemb4+"\ncurrentItemToz:"+currentItemToz);
/*

		console.log("currentpos: "+currentpos);
		if (!(currentpos%5)) { currentpos = currentpos - 5}
		console.log("currentpos2: "+currentpos);
		
		var currentslides = Math.round(currentpos/178)/5;
		var currentslides = (currentpos/178);
		
		console.log("currentslides: "+currentslides);
		currentslides = Math.round(currentslides)
		console.log("currentslides2: "+currentslides);
		// if (!(currentslides%5)) { currentslides = currentslides + 5}
		currentslides = Math.round(currentslides/5)
		console.log("currentslides3: "+currentslides);
		console.log("currentslides2: "+Math.round(currentslides));
		
		//totalslides
		var totalItemz = 1;
		var allprojects = $$('#productoverzicht li');
		for (var i = allprojects.length; i--; ) {
			if (allprojects[i].visible()) {
				totalItemz++;
			};
		}	
		var totalslides = Math.floor(totalItemz/5);
		console.log("totalslides: "+totalslides);
		console.log("currentslides4: "+Math.round(currentslides));*/


		if (totalItemz > currentItemToz){
			// console.log('not last?1')
			return false;
		}else{
			// console.log('last?1')
			return true;
		}
	}else{
		// console.log('not last?2')
		return false;
	}	
}

function moveToPrevious(e){ //naar de vorige projecten
	Event.stop(e);
	if(!isfirst()){
		currentItem--;
		new Effect.Move('productoverzicht', { x: 890, y: 0, transition: Effect.Transitions.sinoidal,afterFinish: function(){getcounters()} });
	}
	return false;
	// return movedtolast;
	// return totalItems;
}

function moveToNext(e){ //naar de volgende projecten
	    Event.stop(e);
		if(!islast()){
			new Effect.Move('productoverzicht', { x: -890, y: 0, transition: Effect.Transitions.sinoidal,afterFinish: function(){getcounters()}});
		}
	return false;
}

function moveToFirst(e){ //naar het eerste project
	Event.stop(e);
	var currentpos = $('productoverzicht').getStyle('left');
	if (currentpos!=undefined) {
	var currentpos = currentpos.replace('px','')
	var currentpos = currentpos.replace('-','')

	new Effect.Move('productoverzicht', { x: currentpos, y: 0, transition: Effect.Transitions.sinoidal,afterFinish: function(){getcounters()} });
	}
	var currentItem = 0;
	var currentItemFrom = 1;
	var currentItemTo = 5;
	if (currentItemTo>totalItems) {
		currentItemTo = totalItems;
	}
	movedtolast++;
	movedtolast=0;
	return false;
}

function moveToLast(e){ //naar het laatste project
	Event.stop(e);
	var movedLeft = $('productoverzicht').getStyle('left');

	if (movedLeft==undefined) {
		movedLeft = 0;
	}else{
		movedLeft = movedLeft.replace('px','');
		movedLeft = movedLeft.replace('-','');	
	}

	//totaal aantal items
	var totalItemzz = 0;
	var allprojects = $$('#productoverzicht li');
	for (var i = allprojects.length; i--; ) {
		if (allprojects[i].visible()) {
			totalItemzz++;
		};
	}
	if (!(totalItemzz%5)) { totalItemzz = totalItemzz - 5}
	// console.log("totalItemzz"+totalItemzz);

	var totalWidth = totalItemzz;
	
	totalWidth = Math.floor(totalItemzz/5)
	// totalWidth = Math.floor(totalWidth);
	totalWidth = (totalWidth*178)*5;

	moveItems = totalWidth - (movedLeft)

	if (moveItems>0) {
		moveItems = '-'+moveItems
	}

	// console.log("totalWidth:"+totalWidth)
	// console.log("moveItems:"+moveItems)


	currentItemFrom = (Math.round(totalItemzz/5)*5)+1
	if (currentItemFrom<1) {
		currentItemFrom=1;
	}
	currentItemTo = totalItems
	new Effect.Move('productoverzicht', { x: moveItems, y: 0, transition: Effect.Transitions.sinoidal,afterFinish: function(){getcounters()} });		
	return false;
}

function getcounters(){ //tel het aantal projecten en waar we nu zijn 
	//totaal aantal items
	var totalItemz = 0;
	var allprojects = $$('#productoverzicht li');
	for (var i = allprojects.length; i--; ) {
		if (allprojects[i].visible()) {
			totalItemz++;
		};
	}
	//bij welk item zijn we nu
	var currentItemb4 = $('productoverzicht').getStyle('left');
	if (currentItemb4!=undefined) {
		var currentItemb4 = currentItemb4.replace('px','');
		var currentItemb4 = currentItemb4.replace('-','');
		var currentItemb4 = currentItemb4/178;
		var currentItemb4 = currentItemb4+1;
	}else{
		var currentItemb4 = 1;
	}
	//tot welk item
	var currentItemToz = (totalItemz-(totalItemz-currentItemb4))+4;
	if (currentItemToz>totalItemz) {
		currentItemToz = totalItemz;
	};
	
	updatecounters(currentItemb4,currentItemToz, totalItemz);
}

function updatecounters(currentItemFrom,currentItemTo,totalItems){ //update de counterdisplay
	displayCurItem = currentItemFrom + " - " + currentItemTo + " van "; 
	$('curprojecten').update(displayCurItem);
	$('totaalprojecten').update(totalItems);
}
