var initial_height = 0;
var move_obj = false;
function collapseBox(){
	$('.toggle').click(function(e){
		tp = $(this).parent();
		var to = $(this).offset().top;
		if($(this).hasClass('hide') && $('div.main-page').height()>initial_height)
			$(document).scrollTo('-=170',300,function(){tp.parent().next('.holder').slideToggle('slow');});
		else
			tp.parent().next('.holder').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
		}
		return false;
	});	
	$('.subtoggle').click(function(e){
		if ($(this).hasClass('hide')){
			var scrollto = $(this).parent().next('.text-hide').height()+30;
			move_obj = $(this).parent().next('.text-hide');
			if(($(document).height() - $(this).parent().next('.text-hide').height()) < $(document).scrollTop()+$(window).height())
			{
				$(document).scrollTo('-='+scrollto,300);
				setTimeout("move_obj.slideToggle('slow');",300);
			}
			else
			{
				move_obj.slideToggle('slow');
			}
		}	
		else
		{
			$(this).parent().next('.text-hide').slideToggle('slow');	
		}
		
		
		
		
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
			$(this).parents('.text-hide').removeClass('hiden');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
			$(this).parents('.text-hide').addClass('hiden');
		}
		return false;
	});	
	$('.sidetoggle').click(function(e){
		var bh = $(this).parents('.block-hide');
		$(this).parent().next('.holder').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
			$(bh).children('.holder').removeClass('hiden');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
			$(bh).children('.holder').addClass('hiden');
		}
		return false;
	});	
}

function footerDown(){
	var footer = $('#footer');	
	var hfooter = footer.height();
	var hwrap = $('#wrap').height();
	var hdoc = $(document).height();	
	var hwin = $(window).height();
	
	if ((hfooter+hwrap) < hwin) {
		$(footer).css('margin-top',hwin-(hfooter+hwrap));
	} else {
		$(footer).css('margin-top',0);
	}
}

function initMain(){
	collapseBox();
	$(window).resize( footerDown );
	footerDown();
}

$(document).ready(function(){
	initMain();
});

$(document).ready(function(){
	initial_height = $('div.main-page').height();
	if ( $("#contactform #submit") )
  	$("#contactform #submit").click(function(i){
    var email_to_test = $("#email").val();
    if ( $("#name").val()=='' ){
      alert('Required field Name is empty');
      return false;
		}
		if ( $("#phone").val()=='' ){
      alert('Required field Phone is empty');
      return false;
		}
		if ( $("#email").val=='' ){
      alert('Required field Email is empty');
      return false;
		}
		if ( !email_to_test.match(/^[\.\-_A-Za-z0-9]+?@[\.\-A-Za-z0-9]+?\.[A-Za-z0-9]{2,6}$/) ){
      alert('Incorrect Email');
      return false;
		}
		if ( $("#message").val=='' ){
      alert('Empty Message text');
      return false;
		}
		
		var name = $("#name").val();
		var email = $("#email").val();
		var comments_text = $("#message").val();
		var phone = $("#phone").val();
		var to = $("#adm_mail").val();
		
		
		
		var send_data = 'name=' + name + '&email=' + email + '&phone=' + phone + '&to=' + to + '&message=' + escape(comments_text);
			$.ajax({
		   type: "POST",
		   url: "../send_email.php ",
		   data: send_data,
		   success: function(msg){
		    $('#contactform').animate({'opacity':0}, 1000, null, function(){
		    	$('#contactform').css('display','none');
				});
		   }
		 });
		
		return false;
  });
});
