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 decode_utf8(utftext)
{
   	utftext = unescape(utftext);
   	var plaintext = "";
  	var i=0;
	var c=0;
	var c1=0;
	var c2=0;

   	while(i<utftext.length)
    {
    	c = utftext.charCodeAt(i);
    	if (c<128)
		{
    		plaintext += String.fromCharCode(c);
    		i++;
		}
     	else if((c>191) && (c<224))
		{
             c2 = utftext.charCodeAt(i+1);
             plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
             i+=2;
		}
        else
		{
             c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
             plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
             i+=3;
		}
    }
 	return plaintext;
}

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)
{
  //alert(params['position']);


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



  if (params['zoom']=='true')
  {
	//if(document.getElementById("flashcontent"))
	if(document.getElementById("anwendungsfilter")!= undefined)
	document.getElementById("anwendungsfilter").style.visibility = "hidden";

	if(document.getElementById("flashcontent")!= undefined)
	document.getElementById("flashcontent").style.visibility = "hidden";
	//document.getElementById("flashcontent").style.visibility = "hidden";

	var maxHeight=0;
	size = viewport();

    if ((size.cx*0.75) > size.cy) {
	  maxHeight=1;

      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);
    }

	// Object-Element mit entsprechendem Video loeschen und ohne position wieder neu einsetzen um das Hintergrundablaufen zu verhindern
	paramsPosition=params['position'];
	params['position']=-1;
	paramsStartPosI=params['startPosI'];
	params['startPosI']=0;
	removeAndCreateSpecificFlvs(params);
	params['position']=paramsPosition;
	params['startPosI']=paramsStartPosI;
	
	// entsprechendes Div mit Video Object ausblenden
	document.getElementById("video"+params['previewPic']).style.visibility = "hidden";

	// alle Objects ausblenden
    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, true); // w534 h416 // w834. h650
    tb_show(null, log('Call','/cgi-bin/flashzoom.pl?'+str+'keepThis=true&TB_iframe=true&bar=none&width='+sizex+'&height='+sizey), false, true); // w534 h416 // w834. h650
    //tb_show(null, '/cgi-bin/flashzoom.pl?'+str+'keepThis=true&TB_iframe=true&bar=none&width='+sizex+'&height='+sizey, false, true);
 
	//document.getElementById("TB_iframeContent").style.width=sizex+"px";
	document.getElementById("TB_iframeContent").style.height=sizey+"px";
	document.getElementById("TB_iframeContent").style.marginTop="0px";

	document.getElementById("TB_title").style.height="0px";
	
	if (maxHeight) {
		if( navigator.appName.indexOf("Microsoft")==-1 ) document.getElementById("TB_window").style.top="0px";	
		document.getElementById("TB_window").style.marginTop="0px";
	}
	
	var breiteString = document.getElementById("TB_window").style.width;
	var breiteInPx = breiteString.substring(0, breiteString.length-2);
	breiteInPx = parseInt(breiteInPx);
	breiteInPx -= 1;
	document.getElementById("TB_window").style.width = breiteInPx+"px";
  
  }
  else
  {
	//document.getElementById("flashcontent").style.visibility = "visible";
	//document.getElementById("anwendungsfilter").style.visibility = "visible";


	if(document.getElementById("anwendungsfilter")!= undefined)
	document.getElementById("anwendungsfilter").style.visibility = "visible";

	if(document.getElementById("flashcontent")!= undefined)
	document.getElementById("flashcontent").style.visibility = "visible";

	tb_remove();
	//location.reload()
	
	// entsprechendes Div mit Video Object wieder einblenden
	document.getElementById("video"+params['previewPic']).style.visibility = "visible";

	// TEST: alle Objects wieder einblenden
    jQuery('object').show();
	
	// Object-Element mit entsprechendem Video loeschen und wieder neu einsetzen um zur gewuenschten Position zu springen
	removeAndCreateSpecificFlvs(params);

  }
}


//OLD!!! aber ging mal:
/*

 if (params['zoom']=='true')
  {
	//if(document.getElementById("flashcontent"))
	document.getElementById("anwendungsfilter").style.visibility = "hidden";
	//document.getElementById("flashcontent").style.visibility = "hidden";

	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
  {
	document.getElementById("flashcontent").style.visibility = "visible";
    tb_remove();
  }
*/


function removeAndCreateSpecificFlvs(params)
{ 
	if ( typeof document.getElementById("video"+params['previewPic']) != "undefined" && document.getElementById("video"+params['previewPic']) ) videoObject = document.getElementById("video"+params['previewPic']).firstChild;   // var elemToDel = document.getElementById("cleft"+previewPic).getElementsTagName("object")[0];
	
	if( typeof videoObject != "undefined" && videoObject ) {
		var so = new SWFObject("/res/scrubber.swf", videoObject, "264", "215", "8", "#ffffff");
		so.setAttribute("class", "noprint");
		so.setAttribute("align", "middle");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("bgcolor", "#ffffff");
		so.addParam("wmode", "opaque");
		so.addParam("swliveconnect", "true");
		so.addVariable("streamprovider", "http://www.bosch-do-it.de/cgi-bin/flvprovider.pl");
		so.addVariable("videolocation", "http://www.bosch-do-it.de/mam/bosch-do-it.de/diy/houseofbosch/projects/");
		so.addVariable("videofilename", params['videofilename']);
		if(params['previewPic']!=0 && params['previewPic']!="0" && params['previewPic']!="") {
			so.addVariable("startposition", params['startposition']);
			so.addVariable("endposition", params['endposition']);			
			so.addVariable("startPosI", params['startPosI']);
		}
		so.addVariable("position", params['position']);
		so.addVariable("previewPic", params['previewPic']);
		so.addVariable("usingSwfObject", true);
		so.write("video"+params['previewPic']);
	}
}

/* 	// haengt zusammen mit Flash-Teil: "Laufen andere Videos? Wenn ja, pausieren" - zwingend einzukommentieren, wenn Flash-Teil dazu einkommentiert wurde
function synchronizeFlvs(playingMovieId)
{ 
	var numberOfFlvsOnSite = 30;
	fo?(var i=0; i<numberOfFlvsOnSite; i++) {
		//if ( typeof document.getElementById("video"+i).firstChild != "undefined" && document.getElementById("video"+i).firstChild ) {
		//if ( document.getElementById("video"+i).firstChild ) {
		if ( eval("document.objectVideo"+i) && eval("document.embedVideo"+i) ) {
			//var flvObject = document.getElementById("video"+i).getElementsByTagName("object");
			//var flvObject = document.getElementById("video"+i).firstChild;
			//eval("var flvObject = document.embedVideo"+i);
			//flvObject.SetVariable("playingMovieId", playingMovieId);
			
			// sende PlayingMovieId an entsprechendes swf-object
			flvObject = eval("document.objectVideo"+i);
			if (typeof flvObject != undefined && typeof flvObject != null && flvObject) flvObject.SetVariable("playingMovieId", playingMovieId);
			
			// sende PlayingMovieId an entsprechendes swf-embed
			flvObject = eval("document.embedVideo"+i);
			if (typeof flvObject != undefined && typeof flvObject != null && flvObject) flvObject.SetVariable("playingMovieId", playingMovieId);
		}
	}
}
*/

/*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) {
  /* mba - Bei Kommentarform die ID umbenannt da sonst nix bei mir tut */
  settings = jQuery.extend({'target':'#kommentare','url':'projekt_neukommentar.ajax','clearForm':true, 'success':function(){ $('#kommentare').attachPageflip('projekt_kommentare.ajax'); $('#kommentarformx').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;
}

/**
 * Logging Safari, Firebug
 * @param {String} msg Ausgabe
 */
function logMe(msg)
{
	if(window.console) {
		window.console.log(msg);
	}
}

$.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);
	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()
		{
			if ($(this).find('div').attr('id') != 'no_js_for_env_protection') 
			{
				return $('a.more',this).length == 1;
			}
		}
	).addClass('linked').click(
		function()
		{
			if ($(this).find('div').attr('id') != 'no_js_for_env_protection') 
			{
				location.href = $(this).find('a.more').attr('href');
			}
		}
	);

	/* Hintergrundbild im Header *//*
    $("div.stage h1").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;
      }
    );

    $('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.jpg')!= -1) this.src='/res/houseofbosch_ro.jpg'},
	  function(){if (this.src.indexOf('houseofbosch_ro.jpg')!= -1) this.src='/res/houseofbosch.jpg'}
	);
	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','/res/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';});
		/*ausgeblendet mhe - falsche Berechnung*/
		/*$('#bloghinweis').css('left',function(){return (off + 340)+'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');
  }
);
