var easing = 'easeOutQuart';
var duration = 800;
var scrollJob = null;

$(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 : 240
    }
  });

  //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
    //$('.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: 1.0
      }, 20);
    },
    function(){
      $(this).find('.preview img').animate({
        opacity: 1.0
      }, 200);
    }
    );
      
  //filters
 /* $('.filter a').click(function(){
    
    $('.filter a').removeClass('active');
    $(this).addClass('active');
    
    var selector = $(this).attr('data-ray-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-ray-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:   '240px',
      height:   '240px',
      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-ray-hash');
    
    el.removeClass('thumb');
    el.find('.preview').fadeOut('fast');
    
    el.animate({
      width:   "710px",
      height:   "720px",
      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-ray-content-url') + '?' + (1 + 40004*(Math.random())),
            success: function(data) {
              //images loaded callback
              el.find('.active_content .canvas .slide').html( data );
            }
          });
        }
        else{
        }
        
        //scroll to viewport if not in it
        clearTimeout( scrollJob );
        scrollJob = setTimeout( "$('#"+el.attr('id')+"').scrollToViewPort( { offset : ( $('#"+el.attr('id')+"').offset().top - 130) } )", 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');
    }
  }   
});

function hashFilter(){
	
	
}
$.param({ filter: '*' })
$.param({ filter: '.raysono' })
$.param({ filter: '.news' })
$.param({ filter: '.arbeiten' })
$.param({ filter: '.job' })
$.param({ filter: '.networks' })

$(window).bind( 'hashchange', function( event ){
  // get options object from hash
  if(typeof $.deparam != "undefined"){
	  	var hashOptions = $.deparam.fragment();
  		// apply options from hash
  		$('.grid').isotope( hashOptions );
		
  		$('.grid .item.active').each( function(){
    		deactivate($(this));
  		});	
  }
			
  if( window.location.hash != '' ){
	  	
	/* var grid = $(".grid").find("[data-ray-hash='" + $(location).attr('hash').substr(1) +"']");
	  	
		
			var filterClass = $(".grid").find("[data-ray-hash='" + $(location).attr('hash').substr(1) +"']").attr('class').replace('item','').replace('isotope-','').replace('item','').replace('isotope-hidden','').replace('thumb','').replace(' ','');
			
			if(filterClass.lenght >= 10){
				filterClass = '.*';	
			}else{
				filterClass = '.' + filterClass;
			
		    $('.grid').isotope({
      			filter: filterClass
    		});
		}*/
			$(".grid").find("[data-ray-hash='" + $(location).attr('hash').substr(1) +"']").click();
		
  }else{
	$("a.dd_right").click();
	$('.dropdownContent_hi a:first-child').click(); 
  }
})
  // trigger hashchange to capture any hash data on init
  .trigger('hashchange');
