$(document).ready(function() {
   var kfzAjaxConnector = '/module/kfz/ajax.php';
   $(".kfz_paginator a").click(function() {
   		var page = $(this).attr("name").replace("getPage_", "");
   		var current_content_id = $("#current_content_id").val();
 		var kfz_content_id = $("#kfz_content_id").val();
 		var currentElement = $(this);
   		$.post(
				kfzAjaxConnector, 
				{ 
					action: 'getPage',
					page: page,
					current_content_id: current_content_id,
					kfz_content_id: kfz_content_id
				}, 
				function(data) { 				
					$("#kfz_results").html(data.content); 
					$(".kfz_paginator a").each(function() {
						if ($(this).hasClass("inactive")) {
							$(this).removeClass("inactive");
							$(this).addClass("active"); 							
						}
					});
					currentElement.removeClass("active");
					currentElement.addClass("inactive"); 
				}, 			
				"json"
			);
   		return false;
   });
   $("#marke").change(function() {
			updateModelle();
		}
   );
   function updateModelle() {
   		$.post(
				kfzAjaxConnector, 
				{ 
					action: 'getModelle',
					marke: $("#marke").val() 
				}, 
				function(data) { 				
					if (data.modelle != '') {
						var output = [];						
						$.each(data.modelle, function(key, value) {
						  var selected = "";
						  if ($("#searchvalueModell").val() == key) var selected = " selected";
						  output.push('<option value="'+ key +'"' + selected + '>'+ value +'</option>');
						});						
						$('#modell').html('<option>-</option>' + output.join(''));
	
					} else {
						$('#modell').html('<option>-</option>');
					}
				}, 			
				"json"
			);
   }
   if ($("#marke").val() != '-') {
   		updateModelle(); 
   }
   $(".toggleSuchformular").click(function() {
		$("#kfz_suchformular").slideToggle('slow', function() {});
   });
   $("#detailimageLink").click(function() {
   		var hrefCheck = $(this).attr("href");
   		$(".kfzbilders").each(function() {
   			if (hrefCheck == $(this).attr("href")) {
   				$(this).trigger('click');  
   			}
   		});
   		return false;
   });
   $(".detailthumb").click(function() {
   		$("#detailimageLink").attr("href", $(this).attr("src").replace("w=290","w=600"));
   		$("#detailimage").attr("src", $(this).attr("src"));
   });
   $(".kfz_empfehlen").click(function() {
 		var gwnr = $("#kfz_detail_id").val();
 		var kfz_content_id = $("#kfz_content_id").val();
 		var current_content_id = $("#current_content_id").val();
		$.post(kfzAjaxConnector, 
			   { action: 'getKfzName', gwnr: gwnr}, 
			   function(data) { 
					$("#empfehlung_fahrzeug").html(data.content);
			   }, 
			   'json');	
		$("#empfehlenDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 350,
			width: 500,
			modal: true,
			buttons: {
				Absenden: function() {		
					$.post(kfzAjaxConnector,
						   { action: 'sendEmpfehlung',
						   	 kfz_content_id: kfz_content_id,
						   	 current_content_id: current_content_id,
						     gwnr: gwnr,
						     formdata: $("#empf_form").serialize() },
						   function(data) {
						   		if (data.success == true) {
						   			alert('Vielen Dank, die Empfehlung wurde versandt!');
						   			$("#empfehlenDialog").dialog('close');
						   		} else {
						   			$("#form_meldung").html('Fehler: Bitte prüfen Sie Ihre Eingaben!<br /><br />');
						   		}
						   }, 
						   'json');		
				},
				Abbrechen: function() { $(this).dialog('close'); return false; }
			}
		});
		$("#empfehlenDialog").dialog('open');	  		
   });
   $(".kfz_anfragen").click(function() {
   		var gwnr = $("#kfz_detail_id").val();
 		var kfz_content_id = $("#kfz_content_id").val();
 		var current_content_id = $("#current_content_id").val();
		$.post(kfzAjaxConnector, 
			   { action: 'getKfzName', gwnr: gwnr}, 
			   function(data) { 
					$("#fzg").html(data.content + " (" + gwnr + ")");
			   }, 
			   'json');	 		
 		$("#anfrageDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 480,
			width: 600,
			modal: true,
			buttons: {
				Absenden: function() {
					$.post(kfzAjaxConnector,
						   { action: 'sendAnfrage',
						   	 kfz_content_id: kfz_content_id,
						   	 current_content_id: current_content_id,
						     gwnr: gwnr,
						     formdata: $("#anfrage_form").serialize() },
						   function(data) {
						   		if (data.success == true) {
						   			alert('Vielen Dank, die Anfrage wurde versandt!');
						   			$("#anfrageDialog").dialog('close');
						   		} else {
						   			$("#form_meldung_anfrage").html('Fehler: Bitte prüfen Sie Ihre Eingaben!<br /><br />');
						   		}
						   }, 
						   'json');						
				},
				Abbrechen: function() { $(this).dialog('close'); return false; }
			}
		});
		$("#anfrageDialog").dialog('open');
   });
   $("#anfrage-tabs").tabs({ selected: 0 });
   $(".erweiterteSucheToggle").click(function() {
		$("#suchFormTBody").slideDown();
		return false;
	});
});
