/*Header Fade*/
var HeadFade = {
    init : function(){
        this.opt = {
            contID : $('#start-headers'),
            contHeight: 290,
            imgCont : $('.start-img'),
            headCont : $('.start-heads'),
            showTime : 4000,
            fadeTime : 1000
        };
        this.counter = 0;
        
        if(this.opt.contID.length){
            this.opt.contID.css({'height':this.opt.contHeight, 'overflow':'hidden'});
            this.opt.headCont.css({'position':'absolute'});
            this.opt.imgCont.css({'position':'absolute'});
            this.prepare();
            this.fadeMe();
        }
    },
    prepare : function(){
        this.opt.contID.children().hide().first().show();
        this.hideHeader();
        this.slideHeader(this.opt.contID.children().first());
    },
    fadeMe : function(){
        this.opt.contID.children(this.opt.imgCont).each(function(i){
            var isLast = (i ==HeadFade.opt.imgCont.length-1);
            var addadd = HeadFade.opt.fadeTime+HeadFade.opt.fadeTime;
            if(i>0){
                $(this)
                    .delay(HeadFade.opt.showTime*(i-1)+HeadFade.opt.fadeTime+addadd)
                    .fadeIn(HeadFade.opt.fadeTime, function(){
                        HeadFade.slideHeader($(this));
                        
                    })
                    .delay(HeadFade.opt.showTime-HeadFade.opt.fadeTime)
                    .fadeOut(HeadFade.opt.fadeTime, function(){
                        if(isLast){
                            HeadFade.fadeMe();
                        }
                    })
                ;
            }
            if(i==0){
                $(this).delay(HeadFade.opt.showTime).fadeOut(HeadFade.opt.fadeTime);
                $(this).delay(HeadFade.opt.showTime*(HeadFade.opt.imgCont.length-2)+HeadFade.opt.fadeTime*1.5).fadeIn(HeadFade.opt.fadeTime, function(){
                    HeadFade.slideHeader($(this));
                });
            }
        });
    },
    
    hideHeader : function(){
        this.opt.headCont.children().css({'position':'relative','left':this.opt.imgCont.css('width'), 'width':400});
    },
    
    slideHeader : function(actImg){
        actImg.children().children().first().animate({'left':400}, HeadFade.opt.fadeTime);
        actImg.children().children().first().next().delay(HeadFade.opt.fadeTime).animate({'left':497}, HeadFade.opt.fadeTime, this.hideHeader());
    }
}

  /** Lightbox **/
  initLightbox = (function(){
    var lbox = $('.lightbox');
    if(lbox.length){
  /*    var langval = 'große Ansicht';
      if(curLang=='en') langval = 'large view';
      if(curLang=='fr') langval = 'large view';
      if(curLang=='es') langval = 'large view';
      if(curLang=='it') langval = 'large view';
      lbox.append('<span class="lbox_append"><span>'+langval+'</span></span>')
  */
      lbox.attr('rel','');
      lbox.append('<span class="lbox_append"><span>&nbsp;</span></span>')
      lbox.fancybox();
      //<img class="no_border" src="fileadmin/templates/img/search_submit_2.gif" width="15" height="14" alt="" />
    }
  
  });

 /* Gallery*/
  initGallery = (function(){
    var gal = $('.gallery');
    var galpic = gal.children('a');
    if(galpic.length){
      gal.each(function(indx){
        var par = $(this).parent().parent().attr('class');
        var num = 100;
        if(par != undefined){
          if(par.match('col_330')){
            num = 4;
          } else if (par.match('col_165') || par.match('box-content')){
            num = 2;
          } else if (par.match('highlightbox') || par.match('highlight')){
            num = 6;
          }
        }
        
        //$(this).append('<a href="#" class="gal-open"><span>mehr</span></a>');
        
        var galChildren = $(this).children('a');
        galChildren.each(function(i){
          if((i+1)%num==0){
            $(this).css('margin-right', 0);
          }
          var myTitle = $(this).next('span').text();
          if(/*i+1 != galChildren.length*/ true){
              $(this).attr('rel', 'gal_'+indx);
          $(this).fancybox({
            'title' : myTitle
          });
          } else {
          $(this).click(function(){
            $(this).parent().children('a').first().trigger('click');
          });
          }
        });
      });
    }
  });

  initStuff = (function(){
    var curLang = $('html').attr('lang');
    
    /* Products: */
    if($('.product_teaser').length > 0){
      $('#productsorter').change(function(){
        $(this).closest("form").submit();
      });
    }
    
    /* Youtube FCE */
    $("a.ytview").click(function() {
      $.fancybox({
        'padding'    : 0,
        'autoScale'    : false,
        'transitionIn'  : 'none',
        'transitionOut'  : 'none',
        'title'      : this.title,
        'width'      : 640,
        'height'    : 385,
        'href'      : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
        'type'      : 'swf',
        'swf'      : {
        'wmode'        : 'transparent',
        'allowfullscreen'  : 'true'
        }
      });
      return false;
    });

    /* BookmarkButton */
    if ($('.bookmarkButton').length > 0){
      var button = $('.bookmarkButton');
      button.click(function() {
        if(button.children('.added').length > 0){
          $.getJSON('index.php?id=162&product='+this.id+'&action=remove&jsoncallback=?', function(data){
            if(data['result']=='removed'){
              button.children('span').removeClass('added');
            }
          });
        }else{
          $.getJSON('index.php?id=162&product='+this.id+'&action=add&jsoncallback=?', function(data){
            if(data['result']=='added'){
              button.children('span').addClass('added');
            }
          });
        }
      });
    }
    
    if($('.bookmarkedProduct span.del').length > 0){
      $('.bookmarkedProduct span.del').click(function(){
        var thisthis = this;
        var suretext = "Produkt sicher aus Merkliste löschen?";
        if(curLang=='en') suretext = "Are you sure to delete the product from the bookmark list?";
        if(curLang=='fr') suretext = "Are you sure to delete the product from the bookmark list?";
        if(curLang=='es') suretext = "Are you sure to delete the product from the bookmark list?";
        if(curLang=='it') suretext = "Are you sure to delete the product from the bookmark list?";
        if(confirm(suretext)){
          $.getJSON('index.php?id=162&product='+this.id+'&action=remove&jsoncallback=?', function(data){
            if(data['result']=='removed'){
              $(thisthis).parent('.bookmarkedProduct').fadeOut();
            }
          });
        }
      });
    }
  });
  
  initProductTeaser = (function(){
    /* Product Teaser: */
    if($('.ptgroupwrap').length > 0){
      var ptgroupwrapCount = 0;
      $(".ptgroupwrap").each(function(index){
        // spaleten:
        if($(this).parents('.col_165').length > 0){
          var inarow = 1
        }        
        else if($(this).parents('.col_330').length > 0){
          var inarow = 2
        }    
        else {
          var inarow = 3
        }
        //
        ptgroupwrapCount+= 1;
        // console.log('/////////////   '+inarow);
        var productstCount = 1;
        var grouptCount = 0;
        var curtClassName = "ptgroup_"+ptgroupwrapCount+"_0";
        $(this).children(".product_teaser").each(function(index){
          // console.log('///   '+curtClassName);
          $(this).addClass(curtClassName);          
          $(this).removeClass('last');
          if(productstCount%inarow==0){
            // console.log('///   '+productstCount+'    '+curtClassName);
            $("."+curtClassName).equalizeCols();
            $(this).addClass('last');
            grouptCount+= 1;
            curtClassName = "ptgroup_"+ptgroupwrapCount+"_"+grouptCount;
          }
          productstCount+= 1;
        });
      });      
    }
  });
  
  $(window).load(function(){
    initProductTeaser();
  });



  $(document).ready(function(){
    //starting lightbox
    initLightbox();
    //starting gallery
    initGallery();
    //starting headfade
    HeadFade.init();
    //starting stuff
    initStuff();
    //starting product teaser..
    // initProductTeaser();
    
    /* Product Slider Ajax*/
    $('.triple li a').each(function(){
        var myAnch = $(this);
        var myHref = $(this).attr('href');
        $.ajax({
            url:myHref,
            dataType:'html',
            success:function(data){
                var response = $(data);
                var firstImage = response.find('#column_center img').first();
                //myAnch.prepend(firstImage.css('height'));
                myAnch.prepend(firstImage.css({width: 120, height:'auto'}));
                
                //if(parseInt(firstImage.css('height'))>100 ){
                //  myAnch.prepend(firstImage.css({height: 110, width:'auto'}));
                //}
                
            }
        });
        myAnch.click(function(ev){
            ev.preventDefault();
            var anchHref = $(this).attr('href');
            
            
            $(this).attr('id', 'svw-actel').parent().parent().parent().find("a").not($(this)).attr('id', ''); // wow!
            //alert(anchHref);
            $.ajax({
                url:anchHref,
                beforeSend:function(){
                    var actH = $('#column_center').css('height');
                    $('#column_center').css('height', actH);
                    
                    $('#column_right div.csc-default').remove();
                    $('#column_center div.csc-default').filter(function(indx){
                        return indx>0;
                    }).remove();
                    
                    var imgLoad = '<img class="load" src="fileadmin/templates/img/ajax-loader.gif" width="32" height="32" style="position:relative;left:50%;margin-left:-16px;" />';
                    $('#column_center').append(imgLoad);
                    $('#column_right').append(imgLoad);
                },
                success:function(myHtml){
                    var myResp = $(myHtml);
                    var cCenter = myResp.find('#column_center > div.csc-default');
                    var cRight = myResp.find('#column_right > div.csc-default');
                    cCenter.find('.sliderbg').remove();
                    cCenter.find('.svw-headline').remove();
                    
                    $('#column_right').html(cRight);
                    $('#column_center .sliderbg').parent().after(cCenter);
                    
                    $('#column_center').css('height', 'auto');
                    //init Stuff after ajax
                    initLightbox();
                    initGallery();
                    initStuff();
                    initProductTeaser();
                    
                    $('.load').detach();
                }
            });
        });
    });
    
    /* Surgery Links*/    
    if($('#surgery-links-small').length){
      var surgery = $('#surgery-links-small');
      var small = true;
    }
    else var surgery = $('#surgery-links');
    if(surgery.length){
        if(small) surgery.find('span').css({'opacity':0,'width':400});
        surgery.find('span.top').css({'opacity':0});
        if(small){
          surgery.find('span.bottom').css({'opacity':0});
          surgery.children().css({'width':22, 'overflow':'hidden'});
        }
        surgery.find('.button').css('opacity', 0.5)
        
        var surgAni = function(obj){
            obj.delay(1000).animate({'opacity':1}, 750).delay(1000).animate({'opacity':0.5}, 750, function(){
                surgAni($(this));
            });
        }
        surgery.children().find('.button').animate({'opacity':1}, 750).animate({'opacity':0.5}, 750, function(){
                surgAni($(this));
        });
        
        
        //surgery.mouseenter(function(){
        //    surgery.children().find('.button').animate('opacity', 0.5);
        //});
        
        surgery.children().bind({
            mouseenter:function(){
                if(small) $(this).css({'width':400, 'overflow':'visible'});
                $(this).css({'overflow':'visible'});
                $(this).find('span.top').stop().animate({'opacity':1});
                if(small) $(this).find('span.bottom').stop().animate({'opacity':1});
                
                if($(this).find('.button').length !=1){
                    $(this).find('.button').first().detach();
                }
                var ovrlBtn = $(this).find('.button').clone();
                $(this).find('.button').css('z-index',-1);
                $(this).prepend(ovrlBtn);
                ovrlBtn.animate({'opacity':1});
            },
            mouseleave:function(){
                var act = $(this);
                $(this).find('span.top').stop().animate({'opacity':0});
                if(small){
                  $(this).find('span.bottom').stop().animate({'opacity':0}, function(){
                    act.css({'width':22, 'overflow':'hidden'});
                  });
                }
                
                if($(this).find('.button').length == 2){
                    var btOvrl = $(this).find('.button').first();
                    btOvrl.next().css('z-index', 0);
                    btOvrl.detach();
                }
                
            }
        });
    }

  /* current lang: */
  var curLang = $('html').attr('lang');
  
  /* Language Selection*/
  var langButton = $('.current_language_button');
  var langs = $('.language_select');
  var fdSpeed = 'fast';
  
  langButton.mouseenter(function(event){
    langs.fadeIn(fdSpeed);
    langButton.addClass('active');
    $('#meta').mouseleave(function(){
      langs.stop().fadeOut(fdSpeed).css('opacity',1);
      langButton.removeClass('active');
    });
  });
  
  /** Standortkarte **/
  $('#europa_klein').click(function(){
    //$('#europa_klein').fadeOut('fast');
    $('#weltkarte').hide();
    $("#europa").fadeIn(3000, function(){
      console.log('geladen');
    });
  });
  
  $('#weltkarte_klein').click(function(){
    //$('#weltkarte_klein').fadeOut('fast');
    $('#europa').hide();
    $("#weltkarte").fadeIn(3000, function(){
      console.log('geladen');
    });
  });
  addLocation('#weltkarte', 'Jacksonville', 165, 218, 'detail infos Jacksonville','gray','blue');
  addLocation('#weltkarte', 'Moskau', 71, 524, 'detail infos Moskau','white','blue');
  addLocation('#weltkarte', '<div style="position: relative; top: 8px; right: 45px;">Dubai</div>', 175, 563, 'detail infos Dubai','black','blue');
  addLocation('#weltkarte', 'Shanghai', 161, 720, 'detail infos Shanghai','gray','blue');
  addLocation('#weltkarte', 'Osaka', 146, 761, 'detail infos Osaka','gray','blue');
  
  //addLocation('#europa', 'London', 52, 194, 'detail infos London','black','blue');
  addLocation('#europa', 'Huizen', 34, 307, 'detail infos Huizen','white','blue');
  addLocation('#europa', 'Mühlheim', 148, 353, 'detail infos Muellheim','white','red');
  addLocation('#europa', '<div style="position: relative; right: 54px;color:#fff;">Colmar</div>', 160, 315, 'detail infos Colmar','white','blue');
  addLocation('#europa', '<div style="position:relative;right: 54px;color:white;">Umkirch</div>', 150, 333, 'detail infos Umkirch','white','red');
  addLocation('#europa', 'Tuttlingen', 157, 345, 'detail infos Tuttlingen','white','redblue');
  addLocation('#europa', 'Vimercate', 226, 350, 'detail infos Vimercate','white','blue');
  
    /* Sliding Headlines */
    var imgHeight = $('#colwrap img').attr('height');
    if($('#surgery-links-small').length){
        imgHeight = $('#surgery-links-small').css('height');
    }
    $('.head-dots').css('height', imgHeight);
    if($('#colwrap').length && $('#colwrap img').length && ($('#colwrap h1').length || $('#colwrap h2').length  )){
    var headslide = $('#colwrap .headslide');
    var sldly = 1000;
    
    //var colBG = $('#colwrap').css('background-color');
    //alert(colBG);
    
    var addMe = (imgHeight <= 150) ? 17 : 0;
    
    headslide.css({'top': imgHeight/2+40+addMe, 'overflow':'hidden'});
    headslide.removeClass('hidden');
  
  
    headslide.children().delay(sldly).each(function(i){
      var myWidth = parseInt($(this).css('width'));
      
      $(this).css({'left':myWidth});
      $(this).delay(sldly*i).animate({
        'left': (i*97)+395
      }, sldly);
    });
  
  
  //$('.headslide').delay(5000).animate({'left':1000}, 1000);
    }
  
  if($('.slider_logo').length > 0){
    var lh = $('.left img').first().attr('height');
    $('.left .slider_logo').css('bottom', 175-lh);
    
    var rh = $('.right img').first().attr('height');
    $('.right .slider_logo').css('bottom', 196-rh);
  }
  
  
});

$(window).bind("load", function() {
  $("div#svwslider").slideView();
  //alert('HÖHÖ ^^ trali trala... loading -- please wait .. .');
});

function addLocation(karte, name, toppx, leftpx, msg, font, type ){
  var randid = GetRandom(99, 9999)+GetRandom(99, 9999);
  font = 'f'+font;
  type = 'd'+type;
  $(karte).append('<div id="'+randid+'" class="standort '+font+' '+type+'">'+name+'</div>');
  //$('#'+randid).css('border', '1px solid red');
  //$('#'+randid).offset({ top: toppx, left: leftpx});
  $('#'+randid).css('left', leftpx+'px');
  $('#'+randid).css('top', toppx+'px');
  $('#'+randid).mouseenter(function() { $.cursorMessage(msg) });
  $('#'+randid).mouseleave($.hideCursorMessage);
  //$(karte).mouseenter($.hideCursorMessage);
}

function GetRandom( min, max ) {
  if( min > max ) {
    return( -1 );
  }
  if( min == max ) {
    return( min );
  }
 
        return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}




