var easing = 'easeOutQuart';
var duration = 100;
var scrollJob = null;

$(document).ready(function(){
  $('form.wpcf7-form .your-name input').attr('placeholder', 'Name');
  $('form.wpcf7-form .your-email input').attr('placeholder', 'E-Mail');
  $('form.wpcf7-form .your-message textarea').attr('placeholder', 'Nachricht');
  $('form.wpcf7-form input#code').attr('placeholder', 'Bitte geben Sie den Code ein');
});  

$(window).resize(function() {
  $('.filter a.active').each( function(){
    $('.arrow img').animate({
      //whatyacallit?
      left: $(this).offset().left - ($('.arrow img').width() / 2)+ ($(this).width() / 2) +'px',
      easing: easing
    }, function(){
      $(this).fadeIn('fast')
    });
  });
});

$(window).load(function() {
  
  //init isotope
  $('.grid').isotope({
    animationEngine : 'jquery',
    animationOptions: {
      duration: duration,
      easing: easing,
      queue: false
    },
    masonry : {
      columnWidth : 236
    }
  });

  //home link
  $('.logo a').click( function(){
    
    if ( window.location.hash == ''){
      return true;
    }
    
    //deactivate all grid items
    $('.grid .item.active').each( function(){
      deactivate( $(this) );
    });
    //clear hash
    window.location.hash = '';
    
    //activate all-filter
    $('p.filter a.all').click();
    
    return false;
  });
  
  //grid listener
  $('.grid .item.thumb').live('click', function(){
    $('.grid .item.active').each( function(){
      deactivate( $(this) );
    });
    activate( $(this) );
  });
        
  //grid hover fx
  $('.grid .item.thumb').hover(
    function(){
      $(this).find('.preview img').animate({
        opacity: 0.6
      }, 20);
    },
    function(){
      $(this).find('.preview img').animate({
        opacity: 0.4
      }, 200);
    }
    );
      
  //filters
  $('.filter a').click(function(){
    
    $('.filter a').removeClass('active');
    $(this).addClass('active');
    
    var selector = $(this).attr('data-wws-filter');
    
    //deactivate all items
    $('.grid .item.active').each( function(){
      deactivate( $(this) );
    });
    
    //apply isotope filter
    $('.grid').isotope({
      filter: selector
    });
    
    $('.arrow img').animate({
      //whatyacallit?
      left: $(this).offset().left - ($('.arrow img').width() / 2)+ ($(this).width() / 2) +'px',
      easing: easing
    }, function(){
      $(this).fadeIn('fast')
    });
    
    return false;
  });
  
  //activate *-filter
  $('.filter a.all').click();

  //activate current post by hash
  if( window.location.hash != '' ){
    $(".grid").find("[data-wws-hash='" + $(location).attr('hash').substr(1) +"']").click();
  }
      
  function deactivate( el )
  {
    el.find('.active_content').hide();
    el.find('.preview').show();
          
    el.removeClass('active');
    el.animate({ 
      width:   '232px',
      height:   '131px',
      easing: easing
    }, duration, function(){ 
      
      //closed callback
      $('.grid').isotope('reLayout'); 
      el.addClass('thumb');
      el.find('.active_content .canvas .slide').cycle('destroy');
            
    }
    );
  }
        
  function activate( el )
  {
    
    window.location.hash = el.attr('data-wws-hash');
    
    el.removeClass('thumb');
    el.find('.preview').fadeOut('fast');
    
    el.animate({
      width:   "704px",
      height:   "536px",
      easing: easing
    }, duration, function(){ 
      
      //opnened callback
      el.addClass('active'); 
      el.find('.active_content').fadeIn();
      
      $('.grid').isotope('reLayout', function(){
        
        //load content if not already done.
        if( el.find('.active_content .canvas .slide').html() == '' ){
          $.ajax({
            url: el.attr('data-wws-content-url'),
            success: function(data) {
              //images loaded callback
              el.find('.active_content .canvas .slide').html( data );
              activateSlideshow( el );
            }
          });
        }
        else{
          activateSlideshow( el );
        }
        
        //scroll to viewport if not in it
        clearTimeout( scrollJob );
        scrollJob = setTimeout( "$('#"+el.attr('id')+"').scrollToViewPort( { offset : ( $('#"+el.attr('id')+"').offset().top - 4) } )", 600 );
      }); 
      
    } 
    );
  }
  
  function activateSlideshow( el )
  {
    //if we has more images, init slide + pager
    if( $(el.find('.active_content .canvas .slide img')).length > 1 ){
      el.find('.active_content .canvas .slide').cycle(
      {
        pager:  el.find('.slide_nav'), 
        pagerAnchorBuilder: function(idx, slide) { 
          return $('<li/>').append( $('<a/>').attr('href', '#') );
        },
        fx : 'scrollHorz',
        easing : easing
      });
      
      el.find('.active_content .canvas .slide_nav').css('left', ((el.width() / 2) - (($(el.find('.active_content .canvas .slide img')).length * 25) / 2))+'px');
    }
    
    
  }
        
        
});




