
/* DOM loaded */
$(document).ready(function() {

  /* Galerie */
  if($('a.fancygalerie').length > 0){
    $('a.fancygalerie').fancybox({
      'hideOnContentClick': false,
      'width'			:	900,
      'height'		:	600
    });
  }
  
  /* Mailer */
  if($('a.fancymailer').length > 0){
    $('a.fancymailer').fancybox({
      'hideOnContentClick': false
     
    });
  }

  /* Karte */
  if($('a.fancymap').length > 0){
    $('a.fancymap').fancybox({
      'hideOnContentClick': false,
      'width'  : 600,
      'height' : 500
    });
  }

  /* Popup */
  if($('a.fancypopup').length > 0){
    $('a.fancypopup').fancybox({
    'autoDimensions' : false
    });
  }
  
  /* Link auf kompletten Teaser */
  $('#teaser .box').each(function() {
    if ($('h2 a', this).length) {
      $(this).css('cursor', 'pointer');
      $(this).click(function() {
          window.location.href = $('h2 a', this).attr('href');
      });
    }
  });

  /* Link auf komplette Meldung */
  $('#news ul li').each(function() {
    if ($('h3 a', this).length) {
      $(this).css('cursor', 'pointer');
      $(this).click(function() {
          window.location.href = $('h3 a', this).attr('href');
      });
    }
  });

  /* Kopfbilder */
  if($('#header-images').length > 0){
    $('#header-images').slideheader({
      'prev' : '#header-images a.prev',
      'next' : '#header-images a.next'
    });
  }

  init_js(); 
});


var imageRegExp = /\.(jpg|gif|png|bmp|jpeg)(.*)?$/i;


/* wird beim Seitenwechsel und beim reload aufgerufen */
function init_js(){
  
  /* Bilder */
  if($('a.fancybox').length > 0){
    $('a.fancybox').fancybox({
      'titleShow'     : true,
      'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
        if (title.match(imageRegExp)) {
          return false;
        }else{
          return '<span id="fancybox-title-over">' +  title + '</span>';
        }
      }
    });
  }

  hide_ladebalken();
}


/* Adressbox oeffnen/schliessen */
function toggle_adress(obj) {
	// Pfeilrichtung aendern
	$(obj).toggleClass("close");
	$(obj).toggleClass("open");
	$(obj).blur();
	// Details ein/ausblenden
	$(obj).next(".details").slideToggle(100);
}


/* Für Routing */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
			window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}


/* Bildwechsel */
function change_galerie_img(galerie_id, img_pos){
  cur_img = img_pos;

  // Thumbnail-Overview aktualisieren
  if(!document.getElementById('img_pos_'+img_pos)) {
    var page = Math.ceil(img_pos/6);
		change_page('box_galerie_thumbnails_'+galerie_id, page, '&galerie_id='+galerie_id);
  }

  // Bild laden
  $.ajax({
    url: '/include/ajax/change_galerie_img.php?galerie_id='+galerie_id+'&img_pos='+img_pos,
    success: function(data) {
      $('#display').fadeOut(125, function(){
        $('#display').empty().html(data).fadeIn(125);
      });
      $('.thumbnails a').removeClass('active');
      $('#img_pos_'+img_pos).addClass('active');
    }
  });
}


/* Seitenwechsel mit fadeIn und fadeOut */
function change_page(box,page,query_str,uri){
	if(box){
    if(document.getElementById('loader_'+box)){
      show_ladebalken('loader_'+box, 0, -20);
    }

    if( typeof $.scrollTo == 'function'){
      $.scrollTo($('#'+box),0);
    }
    $('#'+box).load('/include/ajax/change_page.php?box='+box+'&page='+page+'&content_id='+content_id+'&'+query_str+'&uri='+uri, {}, init_js);
	}
}


 /* Suchergebnis anzeigen */
function show_search_result(box, query_str, uri) {
	if(box) $('#'+box).load('/include/ajax/show_search_result.php?box='+box+'&'+query_str+'&uri='+uri, {}, init_js);
}


/* Suchen */
function clear_session_submit(formular) {
	s=document.createElement('script');
	s.src='/include/ajax/clear_session_page.php?formular='+formular;
	document.getElementsByTagName('head')[0].appendChild(s);
}


/* E-Mail senden */
function mailer_submit(){
	if(document.forms['fancymailer'] != undefined){
    $.fancybox.showActivity();
    $.ajax({
      'url':      '/timm/inc/mailer.php',
      'type':     'POST',
      'data':     $('form[name=fancymailer]').serialize() + '&send=1',
      'success':  function(data) {
         $('#fancybox-inner').html(data);
         $.fancybox.hideActivity();
        }
    });
	}
}


/* Ladebalken einblenden */
function show_ladebalken(box, top_offset, left_offset){
	left_offset = -20;

  var left = $('#'+box).offset().left;
  var top  = $('#'+box).offset().top;

  var padding_top = parseInt($('#'+box).css('padding-top'));
  top_offset += padding_top;
 
	$('#ajax-loader').css('left', left + left_offset + $('#'+box).width() + 'px');
	$('#ajax-loader').css('top',  top  + top_offset  + 'px');
	$('#ajax-loader').fadeIn(100);
}


/* Ladebalken ausblenden */
function hide_ladebalken() {
	$('#ajax-loader').fadeOut(100);
}


/* Standard-Value beim Suchfeld */
function firstempty(feld,wert){
	if(feld.value==wert){
		feld.value='';
	}else if(feld.value==''){
		feld.value=wert;
	}
}


/* Mouseover */
function bgc(theRow,theColor) {
  if(theRow.style.backgroundColor){
    theRow.style.backgroundColor = theColor;
  }
}


/* Link */
function url(v) {
  if(v.substr(0,7)=='http://') {
    window.open(v,'goto');
  }else if(top.location.href!=document.location.href){ // Karte im iFrame
    top.location.href=v;
  } else {
    document.location.href=v;
  }
}
