// Jquery commands
$(document).ready(function() {
    
    // Main Navigation
    var mainNavigation = new DickerFisch('#mainNavContainer',{NavTimeout: new Array(99,250),Fit2ViewportClass:'leftover',LimitLinks:0});
    
    // add body bg image when page has loaded
    if($("body").is(".webshopBody"))
      $("body").removeClass("webshopBody").addClass("webshopBodyBg");
    
    // Pop-Ups    
    $(".popup").click (function () {
      $(this).popupwin();
      return false;
    }); 
    $(".popupinfo").click (function () {
      $(this).popupwin({
        width:      "600",
        height:     "450",    
        directories:"no",
        location:   "no",
        menubar:    "no",
        resizable:  "yes",
        scrollbars: "yes",
        status:     "no",
        toolbar:    "no"
      });
      return false;
    });    
    $(".popuppaypal").click (function () {
      $(this).popupwin({
        width:      "400",
        height:     "350",    
        directories:"no",
        location:   "no",
        menubar:    "no",
        resizable:  "yes",
        scrollbars: "yes",
        status:     "no",
        toolbar:    "no"
      });
      return false;
    }); 
    $(".popupmoredetails").click (function () {
      $(this).popupwin({
        width:      "420",
        height:     "410",    
        directories:"no",
        location:   "no",
        menubar:    "no",
        resizable:  "yes",
        scrollbars: "no",
        status:     "no",
        toolbar:    "no"
      });
      return false;
    });   
    
    $(".packstationPic").click (function () {      
      if ($("#user_adressid").val()!=-2) 
        $("#user_adressid").val("-2");
      ReloadAdress();
    });   
    
    $(".close").click (function () {
      self.close();
    });   
    
    // search form searchterm input
    $('#subnavSucheInput').inputAdvise();
    
    // login form emailaddress input
    $('#kontoEmail').inputAdvise();
    
    // login form password input
    $('#kontoPasswort').inputAdvise();
    
    // newsletter form emailaddress input
    $('#newsletterEmail').inputAdvise();
    $('.newsletterSubmit').click(function() {
      obj = $('#newsletterEmail');
      if (obj.attr("title") == obj.val())
        obj.val("");
    });
    
    // article amount increase
    $("img.iconMore").click(function(){
      var rel = $(this).attr("rel");
      var input = $("input#"+rel);    
      input.attr("value", changeArticleAmount(input.attr("value"), "increase"));
    });
    
    // article amount decrease
    $("img.iconLess").click(function(){
      var rel = $(this).attr("rel");
      var input = $("input#"+rel);      
      input.attr("value", changeArticleAmount(input.attr("value"), "decrease"));
    }); 
    
    // form checks
    $("#registerForm, #userForm, #useraccountForm, #orderForm, #deCardKonto").submit(function(){
    
      /* birthdate check */   
        // get birthdate form elements    
        var oBdDay   = $("#user_birthday");
        var oBdMonth = $("#user_birthmonth");
        var oBdYear  = $("#user_birthyear");    
        // if not existing, don't validate
        if (oBdDay.length && oBdDay.length && oBdDay.length)
        {
	      // do birthdate validation  
	      var result = validateBirthdate({day:oBdDay.val(), month:oBdMonth.val(), year:oBdYear.val(), minAge:18});	
	      if (result == false) {
            alert("Bitte geben Sie ein gültiges Geburtsdatum ein!");
	        oBdDay.focus();
	        return false;
	      }
        }
      /* deutschlandcard validation */
        // validate deutschlandcard number
        var oDcard  = $("#bds_dcardnumber");
        if (oDcard.val()) {
          var result = validateDcard({number:oDcard.val()});
          if (result == false) {
            alert("Bitte geben Sie eine gültige DeutschlandCard Kartennummer an!\n\nDiese darf keine Leerzeichen enthalten und muss 10 Zeichen lang sein.");
	        oDcard.focus();
	        return false;
          }
        }
      	
      return true;
    });
       
    // vendor navigation on sitemap
    $("#h0").css({display:"block"}); 
    $("#l0").css({fontWeight:"bold"});
    $("a.openvendor").click(function() {
      var firstLetter = $(this).attr("rel");
      $("a.openvendor").each(function () {
        $(this).css({fontWeight:"normal"});
      }); 
      $(this).css({fontWeight:"bold"});   
      $("div.vendorlist").each(function () {
        var divid = $(this).attr("id");
        if (divid == firstLetter) {
          $(this).css({display:"block"});
        } else {
          $(this).css({display:"none"});      
        }
      });       
    });
    
    // greetingcard check
    $("#inputGiftSubmit").click(function(){
      if ($("#noCard:checked").val() == 0 && $("#giftmessage").val() != '')
      {
        alert("Bitte wählen Sie eine Grusskarte aus.");
        return false;
      }
    });
    
    // fix submit of user form for ie6
    $("#nextStepSubmit, #nextStepSubmit2").click(function(){
      if ($(".openShippingAddress").attr("name") == 'blshowshipaddress')
      {
        $(".openShippingAddress").attr("name", "");
      }      
    });  
    
    // add selected selectlist item to "tonoticelist" link
    $(".variantSelect").change(function(){
      $("#noticelistlink").attr("href", function(){
        return $(this).attr("href") + '&' + $(".variantSelect").attr("name") + '=' + $(".variantSelect").attr("value");
      });
    });   
});