/*
 * AXRO Office Communication Distribution Supplies
 * General JavaScript
 * URL: http://www.axro.de
 * Author: Tim Isenheim
 */
$(document).ready(function(){

  $.fx.off = false; /* activate animations */

  $('#gallery a').colorbox({
    'close': 'X'
  });
  
  // Print functionality
  $('#printPage').click(function(){
    window.print();
  });
  
  // Equal column heights for section teaser
  equalHeight($('#section-teaser .content'));
  equalHeight($('#section-teaser h3'));
  // Loginboxes
  equalHeight($('#customer_login .inner'));

  /*
   * Custom select menues
   */
  // Breiten der Select menues anpassen
  $('select').each(function(){
    $(this).css('width', $(this).width()+80);
  });

  $('select').not('.localect,.plain').selectmenu({
    positionOptions: {
      my: "left top",
      at: "left bottom",
      offset: "1 0"
  }});

  $('select.localect').selectmenu({
    width: 160,
    positionOptions: {
      my: "left top",
      at: "left bottom",
      offset: "1 0"
  }});

/* ================ */

  // Colorbox for vcards
  $('.vcard .button a').colorbox({close:'X'});

  //var current_lang = $('.languages .current .flag').attr('alt');
  var current_country = $('#team_filter_checkbox').attr('value');
  var quicksand_finished = true;

  $cont = $('#sales');
  $data = $cont.clone(true);
  $backup = $data.find('.vcard');
  
  function axroSort(arr){
  	$result = $();
  	$first = $(arr).find('.teamleader.' + current_country);
  	$second = $(arr).find('.' + current_country).not('.teamleader');
  	$third = $(arr).find('.inactive');
  	$r = $.merge($first,$second);
  	$r = $.merge($r, $third);
  	$result = $result.agregate( $r );
  	return $result;
  }

  function activateSort(language){
   	$('#team_widget .vcard').not('.'+language).not('.none').addClass('inactive').prepend('<div class="blind">&nbsp;</div>');

  	$sorted = axroSort($cont);

  	if(quicksand_finished){
  		$cont.quicksand( $sorted, { useScaling: false}, function(w){ quicksand_finished = true; $('.vcard .button a').colorbox({close:'X'}); } );
  		quicksand_finished = false;
  	}
  }
  
  $.fn.agregate = function(newCollection) {
      return this.pushStack(newCollection, "agregate", "");
  };

  // Sortierung beim Laden der Seite aktivieren
  if( $('#team_filter_checkbox').length > 0 ){
	var checklang = $('#team_filter_checkbox').attr('value');
  	activateSort( checklang );
  }
  
  // Mitarbeiter "Meine Sprache" Funktion
  $('#team_filter_checkbox').click(function(){
  	if($(this).is(':checked')){
		// alle zeigen
   		$('#team_widget .vcard').removeClass('inactive');
  		$('#team_widget .vcard .blind').remove();
  		if(quicksand_finished){
  			$cont.quicksand( $backup, { useScaling: false}, function(w){ quicksand_finished = true; $('.vcard .button a').colorbox({close:'X'}); }  );
  			quicksand_finished = false;
  		}
  	}
  	else{
		activateSort(checklang);
  	}
  });

  // Placeholder
  $('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
      input.val('');
      input.removeClass('placeholder');
    }
  }).blur(function() {
    var input = $(this);
    if (input.val() == '' || input.val() == input.attr('placeholder')) {
      input.addClass('placeholder');
      input.val(input.attr('placeholder'));
    }
  }).blur();

    var mylangs_active = false;
  // Ansprechpartner "Meine Sprachen" Overlay
  $('body').click(function(){ $('#extra-languages').hide(); });

  $('#mylangs').click(function(){
    if(!mylangs_active){
      var i = 167; // default top value
      if( $('#frontpage-teaser').length > 0) i = 153;
      x = $('#extra-languages').show().height();
      $('#extra-languages').css('top', i-x);
      mylangs_active = true;
    }
    else{
      $('#extra-languages').hide();
      mylangs_active = false;
    }
  });

  $('#mylangs').click(function(event){
	event.preventDefault();  /* Trigger nicht als Jumplink verwenden */
	event.stopPropagation(); /* Klick auf Trigger nicht als Klick auf <body> deuten (Event zum Deaktivieren des Overlays) */
  });

});

function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}


