/*
 * 
 *  Anpassen der Kontaktfunktion: 
 *  
 *  contactUrl: Absolute Url zum Kontakt-Script
 *  
 *  Variablen (per POST):
 *  
 *  anrede            Anrede (Herr/Frau)
 *  vorname           Vorname
 *  nachname          Nachname
 *  alter             Alter
 *  strasse           Strasse
 *  ort               Ort
 *  plz               Postleitzahl
 *  telefon           Telefon
 *  email             Email
 *  kontaktgrund      Kontaktrgrund
 *  info              Infos erhalten (1 = Ja)
 *  nachricht         Nachricht
 *  
 **/
// XXXMP var contactUrl = "http://whatever.net/kontakt.php";
var contactUrl = "http://www.stoertebeker.com/sendform.php";
/*
 * 
 *  Anpassen der Bestellfunktion: 
 *  
 *  shopUrl: Absolute Url zum Bestell-Script
 *  
 *  Variablen (per POST):
 *  
 *  anrede            Anrede (Herr/Frau)
 *  vorname           Vorname
 *  nachname          Nachname
 *  alter             Alter
 *  strasse           Strasse
 *  ort               Ort
 *  plz               Postleitzahl
 *  telefon           Telefon
 *  email             Email
 *  menge             Bestellmenge
 *  zahlungsart       Zahlungsart (vorkasse/nachnahme)
 *  terms             AGBs bestätigt
 *  
 **/
//var shopUrl = "http://whatever.net/shop.php";
var shopUrl = "http://www.stoertebeker.com/shop.php";

// Application scripts
$(document).ready(function () {
  // preload some images
  var image1 = $('<img />').attr('src', 'img/content_bg.jpg');
  var image2 = $('<img />').attr('src', 'img/video.jpg');
  
  $('li').click(function () {
    $('li').find('div.klein').not($(this).find('div.klein')).hide();
    if ($(this).hasClass('active')) {
      $(this).removeClass('active');
      $(this).find('div.klein').fadeOut('fast');
      $(this).find('div.gross').fadeOut('fast');
    } else {
      $('li.active').find('div.klein').fadeOut('fast');
      $('li.active').find('div.gross').fadeOut('fast');
      $('li.active').removeClass('active');
      $(this).addClass('active');
      $(this).find('div.klein').fadeIn('fast');
    }
  });
  
  $('li > div.klein a').click(function () {
    $(this).parent().parent().next().find('div:first').fadeIn();
    $(this).parent().parent().next().fadeIn();
    $(this).parent().parent().fadeOut('fast');
    return false;
  });
  
  $('li > div.gross a.zurueck').click(function () {
    if ($(this).hasClass('inactive')) return false;
    
    var actEl = $(this).parent().find('div.active');
    var nextEl = $(this).parent().find('div.active').prev();
    
    nextEl.fadeIn().addClass('active');
    actEl.fadeOut().removeClass('active');
    adjustPager();
    return false;
  });
  
   $('li > div.gross a.naechstes').click(function () {
    if ($(this).hasClass('inactive')) return false;
    
    var actEl = $(this).parent().find('div.active');
    var nextEl = $(this).parent().find('div.active').next();
    
    nextEl.fadeIn().addClass('active');
    actEl.fadeOut().removeClass('active');
    adjustPager();
    return false;
  });
   
  $('div#content > a, div.overlay.kontakt #kontakt > a, div.overlay.kontakt .clickarea').click(function (ecvt) {
    $('.overlay.kontakt').fadeToggle('medium', function () {
      $('div.overlay.kontakt #kontakt form').each(function () {
        this.reset();
      });
      $('div.overlay.kontakt #kontakt .page1').show();
      $('div.overlay.kontakt #kontakt .page2').hide();
    });
    return false;
  });
  
  $('#webshop a, div.overlay.shop #shop > a, div.overlay.shop .clickarea').click(function (evt) {
    $('.overlay.shop').fadeToggle('medium', function () {
      $('div.overlay.shop #shop form').each(function () {
        this.reset();
      });
      $('div.overlay.shop #shop .page1').show();
      $('div.overlay.shop #shop #kiste').show();
      $('div.overlay.shop #shop .page2').hide();
      $('div.overlay.shop #shop .page3').hide();
    });
    $('#webshop').removeClass('active');
    return false;
  });
  
  $('div.overlay.kontakt #kontakt input.submit').click(function () {
    submitKontaktForm($(this).parents("form"));
    return false;
  });
  
  $('div.overlay.shop #shop .page1 input.submit').click(function () {
    submitShopFormStep1($(this).parents("form"));
    return false;
  });
  
  $('div.overlay.shop #shop .page2 input.submit').click(function () {
    submitShopFormStep2($(this).parents("form"));
    return false;
  });
  
  $('div.overlay input.text, div.overlay textarea').keyup(function () {
    resetForm($(this).parents("form"));
    return false;
  });
  $('div.overlay input.text, div.overlay textarea, div.overlay input.radio, div.overlay input.checkbox, div.overlay select').focus(function () {
    resetForm($(this).parents("form"));
    return false;
  });
  
  $('div.overlay a.toggle').click(function () {
    $(this).parents('.overlay').find('div.toggle').slideToggle();
    return false;
  });
  
  $('div.overlay.shop #shop .page2 #nachname, div.overlay.shop #shop .page2 #vorkasse').change(function () {
    updateTotalSum();
    //return false;
  });
 
  $('body #agecheck a.ja').mouseenter(function () {
    $('body #agecheck div').removeClass();
    $('body #agecheck div').addClass('ja');
    return false;
  });
  
  $('body #agecheck a.nein').mouseenter(function () {
    $('body #agecheck div').removeClass();
    $('body #agecheck div').addClass('nein');
    return false;
  });
  
  $('body #agecheck a.ja, body #agecheck a.nein').mouseleave(function () {
    $('body #agecheck div').removeClass();
    return false;
  });
  
  $('body #agecheck a.nein').click(function () {
    $('body #agecheck a.ja, body #agecheck a.nein').unbind();
    return false;
  });
  
  $('body #agecheck a.ja').click(function () {
    $('body #agecheck a.ja, body #agecheck a.nein').unbind();
    $('body #agecheck').fadeOut('medium');
    if (hasFlash) {
      $('body #contentWrapper').fadeIn('medium');
      $('body #flashWrapper').css({top: '-156px'});
      $('body #flashWrapper').animate({opacity:  1.0}, 'medium');
      playVideo();
    } else {
      $('body #contentWrapper').fadeIn('medium', function () {
        animateHeadlines(false);
      });
    }
    return false;
  });
  
  adjustPager();
  updateLayout();
  $(window).resize(updateLayout);
})

animateHeadlines = function (toggle) {
  $('body #content h1#headline'+(toggle ? 2 : 1)).animate({opacity: 1}, 3000).fadeOut(4000);
  $('body #content h1#headline'+(toggle ? 1 : 2)).animate({opacity: 1}, 3000).fadeIn(4000, function() {
    animateHeadlines(!toggle);
  });
}


playVideo = function () {
  if (document.getElementById('flashContent').playVideo) {
    document.getElementById('flashContent').playVideo();
  } else {
    setTimeout("playVideo()", 500);
  }
}

adjustPager = function () {
  $('li > div.gross').each(function () {
    var i = $(this).find('div.active').index();
    if (i == 0) {
      $(this).find('a.zurueck').addClass('inactive');
    } else {
      $(this).find('a.zurueck.inactive').removeClass('inactive')
    }
    if (i == $(this).find('div').length-1) {
      $(this).find('a.naechstes').addClass('inactive');
    } else {
      $(this).find('a.naechstes.inactive').removeClass('inactive')
    }
  });
}

updateLayout = function () {
  $('.overlay').css({width: $(document).width()+'px', height: $(document).height()+'px'});
  $('.overlay .clickarea').css({width: $(document).width()+'px', height: $(document).height()+'px'});
  if ($(window).width() > 1040) {
    $('#flashWrapper').css({left: ($(window).width()/2-$('#content').width()/2-250)+'px'});
  } else {
    $('#flashWrapper').css({left: '-220px'});
  }
}

validateForm = function (form) {
  var validates = true;
  $(form.find('input.text, textarea, select')).each(function () {
    if (
        ($(this).hasClass('email') && $(this).val() != '' && !$(this).val().match(/^.+@.+\..+$/)) ||
        ($(this).hasClass('number') && !$(this).val().match(/^\d*$/)) ||
        ($(this).hasClass('amount') && (!$(this).val().match(/^\d*$/) || $(this).val() < 1)) ||
        ($(this).hasClass('required') && $(this).val().replace(/\s*/, '') == '')
    ) {
      $(this).parent().addClass('error');
      validates = false;
    }
  });
  $(form.find('input.radio.required')).each(function () {
    if (!$(this).attr('checked') && form.find('input[name=' + $(this).attr('name') + ']:checked').length == 0) {
      $(this).parent().addClass('error');
      validates = false;
    }
  });
  
  return validates;
}

formData = function (form) {
  var data = {};
  $(form.find('input.text, input.radio, textarea, select')).each(function () {
    if ($(this).attr('type') == 'checkbox') {
      data[$(this).attr('name')] = $(this).attr('checked') ? 1 : 0;
    } else {
      data[$(this).attr('name')] = $(this).val();
    }
  });
  return data;
}

submitKontaktForm = function (form) {
  if (!validateForm(form)) return false;
  
  var data = formData(form);
  var myContactData = $("#contactForm").serialize(); 

  //console.log(data);
  $.ajax({  
    type: "POST",  
    url: contactUrl+'?'+myContactData, 
    success: function(evt, request, settings) {  
      //console.log("success");
    }  
  });  
  $('div.overlay #kontakt .page1').fadeOut('fast', function () {
    $('div.overlay #kontakt .page2').fadeIn('fast');
  });
}

var shopData;
submitShopFormStep1 = function (form) {
  if (!validateForm(form)) return false;
  
  shopData = formData(form);
  
  updateTotalSum();
  
  $('div.overlay #shop .page1').fadeOut('fast', function () {
    $('div.overlay #shop .page2').fadeIn('fast');
  });
}

submitShopFormStep2 = function (form) {
  if (!form.find('#nachname').attr('checked') && !form.find('#vorkasse').attr('checked')) {
    alert('Bitte wählen Sie eine Zahlungsart.');
    return false;
  }

  if (!form.find('#terms').attr('checked')) {
    alert('Bitte akzeptieren Sie die allgemeinen Geschäftsbedingungen.');
    return false;
  }
  
  shopData['zahlungsart'] = (form.find('#nachname').attr('checked') ? 'nachname' : 'vorkasse');
  var myShopData = $("#shopForm").serialize();

  //console.log(data);
  $.ajax({  
    type: "POST",  
    url: shopUrl+'?'+myShopData, 
    success: function(evt, request, settings) {  
      //console.log("success");
	  if(evt === false){
//		  alert('mail nicht verschickt');
	  }
	  else{
//		  alert('mail abgeschickt');
	  }
    }  
  });

  $('div.overlay #shop .page2').fadeOut('fast');
  $('div.overlay #shop #kiste').fadeOut('fast', function () {
    $('div.overlay #shop .page3').fadeIn('fast');
  });
}

updateTotalSum = function () {
  var price = 29.80 * shopData.menge;
  var deposit = 3.10 * shopData.menge;
  var delivery = ($('div.overlay.shop #shop .page2 #nachname').attr('checked') ? 4.28 : 0);
  $('div.overlay #shop .page2 span.price').html(price.toFixed(2).replace(/\./, ',')+' €');
  $('div.overlay #shop .page2 span.deposit').html(deposit.toFixed(2).replace(/\./, ',')+' €');
  $('div.overlay #shop .page2 span.delivery').html(delivery.toFixed(2).replace(/\./, ',')+' €');
  $('div.overlay #shop .page2 span.sum').html((price+deposit+delivery).toFixed(2).replace(/\./, ',')+' €');
}

resetForm = function (form) {
  $(form.find('label.error')).removeClass('error');
}

closeVideoLayer = function () {
  $('#flashContent').hide();
  animateHeadlines(false);
}

closeCoverLayer = function () {
  $('#flashCover').fadeOut('fast');
}

