var autoStartCases = false;
function plus3CasesObject() {
	this.currentCaseId = 0;
	this.prevCaseId = false;
	this.nextCaseId = false;
	this.swapCount = false;
	this.swapActive = false;
	this.cases = new Array();
	this.addPlus3Case = function (plus3CaseObject) {
		this.cases.push(plus3CaseObject);
	}
	this.hideCase = function(plus3CaseObjectId) {
		this.cases[plus3CaseObjectId].hide();
	}
	this.showCase = function(plus3CaseObjectId) {
		this.cases[plus3CaseObjectId].show();
	}
	this.setCurrent = function(plus3CaseObjectId) {
		this.currentCaseId = plus3CaseObjectId;
		this.setLinks();
	}
	this.init = function(plus3CaseObjectId) {
		if(!plus3CaseObjectId) {
			var plus3CaseObjectId = Math.round(Math.random()*(this.cases.length-1));
		}
		this.showCase(plus3CaseObjectId);
		this.setCurrent(plus3CaseObjectId);
		_self = this;
		jQuery("#next").click(function() {
			_self.nextCase();
		});
		jQuery("#prev").click(function() {
			_self.prevCase();
		});		
		this.setLinks();
		if(autoStartCases) {
			this.swap();
		}
	}
	this.getPrevCaseId = function() {
		this.prevCaseId = this.currentCaseId - 1;
		if(this.prevCaseId<0) {
			this.prevCaseId = this.cases.length-1;
		}
		return this.prevCaseId;
	}
	this.checkSwap = function(auto) {
		if(this.swapActive && !auto) {
			clearInterval(this.swapActive);
		}
		else {
			this.swapCount++;
			if(this.swapCount==3) {
				clearInterval(this.swapActive);
			}
		}
	}
	this.swap = function() {
		_self = this;
		this.swapActive = setInterval(function(){_self.nextCase(1)},4000);
	}
	this.getNextCaseId = function() {
		this.nextCaseId = this.currentCaseId + 1;
		if(this.nextCaseId>=this.cases.length) {
			this.nextCaseId = 0;
		}
		return this.nextCaseId;
	}
	this.setLinks = function() {
		jQuery("#more").attr("href",this.cases[this.currentCaseId].getUrl());
	}
	this.prevCase = function(auto) {
		this.checkSwap(auto);
		var tp = this.getPrevCaseId();
		this.hideCase(this.currentCaseId);
		this.showCase(tp);
		this.setCurrent(tp);
	}
	this.nextCase = function(auto) {
		this.checkSwap(auto);
		var tp = this.getNextCaseId();
		this.hideCase(this.currentCaseId);
		this.showCase(tp);
		this.setCurrent(tp);
	}
	this.getCurrent = function() {
		return this.currentCase;
	}
}
function plus3Case(caseObject) {
	this.htmlObject = caseObject;
	this.setHTML = function() {
	}
	this.getUrl = function() {
		return this.url;
	}
	this.setUrl = function(url) {
		this.url = url;
	}
	this.setId = function(id) {
		this.id = id;
		this.htmlObject.id = id;
	}
	this.hide = function() {
		 this.htmlObject.style.display = "none";
	}
	this.show = function() {
		 this.htmlObject.style.display = "block";
	}
	this.getId = function() {
		return this.htmlObject.id;
	}
}
function loadPlus3cases(casesObjectId) {
	var cases = new plus3CasesObject();
	var caseObjects = jQuery(".case");
	if(caseObjects.length>0) {
	for(i=0;i<caseObjects.length;i++) {
		var caseObject = new plus3Case(caseObjects[i]);
		caseObject.setId("case-"+i);
		a = jQuery("#case-"+i).find("a").first().attr("href");
		caseObject.setUrl(a);	
		cases.addPlus3Case(caseObject);
	}
	cases.init();
	}
}
function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
  }
 return myWidth;
}
function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myHeight = document.body.clientHeight;
  }
 return myHeight;
}
var resizeMode = 0;
var contentHeight = 0;
function resizeContents() {
	if(resizeMode==1) {
		var graphic = jQuery("#graphic");
		var footer = jQuery("#footer");
		var content = jQuery("#content");
		if(getWindowHeight()-contentHeight<410) {
			content.css("top","350px");
			content.css("bottom","auto");
			footer.css("top",370+(contentHeight)+ "px");
			footer.css("bottom","auto");
			graphic.css("height",430+(contentHeight)+ "px");
			graphic.addClass("small");
		}
		else {
			content.css("top","auto");
			content.css("bottom","70px");
			footer.css("top","auto");
			footer.css("bottom","25px");
			graphic.removeClass();
			graphic.css("height","100%");
		}
	}
	else if(resizeMode==2) {
		var graphic = jQuery("#graphic");
		var footer = jQuery("#footer");
		var header = jQuery("#een-greep-uit");
		var content = jQuery("#k-clients");
			header.css("top",(contentHeight)+ "px");
			content.css("top",(contentHeight)+ "px");
			content.css("bottom","auto");
			footer.css("top",(contentHeight+content.height()+50)+ "px");
			footer.css("bottom","auto");
			graphic.css("height",430+(contentHeight)+ "px");
			graphic.addClass("small");
	}
}
function initResizeContents() {
	var content = jQuery("#content");
	if(content.height()==null) {
		var cases = jQuery("#k-cases");
		contentHeight = 300+cases.height();
		resizeMode=2;
	}
	else {
		contentHeight = content.height();
		resizeMode=1;
	}
	resizeContents();	
}
window.onresize = function() {
	resizeContents();
}
window.onload = function() {
	loadPlus3cases("cases");
	initResizeContents();
}
