// Pseudo-Classes & Skins & ul column splitting
// ---------------------------------------------------
$(document).ready(function(){
	$(".metaNavigation dl.typ > dd:last-child a," +
		".layoutColumnRight .csc-default:last-child .item," +
		".productOverview .content:last," +
		".career .item:last") 
		.css({background: "0"});

	$(".layoutColumnRight .csc-default:last-child .item").css({borderBottom: "0"});

	$(".subNavigation div .edgeRight .content .items ul:nth-child(1n+3)").css({margin: "0 0 0 20px"});

	$(".subNavigation div .edgeRight .content .items ul li:last-child," +
		".imageBox a:last-child").css({margin: "0"});
      
   /*$("ul.subnavAG li:nth-child(4)").css({marginBottom: "7px", 
	paddingBottom: "8px", borderBottom: "1px solid #38912C"});*/

	$(".breadcrumb ul li:first-child").css({color: "#aca399"});

	$(".footerExtNavigation dl:nth-child(1n+3)").css({margin: "0 0 0 37px"});

	$(".productDetails th:last, .productDetails td:last-child").css({borderRight: "0"});

	$(".searchCategories select").uniform();

	$('.subMenu ul').easyListSplitter({ colNumber: 2 });
	
	// Einbindung DocCheck HTML-Source ueber Datei
	$.get('fileadmin/templates/salus/docCheckAnmeldung.html', function(data) {
		$('.metaNavigation #B2B22 a').after(data);
	});
	
	$('.jumpDealerSearch').click(function (e) {
		var a = $(e.currentTarget),
			href = e.currentTarget.href.split('#')[0],
			adress = a.parent().find("input").val();

		if (adress && adress != inputDefaults.types.initAddress) {
			a.attr('href', href + '#/adresse:' + adress);
			if (!e.originalEvent) {
				window.location.href = a.attr('href');
			}
			
			// Wenn wir schon auf der Map-Seite sind, dann muss der Suchwert direkt an die Karte übergeben werden
			if ($('#haendlerMap').length) {
				$('.searchbar input').val(adress).next('a.searchAdress').click();
			}
		} else {
			e.preventDefault();
			return false;
		}
	});
	
	$('.searchBarDealer input.zipCity').keyup(function (e) {
		if (e.keyCode == 13) {
			e.preventDefault();
			var inp = $(e.currentTarget),
				btn = inp.next('a.jumpDealerSearch');
				
			btn.click();
		}
	});
});

/**
 Headline-Höhen Synchronisieren in Salus Two-Column-Layouts
**/
var syncHeadline = {
  init: function () {
    console.log('this', this);
    $('div.twoColumns').each(function() {
      var minHeight = 22;
      jQuery(this).find('h2')
        .each(function() {
          var h = $(this).height();
          if(h && h > minHeight) {
            minHeight = h;
          }
        })
        .height(minHeight);
        
      jQuery(this).find('h1')
        .each(function() {
          var h = $(this).height();
          if(h && h > minHeight) {
            minHeight = h;
          }
        })
        .height(minHeight);
    });
  }
}

$(document).ready(function(){
  setTimeout("syncHeadline.init()", 2500);
});


// Meta-Navigation (DocCheck)
// ---------------------------------------------------
var metaNav= {	
	init: function () {
		$(".typ1").mouseenter($.proxy(this, 'over'));
		$(".typ1").mouseleave($.proxy(this, 'out'));
	},
	
	over: function (e) {
		var link = $(e.currentTarget);
		link.find(".subMetaNavigation").addClass("active");
	},
	
	out: function (e) {
		var link = $(e.currentTarget);
		link.find(".subMetaNavigation").removeClass("active");
	}	
}

$(function () {metaNav.init(); });


// Navigation
// ---------------------------------------------------
var navigation= {
	delay:700,
	timeOutId: -1,
	isOpened: false,
	
	init: function () {
		$("div.nav")
			.mouseleave(function () {navigation.isOpened = false;})
			.find(".menuItem")
				.mouseenter($.proxy(this, 'over'))
				.mouseleave($.proxy(this, 'leave'));
	},
	
	over: function (e) {
		var sm = $(e.currentTarget).find('.subMenu');
		window.clearTimeout(this.timeOutId);
		if (this.isOpened) {
			this.openSubmenu(sm);
		} else {
			this.timeOutId = window.setTimeout(function () {navigation.openSubmenu(sm);}, this.delay);
		}
	},
	
	leave: function (e) {
		window.clearTimeout(this.timeOutId);
		var link = $(e.currentTarget);
		link.find(".tab").removeClass("active");
		link.find(".subMenu").removeClass("active");
	},
	
	openSubmenu: function (sm) {
		this.isOpened = true;
		sm.addClass("active").parent().find(".tab").addClass("active");
	}
}

$(function () {navigation.init(); });


// Tabs video-player/teaser-box
// ---------------------------------------------------
var TabbedBox = {
	tabbedBox            : null,
	tabbedBoxLinks       : null,
	tabbedBoxContentDivs : null,
	tabbedBoxSelector    : null,
	autoSwitchInterval	 : 6000,
	autoSwitchIntervalId : -1,

	init : function (tabbedBoxSelector) {
		this.tabbedBox = $(tabbedBoxSelector);

		this.tabbedBoxLinks = this.tabbedBox.find(".tabs a");

		var counter = 0;
		this.tabbedBoxLinks.each(function(){
			$(this).data('index', counter);
			counter = counter + 1;
		});

		this.tabbedBoxContentDivs = this.tabbedBox.find(".display > div");

		counter = 0;
		this.tabbedBoxContentDivs.each(function(){
			$(this).data('index', counter);
			counter = counter + 1;
		});

		// Registers Click-Event for all tabbedBox Tabs. 
		this.tabbedBoxLinks.click(this.tabClick).first().click();
		this.tabbedBox.mouseleave($.proxy(this, "startAutoSwitch"));
		this.tabbedBox.mouseenter($.proxy(this, "stopAutoSwitch"));
		window.setTimeout(function () {TabbedBox.startAutoSwitch()}, 500);
	},

	//Context: Clicked Element
	tabClick : function(e) {
		e.preventDefault();
		
		var allTabs = TabbedBox.tabbedBox.find("a").removeClass("active");
		var clickedTab = $(e.currentTarget).closest('a').addClass("active");
		var index = allTabs.index(clickedTab);

		TabbedBox.tabbedBoxContentDivs.removeClass("active");
		TabbedBox.tabbedBoxContentDivs.eq(index).addClass("active");
	},
	
	activTabHasFlash: function () {
		var activeTabContent = $(".teaserBox .display .active");
		if (activeTabContent.find('object, embed').length) {
			return true;
		}
		return false;
	},
	
	startAutoSwitch : function () {
		this.stopAutoSwitch();
		if (!this.activTabHasFlash()) {
			this.autoSwitchIntervalId = window.setTimeout($.proxy(this, 'doAutoSwitch'), this.autoSwitchInterval);
		}
	},
	
	stopAutoSwitch: function () {
		window.clearTimeout(this.autoSwitchIntervalId);
	},
	
	doAutoSwitch: function () {
		var links = this.tabbedBox.find('.tabs a');
		var next = links.index(links.filter('.active')) + 1;
		if (next >= links.length) {
			next = 0;
		}
		$(links.get(next)).click();
		this.startAutoSwitch();
	}
}

$(function(){
	TabbedBox.init(".teaserBox")
});


// Navigation tree
// ---------------------------------------------------
$(function(){
	$(".category").click(function(){
		$(".category").removeClass("active");
		$(this).addClass("active");
	});
	
	$(".category ul li").click(function(){
		$(".category ul li").removeClass("activeSub");
		$(this).addClass("activeSub");
	});

});


// Small image viewer container with previews
// ---------------------------------------------------
var ImageViewer = {
	imageBox            : null,
	tabbedBoxLinks       : null,
	tabbedBoxContentDivs : null,
	tabbedBoxSelector    : null,

	init : function (tabbedBoxSelector) {
		this.imageBox = $(tabbedBoxSelector);

		this.tabbedBoxLinks = this.imageBox.find("ul.previews a");

		var counter = 0;
		this.tabbedBoxLinks.each(function(){
			$(this).data('index', counter);
			counter = counter + 1;
		});

		this.tabbedBoxContentDivs = this.imageBox.find(".images>div");

		counter = 0;
		this.tabbedBoxContentDivs.each(function(){
			$(this).data('index', counter);
			counter = counter + 1;
		});

		// Registers Click-Event for all tabbedBox Tabs. 
		this.tabbedBoxLinks.click(this.tabClick);
	},

	//Context: Clicked Element
	tabClick : function(e) {
		ImageViewer.imageBox.find("a").removeClass("active");

		var _this = $(this);
		var index = _this.data('index');
		_this.addClass("active");

		ImageViewer.tabbedBoxContentDivs.removeClass("active");
		ImageViewer.tabbedBoxContentDivs.each(function(){
			if ($(this).data('index') == index) {
				$(this).addClass("active");
			}
		});

		e.preventDefault();
	}
}

$(document).ready(function(){
	ImageViewer.init(".imageBox")
	$(ImageViewer.tabbedBoxLinks[0]).click();
});


// Folding boxes
// ---------------------------------------------------

var foldingBoxes = {
	/* Configuration */
	slideSpeed : "fast",

	/* Member */
	accordion   : null,
	items       : null,
	buttons     : null,
	contentDivs : null,

	init : function (accordionSelector) {
		this.accordion = $(accordionSelector);
		this.items     = this.accordion.find('.item');
		this.buttons   = this.items.children('a.fold');
		this.contentDivs = this.items.find('.hiddenContent');

		this.buttons.click(this.accordionClick);
	
		this.parseUrl();
		
		// zuletzt geöffnetes Accordion ausgeklappt anzeigen
		if($.cookie('salusAccordion')){
			var accordionIndex = $.cookie('salusAccordion');
			$('.foldingBoxesContainer').eq(accordionIndex)
				.find('.item, .button').addClass('active')
				.find('.hiddenContent').show();
		}
	},

	/*
	 * Context: Clicked Element
	 */
	accordionClick : function (e) {
		var parentItem = $(this).parent(".item");
		
		// ermitteln welches Akkordeon geöffnet ist
		var accordionCounter = $(this).parent('.item').parent().prevAll('.foldingBoxesContainer').length;
		// lösche Accordion-Cookie
		$.cookie('salusAccordion', null);
		// setze Accordion-Cookie
		$.cookie('salusAccordion', accordionCounter);

		if (parentItem.hasClass('active')) {
			parentItem.removeClass('active');
			parentItem.find('.button').removeClass("active");
			parentItem.find('.hiddenContent').slideUp(foldingBoxes.slideSpeed);
		} else {
			// Akkordeon-Funktion
			if ($('.foldingBoxesContainer').length) {
				$('.foldingBoxesContainer .item, .foldingBoxesContainer .button').removeClass('active');
				$('.foldingBoxesContainer .hiddenContent').slideUp(foldingBoxes.slideSpeed);
			}
			parentItem.addClass("active");
			parentItem.find('.button').addClass("active");
			parentItem.find('.hiddenContent').slideDown(foldingBoxes.slideSpeed);
		}
		e.preventDefault();
	},

	parseUrl : function () {
		if (location.hash) {
			$('a[href=' + location.hash + ']').click();
		}
	}
}

$(document).ready(function(){
	foldingBoxes.init('.foldingBoxesImagePreview');
});


// Input defaults
// ---------------------------------------------------
var inputDefaults = {
	types: {
		"initUsername"	: 'Username',
		"initSearch"	: 'Volltextsuche',
        	"initSearchProducts"	: 'Produkt eingeben',
		"initAddress"	: 'Ihre PLZ / Stadt',
		"initPassword"	: 'Passwort'
	},
	
	init: function () {
		var f;
		for (var type in this.types) {
			f = $('input.'+type)
			f.data('initType', type)
			 .focus($.proxy(this, 'clear'))
			 .blur($.proxy(this, 'setDefault'))
			 .blur();
		}
	},
	
	clear:function (e) {
		var t = $(e.currentTarget);
		if (t.val() == this.types[t.data('initType')]) {
			t.val('');
		}
	},
	
	setDefault:function (e) {
		var t = $(e.currentTarget);
		if (!$.trim(t.val())) {
			t.val(this.types[t.data('initType')]);
		}
	}
	
}
$(function () {inputDefaults.init();});




