$().ready(function() {
	$('#ref-slideshow').serialScroll({
		items:'li',
		prev:'#tpl2,#tplu2', 
		next:'#tpr2,#tpru2', 
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1200,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: false //click on the images to scroll to them
		//exclude:1
	});
	
	//console.log($('#ref_teaser li').length);
});


gefilterteRefs = new Array();

var prods = new Array();
var prodgr = new Array();
var obj = new Array();
var land = new Array();
var datum = new Array();

var valProdgr = 0;
var valProd = 0;
var valObj = 0;
var valLand = 0;
var valDat = 0;


function reset(){
	document.location.href=self.location;
}
	

function sort() {
    var sortiert = false;  
    hilfsvariable = new Array();
    while (!sortiert) {	
      sortiert = true;
      for(var i = 0; i < prods.length-1; i++){
     	if (prods[i]['titel'] > prods[i+1]['titel']) {
            sortiert = false;
            hilfsvariable[0] = prods[i];
            prods[i] = prods[i+1];
            prods[i+1] = hilfsvariable[0];
         }
      }
      
     
      
      for(var i = 0; i < obj.length-1; i++){
     	if (obj[i]['titel'] > obj[i+1]['titel']) {
            sortiert = false;
            hilfsvariable[0] = obj[i];
            obj[i] = obj[i+1];
            obj[i+1] = hilfsvariable[0];
         }
      }
      
      for(var i = 0; i < land.length-1; i++){
     	if (land[i] > land[i+1]) {
            sortiert = false;
            hilfsvariable[0] = land[i];
            land[i] = land[i+1];
            land[i+1] = hilfsvariable[0];
         }
      }
   }
}
function bastel (refs, sender){

	filterProds = new Array();
	filterProdgr = new Array();
	filterObj = new Array();
	filterLand = new Array();
	filterDatum = new Array();


	strland = "";
	strobj = "";
	strprods = "";
	strprodgr = "";
	strdatum = "";
	
		
	if(sender == 'all'){
		prodgr = prodgrGesamt;
		prods = prodsGesamt;
		obj = objGesamt;
		land = landGesamt;
		
		datum = new Array();
		for (i=0;i<datumGesamt.length;i++) {
			datum[datum.length] = datumGesamt[i]['val'];
		}
		
		gefilgr = false;
		gefilprods = false;
		gefilobj = false;
		gefilland = false;
		gefildat = false;

	}else{	
		for (z=0;z<refs.length;z++) {
			if(sender != 'prodgroup'){
				for (y=0;y < refs[z]['prodgr'].length;y++) {
					if (strprodgr.indexOf(refs[z]['prodgr'][y]['id']) == -1) {
						strprodgr = strprodgr + refs[z]['prodgr'][y]['id'] + ", " ;
						filterProdgr[filterProdgr.length] = refs[z]['prodgr'][y];
					}
				}
				gefilgr = true;
				prodgr = filterProdgr;
			}
		
			if(sender != 'produkt'){
				for (y=0;y < refs[z]['prod'].length;y++) {
					if (strprods.indexOf(refs[z]['prod'][y]['id']) == -1) {
						strprods = strprods + refs[z]['prod'][y]['id'] + ", " ;
						filterProds[filterProds.length] = refs[z]['prod'][y];
					}	
				}	
			
				gefilprods = true;
				prods = filterProds;
			}
						
			if(sender != 'objekt'){
				if (strobj.indexOf(refs[z]['obj']['id']) == -1) {
					strobj = strobj + refs[z]['obj']['id'] + ", " ;
					filterObj[filterObj.length] = refs[z]['obj'];
				}
				gefilobj = true;
				obj = filterObj;
			}
						
			if(sender != 'country'){
				if (strland.indexOf(refs[z]['loc']) == -1) {
					strland = strland + refs[z]['loc'] + ", " ;
					filterLand[filterLand.length] = refs[z]['loc'];
				}
				gefilland = true;
				land = filterLand;
			}
		}
		if(sender != 'datum'){
			fillDat(refs);
		}
	}
	
	sort();
	html();
}

function fillDat(refsd) {

	add1 = false;
	add2 = false;
	add3 = false;
	add4 = false;
	add5 = false;
	
	for (z=0;z<refsd.length;z++) {
		diff = 0;
		if(refsd[z]['dat']>0){
			diff = alter(refsd[z]['dat']);
			if(diff == 0){
				add1 = true;
			}else if(diff >=1 && diff <=5){
				add2 = true;
			}else if(diff >5 && diff <=10){
				add3 = true;
			}else if(diff >10 && diff <=20){
				add4 = true;
			}else if(diff >20){
				add5 = true;
			}
		}
	}
	datum = addDat(add1,add2, add3, add4, add5);
	
	return datum;
}

function alter(jahr){
	d = new Date();
	al = 0;
	var dj = d.getYear();
	if (dj < 999){
	  dj += 1900;
	}
	al = dj - jahr;
	
	return al;
}

function addDat(add1,add2, add3, add4, add5){
	
	datum = new Array();
	for (i=0;i<datumGesamt.length;i++) {
		if((datumGesamt[i]['key'] == "dieses Jahr" && add1)
		|| (datumGesamt[i]['key'] == "1-5 Jahre" && add2)
		|| (datumGesamt[i]['key'] == "6-10 Jahre" && add3)
		|| (datumGesamt[i]['key'] == "11-20 Jahre" && add4)
		|| (datumGesamt[i]['key'] == "> als 20 Jahre" && add5)){
			datum[datum.length] = datumGesamt[i]['val'];
		}
	}
	return datum;
}



function html(){
	var cols = new Array();	
	var pro = "";
	var arrayprodgr = new Array();	
	var arrayprod = new Array();
	var arrayobj = new Array();
	var arrayland = new Array();
	var arraydat = new Array();


	if(document.getElementById('prodgroup').value == "0"){
		arrayprodgr = prodgr;
	}else{
		arrayprodgr = prodgrGesamt;
	}
	
	var newTrProdgr = document.createElement( "TR" );
	var newTdCol1Pg = document.createElement( "TD" );
	var newTdCol2Pg = document.createElement( "TD" );
	
	newTdCol1Pg.innerHTML = "<b>"+document.getElementById("lang_prodgruppe").value+"</b>";	
	pro = "";
	yxpgr = 'prodgroup';
	pro = "<select id=prodgroup name=prodgroup style='width:380px;' onchange='change(yxpgr);'><option value='0'>-</option>";
	for(i=0;i < arrayprodgr.length;i++) {
		if(valProdgr == arrayprodgr[i]['id']){
			pro = pro + "<option value='" + arrayprodgr[i]['id'] + "' selected='selected'>" + arrayprodgr[i]['titel'] + "</option>";
		}else{
			pro = pro + "<option value='" + arrayprodgr[i]['id'] + "'>" + arrayprodgr[i]['titel'] + "</option>";
		}				
	}
	pro = pro + "</select>";
	newTdCol2Pg.innerHTML = pro;
	newTrProdgr.appendChild(newTdCol1Pg);
	newTrProdgr.appendChild(newTdCol2Pg);
	cols[cols.length]=newTrProdgr;
	
	
	if(document.getElementById('objekt').value == "0"){
		arrayobj = obj;
	}else{
		arrayobj = objGesamt;
	}
	var newTrObj = document.createElement( "TR" );
	var newTdCol1O = document.createElement( "TD" );
	var newTdCol2O = document.createElement( "TD" );
	
	newTdCol1O.innerHTML = "<b>"+document.getElementById("lang_objtyp").value+"</b>";	
	pro = "";
	yxo = 'objekt';
	pro = "<select id='objekt' name='objekt' style='width:380px;' onchange='change(yxo);'><option value='0'>-</option>";
	for (z=0;z < arrayobj.length;z++) {
		if(valObj == arrayobj[z]['id']){
			pro = pro + "<option value='" + arrayobj[z]['id'] + "' selected='selected'>" + arrayobj[z]['titel'] + "</option>";
		}else{
			pro = pro + "<option value='" + arrayobj[z]['id'] + "'>" + arrayobj[z]['titel'] + "</option>";
		}			
	}
	pro = pro + "</select>";
	newTdCol2O.innerHTML = pro;
	newTrObj.appendChild(newTdCol1O);
	newTrObj.appendChild(newTdCol2O);
	cols[cols.length]=newTrObj;
	
	
	if(document.getElementById('produkt').value == "0"){
		arrayprod = prods;
	}else{
		arrayprod = prodsGesamt;
	}
	var newTrProd = document.createElement( "TR" );
	var newTdCol1P = document.createElement( "TD" );
	var newTdCol2P = document.createElement( "TD" );
	
	newTdCol1P.innerHTML = "<b>"+document.getElementById("lang_produkt").value+"</b>";	
	pro = "";
	yxp = 'produkt';
	pro = "<select id='produkt' name='produkt' style='width:380px;' onchange='change(yxp);'><option value='0'>-</option>";
	for(i=0;i < arrayprod.length;i++) {
		if(valProd == arrayprod[i]['id']){
			pro = pro + "<option value='" + arrayprod[i]['id'] + "' selected='selected'>" + arrayprod[i]['titel'] + "</option>";
		}else{
			pro = pro + "<option value='" + arrayprod[i]['id'] + "'>" + arrayprod[i]['titel'] + "</option>";
		}			
	}
	pro = pro + "</select>";
	newTdCol2P.innerHTML = pro;
	newTrProd.appendChild(newTdCol1P);
	newTrProd.appendChild(newTdCol2P);
	cols[cols.length]=newTrProd;
		
	
	if(document.getElementById('country').value == "-"){
		arrayland = land;
	}else{
		arrayland = landGesamt;
	}
	var newTrCountry = document.createElement( "TR" );
	var newTdCol1C = document.createElement( "TD" );
	var newTdCol2C = document.createElement( "TD" );
	
	newTdCol1C.innerHTML = "<b>"+document.getElementById("lang_land").value+"</b>";	
	pro = "";
	yxc = 'country';
	pro = "<select id='country' name='country' style='width:380px;' onchange='change(yxc);'><option value='-'>-</option>";
	for(z=0;z < arrayland.length;z++) {
		if(valLand == arrayland[z]){
			pro = pro + "<option value='" + arrayland[z] + "' selected='selected'>" + arrayland[z] + "</option>";
		}else{
			pro = pro + "<option value='" + arrayland[z] + "'>" + arrayland[z] + "</option>";
		}	
	}
	pro = pro + "</select>";
	newTdCol2C.innerHTML = pro;
	newTrCountry.appendChild(newTdCol1C);
	newTrCountry.appendChild(newTdCol2C);
	cols[cols.length]=newTrCountry;
	

	if(document.getElementById('datum').value == "-"){
		arraydat = datum;
	}else{
		arraydat = new Array();
		for (i=0;i<datumGesamt.length;i++) {
			arraydat[arraydat.length] = datumGesamt[i]['val'];
		}
	}
	var newTrDat = document.createElement( "TR" );
	var newTdCol1D = document.createElement( "TD" );
	var newTdCol2D = document.createElement( "TD" );
	
	newTdCol1D.innerHTML = "<b>"+document.getElementById("lang_datum").value+"</b>";	
	pro = "";
	yxd = 'datum';
	pro = "<select id='datum' name='datum' style='width:380px;' onchange='javascript:change(yxd);'><option value='-'>-</option>";
	for(z=0;z < arraydat.length;z++) {
		if(valDat == arraydat[z]){
			pro = pro + "<option value='" + arraydat[z] + "' selected='selected'>" + arraydat[z] + "</option>";
		}else{
			pro = pro + "<option value='" + arraydat[z] + "'>" + arraydat[z] + "</option>";
		}			
	}
	pro = pro + "</select>";
	newTdCol2D.innerHTML = pro;
	newTrDat.appendChild(newTdCol1D);
	newTrDat.appendChild(newTdCol2D);
	cols[cols.length]=newTrDat;
		
	var oTableBody = document.getElementById("combo");
	while (oTableBody.firstChild){
	   oTableBody.removeChild(oTableBody.firstChild);
	}
		
	for(var icol=0;icol<cols.length;icol++){
		oTableBody.appendChild(cols[icol]);
	}
}





function filtern() {
		
	var refs = new Array();
	var del = false;
	
	for(i=0;i<refsGesamt.length;i++){
		refs[refs.length] = refsGesamt[i];
	}
		
	if(document.getElementById('prodgroup').value != "0"){
		val = document.getElementById('prodgroup').value;
		for (z=0;z<refs.length;z++) {		
			for (y=0;y < refs[z]['prod'].length;y++) {
				if (refs[z]['prod'][y]['idgr'] == val) {
					del = false;
					break;
				}else{
					del = true;
				}	
			}
			
			if(del){
				refs.splice(z, 1);
				z = z-1;
			}
		}
	}

	del = false;
	if(document.getElementById('objekt').value != "0"){
		val = document.getElementById('objekt').value;
		for (z=0;z<refs.length;z++) {
			if (refs[z]['obj']['id'] == val) {
				del = false;
			}else{
				del = true;
			}
			
			if(del){
				refs.splice(z, 1);
				z = z-1;
			}
		}
	}
	
	del = false;
	if(document.getElementById('produkt').value != "0"){
		val = document.getElementById('produkt').value;
		for (z=0;z<refs.length;z++) {		
			for (y=0;y < refs[z]['prod'].length;y++) {
				if (refs[z]['prod'][y]['id'] == val) {
					del = false;
					break;
				}else{
					del = true;
				}
			}
			
			if(del){
				refs.splice(z, 1);
				z = z-1;
			}
		}
	}
	
	del = false;
	if(document.getElementById('country').value != "-"){
		val = document.getElementById('country').value;
		for (z=0;z<refs.length;z++) {
			if (refs[z]['loc'] == val) {
				del = false;
			}else{
				del = true;
			}
			
			if(del){
				refs.splice(z, 1);
				z = z-1;
			}
		}
	}
	
	del = false;
	
	dats = new Array();
	for (i=0;i<datumGesamt.length;i++) {
		dats[dats.length] = datumGesamt[i]['val'];
	}
	if(document.getElementById('datum').value != "-"){
		val = document.getElementById('datum').value;
		for (z=0;z<refs.length;z++) {
			if(refs[z]['dat'] != 0){
				alt = alter(refs[z]['dat']);
				for (i=0;i<datumGesamt.length;i++) {
					if(val == datumGesamt[i]['val']){
						if(datumGesamt[i]['key'] == "dieses Jahr" && alt == 0){
							del = false;
						}else if(datumGesamt[i]['key'] == "1-5 Jahre" && (alt >=1 && alt <=5)){
							del = false;
						}else if(datumGesamt[i]['key'] == "6-10 Jahre" && (alt >5 && alt <=10)){
							del = false;
						}else if(datumGesamt[i]['key'] == "11-20 Jahre" && (alt >10 && alt <=20)){
							del = false;
						}else if(datumGesamt[i]['key'] == "> als 20 Jahre" && (alt > 20)){
							del = false;
						}else{
							del = true;
						}
					}
				}
			}else{
				del = true;
			}
						
			if(del){
				refs.splice(z, 1);
				z = z-1;
			}
		}
	}
	
	return refs;
}



function change(sender) {

	valProdgr = document.getElementById('prodgroup').value;
	valProd = document.getElementById('produkt').value;
	valObj = document.getElementById('objekt').value;
	valLand = document.getElementById('country').value;
	valDat = document.getElementById('datum').value;
	
	val = document.getElementById(sender).value;
	
	if(document.getElementById('prodgroup').value == "0" 
	&& document.getElementById('objekt').value == "0"
	&& document.getElementById('produkt').value == "0"
	&& document.getElementById('country').value == "-"
	&& document.getElementById('datum').value == "-"){
	
		bastel(refsGesamt, 'all');
		
		prods = new Array();
		prodgr = new Array();
		obj = new Array();
		land = new Array();
		datum = new Array();
		gefilterteRefs = new Array();
		
	}else{	
		gefilterteRefs = filtern();
		bastel(gefilterteRefs, sender);	
	}
}