$(document).ready(function() {

	// put the labels inside the form elements
	var handleFormTips = function() {
		$('form input.tipped, form textarea.tipped').each(function() {
			var elem = $(this);
			// get label value for that form element
			var lv = $(this).attr('title');
			// handle events
			if($(elem).is('textarea')) {
				$(elem).bind('focus', function() {
					if($(elem).text() == lv) {
						$(elem).text('').removeClass('tipped');
					}
				});
				$(elem).bind('blur', function() {
					if($(elem).text() == '') {
						$(elem).text(lv).addClass('tipped');
					}
				});
				if($(elem).text() == '') {
					$(elem).text(lv).addClass('tipped');
				} else {
					$(elem).removeClass('tipped');
				}
			} else {
				$(elem).bind('focus', function() {
					if($(elem).val() == lv) {
						$(elem).val('').removeClass('tipped');
					}
				});
				$(elem).bind('blur', function() {
					if($(elem).val() == '') {
						$(elem).val(lv).addClass('tipped');
					}
				});
				if($(elem).val() == '') {
					$(elem).val(lv).addClass('tipped');
				} else {
					$(elem).removeClass('tipped');
				}
			}
		});
		// prepare form
		$('form').each(function() {
			var f = $(this);
			if($('input.tipped, textarea.tipped', this).length > 0) {
				$(f).submit(function() {
					// untip 'em
					$('input.tipped, textarea.tipped', this).each(function() {
						var elem = $(this);
						var lv = $(this).attr('title');
						if($(elem).is('textarea')) {
							if($(elem).text() == lv) {
								$(elem).text('').removeClass('tipped');
							}
						} else {
							if($(elem).val() == lv) {
								$(elem).val('').removeClass('tipped');
							}
						}
					});
					return true;
				});
			}
			return true;
		});
	}
	handleFormTips();
	
	// infobox accordion
	var infoboxClicked = false; // track user interaction
	$('.accordion .infobox-item').addClass('hidden');
	$('.accordion .infobox-item .ifc').hide();
	$('.accordion .infobox-item h4').click(function() {
		infoboxClicked = true;
		var cont = $(this).parent().find('.ifc');
		var f = function(obj) {
			if($(obj).parent().find(':hidden').length > 0) {
				$(obj).slideDown('fast', function() {
					$(this).parent().addClass('visible').removeClass('hidden');
				});	
				$(obj).parent().animate({
					backgroundColor: '#FFFFFF'
				}, 500);
			} else {
				$(obj).slideUp('fast', function() {	
					$(this).parent().addClass('hidden').removeClass('visible');
				});	
				$(obj).parent().animate({
					backgroundColor: '#e3f3f9'
				}, 500);
			}
		}
		if($('.infobox-item .ifc:visible').length > 0) {
			var t = $('.infobox-item .ifc:visible');
				$(t).slideUp('fast', function() {
					if($(t)[0] != $(cont)[0]) {
						f(cont);
					}
					$(this).parent().addClass('hidden').removeClass('visible');
				});
				$(t).parent().animate({
					backgroundColor: '#e3f3f9'
				}, 500);
		} else {
			f(cont);
		}
	});
	setTimeout(function() {
		if(!infoboxClicked) {
			$('.accordion .infobox-item:first h4').click(); // open first item
		}
	}, 1500);
	
	// tooltips
	$('.tooltip').tooltip({ 
	    bodyHandler: function() { 
			var elems = (this.tooltipText).split(' | ');
			var title = elems.shift();
			var body = elems.shift();
	        return '<div class="tooltip-outer"><div class="tooltip-inner"><div class="tooltip-content"><h5>' + title + '</h5><p>' + body + '</p></div></div></div>'; 
	    }, 
		fade: true,
	    showURL: false 
	});
	
	// do the zebra stripes
	$('.zebra').each(function() {
		$(this).find('.zebra-row:odd').addClass('row-odd');
		$(this).find('.zebra-row:even').addClass('row-even');
	});
	
	// handle local scrolling
	$.localScroll({
		axis: 'y',
		duration: 1000,
		hash: true
	});

	// handle button mouseover
	$('button').mouseover(function() {
		$(this).addClass('hover');
	});
	$('button').mouseout(function() {
		$(this).removeClass('hover');
	});
	
	// handle teaser box mouseover state
	$('.tb-item').each(function() {
		$(this).mouseover(function() {
			$(this).addClass('hover');
		});
		$(this).mouseout(function() {
			$(this).removeClass('hover');
		});
		$(this).click(function() {
			var l = $(this).find('h4:first a').attr('href');
			document.location.href = l;
		});
	});
	
	// make example data tables collapsable
	$('.exi-details').each(function() {
		var d = $(this).find('div');
		$(d).hide();
		$('a', this).click(function() {
			if($(this).is('.closed')) {
				$(this).addClass('open').removeClass('closed').text('weniger Details');
				$(d).slideDown('fast');
			} else {
				$(this).addClass('closed').removeClass('open').text('mehr Details');
				$(d).slideUp('fast');
			}
			return false;
		});
	});
	
	// init date calendar
	$('input.date').calendar();
	
	// ajax/json home page form
	$('form').each(function() {
		var form = $(this);
		$('select#manufacturer', form).change(function() {
			var id = $(this).val();
			var url = "/anfrage/models/" + id + "";
			$.getJSON(url, {format: 'json'}, function(data) {
				var options = '<option value="0">Modell</option>';
				$.each(data.vehicles, function(vehicle_id, vehicle_name) {
					options += '<option value="' + vehicle_id + '">' + vehicle_name + '</option>';
				});
				$('select#model', form).html(options);
			});
		});
	});
	
	// toggle company name field
	$('#frm-r-inquiry-company').hide();
	var toggleCompany = function() {
		if($('#frm-r-inquiry-inquiry-type input:checked').val() == 1) {
			// show company
			$('#frm-r-inquiry-company').slideDown('fast', function() {
				// highlight
				$('#frm-r-inquiry-company').css('background-color', '#42B6D4');
				$('#frm-r-inquiry-company').animate({
					backgroundColor: '#E3F3F9'
				}, 1000);
			});
			
		} else {
			// hide company
			$('#frm-r-inquiry-company').slideUp('fast');
		}
	}
	$('#frm-r-inquiry-inquiry-type input').click(function() {
		toggleCompany();
	});
	toggleCompany();
	
	// toggle date and time field
	$('#detail-date-container').hide();
	var dateContainerIsHidden = true;
	var toggleDateContainer = function() {
		if($('#frm-r-inquiry-contact-type input:checked').val() == 0 || $('#frm-r-inquiry-contact-type input:checked').val() == 1) {
			if(dateContainerIsHidden) {
				// show date container
				$('#detail-date-container').slideDown('fast', function() {
					// highlight
					$('#detail-date-container').css('background-color', '#42B6D4');
					$('#detail-date-container').animate({
						backgroundColor: '#E3F3F9'
					}, 1000);
					dateContainerIsHidden = false;
				});
			}
		} else {
			// hide date container
			$('#detail-date-container').slideUp('fast');
			dateContainerIsHidden = true;
		}
	}
	$('#frm-r-inquiry-contact-type input').click(function() {
		toggleDateContainer();
	});
	toggleDateContainer();
	
	// toggle other vehicle field
	$('#frm-r-inquiry-vehicle-type-other').hide();
	var toggleVehicleTypeOther = function() {
		if($('#frm-r-inquiry-vehicle-type input:checked').val() == 0) {
			// show company
			$('#frm-r-inquiry-vehicle-type-other').fadeIn('fast', function() {
				// highlight
				$('#frm-r-inquiry-vehicle-type-other').css('background-color', '#42B6D4');
				$('#frm-r-inquiry-vehicle-type-other').animate({
					backgroundColor: '#FFFFFF'
				}, 1000);
			});
			
		} else {
			// hide company
			$('#frm-r-inquiry-vehicle-type-other').fadeOut('fast');
		}
	}
	$('#frm-r-inquiry-vehicle-type input').click(function() {
		toggleVehicleTypeOther();
	});
	toggleVehicleTypeOther();
	
	// lets add some vehicle icons to the form
	$('#frm-r-inquiry-vehicle-type label').each(function() {
		$(this).addClass('vl-' + $(this).text().toLowerCase());
		$('input', this).after('<span></span>');
	});
	
});
