function LabeledSlider(element)
{
	this.onclick = function(item_index, item_element) {}
	this.current_item = null;
	this.dragging = false;
	this.element = element;
	this.element.slider = this;
	this.handle = $(".handle", this.element);
	this.items = $('.item', this.element);
	this.current_itemindex = -1;
		
	var _this = this;
	$(this.items).each
	(
		function (i, e)
		{					
			$(e).html('<div class="label">' + $(e).html() + '</div>');
			$(e).click(function() { _this.placeHandle(i); });
			$(e).mousemove(function (e)	{ if (_this.dragging) { _this.placeHandle(i); }});
			$(e).mouseup(function (e) {	_this.dragging = false; });		
			
			$(e).append('<div class="handle">&nbsp;&nbsp;&nbsp;&nbsp;</div>');
			e.handle = $(".handle", e);
			$(e.handle).addClass("invisible");

			$(e.handle).mousedown(function (e) { _this.dragging = true; });
			
			if (i < $(_this.items).length-1)
				$(e).after('<div class="separator"></div>');			
		}
	);
	
	$(this.element).append('<div class="clearer"></div>');
	$(this.element).mousedown(function (e) { return false; });
	this.slider = $(this.element).append('<div class="slider"></div>');
	
	if (this.items.length > 0)
		this.placeHandle(0);
}

LabeledSlider.prototype.setClickHandler = function(handler) 
{
	this.onclick = handler;
}

LabeledSlider.prototype.placeHandle = function(item_index)
{
	if (this.current_itemindex!=item_index){
		if (this.current_item)
		{
			$(this.current_item).removeClass("active");
			$(".handle", this.current_item).addClass("invisible");
		}
		
		this.current_item = this.items[item_index];
	
		$(this.current_item).addClass("active");	
		$(".handle", this.current_item).removeClass("invisible");
	
		this.current_itemindex = item_index;
		this.onclick(item_index, this.current_item);
		
		if(this.items.length == 6) {
			if(item_index == 0) {
				$("#online-berater-teaser-bild").attr('src', '/pb/site/dbv/resourceCached/5/dbv/image/dbv_angestellte_273x186.jpg');
				$("#online-berater-teaser-text").text('Wir bieten Arbeitnehmern im Öffentlichen Dienst maßgeschneiderte Lösungen für individuelle Bedürfnisse an – der Online-Berater hilft Ihnen bei der Suche nach dem richtigen Produkt.');
			}
			else if(item_index == 1) {
				$("#online-berater-teaser-bild").attr('src', '/pb/site/dbv/resourceCached/5/dbv/image/dbv_bildung_273x186.jpg');
				$("#online-berater-teaser-text").text('Verlassen Sie sich in Ihrem Beruf im Bildungswesen auf das einzigartige Vorsorgekonzept der DBV. Der komfortable Online-Berater hilft Ihnen, das für Sie passende Produkt zu finden.');
			}
			else if(item_index == 2) {
				$("#online-berater-teaser-bild").attr('src', '/pb/site/dbv/resourceCached/5/dbv/image/dbv_polizisten_273x186.jpg');
				$("#online-berater-teaser-text").text('Versicherungsschutz mit individuellen Leistungen für Polizisten im Beamtendienst: Mit Hilfe des komfortablen Online-Beraters finden Sie ganz einfach das für Sie passende Produkt.');
			}
			else if(item_index == 3) {
				$("#online-berater-teaser-bild").attr('src', '/pb/site/dbv/resourceCached/5/dbv/image/dbv_bundeswehr_273x186.jpg');
				$("#online-berater-teaser-text").text('Die DBV bietet Bundeswehr-Beamten eine solide Absicherung zu fairen Konditionen. Nutzen Sie den Online-Berater, um schnell und bequem das für Sie passende Produkt zu finden.');
			}
			else if(item_index == 4) {
				$("#online-berater-teaser-bild").attr('src', '/pb/site/dbv/resourceCached/5/dbv/image/dbv_beamte_273x186.jpg');
				$("#online-berater-teaser-text").text('Bleiben Sie als Beamter/-in flexibel in Beruf und Freizeit: der Online-Berater hilft Ihnen bei der Suche nach dem Produkt, das Ihren individuellen Bedürfnissen entspricht.');
			}
			else if(item_index == 5) {
				$("#online-berater-teaser-bild").attr('src', '/pb/site/dbv/resourceCached/5/dbv/image/dbv_ruhestand_273x186.jpg');
				$("#online-berater-teaser-text").text('Der komfortable Online-Berater der DBV hilft Vor-/Ruheständlern bei der Suche nach Lösungen für individuelle Bedürfnisse.');
			}
		}
	}
}

// create slider objects for slider elements on page
// unless they have 'no-auto' set (used with the onlineberater
// which needs it's sliders constructed when it initializes,
// which we can't guarantee otherwise).
$(document).ready(function(){
	$('.labeled-slider').each(function(i,e){

		if (!$(e).hasClass("no-auto"))
		{
			$(e).data("slider", new LabeledSlider(e));
		}
	});
});
