I feel like i'm missing something super simple, but am banging my head against the wall. I have this function called "galleryImageSize", and also a parse of some JSON data. What I would like to have happen is after the data is parsed and the div is given a class of 'active' - run the "galleryImageSize" function to resize the image. I've chained the function call after the click event, but it gives me an error that the whole thing is not a function. Any ideas from the ether?
function galleryImageSize () {
var height = $("#gallery-overlay .activeImage figure img").data('height');
var width = $("#gallery-overlay .activeImage figure img").data('width');
var divHeight = $("#gallery-overlay .activeImage").height();
var divWidth = $("#gallery-overlay .activeImage").width();
var portraitRatio = height / width;
var newWidth = divHeight / portraitRatio;
var ratio = width / height;
var newHeight = divWidth / ratio;
if (height > width) {
if (divHeight > height && divWidth > width) {
$( "#gallery-overlay .activeImage figure img" ).width( width ).height( height );
}
if (divHeight > height && width > divWidth) {
$( "#gallery-overlay .activeImage figure img" ).width( divWidth ).height( height * portraitRatio );
}
if (newWidth > divWidth) {
$( "#gallery-overlay .activeImage figure img" ).width( divWidth ).height( newHeight );
}
else {
$( "#gallery-overlay .activeImage figure img" ).width( newWidth ).height( divHeight );
}
}
else {
if (divWidth > width && divHeight > height) {
$( "#gallery-overlay .activeImage figure img" ).width( width ).height( height );
}
else if (divWidth > width && height > divHeight) {
$( "#gallery-overlay .activeImage figure img" ).width( width * ratio ).height( divHeight );
}
else if (newHeight > divHeight) {
$( "#gallery-overlay .activeImage figure img" ).width( newWidth ).height( divHeight );
}
else {
$( "#gallery-overlay .activeImage figure img" ).width( divWidth ).height( newHeight );
}
}
}
$('.gallery-fire').each(function(e) {
$(document).on('click', '.gallery-fire', function(event){
var request = $(this).data('gallery');
var title = $(this).data('title');
$('#gallery-overlay .postTitle h1').html(title);
$.getJSON(request, function(data) {
for (var i = 0; i < data.length; i++) {
if (i == 0) {
$("#gallery-overlay .activeImage figure").html('<img data-height="'+data[i].height+'" data-width="'+data[i].width+'" src="'+data[i].image+'">');
$("#gallery-overlay .activeImage figcaption").html(data[i].caption+'<small>'+data[i].date+'</small>');
}
}
});
$( '#gallery-overlay' ).addClass( 'active');
}).galleryImageSize ();
});
Updated Code still doesn't work:
$('.gallery-fire').each(function(e) {
$(document).on('click', '.gallery-fire', function(event){
var request = $(this).data('gallery');
var title = $(this).data('title');
$('#gallery-overlay .postTitle h1').html(title);
var get = function() {
$.getJSON(request, function(data) {
for (var i = 0; i < data.length; i++) {
if (i == 0) {
$("#gallery-overlay .activeImage figure").html('<img data-height="'+data[i].height+'" data-width="'+data[i].width+'" src="'+data[i].image+'">');
$("#gallery-overlay .activeImage figcaption").html(data[i].caption+'<small>'+data[i].date+'</small>');
}
}
});
};
$.when( get() ).done(function() {
$('#gallery-overlay').addClass( 'active');
galleryImageSize("#gallery-overlay .activeImage figure img");
});
});
});
function galleryImageSize (value) {
var height = $(value).data('height');
var width = $(value).data('width');
var divHeight = $("#gallery-overlay .activeImage").height();
var divWidth = $("#gallery-overlay .activeImage").width();
var portraitRatio = height / width;
var newWidth = divHeight / portraitRatio;
var ratio = width / height;
var newHeight = divWidth / ratio;
if (height > width) {
if (divHeight > height && divWidth > width) {
$( value ).width( width ).height( height );
}
if (divHeight > height && width > divWidth) {
$( value ).width( divWidth ).height( height * portraitRatio );
}
if (newWidth > divWidth) {
$( value ).width( divWidth ).height( newHeight );
}
else {
$( value ).width( newWidth ).height( divHeight );
}
}
else {
if (divWidth > width && divHeight > height) {
$( value ).width( width ).height( height );
}
else if (divWidth > width && height > divHeight) {
$( value ).width( width * ratio ).height( divHeight );
}
else if (newHeight > divHeight) {
$( value ).width( newWidth ).height( divHeight );
}
else {
$( value ).width( divWidth ).height( newHeight );
}
}
}