/*
 * Cookie tools namespace
 */
var Cookie = {};

/**
 * Write a cookie. Set days to false to create a cookie for the current session only
 */
Cookie.createCookie = function(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toGMTString();
    } else {
        var expires = '';
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

/**
 * Read a cookie value.
 */
Cookie.readCookie = function(name) {
    name += '=';
    var snippets = document.cookie.split(';');
    for (var i = 0; i < snippets.length; i++) {
        var c = snippets[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return '';
}

/**
 * Delete a cookie.
 */
Cookie.eraseCookie = function(name) {
    Cookie.createCookie(name, '');
}


/*
 * popup
 */ 
function popup(url, name, w, h) {
  window.open(
    url,
    name, 
    'location=yes, toolbar=no, resizable=yes, scrollbars=yes, '
     + 'top=' + (screen.height - h) / 2 + ','
     + 'left=' + (screen.width - w) / 2 + ','
     + 'width=' + w + ', height=' + h
  );
}

function goToSpecial(where, width, height) {
    window.open(where,'special','width='+width+',height='+height);
}


/*
 * avoid conflicts with other libs
 */
jQuery.noConflict();
$j = jQuery;

$j(document).ready(function() {
  
  /*
   * Gallery Popup
   */
  $j('a.gallery-popup').click(function(){
    var url = $j(this).attr('href');
    popup(url, 'gallery', 723, 560);    
    return false;    
  });
  
  /*
   * webcode form validation
   */
  if ($j('#webcode-form').length > 0) {
    $j('#webcode-form').validate({
          rules: {
              webcodeId: {
                  required: true,
                  digits: true,
                  minlength: 5,
                  maxlength: 5
              }
          },
          messages: {
              webcodeId: {
                 required: 'Bitte geben Sie einen Wert ein.',
                digits: 'Der eingegebene Code hat das falsche Format.',
                minlength: 'Der eingegebene Code hat das falsche Format.',
                maxlength: 'Der eingegebene Code hat das falsche Format.'
              }
          },
          errorPlacement: function(label, element) {
          label.insertAfter('#webcode-form input[type=submit]');
        }        
      });
  }
  

  /*
   * dropdown main menu
   */
  $j("li.entry_22, li.entry_23, li.entry_34, li.entry_35, li.entry_43, li.entry_44").hover(function() {
    $j(this).children('a').addClass("hse_active");
    $j(this).children('ul').addClass("hse_hover");
  },function(){
    $j(this).children('a').removeClass("hse_active");
    $j(this).children('ul').removeClass("hse_hover");
  });  
  
  
  /*
   * newsletter teaser validation
   */
  if ($j('#hse_teaser-newsletter').length > 0) {
    $j('#hse_teaser-newsletter').validate({
          rules: {
              email: {
                  required: true,
                  email: true
              }
          },
          messages: {
              email: 'Bitte geben Sie eine valide E-Mail-Adresse an.'
          },
          errorPlacement: function(label, element) {
          label.insertAfter( element.next().next() );
        }        
      });
      
      $j('#hse_teaser-newsletter input[name=email]').focus(function(){
        if ($j(this).attr('value') == 'E-Mail-Adresse eintragen') {
           $j(this).attr('value', '');
         }
    });
    
    $j('#hse_teaser-newsletter input[name=email]').blur(function(){
       var newValue = $j(this).attr('value');
       if (newValue == '') {
           $j(this).attr('value', 'E-Mail-Adresse eintragen');
       }
    });
  }
  
  
  /*
   * search forms
   */  
  //searchTerm
  $j('#hse_searchbox input[name=searchTerm]').attr('value', 'Suche');

  $j('#hse_searchbox input[name=searchTerm]').focus(function(){
     if ($j(this).attr('value') == 'Suche') {
    $j(this).attr('value', '');
     }
  });

  $j('#hse_searchbox input[name=searchTerm]').blur(function(){
     var newValue = $j(this).attr('value');
     if (newValue == '') {
       $j(this).attr('value', 'Suche');
     }
  });

  $j('#hse_searchbox').submit(function() {  
    var newValue = $j('#hse_searchbox input[name=searchTerm]').attr('value');
    if (newValue == 'Suche') {
      $j('#hse_searchbox input[name=searchTerm]').attr('value', '');
    }
  });
  
  
    
    $j('#hse_search-content input[type=text]').focus(function(){
      if ($j(this).attr('value') == 'Suchbegriff') {
           $j(this).attr('value', '');
        }
    });
    
    $j('#hse_search-content input[type=text]').blur(function(){
       var newValue = $j(this).attr('value');       
       if (newValue == '') {
           $j(this).attr('value', 'Suchbegriff');
       }
    });
    
    
    /*
   * events form
   */
    $j('#hse_veranstaltungen-form input[name=zipCode]').focus(function(){
      if ($j(this).attr('value') == 'PLZ') {
           $j(this).attr('value', '');
        }
    });
    
    $j('#hse_veranstaltungen-form input[name=zipCode]').blur(function(){
       var newValue = $j(this).attr('value');       
       if (newValue == '') {
           $j(this).attr('value', 'PLZ');
       }
    });
    
    // delete default fields on submit
    $j('#hse_veranstaltungen-form').submit(function() {  
      if ($j('input[name=eventFrom]').val() == 'Starttermin') {
        $j('input[name=eventFrom]').val('');
      }
      if ($j('input[name=eventUntil]').val() == 'Endtermin') {
        $j('input[name=eventUntil]').val('');  
      }
      if ($j('input[name=zipCode]').val() == 'PLZ') {
        $j('input[name=zipCode]').val('');
      }
      
      return true;
    });
  
  
  /*
   * branchenbuch form
   */    
  // form cannot be sent with the default value
  $j("#SuchmaskeEinfach div.hse_error").hide();
  
  $j('#SuchmaskeEinfach').submit(function() {  
    if ($j('#hse_suchbegriff').val() == 'Suchbegriff') {
        $j("div.hse_error").text('Bitte geben Sie einen Suchbegriff ein.').show('fast');
        return false;
      }      
      $j("div.hse_error").hide();
      return true;
  });
  
  
  $j('zipCode').focus(function(){
      if ($j(this).attr('value') == 'PLZ') {
           $j(this).attr('value', '');
        }
    });
    
    $j('zipCode').blur(function(){
       var newValue = $j(this).attr('value');       
       if (newValue == '') {
           $j(this).attr('value', 'PLZ');
       }
    });
  
  
  /*
   * portal search descriptions
   */ 
  $j('ul.ui-tabs-nav li.hse_portal').mouseover(function(){
       $j('#hse_hover-text-portal').show();
    });
    
    $j('ul.ui-tabs-nav li.hse_portal').mouseout(function(){
       $j('#hse_hover-text-portal').hide();
    });
    
    $j('ul.ui-tabs-nav li.hse_bibliothek').mouseover(function(){
     $j('#hse_hover-text-bibliothek').show();
  });
  $j('ul.ui-tabs-nav li.hse_bibliothek').mouseout(function(){
       $j('#hse_hover-text-bibliothek').hide();
    });
    
    $j('ul.ui-tabs-nav li.hse_shop').mouseover(function(){
       $j('#hse_hover-text-shop').show();
    });
    $j('ul.ui-tabs-nav li.hse_shop').mouseout(function(){
       $j('#hse_hover-text-shop').hide();
    });
  
  $j('a.hse_print').click(function(e) {
    e.preventDefault();
    popup(this.href, 'Drucken', 500, 600)
  });

});