/*global $, alertCart, alertQCart, document, jQuery, window, that */
function initSearchBox() {
   $("#searchbox").focus(function(){
      var searchTerm = $("#getsearchterm").attr("value");
      if ($(this).attr("value") === "Suchen" || $(this).attr("value") === "Search" || $(this).attr("value") === searchTerm) { $(this).attr("value",""); }
   });
   $("#searchbox").blur(function(){
      var searchTerm = $("#getsearchterm").attr("value");
      if (jQuery.trim($(this).attr("value")) === "") { $(this).attr("value", searchTerm); }
   });
}

function showMap (mapname) {
   if ($('#standort-map') && mapname) {
      $('#standort-map').attr("src", '/mall/1/staticcontent/'+mapname+'.gif');
   }
}
   
function containerToggle(toggle, container, button_down, button_up) {
   if ($(toggle) && $(container)) {
      $(toggle).click(function() {
         $(container).slideToggle("slow", function() {
            if (button_down && button_up) {
               var imgsrc = $(toggle).attr("src");
      			if (imgsrc) {
                  var matches = imgsrc.match(/_up/);
                  var imgsrcON;
         			if (!matches) {
            			imgsrcON = imgsrc.replace(/_down.gif$/ig,"_up.gif");
            			$(toggle).attr("src", imgsrcON);
         			}
                  else {
            			imgsrcON = imgsrc.replace(/_up.gif$/ig,"_down.gif");
            			$(toggle).attr("src", imgsrcON);
                  }
               }
            }
         });
      });
      $(container).hide();
   }
}

function containerToggleClass(toggle, container, closed, open, toggle2, closed2, open2) {
   if ($(toggle) && $(container)) {
      $(container).hide();
      $(toggle).click(function() {
         $(container).slideToggle("slow", function() {
            if (open && closed) {
               
               if ( $(toggle).attr("class") ==  closed) {
                  $(toggle).removeClass(closed);
                  $(toggle).addClass(open);
                  if (open2 && closed2) {
                     $(toggle2).removeClass(closed2);
                     $(toggle2).addClass(open2);
                  }
               }
               else {
                  $(toggle).removeClass(open);
                  $(toggle).addClass(closed);
                  if (open2 && closed2) {
                     $(toggle2).removeClass(open2);
                     $(toggle2).addClass(closed2);
                  }
               }
            }
         });
      });
   }
}

function containerOpener(trigger) {
   $(trigger).click(function() {
      $("#cart-handle").click();
   });
}

function inputButtons(btplus, btminus) {
   $(btplus).click(function() {
      var basicset = $(this).parent('div').find('.basicset').attr('value');
      var currentval = $(this).parent('div').parent().find('.bt-price').attr('value');
      if (currentval && basicset) { 
         currentval=currentval.replace(/,/,"."); 
         basicset=basicset.replace(/,/,"."); 
         currentval = parseFloat(currentval);
         basicset = parseFloat(basicset);
      }
      else {currentval = basicset; }
      if (basicset && currentval <= 1000000) {
         var newval = currentval + basicset;
         newval = newval.toString();
         newval = newval.replace(/\./,","); 
         $(this).parent('div').parent().find('.bt-price').attr('value', newval);
      }
   });
   $(btminus).click(function() {
      var basicset = $(this).parent('div').find('.basicset').attr('value');
      var currentval = $(this).parent('div').parent().find('.bt-price').attr('value');
      if (currentval) { 
         currentval=currentval.replace(/,/,"."); 
         basicset=basicset.replace(/,/,"."); 
         currentval = parseFloat(currentval);
         basicset = parseFloat(basicset);
      }
      else {currentval = basicset; }
      if (basicset && currentval > basicset) {
         var newval = currentval - basicset;
         newval = newval.toString();
         newval = newval.replace(/\./,","); 
         $(this).parent('div').parent().find('.bt-price').attr('value', newval);
      }
   });
}

function jobToggle() {
   //$('div.job').hide();
  
   $('.toggle-img').toggle(function() {
      var that = this;
   	$(this).parent('h2').next('div').slideDown('fast',function() { $(that).attr("src","/mall/1/staticcontent/bt_up.gif"); });
   },function() {
      var that = this;
   	$(this).parent('h2').next('div').slideUp('fast',function() { $(that).attr("src","/mall/1/staticcontent/bt_down.gif"); });
   });

   $('.toggle-img').siblings("a").toggle(function() {
   	$(this).parent('h2').next('div').slideDown('fast',function() { 
         $(this).siblings("h2").children(".toggle-img").attr("src","/mall/1/staticcontent/bt_up.gif"); 
      });
   },function() {
   	$(this).parent('h2').next('div').slideUp('fast',function() { 
         $(this).siblings("h2").children(".toggle-img").attr("src","/mall/1/staticcontent/bt_down.gif"); 
      });
   });
}

function checkItemForm() {
   $('form.itemform').submit(function() {
      var value = false;
      value = $(this).find('.menge').attr("value");
      
      if (value != false) { return true; }
      else {
         /*
         $('#confirmusage').remove();
         if (lgConfirmShopUsage) {
            $('div.col-1st div').before('<p class="errormessage" id="confirmusage">'+lgConfirmShopUsage+'</p>')
         }
         */
         return false;
      }
   });
}

function cartHints() {
   $('div.cart-hint').hide();
   
   $('div.cart-line').children("span").wrap("<a href=\"#\"></a>");
   $('div.cart-line').children("a").css({"fontSize":"11px"}).click(function() {
   	$(this).parent('div').next('div').slideToggle('fast');
      return false;
   });
   $('div.cart-line:first').css({"marginTop":"20px"});
}

function containerToggleFAQ(class1, class2) {
   $('div.faq-detail').hide();
   $('.strong').removeClass("strong");
   
   $('.faq-button').click(function() {
   	$(this).parent('div').next('div').slideToggle('fast',function() {
         if (class1 && class2) {
            if ( $(this).parent('div').attr("class") ==  "faq "+class1+" "+class2) {
               $(this).parent('div').removeClass(class1);
               $(this).parent('div').removeClass(class2);
               $(this).parent('div').find(".faq-button").attr("src","/mall/1/staticcontent/bt_down.gif");
            }
            else {
               $(this).parent('div').addClass(class1);
               $(this).parent('div').addClass(class2);
               $(this).parent('div').find(".faq-button").attr("src","/mall/1/staticcontent/bt_up.gif");
            }
         }
      });
      return false;
   });
  
   $('div.faq-line').children("span").wrap("<a href=\"#\"></a>");
   $('div.faq-line').children("a").click(function() {
   	$(this).parent('div').next('div').slideToggle('fast',function() {
         if (class1 && class2) {
            if ( $(this).parent('div').attr("class") ==  "faq "+class1+" "+class2) {
               $(this).parent('div').removeClass(class1);
               $(this).parent('div').removeClass(class2);
               $(this).parent('div').find(".faq-button").attr("src","/mall/1/staticcontent/bt_down.gif");
            }
            else {
               $(this).parent('div').addClass(class1);
               $(this).parent('div').addClass(class2);
               $(this).parent('div').find(".faq-button").attr("src","/mall/1/staticcontent/bt_up.gif");
            }
         }
      });
      return false;
   }
   );
}

function initButtonMouseovers() {
   $('img.bt-down').hover(
      function() {
         var imgsrc = $(this).attr("src");
         if (imgsrc) {
            var matches_down = imgsrc.match(/_down/);
            var matches_up = imgsrc.match(/_up/);
            
            if (matches_down) { $(this).attr("src", imgsrc.replace(/_down.gif$/ig,"_down_mo.gif")); }
            else if (matches_up) { $(this).attr("src", imgsrc.replace(/_up.gif$/ig,"_up_mo.gif")); }
         }
      },
      function() {
         var imgsrc = $(this).attr("src");
         if (imgsrc) { 
            var matches_down = imgsrc.match(/_down/);
            var matches_up = imgsrc.match(/_up/);
            
            if (matches_down) { $(this).attr("src", imgsrc.replace(/_down_mo.gif$/ig,"_down.gif")); }
            else if (matches_up) { $(this).attr("src", imgsrc.replace(/_up_mo.gif$/ig,"_up.gif")); }
         }
      }   
   );
   
   $('.bt-right').hover(
      function() {
         var imgsrc = $(this).attr("src");
         if (imgsrc) { $(this).attr("src", imgsrc.replace(/.gif$/ig,"_mo.gif")); }
      },
      function() {
         var imgsrc = $(this).attr("src");
         if (imgsrc) { $(this).attr("src", imgsrc.replace(/_mo.gif$/ig,".gif")); }
      }
   );
}

function fixBuyteaserHeight() {

   var maxHeadHeight = 0;
   $('.buyteaserhead').each(function(){ if (this.offsetHeight>maxHeadHeight) {maxHeadHeight=this.offsetHeight;} });
   $('.buyteaserhead').each(function(){ $(this).css("height",maxHeadHeight); });
   
   var maxBodyHeight = 0;
   $('.buyteaserbody').each(function(){ if (this.offsetHeight>maxBodyHeight) {maxBodyHeight=this.offsetHeight;} });
   $('.buyteaserbody').each(function(){ $(this).css("height",maxBodyHeight); });
}

function fixThickboxLinks() {
   $('.thickbox').each(function() {
      var hrefsrc = $(this).attr("href");
      if (hrefsrc && !hrefsrc.match(/itemstandalone/)) {
         var hrefsrcON = hrefsrc.replace(/item/ig,"itemstandalone");
         hrefsrcON = hrefsrcON + "?KeepThis=true&TB_iframe=true&height=430&width=640&modal=true";
         $(this).attr("href", hrefsrcON);
      }
   });
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

function dynamicLoginFields() {
var input_value = $("#login-input").attr('value');
   var pw_value = $("#pw-input").attr('value');
   
   if (input_value != 'undefined' && pw_value != 'undefined') {
      $("#login-input").focus(function() { 
         if ($(this).attr('value') == input_value) { 
            $(this).attr('value',''); 
         }
      });
      $("#login-input").blur(function() { 
         if (!$(this).attr('value')) {
            $(this).attr('value',input_value);
         }
      });
      
      $("#pw-input").focus(function() {
         if ($(this).attr('value') == pw_value) {
            $(this).attr('value','');
         }
      });
      $("#pw-input").blur(function() { 
         if (!$(this).attr('value')) {
            $(this).attr('value',pw_value);
         }
      });
   }
}

function cartInsertedAlerts() {
   var alertmsg = false;
   var url = this.location.href;
   if (url.match(/qcartInserted=1/)) { alertmsg = alertQCart; }
   else if (url.match(/cartInserted=1/)) { alertmsg = alertCart; }

   if (alertmsg) {
      var de = document.documentElement;
      var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
      var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
   
      $("<div id='inserted-info'><p>"+alertmsg+"</p></div>")
      .insertBefore($("#top"))
      .css({top: (h/2)-35 + 'px', left: (w/2)-175 + 'px'})
      .animate({opacity: 1.0}, 1000)
      .fadeOut('slow', function() {
         $(this).remove();
      });
      
      if (alertmsg == alertCart) {
         $("#cart-handle").click();
      }
   }
}

function checkRegisterB2B() {
   var result = false;
   $('form#registerform').submit(function() {
      var b2b = $('#b2b').length;
      var b2b_cb = $('#b2b').attr("checked");
      var pw_digits = $('#passwd1').val().length;
      
      if ((b2b_cb && b2b_cb !== undefined) || b2b === 0) { result = true; }
      else {
         $('#confirmusage').remove();
         if (lgB2bErrorHint) {
            $('div.col-1st div').before('<p class="errormessage" id="confirmusage">'+lgB2bErrorHint+'</p>');
         }
         result = false;
      }
      
      if (pw_digits >= 6 ) { result = true; }
      else {
         $('#pwdigits').remove();
         if (lgPw6DigitErrorHint) {
            if (b2b === 0) $('.col-1st').prepend('<p class="errormessage" id="pwdigits">'+lgPw6DigitErrorHint+'</p>');
            else $('div.col-1st div').before('<p class="errormessage" id="pwdigits">'+lgPw6DigitErrorHint+'</p>');
         }
         else {
            if (b2b === 0) $('.col-1st').prepend('<p class="errormessage" id="pwdigits">Das Passwort muss mindestens 6 Zeichen enthalten</p>');
            else $('div.col-1st div').before('<p class="errormessage" id="pwdigits">Das Passwort muss mindestens 6 Zeichen enthalten</p>');
         }
         result = false;
      }
      if (result === false) return false;
      else return true;
   });
}

function secureEmail() {
   $('.secure_email').each(function() {
      var $email = $(this);
      var address = $email.text().replace(/\s*\{at\}\s*/, '@')
                                 .replace(/\s*\{punkt\}\s*/g, '.');
      $email.html('<a href="mailto:' + address + '">'+ address +'</a>');
   });
}

$(document).ready(function() {
   
   $('.lnkcursor').hover(function(){
     $(this).css("cursor", "pointer");
   },function(){
     $(this).css("cursor", "default");
   });

   $("#cart-container").css("position","absolute");
   //fixThickboxLinks();

   //Language
   containerToggle("#lang-toggle","#language-container");
   
   // Cart
   containerToggleClass("#upper-cart-toggle", "#cart-content", "upper-ct-closed", "upper-ct-open", "#cart-handle", "cart-closed", "cart-open");
   containerToggleClass("#cart-handle", "#cart-content", "cart-closed", "cart-open", "#upper-cart-toggle", "upper-ct-closed", "upper-ct-open");
   containerOpener(".opener");
   
   // Registrierung
   containerToggle("#bestandskunde-toggle","#bestandskunde-content", "_down.gif", "_up.gif");
   containerToggle("#neukunde-toggle","#neukunde-content", "_down.gif", "_up.gif");
   
   //Jobs   
   jobToggle();
   
   //FAQ
   containerToggleFAQ("faq-box", "active-bg");
   
   inputButtons(".bt-plus", ".bt-minus");
   initButtonMouseovers();
   fixBuyteaserHeight();
   
   dynamicLoginFields();
   cartInsertedAlerts();
   
   initSearchBox();
   
   checkRegisterB2B();
   
   cartHints();
   checkItemForm();
   
   secureEmail();
});
