var activeAjaxAnchor = null;
/*
### Flyoutmenu ###
### mit markierung der ersten Ankerebene
### im TS muss erste Ebene �ber ATagParams auf A.firstlevel gestellt sein
*/
function FlyOutMenu(obj) {

    if (!obj.length) return;
    
    // KLAPPBOX
    
    klappboxCloses = jQuery('.klappbox .close-klappbox');
    klappboxOpens = jQuery('.klappbox .open-klappbox');
   
    
    jQuery(klappboxCloses).each(
        function(intIndex) {     
             /*par = jQuery(this).parent(); 
             hpar = jQuery(this).parent().parent();   
             innerContent = jQuery(hpar).children('.innercontent').first();   
             tmpWidth = jQuery(innerContent).width();  
             tmpPaddingRight = parseInt(jQuery(par).css("padding-right"));
             tmpPaddingLeft = parseInt(jQuery(par).css("padding-left"));  
             tmpWidth = tmpWidth - tmpPaddingRight - tmpPaddingLeft -2 ;
             jQuery(par).width(tmpWidth  + 'px');     */      
             bindCloseKlappBox(this);                 
        }
        
    ); 
    
    jQuery(klappboxOpens).each(
        function(intIndex) {    
                        
             bindOpenKlappBox(this);                 
        }
        
    ); 
    
    //alert(klappboxCloses.length);
    
    
    //jQuery(obj).find("ul.level2").css({display:"none"});
  
  
    /*jQuery(obj).hover(function(){
        //jQuery(this).find("ul").stop(true,true).slideDown(100);
        jQuery(this).find("A.firstlevel").addClass("active");
    
    },function(){
        //jQuery(this).find("ul").stop(true,true).slideUp(100);
       jQuery(this).find("A.firstlevel").removeClass("active");
    
    }); */

}

/*
#### Visuelle Effekt für Loading bei AJAX Request bei
bei den Produktseiten  ###

*/


function bindCloseKlappBox(obj){
  jQuery(obj).unbind();
  jQuery(obj).bind("click",function(event) {
      par = jQuery(this).parent(); 
      hpar = jQuery(this).parent().parent();
      innerContent = jQuery(hpar).children('.innercontent').first();
      jQuery(obj).hide();
      closeBtn = jQuery(par).children('.open-klappbox').show();
      jQuery(innerContent).slideUp('slow', function() {     
            
      
      });
  });
}

function bindOpenKlappBox(obj){
  jQuery(obj).unbind();
  jQuery(obj).bind("click",function(event) {
      par = jQuery(this).parent(); 
      hpar = jQuery(this).parent().parent();
      innerContent = jQuery(hpar).children('.innercontent').first();
      jQuery(obj).hide();
      closeBtn = jQuery(par).children('.close-klappbox').show();
      jQuery(innerContent).slideDown('slow', function() {           
           
      
      });
      
     

  });
}


function loadVisualEffects() {

  var animationDuration = 1500;

     
      /*#### nach laden mittels AJAX Request sanftes einblenden der Elemente des Produktdatensatzes ####*/
          jQuery('#ajaxReload + IMG.loading').ajaxComplete(function(event) {
                 jQuery(".product-description").slideDown(1);
                 //jQuery(".product-description > .innercontent").fadeIn(animationDuration*4);
         jQuery(".product-description > .innercontent").show();
                 jQuery("#ajaxcontent > IMG").fadeIn(animationDuration); 
                /* jQuery(".product-description").show();
                 jQuery(".product-description > .innercontent").show();
                 jQuery("#ajaxcontent > IMG").show();      */
        
          }).ajaxSend(function(event){
         //jQuery(".product-description").slideDown(1);
          jQuery("#ajaxcontent > IMG").fadeOut(1);
          jQuery("#ajaxcontent > .innercontent").hide();
      });
       
      /*#### Nach laden per AJAX wird producticon markiert ####*/  
      jQuery('#ajax .tx-xtpreference-pi1-item > a').bind('click',function(event) {
        if (jQuery('#ajax .tx-xtpreference-pi1-item > a').hasClass("activefocus")) {
          jQuery('#ajax .tx-xtpreference-pi1-item > a').removeClass("activefocus");
            
        }
        jQuery(this).addClass('activefocus');
      });

     

}





/*
### Laden des jeweils ersten Produkts aus einer der Produktkategorieseiten bei Body.onload Event ###
### ###
*/
function loadDefaultProduct(obj) {
 
  if(!obj.length) return ;
  var firstProduct = obj.first().find("a").attr("href");
  obj.first().find("a").addClass("activefocus");
  
  //jQuery("#ajax.tx-xtpreference-pi1-listView .tx-xtpreference-pi1-item A").fadeTo(1, 0.4);    
  
  
  /*### Seitentyp typeNum wird im TS gesetzt ###*/
  /*
  var globalSiteAJAXtypeNum globale Variable in TS gesetzt mittels Konstante 
  */
  ajaxContainer  =  jQuery("#ajaxReload");   
  ajaxContainer.load(firstProduct,{type:globalSiteAJAXtypeNum});

  /*### Laden von visuellen übergangseffekten nach erfolgen AJAX Request ###*/  
  loadVisualEffects();
  


}


function tooltip(obj) {
   if(!obj.length) return ;

   jQuery("body").append('<div id="tooltip" />');
   var tooltip = jQuery("#tooltip");
   var title;
 
  obj.hover(function() {
       title = jQuery(this).attr("title") ?
               jQuery(this).attr("title"):
               "Kein Titel";
   
       jQuery(this).attr("title","");
       tooltip.html(title);
       tooltip.stop(true,true).delay(50).fadeIn("slow").dequeue();
      // tooltip.stop(true,true).delay(50).fadeIn("slow"):
   
      }, function() {
          jQuery(this).attr("title",title);   
          tooltip.stop(true,true).fadeOut("slow");  
         
      }).mousemove(function(e) {
         tooltip.animate({
           top:e.pageY + 10,
           left:e.pageX +10
           
         },1);  
    
  });
 
}





function gradientAnimate(obj) {
   if (!obj.length) return;
 
 /*hover effekt*/
 obj.hover(function() {
       // jQuery(this).animate({"opacity": "0.3","background-color":"#ff0000"}, 100 );
    obj.removeClass("animatefirst");
    jQuery(this).toggleClass("animatefirst","fast");
  
  },function(){
    //jQuery(this).animate({"opacity": "0.8","background-color":"#ffffff"}, 100 );
    jQuery(this).toggleClass("animatelast","fast");
  }); 
 
 
}

function setFormButtonCSS(obj) {
if (!obj.length) return ;
  jQuery(obj).each(function(){
            
       var t=jQuery(this);
      //prepend('<span class="leftEdge" />')
            t.wrap('<span class="middle"></span>').parents('.tx_powermail_pi1_fieldwrap_html').append('<span class="rightEdge" />');
       
         //####
               t.focus(function(){
                                 t.addClass('focus');
               }).blur(function(){
                                t.removeClass('blur');
               });
         
         
         //#### Hover Effekt definieren 
         t.hover(function(){
              t.parent().addClass('hover');
         },function(){
            t.parent().removeClass('hover');
         });   
       
      });
      
}


function showHideFAQs(obj) {
if (!obj.length) return ;
   
 jQuery(".tx-irfaq-pi1.jq A.showAll").bind('click',function(event) {
    event.preventDefault();
    jQuery(".tx-irfaq-pi1.jq UL > LI.firstlevel").addClass("open"); 
    jQuery(".tx-irfaq-pi1.jq UL > LI.open UL.answer").show();
  
 });
 
 jQuery(".tx-irfaq-pi1.jq A.closeAll").bind('click',function(event) {
     event.preventDefault();
     jQuery(".tx-irfaq-pi1.jq UL LI.firstlevel").removeClass("open"); 
     jQuery(".tx-irfaq-pi1.jq UL > LI.firstlevel UL.answer").hide();  
 });
  

}


/*
### Aufklappeffekt für die einzelnen FAQ Items 
*/
function openSingleFAQ(obj) {
if(!obj.length) return ;



    /*### Klapp Effekt für FAQ Modul nun auf JQuery umgestellt oben###*/
    obj.bind("click",function(event) {
        jQuery(this).parent("li").toggleClass("open");
    
    });
  
 
    obj.hover(function() {
      
      jQuery(this).parent("li").find("div.question").addClass("hover");    
    //  jQuery(this).parent("li").find("div.question").stop(true,true).animate({backgroundColor: "#0092d6",color: "#ffffff",fontWeight: "bold"},300);
     // jQuery(this).parent("li").find("div.question IMG").stop(true,true).animate({backgroundPosition:"0px -20px" },1300);
      
      
      }, function() {

    jQuery(this).parent("li").find("div.question").removeClass("hover");    
      //jQuery(this).parent("li").find("div.question").stop(true,true).animate({backgroundColor: "#d6edf8",color:"#0092D6",fontWeight: "bold"},300);
      //jQuery(this).parent("li").find("div.question IMG").stop(true,true).animate({backgroundPosition:"0px 0px" },1300);
      }); 
    
}


function showAllFAQs(obj) {

    if(!obj.length) return ;
    /*var i = 0;
    obj.unbind();
    obj.bind("click",function(event) {   
       
       if(jQuery(obj).hasClass('show')) {        
          jQuery(obj).parent().children("ul li").addClass("open");      
       }
       else {
          jQuery(obj).parent().children("ul li").removeClass("open");
       }
      
        
       event.preventDefault();
       i = i+1;
       obj.parents().find("UL LI.firstlevel").toggleClass("open");
       obj.toggleClass("show");    
       return false;
    });
      */

    jQuery(obj).each(
        function(intIndex) {               
             bindClicktoA(this);                 
        }
        
    );  

    return false;  
}

function bindClicktoA(obj) {
  jQuery(obj).unbind();
  jQuery(obj).bind("click",function(event) {  
        if(!jQuery(obj).hasClass('show')) {
              par = jQuery(this).parent();
              firstUl = jQuery(par).children('ul').first();
              jQuery(firstUl).children('li').addClass('open');
              jQuery(obj).addClass("show");
              return false;
        }
        else {
             par = jQuery(this).parent();
             firstUl = jQuery(par).children('ul').first();
             jQuery(firstUl).children('li').removeClass('open');
             jQuery(obj).removeClass("show");
             return false;
        }
  });

}

function nativeLightbox(obj) {

  jQuery(obj).fancybox({
    'titlePosition' : 'inside',
    'overlayColor' : '#AAA',
    'overlayOpacity' : '0.5',
    'hideOnContentClick' : 'true',
    'speedIn' : '100',
    'speedOut' : '100',
    'transitionIn' : 'fade',
    'transitionOut' : 'elastic'
  });  



}



/*
### Laden der Standardfunktionen für das Projekt ###
*/
function loadDefaultJS() {  
 
 
  jQuery(document).ready(function(){
     
  
//  nativeLightbox(jQuery(".tx-xtpreference-pi1-customer a"));
  
   
   
    setFormButtonCSS(jQuery(".tx-powermail-pi1 input[type=submit]"));   
  
    /*#### ACHTUNG: Sehr perfomante jQuery animation ####*/
    //tooltip(jQuery(".tx-xtpreference-pi1-customer A, .tx-xtpreference-pi1-item A:first-child, .imagecontainer IMG, A.mail"));
    // showHideFAQs(jQuery(".tx-irfaq-pi1.jq A.showAll, .tx-irfaq-pi1.jq A.closeAll"));
    showAllFAQs(jQuery(".tx-irfaq-pi1.jq A.openCloseButton"));
    openSingleFAQ(jQuery(".tx-irfaq-pi1.jq .question"));
    FlyOutMenu(jQuery("UL.menu LI"));
     
  //### Laden des ersten Produkt bei body.onload Event ####
   loadDefaultProduct(jQuery("#ajax.tx-xtpreference-pi1-listView .tx-xtpreference-pi1-item") );
      
    //### Bild mit Gifanimation
    jQuery("#ajaxReload + IMG.loading").hide();
    
    
    var ajaxContainer  =  jQuery("#ajaxReload"); 
    jQuery("#ajax .tx-xtpreference-pi1-item > a").click(function(event) {
         activeAjaxAnchor = jQuery("#ajax .tx-xtpreference-pi1-item > a.activefocus").attr("href");          
         ajaxContainer.load(jQuery(this).attr("href"),{type:globalSiteAJAXtypeNum});   
         return false;
    
    });
    //jQuery("#ajax .tx-xtpreference-pi1-item > a").click(function(event) {return false;});
    //### Bild mit GIF Animation 
   /* jQuery("#ajax .tx-xtpreference-pi1-item > a").mouseenter(function(event) {
         //event.preventDefault();
         activeAjaxAnchor = jQuery("#ajax .tx-xtpreference-pi1-item > a.activefocus").attr("href");   
         
           
         ajaxContainer.load(jQuery(this).attr("href"),{type:globalSiteAJAXtypeNum}); 
         //loadVisualEffects();
    }).mouseleave(function(event) {
          ajaxContainer.load(activeAjaxAnchor,{type:globalSiteAJAXtypeNum});
    
    }) ;  */
      
  });   

}


