$(document).ready(function(){
	
	$('#topic ul').bxSlider({
	  infiniteLoop: false,
    hideControlOnEnd: true,
    auto: true,
    pause: 10000,
    speed: 1000
  });
  
	$('#facts ul').bxSlider({
	  infiniteLoop: false,
    hideControlOnEnd: true,
    speed: 1000
  });
	
  
	$("#tools").hide();
	if( $('.fieldRequired').length == 0  && $('.formSuccess').length == 0 && window.location.hash != '#showContact'){
	  $("#contact").hide();
  }
	
  $(".plusminus").click(function () {
      
	  var objId = $(this).attr('id');
	  var objElem = objId.replace(/toolBar_/g, "");
	  
	  if( objElem == 'contact' ){
	    var otherElem = 'tools';
	  }else if( objElem == 'tools' ){
	    var otherElem = 'contact';
	  }
	  
    $('#'+otherElem).slideUp("slow");
    $('.plusminus').removeClass("activeIcon");
    $('#'+objElem).slideToggle("slow");
    
    if( $('#'+objElem).height() > 20 ){
      $(this).removeClass("activeIcon"); 
    }else{
      $(this).addClass("activeIcon"); 
    }
    
    return false;
  });

  
  //$('#activityBox .question').toggle('show');
  $("#activityBox").click(function () {
      $('#activityBox .question').slideToggle("slow");
      return false;
  });
  
  
  $('#infochart').hide();
  $(".showInfochart").click(function () {
      $('#infochart').fadeIn("slow");
      return false;
  });
  $("#infochart").click(function () {
      $(this).fadeOut("slow");
      return false;
  });
  
  
  $('#infoNav ul').hide();
  //$("#infoNav ul").toggle('hide');
  $("#infoNav li").hover(function () {
      $(this).toggleClass("active");
      $(this).find('ul').fadeToggle('fast');
  });
  
  
  $("#add2Card a,#go2Shop a").hover(function () {
      $('#coursePriceBox').toggleClass("activePriceBox");
  });
  
  
  //$('#imprintBody').hide();
  $(".imprint").click(function () {
      $('#imprintBody').fadeIn("slow");
      return false;
  });
  $("#imprint .close").click(function () {
      $('#imprintBody').fadeOut("slow");
      return false;
  });
  
  
  $('#loginLayer,#searchLayer').hide();
  $("#login").click(function () {
      $('#loginLayer').fadeIn("slow");
      $('#searchLayer').fadeOut("slow");
  });
  $("#search").click(function () {
      $('#searchLayer').fadeIn("slow");
      $('#loginLayer').fadeOut("slow");
  });
  $("#searchLayer .close,#loginLayer .close").click(function () {
      $(this).parent().fadeOut("slow");
      return false;
  });
  
  $('#loginLayer form').submit(function(e){
		e.preventDefault();
		var fUsername = $("#fUser").attr('value');
		var fPassword = $("#fPwd").attr('value');
		var fService = $("#fService").attr('value');
		$.ajax({
			type: "POST",
			timeout: 30000,
			data: {cmd: 'ajax', username: fUsername, password: fPassword, service: fService},
			url: "index.php?id=24",
			//url: "login.html",
			success: function(result) {
			  
			  var searchResult = result;
			  var searchCheck = searchResult.search(/logout/);
			  
				if(searchCheck != -1 || result == "true") {
					$("#ajaxLoader").fadeOut('slow');
					$("#loginMsg").append('<div id="ajax_accept">Sie wurden erfolgreich angemeldet ...</div>');
					$("#ajax_accept").hide().show("slow",function(){ 
						setTimeout(function(){$("#loginLayer").slideUp(500);},500);
						setTimeout(function(){$('#loginLayer form')[0].submit();},1500);
					});
				}else{
					$("#ajaxLoader").remove();
					$("#loginMsg").append('<div id="ajax_error">Die Anmeldung war nicht erfolgreich!</div>');
				}
			}
		})
	});
	$("#fSubmitLogin").ajaxStart(function(){
		$("#loginMsg div").remove();
		$("#loginMsg").append('<div id="ajaxLoader">Überprüfe Daten...</div>');
	});
	
	
  $("a[rel='colorbox']").colorbox({slideshow:false, slideshowSpeed: 10000});
  $("a[rel='colorboxSlide']").colorbox({slideshow:true, slideshowSpeed: 10000});
  
	
	$('input[title],textarea[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));	
		}
		
		$(this).focus(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('').addClass('focused');	
			}
		});
		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');	
			}
		});
	});
	
	
  if( $(".cardTotalNum").html() == 0 ){ $('.cardInfo').hide(); }
  $('#add2Card,.add2Card').click(function(e){
		e.preventDefault();
		$("#ajaxLoaderCard").remove();
		if( $(this).attr('id') == 'add2Card' ){
		  $(this).prepend('<span id="ajaxLoaderCard">&nbsp; &nbsp; &nbsp;</span>');
		}else{
		  $(this).append('<span id="ajaxLoaderCard">&nbsp; &nbsp; &nbsp;</span>');
		}
		var add2Card = $(this).find('a').attr('name');
		var activeObj = $(this);
		$.ajax({
			type: 'GET',
			timeout: 30000,
			data: {cmd: 'add', id: 33, item: add2Card, itemsCount: 1 },
			url: 'index.php',
			success: function(result) {
				if( !isNaN(result) ) {
				  
					$("#ajaxLoaderCard").remove()
					$(".cardTotalNum").html(result);
					$('.cardInfo').slideDown('slow');
					$(activeObj).fadeOut('slow');
					$('#nextCourse').fadeIn('slow');
					
				}else {
				}
			}
		})
	});
	
	
	$('#cardTable .itemsPersonNumber,#cardTable .itemsLicenseNumber').change(function(){
	  
	  // Price calc
	  if( $(this).parent().find('.fieldDiscount').length > 0 ){
	    
	    var str2arr = $(this).parent().find('.fieldDiscount').val();
	    var arrDiscount = str2arr.split("|");
	    arrDiscount.reverse();
	    var licenseNum = $(this).val();
	    var discountValue = false;
	    var discountPrice = false;
	    
      $.each(arrDiscount, function(key, value){
          var str2arrTmp = value;
          var discountKey = str2arrTmp.split(";");
          
          if( parseFloat(licenseNum) >= parseFloat(discountKey[0]) ){
            
            discountValue = discountKey[1];
            discountPrice = discountKey[2];
            
            
            
            return false;
          }
      });
      
      if( discountValue ){
        //alert(discountPrice);
        $(this).parent().parent().find('.discount').html(discountValue);
        $(this).parent().parent().find('.price span').html($().number_format(discountPrice, {numberOfDecimals:2, decimalSeparator:',', thousandSeparator:'.'}));
        var priceSingle = discountPrice;
      }else{
        $(this).parent().parent().find('.discount').html('&nbsp;');
        $(this).parent().parent().find('.price span').html($().number_format($(this).parent().parent().find('.price input').val(), {numberOfDecimals:2, decimalSeparator:',', thousandSeparator:'.'}));
        var priceSingle = $(this).parent().parent().find('.price input').val();
      }
      
	    
	  }else{
	    var priceSingle = $(this).parent().parent().find('.price input').val();
	  }
	  
	  var multiplier = $(this).val();
	  
	  var thisPrice = eval( parseFloat(priceSingle) +'*'+ parseFloat(multiplier) );
	  $(this).parent().parent().find('.priceTotal input').attr('value', thisPrice);
	  
	  var thisPrice = $().number_format(thisPrice, {numberOfDecimals:2, decimalSeparator:',', thousandSeparator:'.'}); 
	  $(this).parent().parent().find('.priceTotal span').html(thisPrice);
	  
	  var recalcPrice = 0;
	  $("#cardTable .priceTotal input").each(function (i) {
	      var tmpPrice = $(this).val();
	      recalcPrice = eval( parseFloat(recalcPrice) +'+'+ parseFloat(tmpPrice) );
     });
    
    var recalcPriceTax = eval( parseFloat(recalcPrice) +'*'+ parseFloat(0.08) );
    var recalcPriceTotalAmount = eval( parseFloat(recalcPrice) +'+'+ parseFloat(recalcPriceTax) );
    
    recalcPriceTax = $().number_format(recalcPriceTax, {numberOfDecimals:2, decimalSeparator:',', thousandSeparator:'.'}); 
    recalcPriceTotalAmount = $().number_format(recalcPriceTotalAmount, {numberOfDecimals:2, decimalSeparator:',', thousandSeparator:'.'}); 
	  $('.priceTax span').html(recalcPriceTax);
	  $('.priceTotalAmount span').html(recalcPriceTotalAmount);
	  
	  
	  // row calc
	  var tmpHolderID = $(this).parent().find('.itemsID').val();
	  var itemsRowCount = $('#itemsTable'+tmpHolderID+' .itemsPersonRow').length;
	  
	  if( itemsRowCount > $(this).val() ){
	    var jMax = itemsRowCount;
	  }else{
	    var jMax = $(this).val();
	  }
	  
	  for(j=1;j<=jMax;j++){
	    
	    if( $('#items'+tmpHolderID+'Row'+j).length == 0 ){
	      var copyCourseDate = $('#items'+tmpHolderID+'date').html();
	      $('#itemsTable'+tmpHolderID).append('<tr id="items'+tmpHolderID+'Row'+j+'" class="itemsPersonRow"><td><input type="text" name="items['+tmpHolderID+'][persons]['+j+'][firstname]" value="" /></td><td><input type="text" name="items['+tmpHolderID+'][persons]['+j+'][lastname]" value="" /></td><td><input type="text" name="items['+tmpHolderID+'][persons]['+j+'][email]" value="" /></td><td><select name="items['+tmpHolderID+'][persons]['+j+'][date]">'+copyCourseDate+'</select></td></tr>');
	      
	    }
      
      if( j > $(this).val() ){
        $('#items'+tmpHolderID+'Row'+j).remove();
      }
      
	  }
	  
	});
	
	/*$('.infoPersonRow').hide();
	$('.itemsRow .title').click(function(){
	    $(this).parent().next('.infoPersonRow').fadeToggle('slow');
	});*/
	
	
  $('#searchLayer form').submit(function(e){
		e.preventDefault();
		$("#searchResult").remove();
		var search = $("#fsearch").attr('value');
		$.ajax({
			type: "GET",
			timeout: 30000,
			data: {id: 26, cmd: 'ajax', search: search},
			url: "index.php",
			success: function(result) {
				if(result) {
					$("#ajaxLoaderSearch").remove();
					$("#searchLayer").append('<div id="searchResult">'+result+'</div>');
					$("#searchResult").hide();
					$("#searchResult").slideDown('slow');
				}
				else {
				  
				}
			}
		});
      return false;
	});
	$("#fSubmitSearch").ajaxStart(function(){
		$("#searchMsg div").remove();
		$("#searchMsg").append('<div id="ajaxLoaderSearch">&nbsp;</div>');
	});

});

