﻿$(document).ready(function() {


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

	$("#tx-ttproducts-listoffers").scrollable({size: 4, clickable: false}); 

	$("#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');
	});

	$("#secNavBox li").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$("#secNavBox li").bind("mouseout", function(){
		$(this).removeClass('hover');
	});

	$(".listitem").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$(".listitem").bind("mouseout", function(){
		$(this).removeClass('hover');
	});
	
	$(".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');
	});
		
	$(".news-single-imgcaption:empty").addClass('empty');
	
	$('#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']
	});

	$(".click-enlarge").fancybox({ "hideOnContentClick": true });

	$(".deleteItem").click(function () { 
		var itemName = $(this).attr("rel");
		$("input[name='"+itemName+"']").val(0);
		$("form[name='changeWK']").attr("action","https://www.skverlag.de/buchshop/warenkorb.html"); 
		$("form[name='changeWK']").submit();  
		return false;
	});
	
	$("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();
		}		
	});	
	
	$(".tt_products_item_single_display h1").each(function(index) {
		var fullStr = $(this).text();
		if (fullStr.length == 0) {
			$(this).remove();
		}		
	});

	$(".tx-ttproducts-listhighlights, .tx-ttproducts-listnewitems").find("a:last").css('margin-right','0');

	$('a.popup').fancybox({ 'hideOnContentClick': true,'width':600,'autoDimensions':false});
	$('a.bigpopup').fancybox({ 'hideOnContentClick': true,'width':840,'height':554});

/*
	$('.tooltip-container a').tooltip({ 
	    bodyHandler: function() { 
			$(this).attr('rel',$(this).next().attr('class'));
			return $('div.'+this.rel).html();
	    },
		showURL: false
	});

	$('.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
	});
*/
	$('button.btn-send-order').click(function() {	
		var formElement = $(this).closest('form');
		$("#dialog").dialog({
			draggable: false,
			//bgiframe: true, //deprecated
			width: 400,
			height: 140,
			resizable: false,
			modal: true,
			buttons: {
				'Zum Warenkorb': function() {
					formElement.attr('action','https://www.skverlag.de/buchshop/warenkorb.html');
					formElement.submit();
					$(this).dialog('close');
				},
				'Weiter einkaufen': function() {
					formElement.submit();
					$(this).dialog('close');
				}	
			}
		});
	});		
	
	$('.csc-linkToTop a').click(function() {
		$('html,body').animate({scrollTop: 0}, 1000);
		return false;
	});	
	
	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"});

	$('.news-single-text ul').wrap('<div style="width:355px; margin:-5px 0 10px 0; display:inline-block;"></div>');
	
	$("#wizard").scrollable({size: 1, clickable: false}).navigator({ 
        navi: "#status", 
        naviItem: 'a', 
        activeClass: 'current' 
    }); 
	
	$(".page input:radio").bind("change", function(){
		$("a.current").addClass('done')
	});

	$('.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();
		}
	});
	
	$("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');
		}
    );

	$('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');
		}
    );

	$('.tx-c1questionnaire-zuList').parent().css('overflow-x', 'hidden');
	$('.tx-c1questionnaire-zuList').parent().css('overflow-y', 'auto');
	
	$(".draggable").draggable({
		revert: 'invalid',
		cursor: 'move',
		grid: [11, 27]
	});
	$(".droppable").droppable({
		activeClass: 'c1-state-active',
		hoverClass: 'c1-state-hover',
		drop: function(event, ui) {
				$(this).addClass('c1-state-highlight');
				ui.draggable.animate({ left: '165px' });
				$(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');
			$(this).prevAll().find(".labelCheckbox").removeClass('hideAllStyles');
			$(this).prevAll().find(".tx-c1questionnaire-ftList-solution").show();
			$(this).prevAll().find(".tx-c1questionnaire-zuList-solution").show();
		},
		function () {
			$(this).html('Lösung(en) anzeigen');
			$(this).prevAll().find(".labelCheckbox").addClass('hideAllStyles');
			$(this).prevAll().find(".tx-c1questionnaire-ftList-solution").hide();
			$(this).prevAll().find(".tx-c1questionnaire-zuList-solution").hide();
		}
    );
	
	$('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");
		}
	});		

	$('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);
		});				
	});	

	$("#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);
	});
	*/

	var magazineTypeVal = $("form #magazinetype").val();
	if (magazineTypeVal == 'Print') {
		$("form .toogleVisibility").show();
	} else {
		$("form .toogleVisibility").hide();
	}	
	$("form #magazinetype").bind("change load", function(){
		if ($(this).val() == 'Print') {
			$("form .toogleVisibility").show();
		} else {
			$("form .toogleVisibility").hide();
		}
	});

	$("#schoolsearch").autocomplete('index.php', {
		autoFill:false,
		minChars:0,
		delay:10,
		extraParams:{eID:"searchschools"}	
	}); 
	$("#schoolsearch").result(function(event, data, formatted) {
			var actionurl = 'index.php?id=360&tx_c1adressdata_pi2[showUid]='+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);
	});
	
	$(".tx-c1adressdata-pi2 div.select").bind("mouseover", function(){
		$(this).addClass('hover');
	});
	$(".tx-c1adressdata-pi2 div.select").bind("mouseout", function(){
		$(this).removeClass('hover');
	});	

	$("#btn-reset-searchfilter").click(function() {	

		$("form[name='schoolSearch'] :checked").each(function(index){
			$(this).removeAttr("checked");
		});		
		$("form[name='schoolSearch']").submit();  
	});
});
