$('#map').hide();
$('#map_in').hide();

function firstVisit () {
	$('#map').hide();
	$.ajax({
		type: "POST",
		url: "ajaxResponder.php",
		data: "hash=startseite&action=nav",
		success: function(response) {
		
			$("div.accordionContent").slideUp("slow");
			$("div.accordionContent").fadeOut();
			$('#startseite').nextAll("div.accordionContent").first('div.accordionContent').show();
			setTimeout(function () {
				$('#startseite').nextAll("div.accordionContent").first('div.accordionContent').html(response);
				
			},800);
		} 			
	});
}

function pageload(hash) {
	
		$('div.div_klick').each(function(){
			var id = $(this).attr("id");
			var new_id = id.replace("active","neutral");
			$(this).attr("id",new_id);
		});
		
		pageTracker._trackPageview("AJAX-Call: Seite " + hash);

		if(hash) {		
			if($.browser.msie) {
				hash = encodeURIComponent(hash);
			}
			hash = hash.replace("!","");
			
			$.ajax({
				type: "POST",
				url: "ajaxResponder.php",
				data: "hash="+hash+"&action=nav&prefix="+prefix+"&inc="+include,
				success: function(response) {
					if(response) {
						$("div.accordionContent").slideUp("slow");
						$("div.accordionContent").fadeOut(0,function(){
							setTimeout(function () {
								
								if(hash != "kontakt" & hash != "impressum" & hash != "sitemap" & hash != "galerie" & hash != "gal_individualisierung" & hash != "gal_farbgebung" & hash != "gal_werbung"  ) {
									$('#'+hash).nextAll("div.accordionContent").first('div.accordionContent').html(response);
									$('#'+hash).nextAll("div.accordionContent").first('div.accordionContent').slideDown(1000);
									$('.highlight_gal_thumb').fadeTo(0,0.3);
	
									if(hash == "haendler") {
										$('#map').fadeIn('slow');
										$('#map_in').fadeIn('slow');
									} else {
										$('#map').hide();
										$('#map_in').hide();
									}
								} else {	
									$('#map').hide();
									$('#map_in').hide();
									$('#last_accordion').html(response); 
									$('#last_accordion').slideDown(1000);
								}
	
							},400);
						});
						
					} else {
						$("div.accordionContent").slideUp();
						
					}
				} 			
			});
			
			var active = hash;
			if(active == "gal_individualisierung") {
				active = "galerie";
			}
			if(active == "gal_farbgebung") {
				active = "galerie";
			}
			if(active == "gal_werbung") {
				active = "galerie";
			}
			if(active != "" && active != "impressum" && active != "kontakt") {
				var active_id = $('#'+active).find("div").attr("id");
				var new_active_id = active_id.replace("neutral","active");
				$('#'+active).find("div").attr("id",new_active_id);
			} 
			
		} else {
			pageload("startseite");
			
		}
		
		
}

$("a[rel='ajax']").live("click", function(){	
	var hash = this.href;
	hash = hash.replace(/^.*#/, '');
	$.historyLoad(hash);
	return false;
});

$('.ajaxPopup').live("mouseover mouseout", function(event) {
	if(event.type == 'mouseover') {
		var id = $(this).attr("target");
		if(id != "") {
			$(this).next("img").css("border","1px solid white").css("margin","-1px");
			$("div#"+id).show();
		}
	} else {
		var id = $(this).attr("target");
		if(id != "") {
			$(this).next("img").css("border","none").css("margin","0px");
			$("div#"+id).hide();
		}
	}
});

$('.car_desc_hover').live("mouseover mouseout", function(event) {
	if(event.type == 'mouseover') {
		$(this).css("border","1px solid white").css("margin","-1px");
	} else {
		$(this).css("border","none").css("margin","0px");
	}
});


$('.textPlaceholder').live("mouseover mouseout", function(event) {
	if(event.type == 'mouseover') {
		var id = $(this).attr("id");
		$(this).prev("img").css("border","1px solid white").css("margin","-1px");
	} else {
		var id = $(this).attr("id");
		$(this).prev("img").css("border","none").css("margin","0px");
	}
});


$('.gal_link').live("click",function(){
	var referer = $(this).attr("title");
	
	$('.gal_link').css("background","url(../gfx/buttons/button_green.png) no-repeat center 10px !important");
	$('#gal_ajax_content').fadeOut(function(){
		$('#loading').fadeIn();
	});
	setTimeout(function(){
		$.ajax({
			type: "POST",
			url: "ajaxResponder.php",
			data: "hash="+referer+"&action=gal",
			success: function(response) {
			$('#loading').fadeOut(function(){
				$('#gal_ajax_content').fadeOut();
				$('#gal_ajax_content').html(response);
				$('#gal_ajax_content').fadeIn();
			});
			} 			
		});
	},700);
});

$('form#formular').live("submit",function() {
	if($('input#ort').val() != "" && $('input#plz').val() != "" && $('#message').val() != "") {
		$.ajax({
			type: "POST",
			url: "ajaxFormular.php",
			data: $(this).serialize(),
			success: function(response) {
				if(response = "1") {
					$('div.true').html();
					$('input#absenden').attr("disabled","true");
					$('div#empty').html("").hide();
					$('ul#kontakt_form').prepend("<div class=true>Erfolgreich abgeschickt</div>");
				}
			} 			
		});
	} else {
		return false;
	}
	return false;
});

$('.highlight_gal_thumb').live("click",function(){
	var big_image = $(this).attr("alt");
	var img_con = $(this).attr("id");
	$('.highlight_gal_thumb').fadeTo(0,0.3).css("border","none");
	$('.highlight_gal_thumb').parent("li").css("border","0px");
	$(this).fadeTo(0,1).parent("li").css("border","1px solid white").css("margin-top","255px");
	$("#"+img_con).attr("src", big_image);
});
