$().ready(function(){
   // Animation der Top-Navigation mit Kwicks
   $("#senior").kwicks({
      max:238,
      spacing: 8
   });

   
   // Bilder innerhalb des Akkordions werden beim Anklicken
   // nocheinmal in gross in der Vordergrund gebracht.
   $("a[rel='Startpaket']").colorbox({
      top: "50px",
      left: "250px",
      current: "Bild {current} von {total}"
   });
   $("a[rel='Erweiterungsmodule']").colorbox({
      top: "50px",
      left: "250px",
      current: "Bild {current} von {total}"
   });


   // Akkordion innerhalb der Tabs
   $("#homepage-tab-1").accordion({
      autoHeight: false,
      navigation:true, 
      collapsible:true,
      active:false
   });
   $("#homepage-tab-2").accordion({
      autoHeight: false,
      navigation:true,
      collapsible:true,
      active:false
   });
   $("#webshop-tab-1").accordion({
      autoHeight: false,
      navigation:true,
      collapsible:true,
      active:false
   });
   $("#webshop-tab-2").accordion({
      autoHeight: false,
      navigation:true,
      collapsible:true,
      active:false
   });
   $("#adcommander-tab-1").accordion({
      autoHeight: false,
      navigation:true,
      collapsible:true,
      active:false
   });
   $("#adcommander-tab-2").accordion({
      autoHeight: false,
      navigation:true,
      collapsible:true,
      active:false
   });

   // Tabs
   $(".tabs").tabs();



   $("#contactFormWelcome").hide();

   $("#contactButton").click(function(){

      $("#contactFormWelcome").html('');
      $("#contactFormWelcome").load("/Kontakt/Rueckruf/", function()
       {
         activeArea = '';
         // Aktiven Bereich ermitteln
         if($("#teaserPager > .activeSlide > div").html())
         {
            activeArea = $("#teaserPager > .activeSlide > div")
               .html().toLowerCase();
         }
         else if( $("#contactButton").parent().parent().attr('id') )
         {
            activeArea = $("#contactButton")
               .parent().parent().attr('id').toLowerCase();
         }

         // Und im Kontaktformular auswählen
         $("#contactFormWelcome .subjectCheckbox").each(function()
         {
            if($(this).val().toLowerCase() == activeArea)
            {
               $(this).attr('checked', true);
            }
            else
            {
               $(this).attr('checked', false);
            }
         });

         $("#contactFormWelcome button").click(function()
         {
            if($("label.error:visible").length > 0)
            {
               return false;
            }

            // Formular serialisieren und per AJAX abschicken
            var formData = $("#contactFormWelcome form").serializeArray();
            $("#contactFormWelcome").load("/Kontakt/", formData, function()
            {
               setTimeout(function() { 
                  $("#contactFormWelcome").dialog('close'); }, 2 * 1000);
               });
               return false;
            });

         // Eingaben per JS-Validieren
         $("#contactFormWelcome form").validate();


         $("#contactFormWelcome").dialog({
            width: 760,
            height: 580,
            modal: true ,
            hide: {effect: 'fade', duration: 1000}
         });
       });


      return false;
   });


   /*
    * Teaser
    */
   // Inhalt fuer die Paginierung auslesen (alt-Tag vom jeweiligen Bild)
   var pageTitles = new Array();
   var pageCounter = 0;
   $("#teaserContent > a > img").each(function(){
      pageCounter++;
      pageTitles[pageCounter] = $(this).attr("alt");
   });

   // Cycle
   
   $("#teaserContent").show().cycle({
      pager: $("#teaserPager"),
      fx: 'fade',
      timeout: 7000
   });

   // Zahlen in der Paginierung ersetzen
   pageCounter = 1;
   $("#teaserPager a").each(function(){
      $(this).html('<div>'+pageCounter+'</div>');
      pageCounter++;
   });

   /*
    * Webshop Gallery
    */

   var webshopTitles = new Array();
   var webshopCounter = 0;
   $("#galleryContent > a > img").each(function(){
      webshopCounter++;
      webshopTitles[webshopCounter] = $(this).attr("alt");
   });

   $("#galleryContent").cycle({
      pager: $("#galleryPager"),
      fx: 'fade',
      timeout: 50000,
      prev: $('.navi .prev'),
      next: $('.navi .next'),
      after: function() {
               var fullTitle = $(this).find('img').attr('title').split('_');
               var title = fullTitle[0];
               var url = fullTitle[1];
               $('#captionText').html(title);
               $('#captionText').append('<br />'+url);
            }
   });

   // Zahlen in der Paginierung ersetzen
   webshopCounter = 1;
   $("#galleryPager a").each(function(){
      $(this).html('<img src="/upload/referrenzen/'
         +webshopTitles[webshopCounter]+'" alt=""/>');
      webshopCounter++;
   });
  
   // die Tab-Anchors richtig anspringen... das tabs plugin von jquerui macht 
   // echt komische sachen :(
   var test = location.href;
   if (test.indexOf("#") != -1)
   {
      location.href = location.href;
   }
   if ($('#ticker')){
      $.ajax( {
         "dataType": 'html',         
         "type": "GET", 
         "url": "/news/feed/",           
         "success": function(data)
         {
            xmlDoc = $.parseXML(data);
            $xml = $ (xmlDoc );
            items = $xml.find('item');
            var counter = 6;
            $.each(items ,function(key,item)
            {               
               counter--;
               if (counter > 0){
                  $('#ticker').append(
                     '<li><a href="'+$("link",item).text()+'">'
                     +$("title",item).text()+'</a></li>'
                  );    
               }
            });
            $('#ticker').liScroll();            
         }
      });
      
   
   }
   
});

