function check_email(email)
{
  /* valid chars for email */
  var emailchars="@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZaöüAÖÜ+-_.0123456789~";
  var emailchars_res = true;
  for(i=0; i<email.length; i++) 
   { 
     emailchars_res && (emailchars.indexOf(email.charAt(i))!=-1) 
   }
  
  /* check lenght, @, .@, !.xxxxx  */
  email_res = ( emailchars_res &&
                email.length >= 6 &&
                email.search('@') >= 1 &&
                email.lastIndexOf('.') > email.search('@') &&
                email.lastIndexOf('.') >= email.length-5
          	   )
			   
  return email_res;
}

var form_error_mark_border  = "1px solid #df7c2a";
var form_error_reset_border = "1px solid #324676";

function form_error_mark(field)
{
  field.style.border = form_error_mark_border;
  field.focus();
}

function form_error_reset(field)
{
  field.style.border = form_error_reset_border; 
}


/* cre8ives.de : winInfo */
function winLoading(h) {
 winHide();
 winInfo('<div style="text-align:center;font-weight:normal;font-size:11px;"><img src="/images/loading.gif" alt="" /><br/><br/>bitte warten ...</div>','loading');
}

function winInfo(h,mode) {
  
  if(mode!='loading') var b='<br><br><span onclick="winHide()" style="cursor:pointer;cursor:hand;color:#dddddd;text-decoration:underline;font-weight:bold;">zurück</a>'; else b='';
  if(mode!='append' || $("#container .winInfo").length <= 0) {

    var $w=$('<div>').addClass("winInfo").css('display','none').html(h+b);
	$w.css({'padding':'20px','position':'absolute','background':'#484651','width':'340px','color':'#ffffff','font-weight':'bold','z-index':'1000','border':'1px solid #222222','font-size':'13px'});
    $w.css({'-moz-border-radius':'1ex','-webkit-border-radius':'1ex'});
	$w.fadeTo('slow', 0.95, function() { });
    $('#container').prepend($w);
   } 
   else 
   {
    var $w=$('#container .winInfo:first').html(h+b);
   }
   $w.css('top',($(window).height()/2-$w.height()/2+$(document).scrollTop())+'px'); 
   $w.css('left',($('#container').width()/2-$w.width()/2)-100+'px'); 
}

function winHide() {
  $('#container .winInfo').fadeOut('slow').delay('500').remove();
}

/* form */
function form_disable(id) {
 var $fe = $('#'+id).find('input, textarea, select');
 $fe.attr('disabled', 'disabled');
 $('#'+id).fadeTo('slow',0.4);
}

function form_enable(id) {
 var $fe = $('#'+id).find('input, textarea, select');
 $fe.attr('disabled', '');
 $('#'+id).fadeTo('slow',1);
}

/* fancy */
$(document).ready(function() {
   $("[rel=fancy]").fancybox({
	  'titlePosition' : 'inside',
	  'transitionIn' : 'elastic',
	  'transitionOut' : 'elastic'
  });
});


/* cre8ives.de : fotoframe */
$(document).ready(function() {
   $("img.foto").each( function() { 
   var w=$(this).width();
   var h=$(this).height();
   d=$("<div class='fotoframe'>").css({'display': 'block', 'width' : (w+10+16)+'px', 'height' : (h+10+16)+'px', 'position' : 'relative'});
   frame1=$("<div style='height:35px;width:69px;position:absolute;' class='png_fotoframe1'>").append("<img src='/images/fotoframe1.png' style='height:35px;width:69px;'/>");
   frame1b=$("<div style='height:"+(h+10)+"px;width:69px;position:absolute;' class='png_fotoframe1'>").append("<img src='/images/fotoframe1.png' style='height:"+(h+10)+"px;width:69px;'/>");
   frame2=$("<div style='height:35px;width:"+(w+10)+"px;position:absolute;' class='png_fotoframe2'>").append("<img src='/images/fotoframe2.png' style='height:35px;width:"+(w+10)+"px;' />");
      
   lt = $("<div class='lt' style='position:absolute;top:0px;left:0px;width:14px;height:14px;overflow:hidden;'>").append(frame1.clone().css({"top":"0px","left":"-14px"})).appendTo(d);
   rt = $("<div class='rt' style='position:absolute;top:0px;left:"+(w+6)+"px;width:20px;height:14px;overflow:hidden;'>").append(frame1.clone().css({"top":"0px","left":"-29px"})).appendTo(d);
   rb = $("<div class='rb' style='position:absolute;top:"+(h+6)+"px;left:"+(w+6)+"px;width:20px;height:20px;overflow:hidden;'>").append(frame1.clone().css({"top":"-15px","left":"-29px"})).appendTo(d);
   lb = $("<div class='lb' style='position:absolute;top:"+(h+6)+"px;left:0px;width:14px;height:20px;overflow:hidden;'>").append(frame1.clone().css({"top":"-15px","left":"-14px"})).appendTo(d);
      
   lc = $("<div class='lc' style='position:absolute;top:14px;left:0px;width:14px;height:"+(h-8)+"px;overflow:hidden;'>").append(frame1b.clone().css({"top":"-6px","left":"0px"})).appendTo(d);
   rc = $("<div class='rc' style='position:absolute;top:14px;left:"+(w+6)+"px;width:20px;height:"+(h-8)+"px;overflow:hidden;'>").append(frame1b.clone().css({"top":"-6px","left":"-49px"})).appendTo(d);
     
   ct = $("<div class='ct' style='position:absolute;top:0px;left:14px;width:"+(w-8)+"px;height:14px;overflow:hidden;'>").append(frame2.clone().css({"top":"0px","left":"-6px"})).appendTo(d);
   cb = $("<div class='cb' style='position:absolute;top:"+(h+6)+"px;left:14px;width:"+(w-8)+"px;height:20px;overflow:hidden;'>").append(frame2.clone().css({"top":"-15px","left":"-6px"})).appendTo(d);

   d.insertBefore(this);
   $(this).appendTo(d).css({'cursor':'pointer','cursor':'hand'});
   } );
});

/* kontakt form */
var errmsg='';
function check_kontaktanfrage_form(form) 
{
  var res=true;
  
  if(res!=false) {  
  /* check Person */
  field = form.person;
  form_error_reset(field);  
  if (field.value=="")
  {
   form_error_mark(field);  
   errmsg='Geben Sie bitte Ihren Namen an.';
   res=false;
  }}

  if(res!=false) {
  /* check EMail */
  field = form.email;
  form_error_reset(field);    
  if (field.value=="")
  {
   form_error_mark(field);    
   errmsg='Sie müssen Ihre E-Mail Adresse angeben.';
   res=false;
  }
  else if(!check_email(field.value))
  {
   form_error_mark(field);    
   errmsg='Die angegebene E-Mail Adresse ist nicht korrekt.';
   res=false;
  }}

  if(res!=false) {
  /* check Nachricht */
  field = form.nachricht;
  form_error_reset(field);  
  if (field.value=="")
  {
   form_error_mark(field);  
   errmsg='Geben Sie bitte Ihren Anfragetext an.';
   res=false;
  }}

  return res;
}

 $(document).ready(function() {
  $("#kontaktanfrage_form").submit(function() {
     if(check_kontaktanfrage_form(document.kontaktanfrage_form)) {
	  winLoading();
	  var f=$("#kontaktanfrage_form");
	  var $ret=$.post(f.attr("action"), f.serialize(), function() { winInfo('Ihre Anfrage wurde nun per E-Mail an uns übermittelt. Wir werden uns bald bei Ihnen melden.','append'); });
	  form_disable("kontaktanfrage_form");
	 } else { winInfo('Ihre Anfrage konnte nicht übermittelt werden:<br/>'+errmsg,'append'); }
	 return false;
	});
  });

/* div fader */
(function($) { $.fn.divFader = $.fn.divFader = function(delay)
  {
    
	delay = delay || 6000;
    initFader = function(el)
    {
      stopFader(el);
      el.items = $("div", el);
	  el.items.css('position','absolute');
      el.items.not(":eq(0)").hide().end();
      el.currentitem = 0;
	  startFader(el);
    };

   startFader = function(el) { el.tickfn = setInterval(function() { doTick(el) }, delay) };
   stopFader = function(el) { clearInterval(el.tickfn); };

   pauseFader = function(el) { el.pause = true; };
   resumeFader = function(el) { el.pause = false; };
   doTick = function(el)
   {
     if(el.pause) return;
     el.pause = true;
	 $(el.items[el.currentitem]).css("z-index", "2");
     $(el.items[el.currentitem]).fadeOut(1400,
       function() {
         $(this).hide();
      }
    );
	
    el.currentitem = ++el.currentitem % (el.items.size());
    $(el.items[el.currentitem]).css("z-index", "1");
	$(el.items[el.currentitem]).fadeIn(1600, function() { el.pause = false; } );

   };

   maxh = 0;
   $.each(this.children(), function(index, child){
   acth = parseInt($(child).outerHeight(true));
   if(acth>maxh) maxh=acth;
   });
   this.css('height',maxh+'px').css('position','relative');
   
   this.each( function() { if(this.nodeName.toLowerCase()!= "div") return; initFader(this); } )
   .hover( function() { pauseFader(this); }, function() { resumeFader(this); } );
   
   
   return this;
  };
})(jQuery);

$(document).ready(function() {
 $(".divFader").divFader();
});

