﻿$(document).ready(function() {
	
	$('#latestNewsImages').cycle({
		timeout:	5000,
		speed:  	750,
		delay:  	1000,
		pause:		1
	});

	$("#tx-ttproducts-listoffers").scrollable({size: 4, clickable: false}); 
	
	// Mouseover in terNav
	$("#primNavBox li").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$("#primNavBox li").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
	$("#primNavBoxAdditional li").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$("#primNavBoxAdditional li").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
	
	// Mouseover in secNav
	$("#secNavBox li").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$("#secNavBox li").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
	
	// Mouseover in SHOP - LIST
	$(".listitem").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$(".listitem").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
	
	// Mouseover in NEWS - LIST
	$(".news-list-item").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$(".news-list-item").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
	$(".termine-list-item").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$(".termine-list-item").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
		
	// Klasse vergeben, wenn Bil in News keine Bild-Untertext hat (wg. Rahmendicke)
	$(".news-single-imgcaption:empty").addClass('empty');
	
	// Kalender für Inputs
	$('#datefrom').datepicker({
		dateFormat: 'dd.mm.yy',
		dayNamesMin: ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'],
		monthNames: ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember']
	});

	// Bildvergrößerung
	$(".click-enlarge").fancybox({ "hideOnContentClick": true });

	// Produkte aus WK löschen
	$(".deleteItem").click(function () { 
		var itemName = $(this).attr("rel");
		$("input[name='"+itemName+"']").val(0);
		$("form[name='changeWK']").attr("action","buchshop/warenkorb.html"); 
		$("form[name='changeWK']").submit();  
	});
	
	// Subtitle in Produktliste kürzen / umschließendes Tag entfernen, wenn leer
	$("span.listitem-subtitle").each(function(index) {
		var fullStr = $(this).text();
		if (fullStr.length > 45) {
			var partStr = fullStr.substr(0, 42);
			$(this).text(partStr+'...');
		} else if (fullStr.length == 0) {

			$(this).remove();
		}		
	});
	$("p.listitem-author").each(function(index) {
		var fullStr = $(this).text();
		if (fullStr.length > 55) {
			var partStr = fullStr.substr(0, 54);
			$(this).text(partStr+'...');
		} else if (fullStr.length == 0) {
			$(this).remove();
		}		
	});	
	
	// Subtitle in Produktdetail: umschließendes Tag entfernen, wenn leer
	$(".tt_products_item_single_display h1").each(function(index) {
		var fullStr = $(this).text();
		if (fullStr.length == 0) {
			$(this).remove();
		}		
	});
	
	// Shophome: kein Margin für letztes Image bei "TOP5" und "Neuerscheinungen"
	$(".tx-ttproducts-listhighlights, .tx-ttproducts-listnewitems").find("a:last").css('margin-right','0');
	

	// Popups
	$('a.popup').fancybox({ 'hideOnContentClick': true,'width':600});
	$('a.bigpopup').fancybox({ 'hideOnContentClick': true,'width':840,'height':554});
	
	// Tooltipps
	$('.tooltip-container a.download').tooltip({ 
	    bodyHandler: function() { 
			$(this).attr('rel',$(this).parent().prev().attr('class'));
			return $("<img/>").attr("src", $('span.'+this.rel+' img').attr('src'));
	    }, 
	    showURL: false
	});

	$('.tooltip-container a').tooltip({ 
	    bodyHandler: function() { 
			$(this).attr('rel',$(this).next().attr('class'));
			return $('div.'+this.rel).html();
	    }, 
	    showURL: false
	});		
	
	// Bestellbutton Dialogbox
	$('button.btn-send-order').click(function() {	
		var formElement = $(this).closest('form');
		$("#dialog").dialog({
			draggable: false,
			bgiframe: true,
			width: 400,
			height: 140,
			resizable: false,
			modal: true,
			buttons: {
				'Zum Warenkorb': function() {
					formElement.attr('action','index.php?id=7');
					formElement.submit();
					$(this).dialog('close');
				},
				'Weiter einkaufen': function() {
					formElement.submit();
					$(this).dialog('close');
				}	
			}
		});
	});		
	
	// scroll-to-top
	$('.csc-linkToTop a').click(function() {
		$('html,body').animate({scrollTop: 0}, 1000);
		return false;
	});	
	
	
	// News
	// accordion for archivemenü
	// das geöffnete Element wird anhand des Newsparameters bestimmt
	var activeElement = 0;
	var archiveYear = $.url.param("tx_ttnews[year]");
	var nowDate = new Date();
	var nowYear = nowDate.getFullYear();
	activeElement = nowYear-archiveYear;
	$('#accordion').accordion({header: 'h4', autoHeight: false, active: activeElement, event: "click"});
	
	// Element ul wird auf der News-Detailseite mit div umschlossen
	$('.news-single-text ul').wrap('<div style="width:355px; margin:-5px 0 10px 0; display:inline-block;"></div>');
	
	
	// Startseite Zeitschriften
	$("#skverlag-page-131 .csc-textpic img").reflect();
	
	
	// Zertifizierte Fortbildung / Fragebogen
	// scrollable 
	 $("#wizard").scrollable({size: 1, clickable: false}).navigator({ 
        navi: "#status", 
        naviItem: 'a', 
        activeClass: 'current' 
    }); 
	
	// beantwortete Fragen werden gekennzeichnet
	$(".page input:radio").bind("change", function(){
		$("a.current").addClass('done')
	});
	
	// Benutzerhinweis, wenn ein Besucher nicht alle 10 Fragen ausgefüllt hat
	$('.zertifizierte-fortbildung #submit-form').click(function() {
		var totalQuestions = 10;
		var answeredQuestions = 0;
		$(".page input:radio:checked").each(function( intIndex ){
			if ($(this).val() > 0) {
				answeredQuestions++;
			}
		});
		if (totalQuestions-answeredQuestions>0) {
			alert('Sie haben nicht alle Fragen beantwortet!');
		} else {
			$(this).closest('form').submit();
		}
	});
	
	//LPN Online: 
	// Beispielantworten (Bereich LPN Online / Ausbildung)

	$("p.exampleanswer").before('<span class="show-answer closed">Antwort anzeigen</span>');

	$('span.show-answer').toggle(
		function () {
			$(this).nextAll("p.exampleanswer").show();
			$(this).html('Antwort ausblenden');
			$(this).removeClass('closed');
			$(this).addClass('open');
		},
		function () {
			$(this).nextAll("p.exampleanswer").hide();
			$(this).html('Antwort anzeigen');
			$(this).removeClass('open');
			$(this).addClass('closed');
		}
    );
	
	
	// Themenauswahl
	$('div.tx-c1questionnaire-themenauswahl-headline').toggle(
		function () {
			$(this).nextAll("div.tx-c1questionnaire-themenauswahl-book").show();
			$(this).removeClass('closed');
			$(this).addClass('open');
		},
		function () {
			$(this).nextAll("div.tx-c1questionnaire-themenauswahl-book").hide();
			$(this).removeClass('open');
			$(this).addClass('closed');
		}
    );

	// Fragentyp Zuordnung
	// Verhindert falsche Positionierung bei Drag-Funktion im FF
	$('.tx-c1questionnaire-zuList').parent().css('overflow-x', 'hidden');
	$('.tx-c1questionnaire-zuList').parent().css('overflow-y', 'auto');
	
	//$(".draggable").draggable({ revert: 'valid' });
	$(".draggable").draggable({
		revert: 'invalid',
		cursor: 'move',
		grid: [11, 27]
	});
	$(".droppable").droppable({
		//accept: '.draggableBox > div',
		activeClass: 'c1-state-active',
		hoverClass: 'c1-state-hover',
		drop: function(event, ui) {
				$(this).addClass('c1-state-highlight');
				ui.draggable.animate({ left: '165px' });
				//alert(ui.draggable.text()); 
				
				$(this).nextAll(':hidden').val(ui.draggable.text()+'#'+$(this).text());
			}
	});	
	
	$(".resetButton").click(function() {
		$(this).prevAll().find(".draggable").animate({top:'0px',left: '0px'},1000);
		$(this).prevAll().find(".droppable").removeClass('c1-state-active');
		$(this).prevAll().find(".droppable").removeClass('c1-state-highlight');
	});
	
	$('.showSolutionButton').toggle(
		function () {
			$(this).html('Lösung(en) ausblenden');
			// Checkboxen
			$(this).prevAll().find(".labelCheckbox").removeClass('hideAllStyles');
			// Textarea
			$(this).prevAll().find(".tx-c1questionnaire-ftList-solution").show();
			// Zuordnungen
			$(this).prevAll().find(".tx-c1questionnaire-zuList-solution").show();
		},
		function () {
			$(this).html('Lösung(en) anzeigen');
			// Checkboxen
			$(this).prevAll().find(".labelCheckbox").addClass('hideAllStyles');
			// Textarea
			$(this).prevAll().find(".tx-c1questionnaire-ftList-solution").hide();
			// Zuordnungen
			$(this).prevAll().find(".tx-c1questionnaire-zuList-solution").hide();
		}
    );
	
	//Kategorieauswahl
	$('input.parent-category').click(function() {	
		if($(this).attr('checked')){
			$(this).nextAll('.child-category').attr("checked","checked");
		} else {
			$(this).nextAll('.child-category').removeAttr("checked");
		}
	});	
	$('input.book').click(function() {	
		if($(this).attr('checked')){
			$(this).parent().nextAll().children().attr("checked","checked");
			
		} else {
			$(this).parent().nextAll().children().removeAttr("checked");
		}
	});		
	
	// Gesamtanzahl via AJAX setzen
	$('form :checkbox').click(function() {	
		
		var arrSelectedCategory = new Array();
		$('div.categories :checked').each(function(index){
			arrSelectedCategory[index] = $(this).val();
		});
		
		var arrSelectedDifficulty = new Array();
		$('.lpn-difficulty :checked').each(function(index){
			arrSelectedDifficulty[index] = $(this).val();
		});
		
		$.post("index.php", { eID:"get_questioncount",categories:arrSelectedCategory.join(','),difficulty:arrSelectedDifficulty.join(',')},
		function(data){		  
			$("#lpn-questioncount").text(data);
		});				
	});	
	
	//Suggestsuche für Produkttitel
	$("#swords").autocomplete('index.php', {
		autoFill:false,
		minChars:0,
		delay:10,
		extraParams:{eID:"searchproducts"}	
	}); 
	$("#swords").result(function(event, data, formatted) {
			var actionurl = 'index.php?id=6&tt_products[product]='+data[1];
			var orgAction = $(this).closest('form').attr('action');
			$(this).closest('form').attr('action',actionurl);
			$(this).closest('form').submit();
			$(this).closest('form').attr('action',orgAction);
	});
	
	
});