function init_navigation() {
  // Navigation Menu
  $('#navigation').superfish({
    delay:       500,                            // one second delay on mouseout 
    animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
    speed:       'fast',                          // faster animation speed 
    autoArrows:  false,                           // disable generation of arrow mark-up 
    dropShadows: false                            // disable drop shadow
  });
}

function init_defaultInputValues() {
  var searchinput = $('#inputSearch');
  searchinput.focus(function() {
    var labelcmp = BDS_LANG.get('JS_SEARCH_LABEL');
    if (searchinput.val() == labelcmp) {
      searchinput.val('');
    }
  });

  searchinput.focusout(function() {
    var labelcmp = BDS_LANG.get('JS_SEARCH_LABEL');
    if (searchinput.val() == labelcmp || searchinput.val() == '') {
      searchinput.val(labelcmp);
    }
  });

  var inputMail = $('#inputMail');
  inputMail.focus(function() {
    var labelcmp = BDS_LANG.get('JS_INC_CMP_LOGIN_RIGHT_EMAIL');
    if (inputMail.val() == labelcmp) {
      inputMail.val('');
    }
  });

  inputMail.focusout(function() {
    var labelcmp = BDS_LANG.get('JS_INC_CMP_LOGIN_RIGHT_EMAIL');
    if (inputMail.val() == labelcmp || inputMail.val() == '') {
      inputMail.val(labelcmp);
    }
  });

  var inputPassword = $('#inputPassword');
  inputPassword.focus(function() {
    var labelcmp = BDS_LANG.get('JS_PASSWORD_CMP');
    if (inputPassword.val() == labelcmp) {
      inputPassword.val('');
    }
  });

  inputPassword.focusout(function() {
    var labelcmp = BDS_LANG.get('JS_PASSWORD_CMP');
    if (inputPassword.val() == labelcmp || inputPassword.val() == '') {
      inputPassword.val(labelcmp);
    }
  });

}

function init_submitLink() {
  $("#content a.jq_submit_link").each(function() {
    $(this).click(function() {
      var form = $(this).closest('form');
      $(this).fadeTo('slow', 0.5);
      form.submit();
      return false;
    });
  });
}

function init_variantSubmitLink() {
  $("#content a.jq_variant_submit_link").each(function() {
	variantForm = $(this).closest('form');
	$(this).click(function() {
      variantsOverlay = bds.openOverlay($('#variants_popup'), { modal:false });
      return false;
    });
  });
}

function init_articlesetSubmitLink(){
	  $("#content a.jq_articleset_submit_link").each(function() {
			articlesetForm = $(this).closest('form');
			$(this).click(function() {
		      articlesetOverlay = bds.openOverlay($('#articleset_popup'), { modal:false });
		      return false;
		    });
		  });
}

function init_removeSubmitLink() {
  $("#jq_submit_basket_remove").click(function() {
    var form = $(this).closest('form');
    var submitBtn = $(this);
    var hiddenField = $('<input />');
    hiddenField.attr('type', 'hidden');
    hiddenField.attr('name', 'removeBtn');
    hiddenField.val('remove');
    hiddenField.appendTo(form);
    submitBtn.fadeTo('slow', 0.5);
    form.submit();
    return false;
  });
}

function init_updateSubmitLink() {
  $("#jq_submit_basket_update").click(function() {
    var form = $(this).closest('form');
    var submitBtn = $(this);
    var hiddenField = $('<input />');
    hiddenField.attr('type', 'hidden');
    hiddenField.attr('name', 'updateBtn');
    hiddenField.val('update');
    hiddenField.appendTo(form);
    submitBtn.fadeTo('slow', 0.5);
    form.submit();
    return false;
  });
}

function init_showShipAdressLink() {
  $("#jq_submit_showShipAdress").click(function() {
    var form = $(this).closest('form');
    var submitBtn = $(this);
    var hiddenField = $('<input />');
    hiddenField.attr('type', 'hidden');
    hiddenField.attr('name', 'blshowshipaddress');
    hiddenField.val('1');
    hiddenField.appendTo(form);
    var hiddenField2 = $('<input />');
    hiddenField2.attr('type', 'hidden');
    hiddenField2.attr('name', 'blhideshipaddress');
    hiddenField2.val('0');
    hiddenField2.appendTo(form);
    submitBtn.fadeTo('slow', 0.5);
    form.submit();
    return false;
  });
  $("#jq_submit_doNotShowShipAdress").click(function() {
    var form = $(this).closest('form');
    var submitBtn = $(this);
    var hiddenField = $('<input />');
    hiddenField.attr('type', 'hidden');
    hiddenField.attr('name', 'blshowshipaddress');
    hiddenField.val('0');
    hiddenField.appendTo(form);
    var hiddenField2 = $('<input />');
    hiddenField2.attr('type', 'hidden');
    hiddenField2.attr('name', 'blhideshipaddress');
    hiddenField2.val('1');
    hiddenField2.appendTo(form);
    submitBtn.fadeTo('slow', 0.5);
    form.submit();
    return false;
  });
}

function init_filter() {

  if (typeof BL_HAS_ACTIVE_EXPANDED_FILTER !== "undefined") {
    if (BL_HAS_ACTIVE_EXPANDED_FILTER == true) {
      $("#productFilterExpanded").toggle();
      $("#productFilterContent").toggleClass('expanded');
    }
  }

  $("#productFilterContent").find('a.jq_filter_expand').click(function(event) {
    $("#productFilterExpanded").slideToggle();
    $("#productFilterContent").toggleClass('expanded');
    event.preventDefault();
  });
  $("#productFilterContent").find('.jq_color_select select').click(
      function(event) {
        $(this).blur();
        $("#colorSelect").fadeToggle();
        event.preventDefault();
      });
  $("#colorSelect").find('.jq_close').click(function(event) {
    $("#colorSelect").fadeToggle();
    event.preventDefault();
  });
  $(".labelCheckbox").find('.checkbox').change(function(event) {
    $("#_filterlist").submit();
  });
  $(".labelRadio").find('.radio').change(function(event) {
    $("#_filterlist").submit();
  });
  $(".dropdown").change(function(event) {
    $("#_filterlist").submit();
  });

  /*
  for (var i=0; i<3; i++) {
	  if ( typeof BDS_SLIDER_DIV_ID_+i !== "undefined" ) {
		  $("#"+BDS_SLIDER_DIV_ID_+i).slider({
				range: true,
				min: BDS_LOWER_THRESH_+i,
				max: BDS_HEIGHER_THRESH_+i,
				values: [ BDS_LOWER_THRESH_+i, BDS_HEIGHER_THRESH_+i ],
				step: 1,
				slide: function( event, ui ) {
					$( "#"+BDS_SLIDER_LABEL_ID_+i ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
				}
			});
		   
		  $( "#"+BDS_SLIDER_LABEL_ID_+i ).val( "$" + $( "#"+BDS_SLIDER_DIV_ID_+i ).slider( "values", 0 ) +
					" - $" + $( "#"+BDS_SLIDER_DIV_ID_+i).slider( "values", 1 ) );
	  }
  }
   */

  if (typeof BDS_SLIDER_DIV_ID_0 !== "undefined") {
    $("#" + BDS_SLIDER_DIV_ID_0).slider(
        {
          range : true,
          min : BDS_MINIMUM_0,
          max : BDS_MAXIMUM_0,
          values : [ BDS_LOWER_THRESH_0, BDS_HEIGHER_THRESH_0 ],
          step : 5,
          slide : function(event, ui) {
            $("#" + BDS_SLIDER_LABEL_ID_0).val(
                "" + ui.values[0] + " - " + ui.values[1] + " cm");
            $("#" + BDS_HIDDEN_LOWER_THRESH_ID_0).val(ui.values[0]);
            $("#" + BDS_HIDDEN_HEIGHER_THRESH_ID_0).val(ui.values[1]);
          },
          stop : function(event, ui) {
            if (BDS_MINIMUM_0 != BDS_MAXIMUM_0) {
                //$("#rangedSliderMoved_0").val("1");
              $("#_filterlist").submit();
            }
          }
        });

    $("#" + BDS_SLIDER_LABEL_ID_0).val(
        "" + $("#" + BDS_SLIDER_DIV_ID_0).slider("values", 0) + " - "
            + $("#" + BDS_SLIDER_DIV_ID_0).slider("values", 1) + " cm");
  }

  if (typeof BDS_SLIDER_DIV_ID_1 !== "undefined") {
    $("#" + BDS_SLIDER_DIV_ID_1).slider(
        {
          range : true,
          min : BDS_MINIMUM_1,
          max : BDS_MAXIMUM_1,
          values : [ BDS_LOWER_THRESH_1, BDS_HEIGHER_THRESH_1 ],
          step : 5,
          slide : function(event, ui) {
            $("#" + BDS_SLIDER_LABEL_ID_1).val(
                "" + ui.values[0] + " - " + ui.values[1] + " cm");
            $("#" + BDS_HIDDEN_LOWER_THRESH_ID_1).val(ui.values[0]);
            $("#" + BDS_HIDDEN_HEIGHER_THRESH_ID_1).val(ui.values[1]);
          },
          stop : function(event, ui) {
            if (BDS_MINIMUM_1 != BDS_MAXIMUM_1) {
              //$("#rangedSliderMoved_1").val("1");
              $("#_filterlist").submit();
            }
          }
        });
    $("#" + BDS_SLIDER_LABEL_ID_1).val(
        "" + $("#" + BDS_SLIDER_DIV_ID_1).slider("values", 0) + " - "
            + $("#" + BDS_SLIDER_DIV_ID_1).slider("values", 1) + " cm");
  }

  if (typeof BDS_SLIDER_DIV_ID_2 !== "undefined") {
    $("#" + BDS_SLIDER_DIV_ID_2).slider(
        {
          range : true,
          min : BDS_MINIMUM_2,
          max : BDS_MAXIMUM_2,
          values : [ BDS_LOWER_THRESH_2, BDS_HEIGHER_THRESH_2 ],
          step : 5,
          slide : function(event, ui) {
            $("#" + BDS_SLIDER_LABEL_ID_2).val(
                "" + ui.values[0] + " - " + ui.values[1] + " cm");
            $("#" + BDS_HIDDEN_LOWER_THRESH_ID_2).val(ui.values[0]);
            $("#" + BDS_HIDDEN_HEIGHER_THRESH_ID_2).val(ui.values[1]);
          },
          stop : function(event, ui) {
            if (BDS_MINIMUM_2 != BDS_MAXIMUM_2) {
               // $("#rangedSliderMoved_2").val("1");
              $("#_filterlist").submit();
            }
          }
        });
    $("#" + BDS_SLIDER_LABEL_ID_2).val(
        "" + $("#" + BDS_SLIDER_DIV_ID_2).slider("values", 0) + " - "
            + $("#" + BDS_SLIDER_DIV_ID_2).slider("values", 1) + " cm");
  }

}

function init_carousel() {
  var imageCarousel = $('#imageCarouselContainer');
  if (imageCarousel) {
    imageCarousel.jCarouselLite( {
      btnNext : ".next",
      btnPrev : ".prev",
      circular : false,
      visible : 4
    });
  }
  ;
  var lastArticlesCarousel = $('#lastArticlesCarouselContainer');
  if (lastArticlesCarousel) {
    lastArticlesCarousel.jCarouselLite( {
      btnNext : ".nextL",
      btnPrev : ".prevL",
      circular : false,
      visible : 5
    });
  }
  ;
  var crossSellingCarousel = $('#crossSellingCarouselContainer');
  if (crossSellingCarousel) {
    crossSellingCarousel.jCarouselLite( {
      btnNext : ".nextL",
      btnPrev : ".prevL",
      circular : false,
      visible : 5
    });
  }
  ;
  var bestsellerCarousel = $('#bestsellerCarouselContainer');
  if (bestsellerCarousel) {
    bestsellerCarousel.jCarouselLite( {
      btnNext : ".nextB",
      btnPrev : ".prevB",
      circular : false,
      visible : 3
    });
  }
  ;
}

function init_tabs() {
  var detailTabs = $('#detailTabs');
  if (detailTabs) {
    if (typeof blSelectTab3 !== "undefined") {
      if (blSelectTab3) {
        detailTabs.tabs();
        detailTabs.tabs("select", 2);
      }
    } else {
      detailTabs.tabs();
    }
  }
  ;
}


function init_showCustomArticleHelp() {
  var customArticleHelp = $('#selectedArticleHelp');
  if (customArticleHelp) {
    $("#selectedArticleHelp .jq_showHelp").hover(function(event) {
      $("#selectedArticleHelp div").fadeToggle();
      event.preventDefault();
    });
  };
  var statusLegend = $('#statusLegend');
  if (customArticleHelp) {
    $("#statusContainer .jq_showHelp").hover(function(event) {
      $("#statusLegend").fadeToggle();
      event.preventDefault();
    });
  };
}

function init_fb_box()
{
	$("#jq_fb_trigger").fancybox({
			'padding'		 : 0,
			'transitionIn'	 : 'fade',
			'transitionOut'	 : 'fade',
			'overlayOpacity' : 0.5,
			'autoDimensions' : true,
			'autoScale'	     : false,
			'titleShow'      : false,
			'width'			 : 600,
			'height'		 : 600,
			'speedIn'		 : 600, 
			'speedOut'		 : 200,
			'type'			 : 'iframe'
  });
}

$(document).ready(function() {
  // onload-functions
    init_navigation(); // navigation
    $('p.eml').defuscate(); // E-Mail-Adresses
    init_submitLink(); // init all submit links
    init_variantSubmitLink(); // init all variant submit links
    init_articlesetSubmitLink(); // init all articleset submit links
    init_filter(); // init filter box
    init_carousel(); // all carousels
    init_tabs(); // all tabs
    init_defaultInputValues(); // default input values
    init_removeSubmitLink(); // remove submit button basket
    init_updateSubmitLink(); // update submit button basket
    init_showShipAdressLink(); // open delivery address on user.tpl
  init_showCustomArticleHelp();  // show helpdiv for custom articles
  init_fb_box(); // init modal container for fb box
  if( $('#catLongDesc').length > 0 ) {
  $('#mainPage').after($('#catLongDesc'));
  }
  });

if (typeof bds == 'undefined')
  bds = {};
//
//
// Open overlay function
// id -> of the dom node to display in Overlay
// options -> fancyBox-Options
// Returns Overlay-Object

bds.openOverlay = function(id, options) {
  return (new bds.Overlay(id, options)).open();
};

bds.Overlay = function(id, options) {

  var content = null;

  // check for backup
  if (typeof bds.Overlay.contentBackup[id] != 'undefined') {
    content = bds.Overlay.contentBackup[id];
  } else {
    // fancy box destroys content, so make a backup
    var tmp = $('<div></div>');
    tmp.append($(id));
    content = tmp.html();
    bds.Overlay.contentBackup[id] = content;
  }

  this.content = $(content);
  this.options = options;

  this.options = $.extend( {
    content : this.content
  }, this.options);
};

// static
bds.Overlay.contentBackup = {};

// Overlay Methods
bds.Overlay.prototype = {
  open : function() {
    this.content.show();
    this.content.css('visibility', 'hidden');

    this.registerClickFunctions();
    this.renderTextContents();
    this.renderInputFieldValues();

    $.fancybox(this.options);

    this.content.css('visibility', 'visible');
    return this;
  },

  close : function() {
    $.fancybox.close();
  },

  getContent : function() {
    return this.content;
  },

  resize : function() {
    return $.fancybox.resize();
  },

  showSpinner : function() {
    return $.fancybox.showActivity();
  },

  hideSpinner : function() {
    return $.fancybox.hideActivity();
  },

  // private
  registerClickFunctions : function() {
    if (!this.options.clickFunctions)
      return;
    var content = this.content;
    $.each(this.options.clickFunctions, function(selector, callback) {
      var element = content.find(selector);
      if (element.length != 0) {
        element.unbind('click').bind('click', callback);
      }
    });
  },

  renderTextContents : function() {
    if (!this.options.textContents)
      return;
    var content = this.content;
    $.each(this.options.textContents, function(selector, html) {
      var element = content.find(selector);
      if (element.length != 0) {
        element.html(html);
      }
    });
  },

  renderInputFieldValues : function() {
    if (!this.options.inputFieldValues)
      return;
    var content = this.content;
    $.each(this.options.inputFieldValues, function(selector, value) {
      var element = content.find(selector);
      if (element.length != 0) {
        element.attr('value', value);
      }
    });
  }
};

// ============================================================================
// Standard Overlay Popups
// ----------------------------------------------------------------------------

// -------
// Confirm
// Usage:
/*
 * bds.Overlay.confirm({ title: 'Merkliste löschen', description: 'Wollen Sie
 * die Merkliste wirklich löschen?', okButtonText: 'Jetzt löschen',
 * cancelButtonText: 'Nicht löschen' }, function (ok) { if (ok) { form.submit(); } } );
 */
bds.Overlay.confirm = function(options, callback) {
  var textContents = {};
  $.each(options, function(k, v) {
    textContents['.jq_' + k] = v;
  });
  var overlay = bds.openOverlay('#jq_genericConfirm', {
    clickFunctions : {
      '.jq_cancelButton' : function() {
        callback(false);
        overlay.close();
      },
      '.jq_okButton' : function() {
        callback(true);
        overlay.close();
      }
    },
    textContents : textContents
  });
};

// -------
// MessageBox
// Usage:
/*
 * bds.Overlay.messageBox(
 * BDS_LANG.get('JS_NOTICE_LIST_MESSAGE_BOX_NO_LIST_CHOSEN_TITLE'),
 * BDS_LANG.get('JS_NOTICE_LIST_MESSAGE_BOX_NO_LIST_CHOSEN_DESCRIPTION') );
 */
bds.Overlay.messageBox = function(title, description) {
  var textContents = {
    '.jq_title' : title,
    '.jq_description' : description
  };
  var overlay = bds.openOverlay('#jq_genericMessageBox', {
    clickFunctions : {
      '.jq_okButton' : function() {
        overlay.close();
      }
    },
    textContents : textContents
  });
};

// ------
// Prompt
// Usage:
/*
 * bds.Overlay.prompt({ title: 'Neue Merkliste erstellen', description: 'Bitte
 * geben Sie einen Namen ein', okButtonText: 'Jetzt erstellen',
 * cancelButtonText: 'Abbrechen', label: 'Name' }, function (value) { if (value
 * !== false) { form.fnc.value = 'createNoticeList'; form.newListName.value =
 * value; form.submit(); } } );
 */
bds.Overlay.prompt = function(options, callback) {
  var textContents = {};
  $.each(options, function(k, v) {
    textContents['.jq_' + k] = v;
  });
  var overlay = bds.openOverlay('#jq_genericPrompt', {
    clickFunctions : {
      '.jq_cancelButton' : function() {
        callback(false);
        overlay.close();
      },
      '.jq_okButton' : function() {
        callback(overlay.getContent().find('.jq_value').attr('value'));
        overlay.close();
      }
    },
    textContents : textContents
  });
};

// ----------------------------
// Modal unclosable Message Box
// Usage:
/*
 * bds.Overlay.modalUnclosableMessage( 'Bitte warten', 'Die gewählte Liste wird
 * geladen.');
 */
bds.Overlay.modalUnclosableMessage = function(title, description, callback) {
  var textContents = {
    '.jq_title' : title,
    '.jq_description' : description
  };
  var overlay = bds.openOverlay('#jq_genericModalUnclosableMessage', {
    textContents : textContents,
    modal : true,
    onComplete : ($.isFunction(callback)) ? callback : null
  });
  return overlay;
};

// ============================================================================

bds.openPrintWindow = function(url) {
  var printWindow = window.open(url, 'printWindow',
      'height=600,width=725,resizable=no,scrollbars=yes');
  printWindow.focus();
};

// ============================================================================
// serializes form-data from given form-id to json
// required libraries: jquery, json2.js (http://www.JSON.org/json2.js)
bds.serializeForm = function(id) {
  var result = {};
  $($(id).serializeArray()).each(function(key, obj) {
    result[obj.name] = obj.value;
  });
  return JSON.stringify(result);
};

// ============================================================================
// flash messages and errors for js

bds.flashError = function(message) {
  this.genericFlashMessage('error', message);
};

bds.flashMessage = function(message) {
  this.genericFlashMessage('message', message);
};

bds.clearFlash = function() {
  $('#jq_flashMessageContainer').html('');
};

bds.genericFlashMessage = function(type, message) {
  var container = $('#jq_flashMessageContainer');
  container.html('<div class="messageBox ' + type + '">' + message + '</div>');
};

$('#bds_simulate_wait').click(function() {
	var overlay = bds.openOverlay($('#jq_uncloseable_modal'), { modal: true,
  overlayOpacity: 0.3}
	);
});

