function htmlEncode(value){
  return $('<div/>').text(value).html();
}

function htmlDecode(value){
  return $('<div/>').html(value).text();
}


$(document).ready(function() {

   /**
   * Arzneimittelvorbestellung-Formular Validierung
   */
   $('#drugsPreorderForm').validate();

   /**
    * Kundenkarten-Formular Validierung
    */
    $('#loyaltyCardOrderForm').validate();

    /**
     * Kontakt-Formular Validierung
     */
    $('#contactForm').validate();

    /**
     * Newsletter-Formular Validierung
     */
    $('#newsletterForm').validate();

    /*
     * Kalender für die Datumsauswahl bei der Kundenkarte
     */
    $('#birthday').datepicker({
       yearRange: "1900:new Date()",
       minDate: new Date(1900, 1-1,1),
       maxDate: '-16y',
       showOn: "both",
       buttonImage: "/images/kalender.gif",
       buttonImageOnly: true,
       changeMonth: true,
       changeYear: true,
       showWeek: true,
       onClose: function ()
       {
           this.focus();
       }
    });

    // Initialisierung des Kalenders
    $("#birthday").datepicker("setDate", $('#birthday').val());

   // Top-Navigation
   $(".topNav > ul").addClass("sub");
   $(".sub > li > ul").css("display","none");

   // Sub-Sub-Navigation
   $("#subSubNavigation > ul").addClass("subArea");

   // Trenner in Sub-Sub-Navigation einfuegen
   $("#subSubNavigation > ul > li").append(" | ");
   tmp = $("#subSubNavigation > ul > li").last().html();
   if (tmp!=null) tmp = tmp.substring(0,tmp.length-2);
   $("#subSubNavigation > ul > li").last().html(tmp);

   $("#sidebarMenu > ul > li > ul").slideToggle(0);
   $("#sidebarMenu > ul > li ").toggleClass("closed");

   // aktuellen Sidebareintrag ausklappen
   entries = $("#sidebarMenu > ul > li ");
   $.each(entries, function (key,value){
      if ($(value)[0].children[0].href == location.href){
         $("ul",this).slideToggle();
         $(this).toggleClass("closed");         
      }
   });
   entries = $("#sidebarMenu > ul > li > ul > li ");
   $.each(entries, function (key,value){
      if ($(value)[0].children[0].href == location.href){         
         $(this).parent().slideToggle();         
         $(this).parent().parent().toggleClass("closed");         
      }
   });
   
   
$("#offerPageOffers h2:contains('ohne Kategorie')").prepend('<img src="/images/kgweitereTopangebote.png"/>');   
$("#offerPageOffers h2:contains('Körperpflege')").prepend('<img src="/images/kgKoerperpflege.png"/>');
$("#offerPageOffers h2:contains('Abnehmen & Diät')").prepend('<img src="/images/kgAbnehmenDiaet.png"/>');
$("#offerPageOffers h2:contains('Mund- & Zahnpflege')").prepend('<img src="/images/kgMundZahnpflege.png"/>');
$("#offerPageOffers h2:contains('Kosmetik & Wellness')").prepend('<img src="/images/kgKosmetikWellness.png"/>');
$("#offerPageOffers h2:contains('Mutter & Kind')").prepend('<img src="/images/kgMutterKind.png"/>');
$("#offerPageOffers h2:contains('Sanitätspr. & Hygiene')").prepend('<img src="/images/kgHygieneSanitaetsprodukte.png"/>');
$("#offerPageOffers h2:contains('Hund, Katze & Co.')").prepend('<img src="/images/kgHundKatze.png"/>');
$("#offerPageOffers h2:contains('Bonbons & Co.')").prepend('<img src="/images/kgBonbons.png"/>');
$("#offerPageOffers h2:contains('Urlaub & Reise')").prepend('<img src="/images/kgUrlaubReise.png"/>');
$("#offerPageOffers h2:contains('Erste Hilfe & Desinfektion')").prepend('<img src="/images/kgErsteHilfe.png"/>');
$("#offerPageOffers h2:contains('Diabetes')").prepend('<img src="/images/kgDiabetes.png"/>');
$("#offerPageOffers h2:contains('Fit & Vital')").prepend('<img src="/images/kgFitVital.png"/>');
$("#offerPageOffers h2:contains('Rund um die Frau')").prepend('<img src="/images/kgFrau.png"/>');
$("#offerPageOffers h2:contains('Abwehrkräfte steigern')").prepend('<img src="/images/kgAbwehrkraefte.png"/>');
$("#offerPageOffers h2:contains('Erkältung')").prepend('<img src="/images/kgErkaeltung.png"/>');
$("#offerPageOffers h2:contains('Schmerzen')").prepend('<img src="/images/kgSchmerzen.png"/>');
$("#offerPageOffers h2:contains('Venen & Hämorrhoiden')").prepend('<img src="/images/kgVenenHaemorrhoiden.png"/>');
$("#offerPageOffers h2:contains('Rund um den Mann')").prepend('<img src="/images/kgMann.png"/>');
$("#offerPageOffers h2:contains('Haut, Haare, Nägel')").prepend('<img src="/images/kgHautHaareNaegel"/>');
$("#offerPageOffers h2:contains('Lust & Liebe')").prepend('<img src="/images/kgLustLiebe.png"/>');
$("#offerPageOffers h2:contains('Allergien & Asthma')").prepend('<img src="/images/kgAllergieAsthma.png"/>');
$("#offerPageOffers h2:contains('Schlaf & Nerven')").prepend('<img src="/images/kgSchlafNerven.png"/>');
$("#offerPageOffers h2:contains('Auge & Ohr')").prepend('<img src="/images/kgAugeOhr.png"/>');
$("#offerPageOffers h2:contains('Herz, Kreislauf, Blut')").prepend('<img src="/images/kgHerzKreislaufBlut.png"/>');
$("#offerPageOffers h2:contains('Niere, Blase, Prostata')").prepend('<img src="/images/kgNiereBlaseProstata.png"/>');
$("#offerPageOffers h2:contains('Magen & Darm')").prepend('<img src="/images/kgMagenDarm.png"/>');
$("#offerPageOffers h2:contains('Gesichtspflege')").prepend('<img src="/images/kgGesichtspflege.png"/>');
$("#offerPageOffers h2:contains('Haarpflege')").prepend('<img src="/images/kgHaarpflege.png"/>');
$("#offerPageOffers h2:contains('Krankenpflegeartikel')").prepend('<img src="/images/kgKrankenpflegeartikel.png"/>');
$("#offerPageOffers h2:contains('Homöopathie / Biochemie')").prepend('<img src="/images/kgHomoeopathieBiochemie.png"/>');
$("#offerPageOffers h2 a").text("");

/**
 * Notdienst von apotheken.de per ajax laden
 */
if ($("#emergencyPlanAjax").length > 0){
   $.ajax({
      type: "GET",
      url: "/Notdienst/ajax/",
      dataType: "xml",
      success: function(data){
         $(data).find('pharmacy').each(function(){            
            $("#emergencyPlanAjax > ul").append(
                  '<li><a href="/Notdienst/'+$("#time").val()+'/'+$(this).attr('Code')+'/">'
                  +$(this).find('name').text()
                  +'</a></li>');            
         });        
      }
   });
}
/**
 * News von apotheken.de per ajax laden
 */
// Artikel auf der Startseite
if ($("#apothekenDeArticles").length > 0){
   $.ajax({
      type: "GET",
      url: "/Artikel/Startseite/",
      dataType: "xml",
      success: function(data){
         var headlines = new Array();
         var headlineCounter = 1;
         $(data).find('articledata').each(function(){            
            $("#apothekenDeArticles").append(
               '<div class="article">'
               +  '<div class="articleLong">'
               +     '<h1>'+$(this).find('title').text()+'</h1>'
               +     '<div class="articleLongImg">'
               +        '<img src="'+$(this).find('pic_path').text()+$(this).find('pic').text()+'"/>'
               +     '</div>'               
               +     $(this).find('content').text()
               +     '<br/>Quelle: apotheken.de | '+$(this).find('pic_credits').text()
               +  '</div>'                           
               +  '<div class="articleShort">'
               +     '<h2>'+$(this).find('title').text()+'</h2>'
               +     '<div class="articleShortImg">'
               +        '<img src="'+$(this).find('pic_path').text()+$(this).find('pic').text()+'" width="300"/><br/>'
               +     '</div>'
               +     '<h3>'+$(this).find('headline').text()+'</h3>'
               +  '</div>'
               +  '<div class="arcicleIntro">'
               +  '</div>'               
               +'</div>'
            );
                        
            headlines[headlineCounter] = $(this).find('title').text();
            headlineCounter++;
            $("#apothekenDeArticles .arcicleIntro").last().html(htmlDecode($(this).find('intro').text())+'<a href="#">Artikel lesen</a>');                     
         });
         
         $(".articleLong").find("b").wrap('<h2/>');
         
         $(".article").each(function(){            
            var dialog = $(".articleLong", this);            
            $(".articleLong", this).dialog({
               autoOpen: false,
               width: 800
            });            
            $("a", this).click(function(){
               $(dialog).dialog("open");
               return false;
            });
            $("img", this).click(function(){
               $(dialog).dialog("open");
               return false;
            });            
         });
         
         // Cycle
         $("#apothekenDeArticles").cycle({
            pager: $("#apothekenDeHeadlines"),
            fx: 'fade',
            timeout: 5000
               
         });
         headlineCounter = 1;
         $("#apothekenDeHeadlines a").each(function(){
            $(this).html(headlines[headlineCounter]);
            headlineCounter++;
         });
      }
   });
}

// Artikel in der Sidebar
if ($("#apothekenDeArticlesSidebar").length > 0){

   $.ajax({
      type: "GET",
      url: "/Artikel/Startseite/",
      dataType: "xml",
      success: function(data){
         $(data).find('articledata').each(function(){            
            $("#apothekenDeArticlesSidebar").append(
               '<div class="article">'
               +  '<div class="articleLong">'
               +     '<h1>'+$(this).find('title').text()+'</h1>'
               +     '<div class="articleLongImg">'
               +        '<img src="'+$(this).find('pic_path').text()+$(this).find('pic').text()+'" />'
               +     '</div>'               
               +     $(this).find('content').text()
               +     '<br/>Quelle: apotheken.de | '+$(this).find('pic_credits').text()               
               +  '</div>'                           
               +  '<div class="articleShort">'
               +     '<div class="articleShortImg">'
               +        '<img src="'+$(this).find('pic_path').text()+$(this).find('pic').text()+'"/><br/>'
               +     '</div>'
               +     '<h2>'+$(this).find('title').text()+'</h2>'               
               +     '<div class="articleShortText">'
               +        $(this).find('headline').text()+' <a href="#">Artikel lesen</a>'
               +     '</div>'
               +  '</div>'
               +  '<div class="arcicleIntro">'
               +  '</div>'               
               +'</div>'
            );
            
         });
         
         $(".articleLong").find("b").wrap('<h2/>');
         
         $(".article").each(function(){            
            var dialog = $(".articleLong", this);            
            $(".articleLong", this).dialog({
               autoOpen: false,
               width: 800
            });            
            $("a", this).click(function(){
               $(dialog).dialog("open");
               return false;
            });
            $("img", this).click(function(){
               $(dialog).dialog("open");
               return false;
            });                        
         });
         
         
         
         // Cycle
         $("#apothekenDeArticlesSidebar").cycle({            
            fx: 'scrollHorz',
            random: 1,
            timeout: 50000,
            speed: 500,
            prev: $('.cycleBox .prev'),
            next: $('.cycleBox .next')
         });         
      }
   });
}




});

