// cased.js
// zeitform Internet Dienste 2009

// add cookie elements
function addCookieElement (cookie, value) {

  if ($.cookie(cookie) && $.cookie(cookie).length > 0) {
    var cook = $.cookie(cookie).split("|");
    var newcookie = new Array();
    for (var i=0; i < cook.length; i++) { 
      if (value != cook[i]) newcookie.push(cook[i]);
    }
    newcookie.push(value);
    $.cookie(cookie, newcookie.join("|"));
    //$.cookie(cookie, $.cookie(cookie) + '|' + value);
  } else {
    $.cookie(cookie, value);
  }
}

// remove cookie elements
function removeCookieElement (cookie, value) {

    var cook = $.cookie(cookie).split("|");
    var newcookie = new Array();
    for (var i=0; i < cook.length; i++) { 
      if (value != cook[i]) newcookie.push(cook[i]);
    }
    $.cookie(cookie, newcookie.join("|"));
}


$(document).ready(function() {

  // add more link to teaser
  $('div.more').each( function(i) { addmorelink(this, 'Weiter'); });
  $('div.more-en').each( function(i) { addmorelink(this, 'More'); });

  // external pages
  $('a[rel=external]').attr("target", "_blank");

  // press image popups
  $('tr.bilder').css("cursor", "pointer")
    .click( function(e) {
      $('div.bilddetails').hide();
      $('div.bilddetails', $(this)).css("cursor", "auto").show();
    });

  $('span.close').css("cursor", "pointer")
    .click( function(e) {
      e.stopPropagation();
      $('div.bilddetails').hide();
    });

  // kontaktblock
  $('div.kontaktinfo').hide();
  var html = $('div.kontaktinfo').html();
  if (html) { 
    $('#kontaktblock').html(html).show();
    //$('.newsblock').hide();
  }

  // tu-publikationen -- obsolete 
  //$("#tupublikationen").load("/files/tupub.html #colNormal table").show();


  // publikationen
  $('.publication p.title a').click(function(){
    $(this).parents('div.publication').next('div.details').toggle();
    $(this).parent('p.title').next('p.info').toggle();
  });

  if (window.location.search.match(/^.*p=(\d+).*$/i)) {
    var id = RegExp.$1;
    $('#pub_' + id + ' a').click();
  }

  // stellenangebote
  $('dd' , $('#stellenangebote')).hide();
  $('dd' , $('#stellenangebote')).each(function(i) { $(this).attr("id", "category_" + i); });
  if ($.cookie('karriere')) {
    var cook = $.cookie('karriere').split("|");
    for (var i=0; i < cook.length; i++) { 
      $('#' + cook[i]).show();
    }
  }

  $('dt' , $('#stellenangebote')).each(function(i) {
    var count = $(this).next('dd').children('ul').children('li').size() || "0";
    $(this).append(' <span' + (count > 0 ? ' class="red"' : '') + '>(' + count + ')</span>');
    if (count > 0) {
      $(this).css("cursor", "pointer")
        .click( function() {
		  var e = $(this).next('dd');
                  e.toggle();
		  if (e.is(':hidden')) {
		    removeCookieElement('karriere', e.attr('id'));
		  } else {
		    addCookieElement('karriere', e.attr('id'));
		  }

      });
    }
  });


  // kalender
  //$('#kalender').load("/cgi-bin/cal.pl", null, loadcal);

  // highlite search results

  //if (document.referrer.match(/^.*s=([^&]+)&?.*$/i)) {
  if (window.location.search.match(/^.*hilite=([^&]+)&?.*$/i)) {
    var query = decodeURIComponent(RegExp.$1);
    query = query.replace(/\'|"/, ''); // "\' fix syntax hilite
    query = query.split(/[\s,\+\.]+/);
    //console.log(query);
    for (var i=0; i < query.length; ++i) {
      $('#col3_content').highlight(query[i]);
    }
    $('span.highlight').closest('.more,.more-en').each(function(){
      $(this).show();
      $(this).prev('.morelink').hide();
    });
  }
  // $('body').highlight('CASED');


  // google map
  $('#map').jmap('init', {
                          'mapType':   'hybrid',
		          'mapEnableType': true,
                          'mapZoom':   16,
                          'mapShowjMapsIcon': false,
		          //'mapCenter': [49.87267, 8.6354]
		          'mapCenter': [49.87243, 8.63368]
			  });
  $('#map').jmap('AddMarker', {
                          'pointLatLng': [49.87210, 8.6350],
                          'pointHTML': $('#kontaktdetails').html()
		          });

  // sort table
  $('#bildarchiv').tablesorter(); 

});


function loadcal() {
  $('#cal_next,#cal_prev').css('cursor','pointer').unbind().click(function(){
    $('#kalender').load("/cgi-bin/cal.pl", { date: $(this).attr('rel') }, loadcal); 
  });
}

// add more link
function addmorelink (more,text) {
  $(more).hide();
  $('<div class="morelink"><a href="javascript:void(0);">' + text + '&gt;&gt;</a></div>')
    .insertBefore(more)
    .click(
      function () {
        $(this).hide();
        $(more).show();
      }
    );
}
