Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
	//set defaults
	settings = jQuery.extend({
		scope: 'body',
		reverse: false
	}, settings);

	var pxVal = (this == '') ? 0 : parseFloat(this);
	var scopeVal;
	var getWindowWidth = function(){
		var de = document.documentElement;
		return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
	};

	/* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size.
		For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size.
		When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size)
		to get an accurate em value. */

	if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
		var calcFontSize = function(){
			return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
		};
		scopeVal = calcFontSize();
	}
	else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };

	var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
	return result;
};

$().ready(function() {
  window.setTimeout("$('#tobasketflash').fadeOut('slow')", 6000);

  $('a[rel="external"]').bind("click", function(e) {
    window.open($(this).attr("href"));

    e.preventDefault();
  });

  $('.dinfo').removeAttr("title");

  $('a[data-action="shipping"]').each(function() {
    var t = $(this);

    t.bind("click", function(e) {
      e.preventDefault();

      var amount = 1;

      var amfield = t.parent().parent().parent().find("input[class~=am]");
      if (amfield.length > 0) {
        amount = amfield.val();
      }

      if (amount == 0) amount = 1;

      t.qtip({
        content: {
          url: _basedir + 'index.php?cl=ajax_autocomplete&fnc=shipping&aid=' + t.data("aid") + '&am=' + amount
        },
        show: 'mousedown',
        hide: 'mouseout',
        style: {
          background: '#E8F5BE',
          color: '#000',
          border: {
            width: 3,
            radius: 5,
            color: '#0076BD'
          },
          width: 140
        },
        position: {
          corner: {
            tooltip: "topMiddle"
          }
        }
      });

      t.trigger("mousedown");

    });

  });

  var options, a;
  jQuery(function() {
    options = {
      serviceUrl: _basedir + 'index.php',
      params: {
        cl: "ajax_autocomplete",
        fnc: 'quicksearch'
      },
      width: 400,
      deferRequestBy: 250,
      minChars: 2,
      onSelect: function(value, data) {
        document.location.href = '?cl=details&anid=' + data;
      }
    };

    a = $('#searchstring').autocomplete(options);
  });

  $('.box.foldable').each(function() {
    var self = $(this);
    var content = self.children(".content");

    if (! self.hasClass("unfolded")) content.hide();
    self.addClass("folded");

    self.children(".head").click(function() {
      var s = $(this);
      if (content.css("display") == "block") {
        content.slideUp();
        self.addClass("folded").removeClass("unfolded");

      } else {
        content.slideDown();
        self.removeClass("folded").addClass("unfolded");

      }

      return false;
    });

  });

  var ps = $('.productslider');
  if (ps.length > 0) ps.productSlider({
    prevText: '&laquo; zurück',
    nextText: 'vor &raquo;',
    height: 200,
    //autoscroll: 10000
    itemsPerSlide: 4
  });

  var ps = $('.productslider2');
  if (ps.length > 0) ps.productSlider({
    autoscroll: 3500,
    itemsPerSlide: 1,
    minimap: true
  });

  var variantlist = $('#accessoires .variantslist .variantlist');

  if (variantlist) {
    variantlist.hide();

    var foldout_link = $('<div class="grid_2 alpha omega ralign"><a href="#" class="fout">Ausführungen anzeigen</a></div>');
    var foldout_func = function() {
      variantlist.hide();

      $(this).prev().slideDown();

      $('a.fout').show();
      $(this).children("a").hide();

      return false;
    };

    foldout_link.insertAfter(variantlist).bind("click", foldout_func);

  }

  $('.details #variants .product input.submit').hover(function() {
    var t = $(this);
    var amfield = t.prev();

    if (amfield.attr("value") == "0") {
      amfield.attr("value", "1");
    }
  }, function() {
    var t = $(this);
    var amfield = t.prev();

    if (amfield.attr("value") == "1" && ! amfield.data("locked") == true) {
      amfield.attr("value", "0");
    }

  });

  $('.details #variants .product input.am').keydown(function(e) {
    var t = $(this);

    if (e.keyCode == 48) t.data("locked", false);
    else t.data("locked", true);
  });

  $.fn.equalHeights = function(px) {
  	$(this).each(function(){
  		var currentTallest = 0;
  		$(this).children().each(function(i){
  			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
  		});
  		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
  		// for ie6, set height since min-height isn't supported
  		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
  		$(this).children().css({'min-height': currentTallest});
  	});
  	return this;
  };

  $.fn.sameHeight = function(maxcols) {
    var maxheight = 0;
    var i = 1;
    var lastthis = $();
    if (! maxcols) var maxcols = 2;

    //console.log("maxcols: " + maxcols);

    var calcHeight = function(what) {
      var height = $(what).height();

      //console.log("i " + i + " = " + height);

      if (height > maxheight) {
        maxheight = height;
        if (i == maxcols) {
          lastthis.height(maxheight);
        }
      }

      $(what).height(maxheight);

      if (i % maxcols == 0) {
        maxheight = 0;
        i = 0;
      }

      i++;

      lastthis = $(what);

    };

    // calc height for each col
    return this.each(function() {
      calcHeight(this);

    }); // end given elements loop

    return this;

  };

  /*
  $('#content .product.halfwidth').sameHeight();
  window.setTimeout("$('#content .product.halfwidth').sameHeight()", 2000);
  window.setTimeout("$('#content .product.halfwidth').sameHeight()", 5000);
  window.setTimeout("$('#content .product.halfwidth').sameHeight()", 7000);
  */

  //$('#content .product.onethirdwidth').sameHeight(3);
  $('#content .fullwidth.threecols.products').equalHeights();
  /*
  window.setTimeout("$('#content .product.onethirdwidth').sameHeight(3)", 2000);
  window.setTimeout("$('#content .product.onethirdwidth').sameHeight(3)", 5000);
  window.setTimeout("$('#content .product.onethirdwidth').sameHeight(3)", 7000);
  */

  $("a.thickbox").fancybox({
    titleShow: false
  });

	$("a.popup").fancybox({
		'frameWidth': 600,
		'frameHeight': 250,
		'overlayShow': false,
		'type': 'iframe',
		'hideOnOverlayClick': false,
		'hideOnContentClick': false
	});

  var piclinks = $('#morepics a');
  piclinks.unbind("click");
  piclinks.click(function() {
    var t = $(this);
    piclinks.removeClass("active");
    t.addClass("active");

    $('#largepic').attr("href", t.attr("href"));
    $('#largepic > img').attr("src", t.attr("href"));

    return false;
  });


});
