/**
* Workfashion Website
*
* @author Philipp Schilter
* @author Felipe Kaufmann
* @author Pascal Näf
*
**/
var homeImageBefore;

$(document).ready(function(){
  
  /*****************************************/
  /*********** Email protection ************/
  /*****************************************/
  
  $('h4.mailme').mailme();
  
  /*****************************************/
  /****** References on the home site ******/
  /*****************************************/

  $('.slide-up').live('click', function(event) {
    return;
    event.preventDefault();
    var activeImage = $(this).parent();
    var scrollingImages = $(this).parent().parent().parent();
    var scrollableArea = $(this).parent().parent();
    //activeImage.addClass('active-image'); // highlighting the active image
    //scrollingImages.addClass('selected'); // add class for interrupting the sliding
    /*if ($('#references-home').is(':hidden')) {
      $('#references-home').show('slide', { direction: 'down' }, 1000); // do the slide-up effect
    } else {
      $('#references-home').hide('slide', { direction: 'down' }, 1000);
      activeImage.removeClass('active-image');
      scrollingImages.removeClass('selected');
      if (scrollableArea.children().hasClass('active-image')) {
        scrollableArea.children().removeClass('active-image');
      }
    }*/
  });
  
  // other opportunity for continuing the carousel
  $('.close-reference-image').live('click', function(event) {
    event.preventDefault();
    $(this).parent().parent().parent('#references-home').hide('slide', { direction: 'down' }, 1000);
    //$('.scrollableArea').children().removeClass('active-image');
    $('.scrollableArea').children().removeClass('active-image');
    $('.scrollWrapper').removeClass('selected');
  });
  
  /*********************************/
  /****** LOVELY AJAX REQUEST ******/
  /*********************************/
  
  $.manageAjax.create('searchQueue', {
      cacheResponse: false,
      abortOld: true
  });
  
  $('.image').live('click', function(event) {
    var activeImage = $(this);
    var scrollingImages = $(this).parent().parent();
    var scrollableArea = $(this).parent();
    homeImageBefore = $('#content-home p img').attr('src');
    if (scrollableArea.children().hasClass('active-image')) {
      scrollableArea.children().removeClass('active-image');
      $(this).addClass('active-image');
    }
    $(this).addClass('active-image');
    scrollingImages.addClass('selected');
    $.manageAjax.add('viewQueue', {
        url: $(this).children().attr('id'),
        data: '',
        method: 'get',
        beforeSend: function(html) {
          if (!$('#references-home').is(':hidden')) {
            $('#references-home').hide('slide', { direction: 'down' }, 1000); // do the slide-up effect
          }
        },
        success: function(html) {
            $('#ajax-container').replaceWith(html);
            $('#content-home p').css('background-image', 'url('+homeImageBefore+')');
            $('#content-home p img').animate({ opacity: 1.0 }, 2000 );
            $('#references-home').show('slide', { direction: 'down' }, 1000);
        }
    });
    event.preventDefault();
  });
  
  
  /***************************/
  /******* OLD VERSION *******/
  /***************************/
  /*
  $('.image').live('click', function(event) {
    var activeImage = $(this);
    var scrollingImages = $(this).parent().parent();
    var scrollableArea = $(this).parent();
    //var nextPage = parseInt($('#ajax-container').attr('class')) + 1;
    $.manageAjax.add('viewQueue', {
        url: $(this).children().attr('id'),
        data: '',
        method: 'get',
        beforeSend: function(html) {
          //$('#spinner').fadeIn('slow');
        },
        complete: function(html) {
          //$('#spinner').fadeOut('slow');
        },
        success: function(html) {
            $('#ajax-container').after(html);
            
            //$('#ajax-container').attr('id', 'ajax-container-old');
            
            
            
            var zIndexOfOld;
            $('#ajax-container').after(html).attr('id', 'ajax-container-old');
            zIndexOfOld = parseInt($('#ajax-container-old').css('z-index'));
            $('#ajax-container').attr('id', 'ajax-container-new');
            $('#ajax-container-old').css('z-index', zIndexOfOld + 1);
            $('#ajax-container-old').fadeOut('slow');
            $('#ajax-container-new').css('top', 0);
            
            
        }
        
        
    });
    event.preventDefault();
    if (scrollableArea.children().hasClass('active-image')) {
      scrollableArea.children().removeClass('active-image');
    } else {
      $(this).addClass('active-image');
    }
    
  });
  
	$('#content-home').ajaxStart(function() {
    //$('#spinner').fadeIn('slow');
    console.log('ich habe begonnen');
  });
  
  $('#content-home').ajaxSuccess(function() {
    //$('#spinner').fadeOut('slow');
    console.log('ich habe fertig');
  });*/
  
    
  /*****************************/
  /****** Teaser function ******/
  /*****************************/
  
  $('.teasertitle').click(function() {
    if ($(this).next().is(':hidden')) {
      $(this).next().show('slow');
      $(this).parent().addClass('teaser-element-active');
    } else {
      $(this).next().hide('slow'); // click on the teaser title -> hides the text element
      $(this).parent().removeClass('teaser-element-active');
    }
  });
  
  // arrow for hiding the text element
  $('.close-teaser').click(function(event) { 
    $(this).parent().hide('slow');
    event.preventDefault();
    event.stopPropagation();
    $(this).parent().parent().removeClass('teaser-element-active');
  });
  
  
  /********************************/
  /****** Language selection ******/
  /********************************/
  
  /* 
  * now at this moment we dont need this event because there are just two languages
  *
  $('.language').click(function(event) {
    if ($('#language-dropdown').is(':hidden')) {
       $('#language-dropdown').show('slide', { direction: 'up' }, 500);
       $(this).children().attr({src : "/images/workfashion/language_up.png"});
     }
     else {
       $('#language-dropdown').hide('slide', { direction: 'up' }, 500);
       $(this).children().attr({src : "/images/workfashion/language_down.png"});
     }     
  event.preventDefault();
  });
  
  $('ul.help-navi-list li:last a').click(function(event) {
    if ($('#language-dropdown').is(':hidden')) {
       $('#language-dropdown').show('slide', { direction: 'up' }, 500);
       $('.language').children().attr({src : "/images/workfashion/language_up.png"});
     }
     else {
       $('#language-dropdown').hide('slide', { direction: 'up' }, 500);
       $('.language').children().attr({src : "/images/workfashion/language_down.png"});
     }
     event.preventDefault();
  });
  */
  
  /**********************************/
  /************ Teamsite ************/
  /**********************************/
  $('#bild0').bind('mouseover mouseout', function() {
    $('#corporate0').toggleClass('active-information');
  });
  
  $('#corporate0').bind('mouseover mouseout', function() {
    $('#bild0').toggleClass('active-team-image');
    $('#corporate0').toggleClass('active-information');
  });
  
  $('#bild1').bind('mouseover mouseout', function() {
    $('#corporate1').toggleClass('active-information');
  });
  
  $('#corporate1').bind('mouseover mouseout', function() {
    $('#bild1').toggleClass('active-team-image');
    $('#corporate1').toggleClass('active-information');
  });
  
  $('#bild2').bind('mouseover mouseout', function() {
    $('#healthcare0').toggleClass('active-information');
  });
  
  $('#healthcare0').bind('mouseover mouseout', function() {
    $('#bild2').toggleClass('active-team-image');
    $('#healthcare0').toggleClass('active-information');
  });
  
  $('#bild3').bind('mouseover mouseout', function() {
    $('#safety0').toggleClass('active-information');
  });
  
  $('#safety0').bind('mouseover mouseout', function() {
    $('#bild3').toggleClass('active-team-image');
    $('#safety0').toggleClass('active-information');
  });
  
  $('#bild4').bind('mouseover mouseout', function() {
    $('#construction0').toggleClass('active-information');
  });
  
  $('#construction0').bind('mouseover mouseout', function() {
    $('#bild4').toggleClass('active-team-image');
    $('#construction0').toggleClass('active-information');
  });

  /* NEW TEAM JS */
	$('#bild0').bind('mouseover mouseout', function() {
	    $('#text0').toggleClass('active-information');
	});

	$('#bild1').bind('mouseover mouseout', function() {
	  $('#text1').toggleClass('active-information');
	});

	$('#bild2').bind('mouseover mouseout', function() {
	  $('#text2').toggleClass('active-information');
	});

	$('#bild3').bind('mouseover mouseout', function() {
	  $('#text3').toggleClass('active-information');
	});

	$('#bild4').bind('mouseover mouseout', function() {
	  $('#text4').toggleClass('active-information');
	});

	$('#bild5').bind('mouseover mouseout', function() {
	  $('#text5').toggleClass('active-information');
	});

	$('#bild6').bind('mouseover mouseout', function() {
	  $('#text6').toggleClass('active-information');
	});

	 $('#text0').bind('mouseover mouseout', function() {
	  $('#bild0').toggleClass('active-team-image');
	  $('#text0').toggleClass('active-information');
	});

	$('#text1').bind('mouseover mouseout', function() {
	  $('#bild1').toggleClass('active-team-image');
	  $('#text1').toggleClass('active-information');
	});

	$('#text2').bind('mouseover mouseout', function() {
	  $('#bild2').toggleClass('active-team-image');
	  $('#text2').toggleClass('active-information');
	});

	$('#text3').bind('mouseover mouseout', function() {
	  $('#bild3').toggleClass('active-team-image');
	  $('#text3').toggleClass('active-information');
	});

	$('#text4').bind('mouseover mouseout', function() {
	  $('#bild4').toggleClass('active-team-image');
	  $('#text4').toggleClass('active-information');
	});

	$('#text5').bind('mouseover mouseout', function() {
	  $('#bild5').toggleClass('active-team-image');
	  $('#text5').toggleClass('active-information');
	});

	$('#text6').bind('mouseover mouseout', function() {
	  $('#bild6').toggleClass('active-team-image');
	  $('#text6').toggleClass('active-information');
	});

  
  /* This is basic - uses default settings */

  $("a#single_image").fancybox();

  /* Using custom settings */

  $("a#inline").fancybox({
    'hideOnContentClick': true
  });

  $("a.group").fancybox({
    'speedIn' : 600, 
    'speedOut' : 200, 
    'overlayShow' : false
  });

  $(".iframe").fancybox({
    'width'	: 472,
    'height' : 310,
    'autoScale' : false,
    'transitionIn' : 'none',
    'transitionOut' : 'none',
    'type' : 'iframe'
  });


  /**
   * JS Selectbox behaviour for Resellers
   */
  $('input[type=submit]','#content-container-reseller').hide();
  $('select','#content-container-reseller').each(function() {
      $(this).change(function() {
          $.manageAjax.add('searchQueue', {
              url: '/resellers.js',
              dataType: 'html',
              data: $("#reseller_form").serialize(),
              method: 'get',
              success: function(html) {
                  $('#resellers_table').html(html);
              }
          });
      });
  });  

});

/********************************/
/******** Merry Go Round ********/
/********************************/

$(function() {
  $("#carousel-container").smoothDivScroll({
    autoScroll: "always",
    autoScrollDirection: "endlessloop",
    pauseAutoScroll: "mouseover",
    autoScrollSpeed: 2
  });
});

