$(function() {
  
  // window.resizeTo(1024,768)
	
	////
	// RESIZE EXISTING BACKSTAGE VIDEOS
	//
	$('object', '.backstage').each(function(){
		$(this).attr('width','300').attr('height','182');
		$(this).find('embed').attr('width','300').attr('height','182');
	})
  
  timer = null;
  $('.postbrick a.img').hover(function() {
    a = $(this);
    a.css('width', $(this).width()).css('height', $(this).height());
    timer = setInterval(function() {
      hidden_first = a.children('img.hidden:first');
      visible = a.children('img.visible');
      visible.fadeOut(50, function() {
        visible.removeClass('visible').addClass('hidden').appendTo(a);
        hidden_first.fadeIn(50, function() {hidden_first.removeClass('hidden').addClass('visible')});
      })
    }, 500)
  }, function() {
    clearInterval(timer);
  })
  
});

$(window).load(function() {
  $('.all_posts .all_posts').masonry({ itemSelector: '.postbrick' });
});

$('.post-body object').entwine({
  onmatch: function() {
    max_width = 300;
    width = $(this).width();
    height = $(this).height();
    if (width > max_width) {
      ratio = max_width/width;
      new_height = height*ratio;
      $(this).width(max_width).height(new_height);
      $(this).chilren('embed').width(max_width).height(new_height);
    }
  }
});

$('.post-body embed').entwine({
  onmatch: function() {
    max_width = 300;
    width = $(this).width();
    height = $(this).height();
    if (width > max_width) {
      ratio = max_width/width;
      new_height = height*ratio;
      $(this).width(max_width).height(new_height);
    }
  }
});

$('.post-body img').entwine({
  onmatch: function() {
    $(this).appendTo('.post-images-temp');
  }
});

$('.post-images-temp img').entwine({
  onmatch: function() {
    max_width = 300;
    width = $(this).width();
    height = $(this).height();
    if (width > 0) {
      $(this).appendTo('.post-images');
      if (width > max_width) {
        ratio = max_width/width;
        new_height = height*ratio;
        $(this).width(max_width).height(new_height);
      }
      $(this).show();
    }
  }
});

$('.post-images img').entwine({
  onmatch: function() {
    $('.post-body .show-gallery').removeClass('hidden');
  }
})

$('.post-body a.show-gallery').entwine({
  onclick: function() {
    if ($('.gallery').hasClass('loaded')) {
      $('.gallery').fadeIn(200).addClass('pull-4');
    } else {
      $('.gallery').fadeIn(200).addClass('loaded');
      $('.post-images img').each(function(index) {
        img = $(this).clone().appendTo('.gallery');
        ratio = $('.gallery').width()/img.width();
        img.height(img.height()*ratio).width('100%');
        index = img.index('.gallery img')+1;
        $('.gallery p .index').append('<a href="#" id="'+index+'">'+index+'</a>');
      });
      $('.gallery').addClass('pull-4').height($('#container').height());
      $('.gallery img:not(:first)').hide();
      $('.gallery .toolbar .pagination .index a:first').addClass('current');
      return false;
    }
  }
});

$('.gallery p.close a').entwine({
  onclick: function() {
    $('.gallery').fadeOut(200, function() {
      $('.gallery').removeClass('pull-4');
    });
    return false;
  }
});

$('.gallery .toolbar .pagination a').entwine({
  onclick: function() {
    action = $(this).text();
    if (action == 'Prev') {
      $('.gallery').prev();
    } else if (action == 'Next') {
      $('.gallery').next();
    } else {
      $('.gallery').index(action);
    }
    return false;
  }
});

$('.gallery').entwine({
  next: function() {
    visible = $('.gallery img:visible').index('.gallery img');
    var length = $('.gallery img').length-1;
    if (visible == length) {
      next = 0;
    } else {
      next = visible+1;
    }
    $(this).changeout(next);
  },
  prev: function() {
    visible = $('.gallery img:visible').index('.gallery img');
    if (visible == 0) {
      prev = $('.gallery img').length-1;
    } else {
      prev = visible-1;
    }
    $(this).changeout(prev);
  },
  index: function(i) {
    $(this).changeout(i-1);
  },
  changeout: function(next) {
    $('.gallery img:visible').fadeOut(100, function() {
      $('.gallery img:eq('+next+')').fadeIn(100);
      $('.gallery .toolbar .pagination .index a').removeClass('current');
      $('.gallery .toolbar .pagination .index a#'+(next+1)).addClass('current');
    });
  }
});

function extension(fname) {
	if(fname != undefined){
  var pos = fname.lastIndexOf(".");
  var strlen = fname.length;
  if(pos != -1 && strlen != pos+1) {
    var ext = fname.split(".");
    var len = ext.length;
    var extension = ext[len-1].toLowerCase();
  } else {
    extension = "No extension found";
  }
  return extension;
}
}


