function searchCars(page) {
	page = typeof (page) != 'undefined' ? page : $('#as24xml_page').attr("value");
	$('#as24xml_page').attr("value", page);
	if(page != 'null') {
		$.cookie('page', page, {
			expires : 0,
			path : '/'
		});
	}

	$('#results tbody tr').remove();
	$('#results tbody').append('<tr><td colspan=7>' + labels['Fahrzeuge werden gesucht...'] + '</td></tr>');
	$.getJSON('/typo3conf/ext/ic_as24xml/res/ajax/as24.php', createSearchParams(), function(data, textStatus) {
		displayCars(data);
	});
}

function sortCars(sort) {
	$('#results thead img').remove();
	var sorticon = "/typo3conf/ext/ic_as24xml/res/img/sort_asc.png";
	var thesort = sort.replace('_asc', '');
	var thesort = thesort.replace('_desc', '');
	if($('#as24xml_sort').attr("value").indexOf('_asc') != -1) {
		$('#as24xml_sort').attr("value", sort.replace('_asc', '_desc'));
		sorticon = "/typo3conf/ext/ic_as24xml/res/img/sort_desc.png";
	} else {
		$('#as24xml_sort').attr("value", sort.replace('_desc', '_asc'));
		sorticon = "/typo3conf/ext/ic_as24xml/res/img/sort_asc.png";
	}

	$('#as24xml_sort_' + thesort).after(" <img src='" + sorticon + "' />");

	searchCars();
}

function getNextPage(page) {
	$('#as24xml_page').attr("value", page);
	searchCars();
}

function createSearchParams() {
	var data = Object();

	$.each($('#as24xml_searchmask input:text, #as24xml_searchmask input:hidden, #as24xml_searchmask input[type="radio"]:checked, #as24xml_searchmask select'), function(i, val) {

		switch(val.name) {
			case "price":
				var cval = val.value.replace(/CHF/g, "");
				cval = cval.replace(/</g, "");
				cval = cval.replace(/ /g, "");
				cval = cval.split("-");
				data['pricefrom'] = cval[0];
				data['priceto'] = cval[1];

				$.cookie('pricefrom', cval[0], {
					expires : 0,
					path : '/'
				});
				$.cookie('priceto', cval[1], {
					expires : 0,
					path : '/'
				});
				break;

			case "year":
				var cval = val.value.replace(/ /g, "");
				cval = cval.split("-");
				data['yearfrom'] = cval[0];
				data['yearto'] = cval[1];
				$.cookie('yearfrom', cval[0], {
					expires : 0,
					path : '/'
				});
				$.cookie('yearto', cval[1], {
					expires : 0,
					path : '/'
				});
				break;

			case "mileage":
				var cval = val.value.replace(/km/g, "");
				cval = cval.replace(/ /g, "");
				cval = cval.split("-");
				data['mileagefrom'] = cval[0];
				data['mileageto'] = cval[1];
				$.cookie('mileagefrom', cval[0], {
					expires : 0,
					path : '/'
				});
				$.cookie('mileageto', cval[1], {
					expires : 0,
					path : '/'
				});
				break;

			case "consumption":
				var cval = val.value.replace(/l/g, "");
				cval = cval.replace(/ /g, "");
				cval = cval.split("-");
				data['consumptionfrom'] = cval[0];
				data['consumptionto'] = cval[1];
				$.cookie('consumptionfrom', cval[0], {
					expires : 0,
					path : '/'
				});
				$.cookie('consumptionto', cval[1], {
					expires : 0,
					path : '/'
				});
				break;

			default:
				data[val.name] = val.value;
				$.cookie(val.name, val.value, {
					expires : 0,
					path : '/'
				});
				break;
		}

	});
	return data;
}

function displayCars(data) {
	if(data['error']) {
		$('#results tbody tr').remove();
		$('#results tbody').append('<tr><td colspan=7>' + data['error'] + '</td></tr>');
		$('.paging li').remove();
		$('.paging_count').text('-');
	} else {

		if(data['carcount'] == 500) {
			$('.paging_count').text(labels['uber'] + ' ' + data['carcount']);
		} else {
			$('.paging_count').text(data['carcount']);
		}
		//$('.paging_count').append('<br />');
		$('.paging li').remove();
		showpages = 7;
		if(data['paging']['@attributes']['page'] > 6) {
			start = parseInt(data['paging']['@attributes']['page']) - 5;
		} else {
			start = 1;
		}

		if(data['paging']['@attributes']['page'] == 1) {
			$('.paging ul').append('<li class="paging_first">' + labels['vorherige'] + '</li>');
		} else {
			$('.paging ul').append('<li class="paging_first"><a href="" onclick="javascript:getNextPage(' + (data['paging']['@attributes']['page'] - 1) + ');return false;">' + labels['vorherige'] + '</a></li>');
			if(start > 1) {
				$('.paging ul').append('<li><a href="" onclick="javascript:getNextPage(1);return false;">1</a> ... </li>');
			}
		}

		if(data['paging']['@attributes']['pagetotal'] < showpages) {
			showpages = parseInt(data['paging']['@attributes']['pagetotal']);
		}

		for(var i = start; i < start + (showpages); i++) {
			if(i <= 25) {
				if(i == data['paging']['@attributes']['page']) {
					$('.paging ul').append('<li><b>' + i + '</b></li>');
				} else {
					$('.paging ul').append('<li><a href="" onclick="javascript:getNextPage(' + i + ');return false;">' + i + '</a></li>');
				}
			}
		}
		if(data['paging']['@attributes']['page'] == data['paging']['@attributes']['pagetotal']) {
			$('.paging ul').append('<li>' + labels['nachste'] + '</li>');
		} else {
			nextpage = parseInt(data['paging']['@attributes']['page']) + 1;
			$('.paging ul').append('<li><a href="" onclick="javascript:getNextPage(' + nextpage + ');return false;">' + labels['nachste'] + '</a></li>');
		}

		$('#results tbody tr').remove();
		// Bei carcount == 1 generiert json_encode kein Array, deswegen die Abfrage.
		if(data.carcount == 1) {

			data.cars.car['price'] = number_format(parseInt(data.cars.car['price']), 0, ".", "'") + ".-";
			if(data.cars.car['cartype'] == 'Neuwagen') {
				data.cars.car['firstreg_year'] = data.cars.car['cartype'];
			}
			// leasingaktion auszeichnen
			showIcon = "";
			val = data.cars.car;
			extrainfo = String(val['extrainfo']);
			
			/*
			if(extrainfo.search("4.44") != -1 || extrainfo.search("4,44") != -1) {
				showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '44);" onmouseout="hideThumb(' + val['id'] + '44);"><img src="/typo3conf/ext/ic_as24xml/res/img/speedleasing_12x12.gif"/></a><div id="info_' + val['id'] + '44" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/444_speedleasing.gif" /></div>';
				if(val['thumbnail'].length) {
					showIcon = '&nbsp;&nbsp;' + showIcon;
				}
			}
			if(extrainfo.search("2.22") != -1 || extrainfo.search("2,22") != -1) {
				showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '66);" onmouseout="hideThumb(' + val['id'] + '66);"><img src="/typo3conf/ext/ic_as24xml/res/img/speedleasing_12x12.gif"/></a><div id="info_' + val['id'] + '66" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/222_speedleasing.gif" /></div>';
				if(val['thumbnail'].length) {
					showIcon = '&nbsp;&nbsp;' + showIcon;
				}
			}
			*/
			if(extrainfo.search("4.91") != -1 || extrainfo.search("4,91") != -1) {
				showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '44);" onmouseout="hideThumb(' + val['id'] + '44);"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing.png"/></a><div id="info_' + val['id'] + '44" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing_big.png" /></div>';
				if(val['thumbnail'].length) {
					showIcon = '&nbsp;&nbsp;' + showIcon;
				}
			}
			if(extrainfo.search("2.91") != -1 || extrainfo.search("2,91") != -1) {
				showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '66);" onmouseout="hideThumb(' + val['id'] + '66);"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing.png"/></a><div id="info_' + val['id'] + '66" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing_big_291.png" /></div>';
				if(val['thumbnail'].length) {
					showIcon = '&nbsp;&nbsp;' + showIcon;
				}
			}
			/*
			if(extrainfo.search("Christmas Shopping") != -1 || extrainfo.search("Christmas shopping") != -1) {
				showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '66);" onmouseout="hideThumb(' + val['id'] + '66);"><img src="/typo3conf/ext/ic_as24xml/res/img/xmas-icon-16x16.png"/></a><div id="info_' + val['id'] + '66" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/xmas-icon-200x200.png" /></div>';
				if(val['thumbnail'].length) {
					showIcon = '&nbsp;&nbsp;' + showIcon;
				}
			}
			*/
			if(data.cars.car['thumbnail'] != 'http://www.autoscout24.ch/images/img_no_photo_100_75.png') {

				$('#results tbody').append('<tr><td><a href="' + location.pathname + '?action=detail&carid=' + data.cars.car['id'] + '"><b>' + data.cars.car['makemodel'] + '</b></a></td><td><a href="' + location.href + '?action=detail&carid=' + data.cars.car['id'] + '" onmouseover="showThumb(' + data.cars.car['id'] + ');" onmouseout="hideThumb(' + data.cars.car['id'] + ');"><img src="/typo3conf/ext/ic_as24xml/res/img/camera.gif"/></a><div id="info_' + data.cars.car['id'] + '" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="' + data.cars.car['thumbnail'] + '" /></div>' + showIcon + '</td><td>' + data.cars.car['bodytype'] + '</td><td>' + data.cars.car['firstreg_year'] + '</td><td>' + data.cars.car['bodycol'] + '</td><td>' + data.cars.car['mileage'] + '</td><td>' + data.cars.car['price'] + '</td></tr>');

			} else {
				$('#results tbody').append('<tr><td><a href="' + location.pathname + '?action=detail&carid=' + data.cars.car['id'] + '"><b>' + data.cars.car['makemodel'] + '</b></a></td><td>' + showIcon + '</td><td>' + data.cars.car['bodytype'] + '</td><td>' + data.cars.car['firstreg_year'] + '</td><td>' + data.cars.car['bodycol'] + '</td><td>' + data.cars.car['mileage'] + '</td><td>' + data.cars.car['price'] + '</td></tr>');

			}

		} else {
			if(data['cars']['car'].length) {
				$.each(data['cars']['car'], function(i, val) {

					//function number_format (number, decimals, dec_point, thousands_sep)
					val['price'] = number_format(parseInt(val['price']), 0, ".", "'") + ".-";
					if(val['cartype'] == 'Neuwagen') {
						val['firstreg_year'] = val['cartype'];
					}
					if(val.bodytype.length == undefined) {
						val.bodytype = "";
					}
					// leasingaktion auszeichnen
					showIcon = "";
					extrainfo = String(val['extrainfo']);
					/*
					if(extrainfo.search("4.44") != -1 || extrainfo.search("4,44") != -1) {
						showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '44);" onmouseout="hideThumb(' + val['id'] + '44);"><img src="/typo3conf/ext/ic_as24xml/res/img/speedleasing_12x12.gif"/></a><div id="info_' + val['id'] + '44" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/444_speedleasing.gif" /></div>';
						if(val['thumbnail'].length) {
							showIcon = '&nbsp;&nbsp;' + showIcon;
						}
					}
					if(extrainfo.search("2.22") != -1 || extrainfo.search("2,22") != -1) {
						showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '66);" onmouseout="hideThumb(' + val['id'] + '66);"><img src="/typo3conf/ext/ic_as24xml/res/img/speedleasing_12x12.gif"/></a><div id="info_' + val['id'] + '66" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/222_speedleasing.gif" /></div>';
						if(val['thumbnail'].length) {
							showIcon = '&nbsp;&nbsp;' + showIcon;
						}
					}
					*/
					if(extrainfo.search("4.91") != -1 || extrainfo.search("4,91") != -1) {
						showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '44);" onmouseout="hideThumb(' + val['id'] + '44);"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing.png"/></a><div id="info_' + val['id'] + '44" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing_big.png" /></div>';
						if(val['thumbnail'].length) {
							showIcon = '&nbsp;&nbsp;' + showIcon;
						}
					}
					if(extrainfo.search("2.91") != -1 || extrainfo.search("2,91") != -1) {
						showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '66);" onmouseout="hideThumb(' + val['id'] + '66);"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing.png"/></a><div id="info_' + val['id'] + '66" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/icon_swissleasing_big_291.png" /></div>';
						if(val['thumbnail'].length) {
							showIcon = '&nbsp;&nbsp;' + showIcon;
						}
					}
					/*
					if(extrainfo.search("Christmas shopping") != -1 || extrainfo.search("Christmas Shopping") != -1) {
						showIcon = '<a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + '66);" onmouseout="hideThumb(' + val['id'] + '66);"><img src="/typo3conf/ext/ic_as24xml/res/img/xmas-icon-16x16.png"/></a><div id="info_' + val['id'] + '66" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="/typo3conf/ext/ic_as24xml/res/img/xmas-icon-200x200.png" /></div>';
						if(val['thumbnail'].length) {
							showIcon = '&nbsp;&nbsp;' + showIcon;
						}
					}
					*/
					//console.log(val['thumbnail']);
					if(val['thumbnail'] != 'http://www.autoscout24.ch/images/img_no_photo_100_75.png') {
						$('#results tbody').append('<tr><td><a href="' + location.pathname + '?action=detail&carid=' + val['id'] + '"><b>' + val['makemodel'] + '</b></a></td><td><a href="' + location.href + '?action=detail&carid=' + val['id'] + '" onmouseover="showThumb(' + val['id'] + ');" onmouseout="hideThumb(' + val['id'] + ');"><img src="/typo3conf/ext/ic_as24xml/res/img/camera.gif"/></a><div id="info_' + val['id'] + '" style="visibility: hidden; position: absolute; margin-top: 15px;"><img src="' + val['thumbnail'] + '" /></div>' + showIcon + '</td><td>' + val['bodytype'] + '</td><td>' + val['firstreg_year'] + '</td><td>' + val['bodycol'] + '</td><td>' + val['mileage'] + '</td><td>' + val['price'] + '</td></tr>');

					} else {
						$('#results tbody').append('<tr><td><a href="' + location.pathname + '?action=detail&carid=' + val['id'] + '"><b>' + val['makemodel'] + '</b></a></td><td>' + showIcon + '</td><td>' + val['bodytype'] + '</td><td>' + val['firstreg_year'] + '</td><td>' + val['bodycol'] + '</td><td>' + val['mileage'] + '</td><td>' + val['price'] + '</td></tr>');

					}
				});
			} else {
				$('#results tbody').append('<tr><td colspan=7><a href="?carid=' + val['id'] + '">' + data['cars']['car']['makemodel'] + '</a></td></tr>');
			}

		}

	}
}

function showThumb(id) {
	document.getElementById("info_" + id).style.visibility = "visible";
}

function hideThumb(id) {
	document.getElementById("info_" + id).style.visibility = "hidden";
}
