// jquery based javascript for burnus
// zeitform Internet Dienste OHG, 2009

function showtext (id) { 
  $('#' + id).show(); 
}

function hidetext (id) { 
  $('#' + id).hide(); 
}

function subNavToggle(id) {
  if (id) { 
    $('#subproducts ul').hide();
    $('#smenu' + id).show();
  } else {
    $('#smenu1').show();
  }
}

function infoExplode(start, explode) {
  if ($('#' + explode).is(':visible')) {
    $('#' + explode).fadeOut('slow');
    this.lastElement = false;
  } else {
    if (this.lastElement) {
      //$(this.lastElement).fadeOut('slow');
      $('.zoombox').fadeOut('slow');
    } 
    $('#' + explode).fadeIn('slow');
    this.lastElement = explode;
  }
}

// bewerbung

var reqd = {
  1: [ "pos" ],
  2: [ "Anrede", "Vorname", "Nachname", "Strasse", "Plz", "Ort", "Land", "Telefon", "Email" ],
  3: [ "Schule", "Schule_Dauer", "Schule_Schulabschluss", "Schule_Note" ],
  5: [ "Sprache", "Sprache_Level"], // , "Programm", "Programm_Level", "Sap", "Sap_Level" ],
  6: [ "eintritt", "gehalt", "reisetaetigkeit" ],
  7: [ "anschreiben", "foto", "lebenslauf", "zeugnisse", "check:datenschutz" ]
};

function check_bewerbung_submit () {
  //console.log(reqd);
  var err = true;
  for (var j in reqd) {
    for (var i=0; i < reqd[j].length; i++) {
      if (reqd[j][i].match(/^check:(.+)$/)) {
	var n = RegExp.$1;
	var v = $(':input[name=' + n + ']');
	if(!v.is(':checked')) { err = false; }
      } else {
	var v = $(':input[name=' + reqd[j][i] + ']');
	if (!v.val()) { err = false; }
      }
    }
    //console.log(j, reqd[j]);
  }
  //console.log("err:", err);
  return err;
  
}

function verify (p) {

  // clean up
  $(':input').removeClass('alert'); 
  $('#fill').remove();

  // find current page
  var current = 0;
  $('div.formular').each( function() { 
    if ($(this).is(':visible')) {
      var id = $(this).attr('id');
      current = id.match(/\d+$/);
    }
  });


  // do not verify when going back
  //console.log('p='+p+' current='+current); 
  if (p < current) return true;

  // if nothing required ok
  if (! reqd[current]) return true;

  // iterate throught required
  var err = 0;
  for (var i=0; i < reqd[current].length; i++) {

    if (reqd[current][i].match(/^check:(.+)$/))
      {
	var n = RegExp.$1;
	var v = $(':input[name=' + n + ']');
	//console.log('check: ', v);
	if(!v.is(':checked')) {
	  //console.log('unchecked: ', v);
	  $(v).parent('label').addClass('alert');
	  err = 1;
	}
      }
    else
      {
	var v = $(':input[name=' + reqd[current][i] + ']');
	//console.log("check " + $(v).attr('name') + " val:" + v.val());
	if (!v.val()) {
	  $(v).addClass('alert');
	  err = 1;
	  //console.log("check " + $(v).attr('name'));
	}
      }
  }

  // is something is missing, show error
  if (err>0) {
    $('#bewerbung' + current).append('<p id="fill">Bitte f&uuml;llen Sie alle Pflichtfelder aus</p>');
    return false;
    //console.log('err');
  } 

  return true;

}


function page (p) {

  //console.log('click ' + p);

  if (!verify(p)) return;

  $('div.formular').each( function(i) { 
    if (i == p ) { $(this).show(); } else { $(this).hide();  }
  });

  // navigation right
  $('#steps li').removeClass('active');
  $('#step' + (p+1)).addClass('active');

  $('#step' + (p+1)).unbind('click').click(function(){page(p);}).css('cursor','pointer');

  // progress bar
  $('#progress').attr('src', '/images/step' + (p+1) + '-7.png');
}

$(function() {

  // external links
  $('a[rel=external]').attr('target', '_blank');

  // bilddatenbank
  $('#pictures tr').each(function(){
    var ctx = this;
    // hide all images but the first
    $('div.previewpop', $(ctx)).each(function (i) {
      if (i > 0) $(this).hide();
    });
    // on hover hide all images, show the element before the one before (prev().prev())
    $('div.download a', $(ctx)).hover(function() {  
      var rel = $(this).attr("rel");
      $('div.previewpop', $(ctx)).hide();
      $('div.' + rel).show();
    },function() {});
  });

  // bewerbung
  $('#bewerbung').submit(check_bewerbung_submit);

  $('div.formular').each( function(i) { 
    // hide all but first
    if (i != 0 ) {
      $(this).hide(); 
    }

    // add click handler
    //$('#step' + (i+1)).click(function(){page(i);}).css('cursor','pointer');

    // add backwards
    if (i > 0)
      $(this).append('<div class="back"><a href="javascript:page(' + (i - 1) + ');"><img src="/images/pfeil_back.png" width="14" height="11" alt="&lt;"> zur&uuml;ck</a></div>');

    // add forward
    if (i < $('div.formular').length - 1) 
      $(this).append('<div class="more"><a href="javascript:page(' + (i + 1) + ');">weiter<img src="/images/pfeil_more.png" alt="-&gt;" width="14" height="11"/></a></div>');

  });

  // bewerbung - hilfetext einblenden
  $('.help').click(function(){
    $('div.helptext').hide();
    $(this).next('div.helptext').show();
  });

  // tooltip close button
  $('div.helptext').each(function(){
    $(this).prepend('<img class="close_icon" alt="" title="Fenster schliessen" src="/images/icon-close.png">');

  });
  
  $('img.close_icon').click(function(){
    $('div.helptext').hide();
  });

  // add verify to submit
  $('#bewerbung').submit(function(){ if (!verify()) return false; })

  // add click handler
  $('#step1').click(function(){page(0);}).css('cursor','pointer');

  // ausbildung hinzufügen, etc.
  $('.more_line')
    .click(function(){
	     var t = $(this).prev('table').clone();
	     $(':input', $(t)).each(function(){
  	        $(this).val('');
		var n = $(this).attr('name');
		var i = 2;
		if (n.match(/_(\d+)$/)) {
		  i = parseInt(RegExp.$1) + 1;
		  n = n.replace(/_(\d+)$/, '');
		}
		n = n + '_' + i;
		$(this).attr('name', n).removeClass('alert');;
             });

	     $(this).before(t);
    })
    .css('cursor','pointer');

});


