$(document).ready(function(){

		$('#summary').fadeOut('slow');
		$('#page_overlay').fadeOut('slow');

justLoaded = true;

lineSize();
$(window).resize(function () {lineSize()});

$('#page_overlay').height($(window).height());


$('#top_icons #i_3').click(function(){
	$('#top_menu div div[page=page_contacts]').click()
return false;
});
$('#top_menu div div')
   .css( {backgroundPosition: "0px 0"} )
   .mouseover(function(){
      if(!$.browser.msie) $(this).parent().stop().animate({backgroundPosition:"(-300px 0)"}, {duration:1000});
   })
   .mouseout(function(){
      if(!$.browser.msie) $(this).parent().stop().animate({backgroundPosition:"(0px 0)"}, {duration:500});
   })
   .click(function(){
      var pageWidth = $(window).width(),
			 $pauseTime = 0;

		$fromPage = $('#top_menu div div.sel').attr('page');
		$thisPage = $(this).attr('page');
		
      $('#top_menu div div').removeClass('sel');
      $(this).addClass('sel');
      $('#about_agb').fadeOut();
      $('#about_haftungsbeschluss').fadeOut();

		//return default values
		$(".content_bl").height('450px');
		$("#line").css('z-index','5');
		$(".content_bl").css('z-index','4');

      if ($(this).attr('page') == 'page_1' || $fromPage == 'page_3') {
         if ($("#line_common").css('margin-left') != '0px') {
            $("#line_portfolio").animate({marginLeft: '-'+ pageWidth +'px'}, 700, function(){ $("#line_portfolio").css('margin-left', pageWidth +'px'); });
            $pauseTime = 700;
			}
			
         $("#line_common").animate({marginLeft: '0px'}, $pauseTime, function(){
	         if ($thisPage == 'page_1') {
					$(".content_bl").slideUp(1000);
					$("#line").stop().animate({top: "103px"}, 1000);
				}/*
	         else {
					showPage($thisPage);
				}*/
			});
      }
      if ($(this).attr('page') == 'page_3') {
         if ($("#line_portfolio").css('margin-left') != '0px') {
            $("#line_common").animate({marginLeft: '-'+ pageWidth +'px'}, 700, function(){ $("#line_common").css('margin-left', pageWidth +'px'); });
            $pauseTime = 700;
			}
         $("#line_portfolio").animate({marginLeft: '0px'}, $pauseTime, function(){
	         $(".content_bl").slideUp(1000);
	         $("#line").stop().animate({top: "103px"}, 1000);
			});
      }
      else if ($(this).attr('page') == 'page_4') {
         showPage('page_calc');
      }
      else if ($(this).attr('page') != 'page_1') showPage($(this).attr('page'));
      return false;
   });
   

$('.open_calc').click(function(){
		//return default values
		$(".content_bl").height('450px');
		$("#line").css('z-index','5');
		$(".content_bl").css('z-index','4');

      $('#top_menu div div').removeClass('sel');
      $('#top_menu div div[page=page_4]').addClass('sel');

      showPage('page_calc');
      return false;
   });

if (location.hash == '#preise') {
   $('#top_menu div div[page=page_4]').click();
};


$('#p_links_in a').click(function(){
   var PIC = $(this).attr('href');
      $('#p_links_in a').removeClass('sel');
      $(this).addClass('sel');
         $("#line_portfolio .top_pic img").fadeOut();
         $("#"+PIC).fadeIn();
      return false;
   });


//Contacts form value switcher /input
$('input.switch').focus(function(){
   var defaultVal = $(this).attr('default_val'),
       currentVal = $(this).val();
   $(this).removeClass('error');
   if (currentVal == defaultVal) $(this).val('');
}).blur(function(){
   var defaultVal = $(this).attr('default_val'),
       currentVal = $(this).val();
   if (currentVal == '') $(this).val(defaultVal);
})
//Contacts form value switcher /textarea
$('textarea.switch').focus(function(){
   var defaultVal = $(this).attr('default_val'),
       currentVal = $(this).val();
   $(this).removeClass('error');
   if (currentVal == defaultVal) $(this).val('');
}).blur(function(){
   var defaultVal = $(this).attr('default_val'),
       currentVal = $(this).val();
   if (currentVal == '') $(this).val(defaultVal);
})


$('#p_arrs a').mouseover(function(){
   var SIDE = $(this).attr('side');
   portfolioSlide(SIDE);
}).mouseout(function(){
   $("#p_links_in").stop();
});


//services hover
$('#services_bl .item').hover(function(){
   $(this).addClass('hover');
}, function(){
   $(this).removeClass('hover');
});
//services_sub hover
$('#services_bl .item .subcats a').hover(function(){
   $(this).parents('.item').children('a.arr').addClass('hover');
}, function(){
   $(this).parents('.item').children('a.arr').removeClass('hover');
});

//services click
$('#services_bl a').click(function(){
		var $openPage = $(this).parents('.item').attr('id');
      $("#services_calc_bl").animate({marginRight: '-300px', opacity: 0}, 700);
      $("#services_bl").animate({marginLeft: '-700px', opacity: 0}, 700);
      $('.services_desr#descr_'+ $openPage).animate({marginLeft: '34px'}, 200, function(){$('.services_desr#descr_'+ $openPage).fadeIn(500);});
      return false;
   });

//services back click
$('.services_desr a.back').click(function(){
      $("#services_calc_bl").animate({marginRight: '0px', opacity: 1}, 700);
      $("#services_bl").animate({marginLeft: '0px', opacity: 1}, 700);
      $(this).parent().fadeOut();
      return false;
   });


//about agb click
$('#about_anim a.agb').click(function(){
      $('#page_about .right_calc').animate({marginRight: '-300px'}, 1000);
      $('#about_anim a.agb').fadeOut(300);
      $('#about_anim a.haftungsbeschluss').fadeOut(300);
      $('#about_anim #add_info').slideUp(300,
			function(){
				$('#about_anim').animate({marginLeft: '-700px'}, 700,
					function(){$('#about_agb').slideDown(300);}
				);
			});
      return false;
   });

//about haftungsbeschluss click
$('#about_anim a.haftungsbeschluss').click(function(){
      $('#page_about .right_calc').animate({marginRight: '-300px'}, 1000);
      $('#about_anim a.agb').fadeOut(300);
      $('#about_anim a.haftungsbeschluss').fadeOut(300);
      $('#about_anim #add_info').slideUp(300,
			function(){
				$('#about_anim').animate({marginLeft: '-700px'}, 700,
					function(){$('#about_haftungsbeschluss').slideDown(300);}
				);
			});
      return false;
   });

//about back click
$('#page_about a.back').click(function(){
      $('#page_about .right_calc').animate({marginRight: '0px'}, 1000);
      $(this).parent().slideUp(300,
			function(){
				$('#about_anim').animate({marginLeft: '0px'}, 700,
					function(){$('#about_anim #add_info').slideDown(300); $('#about_anim a.agb').fadeIn(300);$('#about_anim a.haftungsbeschluss').fadeIn(300);}
				);
			});

      return false;
   });


//Contact form send
$('#contacts_form a.submit').click(function(){

   $('#contacts_form .error_text').hide();

	if (checkReq('#contacts_form form') == false) {
		$('#contacts_form .error_text').show();
	}
	else {

		$.ajax({
	   	url: '/index.php?ajax=contacts',
   		data: 'act=contacts_send&'+ $('#contacts_form form').serialize(),
   		beforeSend: function(){
				$('#contacts_form .in').hide();
				$('#contacts_form .tree').show();
   		},
   		complete: function(){
      		$('#contacts_form .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   $('#contacts_form .thanks').fadeIn(1000, function() {
   		   $('#contacts_form .thanks').fadeOut(1500, function() {
					formClear('#contacts_form form');
					$('#contacts_form .in').fadeIn(1000);
				});
				});
	   	}
		});

	}

   return false;
});


//Main block, show contactform
$('.main_in .right_bl a.more').click(function(){
	$(this).parents('.right_bl').slideUp(300, function() {
		$(this).parents('.main_in').children('.contacts_bl').slideDown(300);
	});
      return false;
   });
//Contact form send
$('.contacts_form a.submit').click(function(){

   var $postData = $(this).parents('form').serialize(),
		 $formNum = '#form_' + $(this).attr('form_num');

   $('.contacts_form .error_text').hide();
   
	if (checkReq('.contacts_form form'+$formNum) == false) {
		$('.contacts_form .error_text').show();
	}
	else {

		$.ajax({
	   	url: '/index.php?ajax=contacts',
   		data: 'act=contacts_send&'+ $postData,
   		beforeSend: function(){
				$('.contacts_form form'+$formNum).parents('.contacts_form').children('.in').hide();
				$('.contacts_form form'+$formNum).parents('.contacts_form').children('.tree').show();
   		},
   		complete: function(){
      		$('.contacts_form form'+$formNum).parents('.contacts_form').children('.tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   $('.contacts_form form'+$formNum).parents('.contacts_form').children('.thanks').fadeIn(1000, function() {
   		   $('.contacts_form form'+$formNum).parents('.contacts_form').children('.thanks').fadeOut(1500, function() {
					formClear('.contacts_form form');
					$('.contacts_form form'+$formNum).parents('.contacts_form').children('.in').show();
					$(this).parents('.contacts_bl').hide();
					$(this).parents('.main_in').children('.right_bl').slideDown(300);
				});
				});
	   	}
		});

	}

   return false;
});
//close contacts form
$('.contacts_form a.close').click(function(){

   formClear('.contacts_form form');
   $('.contacts_form .error_text').hide();
	$(this).parents('.contacts_bl').slideUp(300, function() {
		$(this).parents('.main_in').children('.right_bl').slideDown(300);
	});

   return false;
});


//Main Corporate Identity click
$('a.service_corporate_identity').click(function(){

   $('#top_menu div div[page=page_services]').click();
	$("#services_bl").animate({opacity: 1}, 1000, function(){$('#services_bl .item#num_1 a').click();});

   return false;
});





picRotate();
arrowBlink();

//MAIN!
});


//Main arrows blinking
var $blinkedNum = 0;
var $blinkedArrows = new Array('arr_l_hover', 'arr_r_hover');
function arrowBlink () {
	$p = $blinkedArrows[$blinkedNum];
	$('a#'+$p).fadeIn(200, function () {
	   $('a#'+$p).fadeOut(120, function () {
	   	$('a#'+$p).fadeIn(300, function () {
	   		$('a#'+$p).fadeOut(150);
			});
		});
	});

	$blinkedNum++;
	if ($blinkedNum > 1) $blinkedNum = 0;

setTimeout(arrowBlink,2000);
}


function lineSize () {
   $('#line').width(0);
   $('#line').width($(window).width());
   $('#line').css('margin-left', ($(window).width() - 963) / -2);
}


var PIC = 1;
function picRotate () {
      var PICS_NUM = $('#line_common .top_pic img').length,
          NEXT = PIC+1;
          if (PIC == PICS_NUM) NEXT = 1;
      var PIC_1 = '#top_pic_'+PIC,
          PIC_2 = '#top_pic_'+NEXT;

$(PIC_1).fadeTo(2000, 1, function () {
   $(PIC_1).fadeOut(3000);
   $(PIC_2).fadeIn(3000);
});

setTimeout(picRotate,15000);

PIC = NEXT;
}


function showPage(PAGE) {
justLoaded;

if (justLoaded == false) $(".content_bl").slideUp(1000);

if (justLoaded == true) fTime = 0;
else fTime = 1000;
$("#line").stop().animate({
      top: "103px"
   }, fTime,
   function(){
   
   	$("#services_bl").css('margin-left', '0px');
      $("#services_bl").animate({opacity: 1}, 1);
		$("#services_calc_bl").css('margin-right', '0px');
		$("#services_calc_bl").animate({opacity: 1}, 1);
		$(".services_desr").hide();
   	$("#about_anim").css('margin-left', '0px');
   	$("#about_anim #add_info").show();
   	$("#about_anim a.agb").show();
   	$("#about_anim a.haftungsbeschluss").show();
		$("#page_about .in .right_calc").css('margin-right', '0px');
		$("#about_agb").hide();
		$("#about_haftungsbeschluss").hide();

      $(".content_bl#"+PAGE).slideDown(700);
//      $(".content_bl#"+PAGE).animate({height: "347px"}, 700);
      $("#line").animate({
         top: "450px"
      }, 700,
         function(){
//         $(".content_bl#"+PAGE).animate({height: "257px"}, 700);
         $("#line").animate({
            top: "360px"
         }, 500,
            function(){$("#line").animate({
               top: "410px"
            }, 500,
               function(){$("#line").animate({
                  top: "375px"
               }, 500,
                  function(){$("#line").animate({
                     top: "395px"
                  }, 500,
                     function(){$("#line").animate({
                        top: "380px"
                     }, 550,
                        function(){$("#line").animate({
                           top: "385px"
                        }, 500,
                           function(){
                              $(".content_bl#"+PAGE).height('282px'); $("#line").css('z-index','4'); $(".content_bl#"+PAGE).css('z-index','100');
                           }
                        );}

                     );}

                  );}

               );}

            );}

         );}

      );}

   );

   justLoaded = false;

}


currentSlideBlock = 1;

function mainSlide (SIDE) {
   var currentMargin = $("#main_bl_in").css('margin-left'),
       blocksNum = $(".main_in").length;
       slideWidth = 963;

   if (currentSlideBlock == 1 & SIDE == -1) {
      MARGIN = slideWidth * (blocksNum - 1) * -1;
      currentSlideBlock = blocksNum;
   }
   else if (currentSlideBlock == blocksNum & SIDE == 1) {
      MARGIN = 0;
      currentSlideBlock = 1;
   }
   else {
      MARGIN = parseInt(currentMargin) + slideWidth * SIDE * -1;
      currentSlideBlock = currentSlideBlock + SIDE;
   }

   MARGIN += 'px';

   $("#main_bl_in").animate({
      marginLeft: MARGIN
   }, 1000);

   return false;
}


countSlidePortfolio = 0;
function portfolioSlide (SIDE) {
   var currentMargin = parseInt($("#p_links_in").css('margin-top')),
       blockHeight = $("#p_links_in").height(),
       blocksNum = $("#p_links_in a").length,
       slideTime = 70 * blocksNum;

countSlidePortfolio = countSlidePortfolio + 1;

   if (currentMargin >= 48 & SIDE == -1) {
      if (blockHeight >= 120) MARGIN = (blockHeight - 120) * -1;
      else MARGIN = 48;
   }
   else if ((currentMargin == blockHeight * -1 || currentMargin == (blockHeight - 120) * -1) & SIDE == 1) {
      MARGIN = 48;
   }
   else {
      MARGIN = currentMargin - parseInt(SIDE);
      slideTime = 3;
   }

   MARGIN += 'px';

   $("#p_links_in").animate({
      marginTop: MARGIN
   }, slideTime, function () {portfolioSlide (SIDE);});

   return false;
}


function checkReq (FORM) {
	var $error = 0,
	$fieldSet = $(FORM +' input.req').add(FORM +' textarea.req'),
	$fieldSetAll = $(FORM +' input').add(FORM +' textarea');

   $.each($fieldSet, function(i, field){
			$fieldSet.eq(i).removeClass('error');
		if (field.value == '' || field.value == field.defaultValue) {
			$error++;
			$fieldSet.eq(i).addClass('error');
		}
   });

   if ($error > 0) return false;
   else {
	   $.each($fieldSetAll, function(i, field){
			if ($fieldSetAll.eq(i).hasClass('req') == false & field.value == field.defaultValue) field.value = '';
		});
   }
}

function formClear (FORM) {
	var $fieldSetAll = $(FORM +' input').add(FORM +' textarea');

	$.each($fieldSetAll, function(i, field){
		field.value = field.defaultValue;
	});

}
