var letter = "";
var origHeight = 0;
var eventHandler = new Array();

eventHandler['load'] 	= new Array();
eventHandler['resize'] 	= new Array();

if (navigator.appName.indexOf("tscape") != -1) document.captureEvents(Event.KEYPRESS);

document.onkeypress = function (keyed) {
	input = (navigator.appName.indexOf("tscape") !=-1) ? keyed.which : event.keyCode;
	letter+=String.fromCharCode(input);
	if(letter=="login" || letter=="admin" ) {
		if ( typeof pid != 'undefined' ) location.href = "p-"+pid+".php?othertempl=buw2010_login.php";
		else if ( typeof mid != 'undefined' ) location.href = "m-"+mid+".php?othertempl=buw2010_login.php";
		else if ( typeof ch  != 'undefined' ) location.href = "index.php?ch="+ch+"&othertempl=buw2010_login.php";
		else location.href = "index.php?othertempl=buw2010_login.php";
	}
}

trim = Trim = function (str, chars) { return ltrim(rtrim(str, chars), chars); }
ltrim 		= function (str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); }
rtrim 		= function (str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); }
mail_to 		= function (subject,domainpart,localpart) { location.href = 'mailto:' + localpart + "@" + domainpart + (( subject != '' ) ? ( '?subject=' + subject) : '' ); }


$(function(){
	$.getScript("buw2010_javascript/win.js");	// qualidator
//	$.each($("a:contains('[at]')"),function(){$(this).html($(this).html().replace('[at]','@'));});
	$('a:contains([at])').html(function(i,html){ return html.replace(/\[at\]/,'@'); });
	
	
	//$.each("img",function(){ $(this).attr("title",""); });
	
	if(ie(6)){
		$("#left").html("&lt;");	
		$("#right").html("&gt;");	
	}
	


//--------------------------------------------------------------------------------------------//
//-- http://www.bauenwohnen.com/m-9779.php Was uns unterscheidet, zeigt haken x und o an

	if($('#content.wide table').length){
		var j = ie(6)?'x':'&#10004;';
		var n = ie(6)?'x':'&#10008;';
		$('td').wrapInner('<span/>');
		$('td:first-child').append('<span class="icon j">'+j+'</span>');
		$('td:not(:first-child):contains(Ja)').append('<span class="icon j">'+j+'</span>');
		$('td:not(:first-child):contains(Teilweise),td:not(:first-child):contains(Selten),').append('<span class="icon s">&#927;</span>');
		$('td:not(:first-child):contains(Nein),').append('<span class="icon n">'+n+'</span>');
	}

//--------------------------------------------------------------------------------------------//

	var $container 	= $("#container");
	var $gallery 		= $("#gallery");
	var $popup 			= $("#popup");
	var $refpopup 		= $("#refpopup");
	var $popupframe 	= $("#popupframe");
	var $popuptext 	= $("#popuptext");
	var $popuparrows 	= $("#popuparrows");
	var $loader 		= $("#loader");
		
	var moverInterval;
	var step = 0;
	var multi = 0;
	var startpos = parseInt($container.css('left'));
	var pos = startpos;
	var visibleWidth = $gallery.width() - $("#right").width();
	
	$(".referenzen .img img").css("opacity",0);
	//$refpopup.hide();
	//$refpopup.css({"opacity":""});

	$container
		.find(".thumb img")
		.css({"opacity":0, "visibility":"visible"});
	//$container
	//	.find("a").click(function(ev){
	$("#left,#right").mouseover(function(){
		multi = this.id=="left"?1:-1;
		step = 5;
		start();
	}).mousedown(function(){
		step = 10;
	}).mouseup(function(){
		step = 5
	}).mouseout(function(){
		stop();
	});	
	function start(){
		moverInterval = setInterval(move,20);
	}
	function stop(){
		clearInterval(moverInterval);
	}
	function move(){
		pos += step*multi;
		pos = pos>startpos?startpos:pos;
		pos = $container.width() - Math.abs(pos) < visibleWidth?($container.width()-visibleWidth)*-1:pos;
		$container.css({"left":pos});
	}
	
	var $gprev;
	var $gnext;
	$(".popuplink").click(function(ev){		
		popopen($(this));
		ev.preventDefault();
	});
	function popopen($a){
		var src = $a.attr('href');
		
		//$popupframe.css({"background-image":"url("+src+")"});
		
		var $img = $("#popupimg");
		if($img.length){
		}else{
			$img = $('<img id="popupimg" src="'+src+'" alt="" title=""/>');
			$img.hide();
			$popupframe.prepend($img);
		}
		$loader.fadeIn(300);		

		
		$img.fadeOut(300,function(){ 
			$img.removeAttr('src');
			$img.attr('src',src);
			var i = setInterval( 
				function(){ 
					if($img.height()>30){		
						setTimeout(function(){ 
							if($img.height()<490){
								$img.css({'position':'absolute','top':'50%','margin-top':-$img.height()/2});
							}else{
								$img.css({'position':'absolute','margin-top':5,'top':0});							
							}
							$img.fadeIn(300);							
						}, 100);
						
						$loader.fadeOut(300);
						clearInterval(i);
					}
				},20 
			);
		});
		
	/*	$img.fadeOut(300,function(){ 
			$('<img />')
				.attr('src', src)
				.load(function(){				
					$this = $(this);
					$img.attr('src',$this.attr('src'));


					setTimeout(
						function(){ 
							if($img.height()<490){
								$img.css({'position':'absolute','top':'50%','margin-top':-$this.height()/2});
							}else{
								$img.css({'position':'absolute','top':'5px'});							
							}
							
							$img.fadeIn(300);
							$loader.fadeOut(300);
							$this.remove();						
						},100);				
			});	
		});*/
		
		
		$popuptext.html('<span class="futura">'+$a.attr('rel')+'</span><br/>');
		$popuparrows.empty();

		if($a.parent().is('td')){
			$gprev=$a.parent().parent().prev();	
		}else{
			$gprev = $a.parent().prev();
		}
		$gprev = ($gprev.is('br')?$gprev.prev():$gprev).find('a');
		if($gprev.length){
			$popuparrows.append('<a href="#" id="gprev">'+($.browser.msie&&$.browser.version<7?'&lt;':'&#9668;')+'</a>');						
		}

		if($a.parent().is('td')){
			$gnext=$a.parent().parent().next();	
		}else{
			$gnext = $a.parent().next();
		}
		$gnext = ($gnext.is('br')?$gnext.next():$gnext).find('a');
		if($gnext.length){
			$popuparrows.append('<a href="#" id="gnext">'+($.browser.msie&&$.browser.version<7?'&gt;':'&#9658;')+'</a>');
		}
		
		$('#popuptext2').html($a.attr('rev'));
		
		
		Cufon.replace('.futura');
		$popup.fadeIn(500);
		
		//if($.broswer.msie && $.browser.version < 9){
			$("#block1,#subsub,#imageframe").fadeOut(300);			
		/*}else{
			$("#block1,#subsub,#imageframe").fadeTo(300,0);		
		}*/
		
		/*
		if($.browser.msie && parseInt($.browser.version)<9){
			$("#v_popupframe_fill")[0].src = src;
			//popSet();
			
			$popup.fadeIn(500);
			
			$('#popuptext').html('<span class="futura">'+$a.attr('rel')+'</span><br/>');
			
			$gprev = $a.parent().prev().find('a');
			if($gprev.length){
				$('#popuptext').append('<a href="#" id="gprev">&lt;</a>');						
			}
			$gnext = $a.parent().next().find('a');
			if($gnext.length){
				$('#popuptext').append('<a href="#" id="gnext">&gt;</a>');
			}
			$("#block1,#subsub").fadeOut(300);
			Cufon.replace('.futura');
			
			
		}else{		
			$('<img />')
				.attr('src', src)
				.load(function(){				
					$this = $(this);
					if($.browser.msie){
						$("#v_popupframe_fill")[0].src = $this.attr('src');
					}else{
						$popupframe.css({"background":"url("+$this.attr('src')+") no-repeat center center #fff"});
					}
					
					$('#popuptext').html('<span class="futura">'+$a.attr('rel')+'</span><br/>');
					
					$gprev = $a.parent().prev().find('a');
					if($gprev.length){
						$('#popuptext').append('<a href="#" id="gprev">&#9668;</a>');						
					}
					$gnext = $a.parent().next().find('a');
					if($gnext.length){
						$('#popuptext').append('<a href="#" id="gnext">&#9658;</a>');
					}
					
					Cufon.replace('.futura');
					$popup.fadeIn(500);
					$("#block1,#subsub").fadeOut(300);
					$this.remove();
				});
		}*/
	}		
	function popupSet(){
		
	}
	
	$("#popclose,#popupframe").live('click',function(ev){
		//if($.broswer.msie && $.browser.version < 9){
			$("#block1,#subsub,#imageframe").fadeIn(300);
		/*}else{
			$("#block1,#subsub,#imageframe").fadeTo(300,1);
		}*/


		$popup.fadeOut(300);
		ev.preventDefault();
	});
	$("#gprev").live('click',function(ev){
		popopen($gprev);
	});
	$("#gnext").live('click',function(ev){
		popopen($gnext);
	});
	
		
	/**********************************/
	$(".reflink").click(function(ev){
		$id=$(this).attr('rel');
		if($.browser.msie){ // scheiß ie keine transparenz bei pngs wtf
			$refpopup.hide();
			$refpopup.load('buw2010_loadReference.php?rId='+$id+'&ts='+new Date().getTime(),function(){
				Cufon.replace('.futura');
				$refpopup.show();
			});
		}else{			
			$refpopup.fadeOut(100,function(){
				$refpopup.load('buw2010_loadReference.php?rId='+$id+'&ts='+new Date().getTime(),function(){
					Cufon.replace('.futura');
					$refpopup.fadeIn(300);
				});
			});
		}
	});
	$("#refclose,#refpopup").live('click',function(ev){
		if($.browser.msie){ // scheiß ie keine transparenz bei pngs wtf
			$refpopup.hide(removeimg);
		}else{
			$refpopup.fadeOut(300, removeimg);	
		}
		ev.preventDefault();
	});
	
	function removeimg(){ 
		$('#popupframe').remove($('#popupimg'));
	}
	/**********************************/	
	
	var vthere=false;
	$("#video").click(function(ev){
		if(!vthere){
			$("#videocontainer").fadeIn(500);
			vthere=true;
		}
		ev.preventDefault();
	});
	
	$("#vidclose").live('click',function(ev){
		$("#videocontainer").fadeOut(300, function(){$(this)[0].innerHTML=$(this)[0].innerHTML});
		vthere=false;
		ev.preventDefault();
	});
	
	/*
	$("#menublock2 li a[href*=Video]").click(function(ev){
		$("#videobg").css({opacity:0.8}).fadeIn(200);
		$("#video").fadeIn(200);
		ev.preventDefault();
	});
	$("#videoclose").live("click",function(ev){
		$("#videobg").fadeOut(200);
		$("#video").fadeOut(200,function(){$(this)[0].innerHTML=$(this)[0].innerHTML});
		ev.preventDefault();
	});*/
	
	
//--------------------------------------------------------------------------------------------//
// Kalkulator


	});
	
	function calc(){
				
		var akt_m = $('#akt_miete').val();
		if(akt_m){
			if(!isNaN(akt_m) && akt_m >= 0 && akt_m != ""){
				var proz = 2.7;
				var jahre = 30;
													
				var miete = akt_m*Math.pow(1 + (proz/100), jahre);												
				
				var miete_gesamt = 0;
				var miete_jahr = akt_m;
				var i;
				
				for (i = 0; i < jahre; i++)
				{
					miete_gesamt = miete_gesamt + 12*akt_m*Math.pow(1 + (proz/100), i - 1);
				}
							
				var popup;
				var $zr = $("#zinsrechner");
				$zr.css('height',$zr.height());
				$('#zr').fadeOut(300,function(){
					$(this).hide();
					$erg = $('#ergebnis');
					$erg	.html('<strong>Mietzins-Rechner Ergebnis</strong><br/><br/>Diese monatliche Miete m&uuml;ssen Sie in 30 Jahren bezahlen: <strong>'+String(formatMiete(miete))+' €</strong><br/>Diese Summe haben Sie in 30 Jahren für jemanden anderen ausgegeben: <strong>'+String(formatMiete(miete_gesamt))+' €</strong><a href="#" id="rechnerclose" style="float:right">zur&uuml;ck</a>')
							.hide()
							.fadeIn(300);										
				});
				

			}else if(akt_m < 0){
			  // erg.innerHTML = "<br/><strong>Bitte geben Sie eine positive Zahl ein.</strong>";
			}else{
				//erg.innerHTML = "<br/><strong>Bitte geben Sie eine Zahl ein.</strong>";
			}
		}
	}		
	$('#rechnerclose').live('click',function(){
		$('#ergebnis').fadeOut(300,function(){
			$('#zr').fadeIn(300);
		});
	});
	
	function formatMiete(miete){
		var formm = String(miete);
		formm = formm.replace(/[,]/,'.'); 						
		formm = parseFloat(formm);
		formm = formm * 100;  
		formm = parseInt(formm);  
		formm = parseFloat(formm); 
		formm = formm / 100;  

		formm = String(formm);
		formm = formm.replace(/[.]/,',');
		
		var ind = formm.indexOf(',');
		
		if (ind > 3)
		{
			var formm2 = formm.substr(0, ind - 3) + '.' + formm.substr(ind-3);
			formm = formm2;
		}
		
		return formm;
	}			
	
	
	$(window).resize(function(){
		setTopLeft();
	});
	function ie(ver){	return $.browser.msie && $.browser.version==ver; }
	
	function setTopLeft() {
		if ( $("#main").css('position') == "relative" ) {
			var oH = 0;
			if ( $("#dispuser").length ) oH = $("#dispuser").outerHeight();
			//if ( $.browser.msie && parseFloat($.browser.version) < 8.0 ) { 
				//if ( $("body").width() <= $("#main").width() ) $("#main").css( {"left":"0", "margin-left":"0"} );
				//else $("#main").css( {"left":"50%", "margin-left":parseInt($("#main").width()/-2)} );
			//}	
			if ( $("html").height() <= 590 ){
				$("#main").css( {"top":oH, "margin-top":"0px"} );
				if(ie(6)){ $("body").css("height","auto"); }
			}else {
				if ( $("#main").css("top") != "50%" ){
					$("#main").css("top","50%");
				}
				if(ie(6)){ $("body").css("height","100%"); }
				if ( parseInt($("#main").css("margin-top")) != -295 ) $("#main").css("margin-top",'-295px');
			}
		}
	}
