/**
* Bernd Remmers Website
*
* The www.remmers.ch website is heavily using JavaScript
* for visual enjoyment, but the whole site degrades beautifully
* using the unobtrusive JavaScript philosophy.
*
* @author Roman Simecek
* @date 2009-10-20
*
*/

OFFSET_Y = -30;
OFFSET_X = 10;
FADE_SPEED = 7000;
TIMEOUT = FADE_SPEED-2000;
FADE_HEIGHT = '417px';
MOVE_TIME = 80000;
MOVE_START = '0px 0px';
MOVE_END = '-400px -850px';
individualText = new Array("Bewerten", "Gestalten", "Begleiten");
individual = 0;
activeQuoteNumber = 1;

$(document).ready( function(){
  addTeamBehavior();
  addQuicklinksBehavior();
  addLightboxBehavior();
  addHomeMovieBehavior();
  addContentSliderBehavior();
  addContentMoreBehavior();
  addStandardsolutionsBehavior();
  addHomeAnimation();
  addIndividualLoesungAnimation();
  addStandartLoesungAnimation();
  addQuoteAnimation();
});

function addQuoteAnimation() {

  if ($(".csc-frame-frame1").length > 1) {
    $(".csc-frame-frame1").hide();
    $(".csc-frame-frame1:first").show();
  
    setInterval("switchQuote()", 8000 );
  }
  
}

function switchQuote() {
  var quoteElements = $(".csc-frame-frame1");

  $(quoteElements[activeQuoteNumber-1]).fadeOut(1000, function () {

    if (activeQuoteNumber == quoteElements.length) {
      activeQuoteNumber = 0;
    }

    $(quoteElements[activeQuoteNumber]).fadeIn(1000, function() { activeQuoteNumber++; });
  });
}

function addStandartLoesungAnimation() {
  if($('#slogan-animation-standart').length > 0){
    $('#slogan-animation-standart').hide();
    $('#content-ganze-breite-standart h1').wrapInner('<div style="display:none;">');
    if($('#slogan-animation-standart').hasClass('english')){
      loadFace('moving_face_standart_eng.swf');
    }else{
      loadFace('moving_face_standart.swf');
    }
  }
}

function standartLoesungAnimation(){
  individual++;
  if(individual >= 3){
    individual = 0;
  }
  $('#slogan-animation-standart-'+individual).show();
  $('#slogan-animation-standart-'+individual).fadeOut(1000);
}

function addIndividualLoesungAnimation() {
  if($('#slogan-animation-individual').length > 0){
    $('#slogan-animation-individual').hide();
    $('#content-ganze-breite h1').css({margin: '376px 0 0 19px'});
    $('#content-ganze-breite h1').wrapInner('<div style="display:none;">');
    if($('#slogan-animation-individual').hasClass('english')){
      loadFace('moving_face_individual_eng.swf');
    }else{
      loadFace('moving_face_individual.swf');
    }
  }
}

function loadFace(swf){
  if($('#slogan-flash').length > 0){
    var params = {}; 
    params.width = 800;
    params.play = true;
    params.height = 417;
    params.wmode = "opaque";
    swfobject.embedSWF("http://server68.hostpoint.ch/~remmers/remmers.ch/fileadmin/images/animationen/"+swf, "slogan-flash-inner", "800","417", "9.0.0", "fileadmin/templates/expressInstall.swf", null, params); 
  }
}

function individualLoesungAnimation() {
  individual++;
  if(individual >= individualText.length){
    individual = 0;
  }
  $('#slogan-animation-individual').remove('.inner');
  $('#slogan-animation-individual').text(individualText[individual]);
  $('#slogan-animation-individual').wrapInner('<div class="inner">');
  $('#slogan-animation-individual .inner').fadeOut(500);
}

function addIndividualLoesungAnimationStepTwo(){
  $('#slogan-animation-individual .inner').fadeOut(125, function(){
    $('#slogan-animation-individual').remove('.inner');
    $('#slogan-animation-individual').text(individualText[individual]);
    $('#slogan-animation-individual').wrapInner('<div class="inner">');
    individual++;
    setTimeout(addIndividualLoesungAnimationStepTwo(), 1000);
  });
}

function addGanzeBreiteAnimation(){
  if($('#content-ganze-breite').length>0){
    $('#content-ganze-breite').css({backgroundPosition: MOVE_START});
    $('#content-ganze-breite').animate({backgroundPosition: MOVE_END}, 
    MOVE_TIME,
    'linear',
    function(){
      goBackGanzeBreiteAnimation();
    });
  }
}

function goForwardGanzeBreiteAnimation() {
  $('#content-ganze-breite').animate({
    backgroundPosition: MOVE_START
  }, 
  MOVE_TIME,
  'linear', 
  function(){
    goBackGanzeBreiteAnimation();
  });
}

function goBackGanzeBreiteAnimation() {
  $('#content-ganze-breite').animate({
    backgroundPosition: MOVE_END
  }, 
  MOVE_TIME,
  'linear', 
  function(){
    goForwardGanzeBreiteAnimation()
  });
}

function addHomeAnimation() {
  if($('#homeslider').length>0){
    $('ul#homeslider0').innerfade({
      startdelay: -10,
      speed: FADE_SPEED,
      timeout: TIMEOUT*3,
      type: 'startdelay',
      containerheight: FADE_HEIGHT
    });
    $('ul#homeslider1').innerfade({
      startdelay: TIMEOUT+TIMEOUT,
      speed: FADE_SPEED,
      timeout: TIMEOUT*3,
      type: 'startdelay',
      containerheight: FADE_HEIGHT
    });
    $('ul#homeslider2').innerfade({
      startdelay: TIMEOUT,
      speed: FADE_SPEED,
      timeout: TIMEOUT*3,
      type: 'startdelay',
      containerheight: FADE_HEIGHT
    });
  }
}

/* GOOGLE MAPS*/
function load() {
if (GBrowserIsCompatible()) {
         var map = new GMap2(document.getElementById("map"));
         map.addControl(new GLargeMapControl());
         var MapTypes = map.getMapTypes();
         MapTypes[0].getName= function() { return "Karte";}
         MapTypes[1].getName = function() { return "Satellit";}
         MapTypes[2].getName = function() { return "Hybrid";}
         map.addControl(new GMapTypeControl());
         map.setCenter(new GLatLng(47.18469600038913, 8.512001037597656), 13);
         var info='<div style="background-color: #fff; width: 200px; font-size: 13px; FONT-FAMILY: Verdana, Tahoma, Arial, Sans-Serif;"><B>Bernd Remmers Consultants AG</B><hr>Landis & Gyr-Strasse 1<BR>CH-6301 Zug<BR>Tel +41 (0) 41 725 34 00<BR>Fax +41 (0) 41 725 34 01</div>';
         var point = new GLatLng(47.1761274, 8.5120738);
         var marker = new GMarker(point);
         GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(info);
         });
         map.addOverlay(marker);
         marker.openInfoWindowHtml(info);

}
}

function addStandardsolutionsBehavior() {
  if($('a[title=Leseprobe]').length>0){
    $('a[title=Leseprobe]').each(function(){
      $(this).lightbox();
    });
  }
}

function addContentMoreBehavior(){
  if($('div.content-mehr').length>0){
    var i = 0;
    $('#content div.slide').each(function(){
      if(i>0){
        $(this).hide();
        $(this).addClass('to-hide');
        if($('#content div.slide').length == i+1 ){
          $(this).prepend('<p class="bodytext"><a id="content-mehr-hide" href="#"><< close</a></p>');
        }
      }else{
        $(this).append('<p class="bodytext"><a id="content-mehr-show" href="#">>> more</a></p>');
      }
      i++;
    });
    $('#content-mehr-show').click(function(e){
      e.preventDefault();
      $('#content-mehr-show').fadeOut(125, function(){
        $('#content div.slide').each(function(){
          $(this).fadeIn(125);
        });
      });
    });
    $('#content-mehr-hide').click(function(e){
      e.preventDefault();
      $('#content div.to-hide').each(function(){
        $(this).fadeOut(125, function(){
          $('#content-mehr-show').fadeIn(125);
        });
      });
    });
  }
}

function addContentSliderBehavior(){
  if($('div.content-slider').length>0){
    $('div.content-slider').attr('style', 'overflow: hidden;');
    $('#inner-content-slider').attr('style', 'width: 5000px;');
    $('#inner-content-slider .slide').each(function(){
      $(this).attr('style', 'float:left;');
    });
    $('#inner-content-slider a').each(function(){
      if($(this).attr('href')==null || $(this).attr('href')==''){
        $(this).remove();
      }
    });
    $('#sitemap ul li:first-child a').addClass('jFlowSelected');
    $('#sitemap ul li a').each(function(){
      $(this).addClass('jFlowControl');
    });
    $("#sitemap").jFlow({
      slides: "#inner-content-slider",
      controller: ".jFlowControl",
      slideWrapper : "#jFlowSlide",
      selectedWrapper: "jFlowSelected",
      auto: true,
      width: "550px",
      height: "500px",
      //duration: 400
      duration: 700
    });
    
  }
}

function addQuicklinksBehavior(){
  if($('#quicklinks').length>0){
    $('#quicklinks').prepend('<div id="show-quicklinks"><a id="show-quicklinks-link" href="#">Quick Links</a></div>');
    $('#quicklinks').wrapInner('<div id="show-quicklinks-slider">');
    $('#quicklinks ul li a').each(function(){
      $(this).hide();
    });
    $('#quicklinks').hover(function(){
      $('#show-quicklinks-link').fadeOut(125, function(e){
        //$('#show-quicklinks-link').addClass('hidden-link');
        $('#show-quicklinks-slider').animate({marginLeft:"-150px"}, 125, function(){
          $('#quicklinks ul li a').each(function(){
            $(this).fadeIn(125);
          });
        });
      });
    },function(e){
      $('#quicklinks ul li a').each(function(){
        $(this).fadeOut(125, function(){
          $('#show-quicklinks-slider').animate({marginLeft:"0px"}, 125, function(){
            $('#show-quicklinks-link').fadeIn(125);
            //$('#show-quicklinks-link').removeClass('hidden-link');
            $('#show-quicklinks-link').removeAttr('style');
            $('#show-quicklinks-slider').removeAttr('style');
          });
        });
      });
      e.stopImmediatePropagation()
    });
  }
}

function addHomeMovieBehavior(){
  if($('#playmovie').length>0){
    $('body').prepend('<div id="close-flash-container"><a href="#">Close [X]</a></div>');
    $('body').prepend('<div id="flash-container"><div id="slogan-flash-inner"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div></div>')
    $('body').prepend('<div id="flash-overlay"/>');
    $('#playmovie a').click(function(e){
      e.preventDefault();
      $('#flash-overlay').animate({ height: "100%" }, 125, function(){
//      $('#flash-overlay').animate({ display: "block", height: "100%" }, 125, function(){
        $('#flash-container').fadeIn(125, function(){
          $('#flash-container').animate({width:"420px", marginLeft:"-210px"}, 500, function() {
            var params = {}; 
            params.width = 420;
            params.height = 308;
            var flashvars = {
              file: "movie.flv",
              image: "movie.jpg",
              play: true,
              wmode: "opaque"
            };
            swfobject.embedSWF("fileadmin/movie/movie.swf", "slogan-flash-inner", "420","308", "9.0.0", "fileadmin/templates/expressInstall.swf", flashvars, params);
            $('#flash-container object').fadeIn(125, function(){
  //            $('#flash-container').animate({borderLeft:"0px solid white", borderRight:"0px solid white"}, 125, function(){
                $('#close-flash-container').fadeIn(125);
    //          });
            });
          });
        });
      });
    });
    $('#close-flash-container').click(function(){
      $('#close-flash-container').fadeOut(125, function(){
   //     $('#flash-container').animate({borderLeft:"1px solid white", borderRight:"1px solid white"}, 125, function(){
          $('#flash-container object').fadeOut(125, function(){
            $('#flash-container').animate({width:"0px", marginLeft:"0px"}, 500, function(){
              $('#flash-container').fadeOut(125, function(){
                $('#flash-overlay').animate({ height: "0" }, 125);
//                $('#flash-overlay').animate({ display: "none", height: "0" }, 125);
              });
            });
     //     });
        });
      });
    });
  }
}

function addLightboxBehavior(){
  if($('* a.lightbox').length>0){
    $('* a.lightbox').each(function(){
      $(this).lightbox({fitToScreen: true});
    })
  }
  if($('#homeslider').length==0){
    if($('* a[rel^=lightbox]').length>0){
      $('* a[rel^=lightbox]').each(function(){
        $(this).lightbox({fitToScreen: true});
      });
    }
  }
}

function addTeamBehavior() {
  if($('div#bild1').length>0){
    $('a.internal-link img').each(function(){
      var temp = $(this).attr('alt').split(',');
      var name = temp[0].split(' ');
      if(temp[1] == null){
        temp[1] = '';
      }
      $('body').append('<div id="team-mouseover-'+name[0]+'" style="z-index:10000;background-color:#FFFFFF;font-size:11px;display:none;position:absolute;padding:5px;width:150px;"><b>'+temp[0]+'</b><br />' + temp[1]+'</div>');
      $(this).queue(name[0]);
      $(this).hover(function(){
        if($('body').attr('action')=='false'){
          $(this).addClass('active-images');
          hideImages();
          $('#team-mouseover-'+name[0]).fadeIn(0);
          $().mousemove(function(e){
            $('#team-mouseover-'+name[0]).css('top', e.pageY+OFFSET_Y);
            $('#team-mouseover-'+name[0]).css('left', e.pageX+OFFSET_X);
          });
        }
      },function(){
        $('body').attr('action', 'false');
        showImages();
        $('#team-mouseover-'+name[0]).fadeOut(0);
      });
    });
  }
}

function showImages(){
  $('.contenttable a.internal-link img').each(function(){
    $(this).animate({opacity: "1.0"}, 500);
    $(this).dequeue();
    if($(this).hasClass('active-images')){
      $(this).removeClass('active-images')
    }
  });
}

function hideImages(){
  $('.contenttable a.internal-link img').each(function(){
    if(!$(this).hasClass('active-images')){
      $(this).animate({opacity: "0.3"}, 500);
      $(this).dequeue();
    }
  });
}
