var d = 1;
var prop;
var offset = 155;
var aniTime = 1500;
var scrollDist = 0;
var val;
var bound;
var topper = 0;
var h1;
var h2;
var stageLinks =["index.php?page=cad","index.php?page=about_us","index.php?page=technologies","index.php?page=consulting"];
var fadeNot = false;
var h_rem;
var txtIn = ['Firma*','Straße*','PLZ/Ort*','Ansprechpartner*','Telefon*','E-Mail*','Vor-, Nachname des Teilnehmers*','Vor-, Nachname des Teilnehmers','Firma','Straße','PLZ/Ort'];
var route = '';
var $ob;
var oben;
var blockBeding = false;

$(document).ready(function(){
	
	
	Cufon.replace('h1', {color: '#58585a'}); 
	Cufon.replace('h1.orange', {color: '#e48208'}); 
	Cufon.replace('h1.blau', {color: '#0097d3'}); 
	Cufon.replace('h1.gruen', {color: '#a2b400'}); 
	Cufon.replace('h1.gelb', {color: '#f1bf12'}); 
	
	Cufon.replace('h2'); 
	Cufon.replace('h2.blau', {color: '#0097d3'}); 
	Cufon.replace('h2.con', {color: '#e48208'}); 
	Cufon.replace('h2.gruentechnologies', {color: '#a2b400'});
	Cufon.replace('h2.gruen_technologies', {color: '#a2b400'});
	Cufon.replace('h2.gruencad', {color: '#f1bf12'});
	Cufon.replace('h2.gruen_cad', {color: '#f1bf12'});
	Cufon.replace('h2.gruen_', {color: '#58585a'});
	
	Cufon.replace('h5');
	Cufon.replace('h5.blau', {color: '#0097d3'}); 
	Cufon.replace('h5.orangeSub', {color: '#ee7814'});
	Cufon.replace('h5.gruenSub', {color: '#8fa008'}); 
	Cufon.replace('h5.gruen', {color: '#8fa008'}); 
	Cufon.replace('h5.gelbSub', {color: '#f1bf12'}); 
	Cufon.replace('h5.gruen_cad', {color: '#f1bf12'}); 
	Cufon.replace('h6');
	Cufon.replace('h6.orange', {color: '#ee7814'});
	
	$("input#firma").smartFocus(txtIn[0]);
	$("input#strasse").smartFocus(txtIn[1]);
	$("input#ort").smartFocus(txtIn[2]);
	$("input#ansprech").smartFocus(txtIn[3]);
	$("input#tel").smartFocus(txtIn[4]);
	$("input#mail").smartFocus(txtIn[5]);
	$("input#rechFirma").smartFocus(txtIn[8]);
	$("input#rechStrasse").smartFocus(txtIn[9]);
	$("input#rechOrt").smartFocus(txtIn[10]);
	$("input#teil1").smartFocus(txtIn[6]);
	$("input#teil2").smartFocus(txtIn[7]);
	$("input#teil3").smartFocus(txtIn[7]);
	$("input#teil4").smartFocus(txtIn[7]);
	$("input#teil5").smartFocus(txtIn[7]);
	$("input#teil6").smartFocus(txtIn[7]);
	$("input#teil7").smartFocus(txtIn[7]);
	$("input#teil8").smartFocus(txtIn[7]);
	$("input#teil9").smartFocus(txtIn[7]);
	
	
	if($('div#logoSigns').children().length>1){
		$('div#logoSigns > img:nth-child(2)').css("opacity","1.0");	
		setTimeout("change();",500);
	}else{
		fadeNot = true;
		$('div#logoSigns > img').css("opacity","1.0");
		$('div#imgs > img').css("opacity","1.0");	
	}
	
	$('input#anmelden').hover(
		function(){$(this).css({"border":"1px solid gray","cursor":"pointer"})},
		function(){$(this).css({"border":"none","cursor":"auto"})}
	);
	
	if ($('#imgs').children().size() == 1) {
		$('#imgs').css("cursor", "default");
		$('#stoer').css("cursor", "default");
	}
	$('#stoer, #imgs').click(function(){
		if($('#imgs').children().size() > 1) document.location.href = stageLinks[d-1];
	});
	
	$('.zurueck').click(function(){
		 $('html, body').animate({scrollTop:0}, 1000,'easeOutQuint');
	});
	
	$('.vorSetz').mouseover(function(){
		if(!blockBeding){
			var $obj = $(this).siblings('.vorSetzHide');
			if($obj.attr("id") == ""){
				h_rem = $obj.height(); 	
				$obj.attr("id",h_rem);
			}
			$obj.css({'height':'35px','display':'block','opacity':'0'});
			$obj.animate({opacity:1},100,function(){	
				if($(this).attr('id') > 37){
					$(this).animate({height:$(this).attr('id')},200);
					$(this).parent().animate({height:parseInt($(this).attr('id'))+20},200);
				}
			});
		}
	});
	
	$('.vorSetz_cad').mouseover(function(){
		if(!blockBeding){
			
			var $obj = $(this).siblings('.vorSetzHide_cad');
			
			if($obj.attr("id") == ""){
				h_rem = $obj.height(); 	
				$obj.attr("id",h_rem);
			} 
			

				$obj.css({'height':'35px','display':'block','opacity':'0'});
				$obj.animate({opacity:1},100,function(){	
					if($(this).attr('id') > 37){
						$(this).animate({height:$(this).attr('id')},200);
						$(this).parent().animate({height:parseInt($(this).attr('id'))+20},200);
					}
				});

		}
	});
	
	$('.vorSetzHide, .vorSetzHide_cad').mouseleave(function(){
		if(!blockBeding){
			h_rem = $(this).height(); 
			if(h_rem > 36){
				$(this).animate({height:35},200,function(){
					$(this).animate({opacity:0},200,function(){
						$(this).css({'display':'none'});
					});
				});
				$(this).parent().animate({height:56},200);
			}else{
				$(this).animate({opacity:0},200,function(){
					$(this).css({'display':'none'});
				});
			}
		}
	});
	
	$('#rech > a').click(function(){
		$('#moreRech').css('display','block');
	});
	
	$('form').submit(function(){
		var check = true;
		if($("input#firma").val() == txtIn[0]){ 
			check = false;
			$("input#firma").addClass("red");
		}
		if($("input#strasse").val() == txtIn[1]){
			 check = false;
			 $("input#strasse").addClass("red");
		}
		if($("input#ort").val() == txtIn[2]){
			check = false;
			$("input#ort").addClass("red");
		}
		if($("input#ansprech").val() == txtIn[3]){
			check = false;
			$("input#ansprech").addClass("red");
		}
		if($("input#tel").val() == txtIn[4]){
			check = false;
			$("input#tel").addClass("red");
		}
		if($("input#mail").val() == txtIn[5]){
			check = false;
			$("input#mail").addClass("red");
		}
		if($("input#teil1").val() == txtIn[6]){
			check = false;
			$("input#teil1").addClass("red");
		}
		if ($('.ch:checked').val() == undefined) {
			check = false;
			$(".pflicht:not(:first)").addClass("redTxt");
		}else{
			$(".pflicht:not(:first)").removeClass("redTxt");
		}
		if(!check){ 
			$(".pflicht:first").addClass("redTxt")
			 return false;
		}else{
			 return true;
		}
	});
	
	$('td.nonter > a').live('click', function() {
		$('#antwort').css('display','block');
		return false;
	});
	
	if($('#antwortBestell').size() > 0){
		$('#main > #frame > h2').css("display","none");
		$('#main > #frame > div:first').css("display","none");
	}
	
 	if (route != '') {
		setTimeout("Scroll();", 500);
	} else {
		$('#frame').css("left", "0px");
	}
});

function change(){
	$('div#stoer').css("opacity",0.0);
	$('div#stoer').animate({opacity:1.0},700);
	$('div#imgs > img:nth-child('+d+')').animate({opacity:0.0},1500);
	$('div#logoSigns > img:nth-child('+d+')').animate({opacity:0.0},1500);
	
	for(var i=0;i<7;i++){
		$('div#stoer').append('<div class="stoerLine"></div>');
		var w = parseInt(Math.random() * 30);
		var p = parseInt(Math.random() * 800);
		var dir = parseInt(Math.random() * 50);
		var temp_dir = 0;
		if(p > 400) dir = dir*-1;
		$('div#stoer > div:last').css("width",w).css("left",p).animate({left:'+='+dir},3000,function(){});
	}
	setTimeout("showNext()",500)	
	setTimeout("fader();",1000);
	setTimeout("change();",7000);
}

function fader(){
	$('div#stoer').animate({opacity:0.0},1000,function(){$('div#stoer > div').remove();});
}

function showNext(){
	if(d < 4) {
		d++;
		$('div#imgs > img:nth-child('+d+')').animate({opacity:1.0},2500);
		$('div#logoSigns > img:nth-child('+d+')').animate({opacity:1.0},1500);
	}else{
		d = 1;
		$('div#imgs > img:nth-child(1)').animate({opacity:1.0},2500);
		$('div#logoSigns > img:nth-child(1)').animate({opacity:1.0},2500);
	}
}

function Scroll(){

		blockBeding = true;
		if(route !='' && route !='ue' && route !='uec' && route.substr(0,2) !='hd'){
			var t = route.substr(0,2);
			var p = route.substr(2,route.length-2);
			var arr = [];
			$('h1').each(function(index) {
	    		arr[index] = $(this);
	 		});
			switch(t){
				case "gt": 
					$ob = arr[0];
					break;
				case "mt":
					$ob = arr[1];
					break;
				case "at": 
					$ob = arr[2];
					break;
				case "st": 
					$ob = arr[3];
					break;
				case "ut": 
					$ob = arr[4];
					break;
				default:break;
			}
			$ob = $ob.siblings('#' + p);
			var pos = $ob.position();
			if($.browser.msie) oben = pos.top +26;
			else oben = pos.top +25;
			
		}
		if(route !='' && route !='ue' && route !='uec' && route.substr(0,2) =='hd'){
			var p = route.substr(2,route.length-2);
			var arr = [];
			$('h1').each(function(index) {
	    		arr[index] = $(this);
	 		});
			switch(p){
				case "1": 
					$ob = arr[0];
					break;
				case "2":
					$ob = arr[1];
					break;
				case "3": 
					$ob = arr[2];
					break;
				case "4": 
					$ob = arr[3];
					break;
				case "5": 
					$ob = arr[4];
					break;
				default:break;
			}
			var pos = $ob.position();
			if(p == 1){
				oben = pos.top;
			}else{
				if($.browser.msie) oben = pos.top +33;
				else oben = pos.top +30;
			}
			
		}
		if(route =='ue'){
			var pos = $('.txt:last').position();
			if($.browser.msie) oben = pos.top +2;
			else oben = pos.top ;
		}
		if(route =='uec'){
			var pos = $('.txt:last').position();
			if($.browser.msie) oben = pos.top -65;
			else oben = pos.top -77;
		}
		
		$('html, body').scrollTo(parseInt(oben) , 1,{
			onAfter: function(){
				blockBeding = false;
				setTimeout("zeiger();",200);
			}
		});
}

function zeiger(){
	$('#frame').css("left","0px");
}

jQuery.fn.smartFocus = function(text) {
	$(this).val(text).focus(function(){
		if($(this).val() == text){
			$(this).val('');
			if($(this).attr('id') == 'teil6' || $(this).attr('id') == 'teil7' || $(this).attr('id') == 'teil8'){
				$(this).next().css('display','block');
			};
		}
		}).blur(function(){
			if( $(this).val() == ''){
			$(this).val(text);
			$(this).removeClass('mark');
		}else{
			$(this).removeClass("red").addClass('mark');
			if($(this).parent().attr('id') == 'Teilnehmer'){
				 calcCosts();
			};
		};
	});
};

function calcCosts(){
   if (strPauschal != 'j') {
	   var c = 0;
	   $('#Teilnehmer').children().each(function(i) {
    	   if($(this).hasClass('mark')) c++;
  	   });
  	   $('div#kosten').text("").text('Kosten gesamt: ' + costs*c +' €');
   }
   else {                                                           
      $('#kosten').text("").text('Kosten gesamt: ' + costs +' €');
   }
}

function getDate(tsGetMonth, intTrainingId, strPage, intPid) {
   $.getJSON('ajaxDispatcher.php?request=calendar&page=' + strPage + '&pid=' + intPid + '&getDate=' + tsGetMonth + '&termin=' + intTrainingId, function(json) {  
      $('#calendarContainer').html(json.content);
   });  
}

