function recurseVars(v)
{
  var txt = "";
  switch (typeof(v))
  {
    case "boolean": return "[bool]"+(v?'yes':'no');
    case "string": return "[string]"+v;
    case "number": return "[number]"+v;
    case "undefined": return "[undefined]";
    case "function": return "[function]"+v;
    case "object":
      txt += "[object]\n";
      for (var item in v)
      {
        txt += "  propertyname="+item+ " value="+v[item]+"\n";
      }
      return txt;
  }
}
function log(hint,msg) {
	
  if (window.console)
  {
    window.console.log(hint+":\n"+recurseVars(msg));
  }
  else
  {
  //	alert(hint+":\n"+recurseVars(msg));
  }
  return msg;
}

function viewport() {
  var e = document.documentElement || {},
  b = document.body || {},
  w = window;

  return {
    x: w.pageXOffset || e.scrollLeft || b.scrollLeft || 0,
    y: w.pageYOffset || e.scrollTop || b.scrollTop || 0,
    cx: min( e.clientWidth, b.clientWidth, w.innerWidth ),
    cy: min( e.clientHeight, b.clientHeight, w.innerHeight )
  };

  function min() {
    var v = Infinity;
    for( var i = 0;  i < arguments.length;  i++ ) {
      var n = arguments[i];
      if( n && n < v ) v = n;
    }
    return v;
  }
}

function flashzoom(params)
{
  var str = "";
  if (typeof(params) === "object")
  {
    for (var prop in params)
    {
      str += prop+"="+encodeURIComponent(params[prop])+"&";
    }
  }



  if (params['zoom']=='true')
  {
    size = viewport();
    if ((size.cx*0.75) > size.cy) {
      sizey = Math.floor(size.cy - 20);
      sizex = Math.floor(sizey * 1.33);
      
    }
    else
    {
      sizex = Math.floor(size.cx - 20);
      sizey = Math.floor(sizex * 0.75);
    }

    jQuery('object').hide();
    // Ohne IFrame
    //tb_show(null, 'flashzoom.ajax?'+str+'bar=none&width='+sizex+'&height='+sizey, false);
    // Mit IFrame
    tb_show(null, log('Call','flashzoom.ajax?'+str+'keepThis=true&TB_iframe=true&bar=none&width='+sizex+'&height='+sizey), false);
  }
  else
  {
    tb_remove();
  }
}

/*holt gets in indiziertes array*/
function parseWindowLocation(aLocation)
{
  var re = /[?&]([^=]+)=([^&]+)/g;
  var maches = null;
  var urlParts = new Array();
  maches = aLocation.match(re);
  if(maches != null)
  {
    for(var i=0;i< maches.length;i++)
    {
      var urlPart = maches[i].split("=");
      urlParts[urlPart[0].substr(1)] = urlPart[1];
    }
  }
  return urlParts;
}

/* Ajaxform (zusammen mit PageFlip) */
$.fn.attachAjaxForm = function(settings) {
  settings = jQuery.extend({
    'target':'#kommentare',
    'url':'projekt_neukommentar.ajax',
    'clearForm':true,
    'success':function(){
      $('#kommentare').attachPageflip('projekt_kommentare.ajax'); $('#kommentarform').attachAjaxForm();
    }
    },settings);
  this.ajaxForm(settings);
}

/* PageFlip: AJAX-Nachladen von weiter/zurück-Seiten */

function addCommentLinks(selector, href, param, settings) {
  selector.load(href+param,
    function(){
      selector.attachPageflip(href,settings);
      jQuery('#kommentare .cright').find('a').click(
        function() {
          jQuery(this).after( jQuery('#verstoss')[0] );
          jQuery('#verstoss').fadeIn('slow');
          return false;
        }
        );

      jQuery('#verstoss span.close').click(
        function() {
          jQuery('#verstoss').fadeOut('slow');
        }
        );

      jQuery('div#verstoss h2').click(
        function() {
          jQuery('#verstoss').fadeOut('slow');
        }
        );

      jQuery('#kommentarform').attachAjaxForm();

    }
    );
  return false;
}

$.fn.attachPageflip = function(ajaxURL,settings) {
  settings = jQuery.extend({
    'container':'.blaettern',
    'back':'a.back.ajax',
    'more':'a.more.ajax'
  },settings);
  var element = this;
  var backhref=this.find(settings.back).attr('href');
  var morehref=this.find(settings.more).attr('href');
  if (backhref != undefined) 
  {
    backhref = backhref.substr(backhref.indexOf('?'));
  }
  if (morehref != undefined) 
  {
    morehref = morehref.substr(morehref.indexOf('?'));
  }

  this.find(settings.container + ' ' + settings.back).click((function(){
    return addCommentLinks(element,ajaxURL,backhref, settings)
    }));

  this.find(settings.container + ' ' + settings.more).click((function(){
    return addCommentLinks(element,ajaxURL,morehref, settings)
    }));
  return this;
}

/* Tabs-FUnktion für den Content */
$.fn.tabs = function(settings)
{
  settings = jQuery.extend({
    'cssclass':'switcher',
    'hideclass':'hidden',
    'aktiv':window.location.hash
    },settings);

  this.addClass(settings.cssclass);

  links = this.find('a[@href]');

  if (!settings.aktiv) {
    settings.aktiv = links.filter(':first').addClass('aktiv').attr('href');
  }
  else
  {
    links.filter('[@href='+settings.aktiv+']').addClass('aktiv');
  }

  links.click(function()
  {
    jQuery(settings.aktiv).addClass(settings.hideclass);
    jQuery('a[@href='+settings.aktiv+']').removeClass('aktiv');
    settings.aktiv = jQuery(this).addClass('aktiv').attr('href');
    jQuery(settings.aktiv).removeClass(settings.hideclass);
    if(settings.aktiv == '#zubehoer') {
      getCategoriesOfAccessoryProducts(document.getElementById('selzubehoer'), documentURL);
    }
    return false;
  });

  links.not('[@href='+settings.aktiv+']').each(function(){ 
    jQuery(jQuery(this).attr('href')).addClass(settings.hideclass);
  });
}

$.fn.selectswitch = function(settings)
{
  settings = jQuery.extend({
    'hideclass':'hidden'
  });
  this.find('option:not(:first)').each(function(){
    jQuery(jQuery(this).attr('value')).addClass(settings.hideclass);
  }).end().change(
    function(){
      jQuery(this).find('option').each(function(){
        jQuery(jQuery(this).attr('value')).addClass(settings.hideclass);
      });
      jQuery(this.value).removeClass(settings.hideclass);
    });
  return this;
}

$(document).ready(
  function() {

    /* Tabs-Navigation im Content erzeugen */
    $('#navigation').tabs();
    $('#selzubehoer').selectswitch();

    /* Verlinken von großen DIV-Bereichen
	$('.col2[a.more], .col1[a.more], .c220 .cright[a.more], .c220 .cleft[a.more], .c121 .cright[a.more]').filter(
		function()
		{
			return $('a.more',this).length == 1;
		}
	).addClass('linked').click(
		function()
		{
			location.href = $(this).find('a.more').attr('href');
		}
     
	);*/
	
    /* Hintergrundbild im Header
    $("div.stage h2").addClass("invisible").parent().css(
		{'background-image':function()
			{
				return "url('"+encodeURI($(this).attr('class'))+".font.png?text="+encodeURIComponent($(this).find('h1').html())+"&height="+$(this).height()+"')";
			}
		}
	);*/

    //    $('#boschnavi h1').css({'background':function(){return 'url(navihead.font.png?text='+encodeURIComponent($(this).text())+') no-repeat left top'}}).text('');
    //    $('h3[em]').each(function(){ $(this).html('<img src="h3em.font.png?text='+encodeURIComponent($(this).text())+'" alt="'+$(this).text()+'" title=""/>');});

    /* "XYZ bloggen" einblenden */
    $('a.blog').click(
      function() {
        $(this).toggleClass('open');
        $('.blogcode').toggle();
        return false;
      }
      );

    /* Materialliste einblenden */
    $('a.bloghinweis').click(
      function() {
        $('#bloghinweis').fadeIn('slow');
        blogattach();
        return false;
      }
      );

    function blogattach() {
      $('#bloghinweis span.close').unbind('click').click(
        function() {
          $('#bloghinweis').fadeOut('slow');
        }
        );
      $('#bloghinweis h2').unbind('click').click(
        function() {
          $('#bloghinweis').fadeOut('slow');
        }
        );
    }
    
    /* Verstoss melden einblenden */
    $('#kommentare .cright').find('a').click(
      function() {
        $(this).after( $('#verstoss')[0] );
        $('#verstoss').fadeIn('slow');
        verstossattach();
        return false;
      }
      );

    function verstossattach() {
      $('#verstoss span.close').unbind('click').click(
        function() {
          $('#verstoss').fadeOut('slow');
        }
        );
      $('#verstoss h2').unbind('click').click(
        function() {
          $('#verstoss').fadeOut('slow');
        }
        );
      $('#verstoss form').ajaxForm(verstossoptions);
    }

    var verstossoptions = {
      target: '#verstoss',
      url: 'verstossmelden.ajax',
      success: verstossattach,
      beforeSubmit: function(a,b,c){
        b.find("'input[@type='submit']").attr('disabled','disabled'); return true;
      }
    };

    /* Newsletterabo einblenden */
    $('a.newsabo').click(
      function() {
        $('#newsletter').fadeIn('slow');
        newsattach();
        return false;
      }
      );

    function newsattach() {
      $('#newsletter span.close').unbind('click').click(
        function() {
          $('#newsletter').fadeOut('slow');
        }
        );
      $('#newsletter h2').unbind('click').click(
        function() {
          $('#newsletter').fadeOut('slow');
        }
        );
      $('#newsletter form').ajaxForm(newsletteroptions);
    }

    /* Newsletter-Formular per AJAX */
    var newsletteroptions = {
      target: '#newsletter',
      url: 'newsletterabo.ajax',
      success: newsattach,
      beforeSubmit: function(a,b,c){
        b.find("'input[@type='submit']").attr('disabled','disabled'); return true;
      }
    };


    /* Tooltips for alle <dfn>-Tags im Content */
    $('dfn').addClass('lexikonized').attr('title',function() { 
      return $(this).text()
      }).Tooltip(

      {
      delay:  0,
      track: true,
      prefix: 'Lexikon: ',
      ajaxOptions:
      {
        url:'lexikon_peek.ajax',
        param: function(t){
          return {
            keyword:$(t).text()
            };
        }
      }
    }
    );

    $('.cright .icons img').Tooltip(
    {
      delay: 150,
      track: true,
      showBody: ' - '
    }
    );


    /* AJAX-Blättern für Kommentare */
    $('#kommentare').attachPageflip('projekt_kommentare.ajax');

    /* Kommentarformular mit AJAX zum Server schicken */
    $('#kommentarform').attachAjaxForm();

    $('.blogcode input').click(
      function(){
        this.focus();
        this.select();
      }
      );

    /* Navigation hovern */

    $('#boschnavi .aktiv').prev().addClass('before');

    $('#houseofbosch img').hover(
      function(){
        if (this.src.indexOf('houseofbosch.gif')!= -1) this.src='neutral/layout/houseofbosch_ro.gif'
          },
      function(){
        if (this.src.indexOf('houseofbosch_ro.gif')!= -1) this.src='neutral/layout/houseofbosch.gif'
          }
      );
    if ($.browser.mozilla)
    {
      $('#marginalspalte input, label').css('opacity','0');
		
      setTimeout(function(){
        $('#marginalspalte input, label').css('opacity','1');
      },50);
    }
    if (!($.browser.msie))
    {
      $('#houseofbosch.aktiv img').attr('src','neutral/layout/houseofboschaktiv.png');
    }
	
    if ($.browser.msie)
    {
      setTimeout(function(){
        var isIE7 = false;
        /* @cc_on
		@if (@_jscript_version <= 5.7)
		isIE7 = true;
		@end
		@*/
        off = document.getElementById('top').offsetLeft;

        $('#verstoss').css('left',function(){
          return (off + 240)+'px';
        });
        $('#materialliste').css('left',function(){
          return (off + 180)+'px';
        });

        $('#houseofbosch.aktiv a').appendTo('body').css({
          position:'absolute',
          top:'143px',
          left:off+'px',
          'z-index':10
        }).attr('id','newhouseofbosch');
        if (!isIE7) $('#newsletter').appendTo('body').css({
          position:'absolute',
          left:off+174+'px',
          top:'600px',
          'z-index':'500'
        });

        $('.teaser.downloads').find('.col1:eq(0)').parent().width(180).find('.col1:eq(1)').parent().width(360).find('.col1:eq(2)').parent().width(540);

        $('#boschnavi dd:not(.aktiv):not(#houseofbosch)').hover(
          function(){
            $(this).addClass('over');
          },
          function(){
            $(this).removeClass('over');
          }
          );
        $('.keywords td').hover(
          function(){
            $(this).addClass('over');
          },
          function(){
            $(this).removeClass('over');
          }
          );
      
      }, 50);

    }

  //    xhr();

  //    alert('FX installed');
  }
  );

function getCategoriesOfAccessoryProducts(selectObj, documentURL) {
  //var result = "";

  document.getElementById('zubehoerdetailsTable').style.visibility = "hidden";
  document.getElementById('loaderImg').style.display = "block";
  selectObj.setAttribute("disabled","disabled");

  $.ajax({
    type: "GET",
    async: true,
    url: documentURL + '&attr_id=' + selectObj[selectObj.selectedIndex].value + "&time=" + (new Date().getTime()),
    success: function(data){
      document.getElementById('zubehoerdetailsTable').innerHTML = data;
      selectObj.removeAttribute("disabled");
      document.getElementById('loaderImg').style.display = "none";
      document.getElementById('zubehoerdetailsTable').style.visibility = "visible";
      $('#zubehoerdetailsTable a.tracker').click(function(){
        var clickedCategory = this.innerHTML.replace(/'/,"\'");
        //alert("'Passendes Zubehör', ''+prodName+'', ''+kategorieName+clickedCategory+''");
        //alert(pageTracker);
        pageTracker._trackEvent('Passendes Zubehoer', prodName, kategorieName+' - '+clickedCategory);
      });
      tb_init('#zubehoerdetailsTable a.thickbox');
    }
  });
//return result;
}

function showPage(href){
  location.href = href;
}
