/*
 * Contains functions for performing eye4bw specific actions
 */


/*
 * Replaced currently selected photo with clicked thumbnail's fullsize photo.
 * For Alternate views
 */
function changeSelectedPhotoAlt(newPhotoId, title, viewCount, commentsCount, width, height) {
    var thumb = $("#photo" + newPhotoId);
    var photo = $("#selectedPhoto");
    var selectedPhotoLink = photo.parent();

    var newUrl = selectedPhotoLink.attr('href').split('/');
    newUrl[newUrl.length - 1] = newPhotoId;
    
    selectedPhotoLink.attr("href", newUrl.join('/'));
    $(".selected").attr('class', '');
    thumb.parent().attr('class', 'selected');


    photo.fadeOut('normal', function() {
        photo.width(width);
        photo.height(height);
        photo.attr('src', $(".selected").children()[0].src.replace('_thumb', ''));
    });
    
    $("#selectedPhotoTitle").html(title);
    $("#selectedPhotoViewCount").html(viewCount);
    $("#selectedPhotoCommentCount").html(commentsCount);

    photo.fadeIn('normal');
}

/*
 * Replaced currently selected photo with clicked thumbnail's fullsize photo.
 * For user-favorite-photos views
 */
function changeUserFavoritePhoto(newPhotoId, title, viewCount, commentsCount, userName, width, height) {
    var thumb = $("#photo" + newPhotoId);
    var photo = $("#selectedPhoto");
    var selectedPhotoLink = photo.parent();

    var newUrl = selectedPhotoLink.attr('href').split('/');
    newUrl[newUrl.length - 1] = newPhotoId;

    selectedPhotoLink.attr("href", newUrl.join('/'));
    $(".selected").attr('class', '');
    thumb.parent().attr('class', 'selected');

    photo.fadeOut('normal', function() {
        photo.width(width);
        photo.height(height);
        photo.attr('src', $(".selected").children()[0].src.replace('_thumb', ''));
    })
    
    $("#selectedPhotoTitle").html(title);
    $("#selectedPhotoViewCount").html(viewCount);
    $("#selectedPhotoCommentCount").html(commentsCount);
    $("#selectedPhotoUser").text(userName)
    photo.fadeIn();
}

/*
 * Replaced currently selected photo with clicked thumbnail's fullsize photo.
 * 
 */
function changeSelectedPhoto(selectedPhoto, newPhotoId, newUserName, newUserUrl, newTitle, viewCount, commentCount) {
	
    var thumb = $("#" + selectedPhoto.id);
    var photo = $("#selectedPhoto");
    var selectedPhotoLink = photo.parent();
    var userName = $("#selectedPhotoUser");

    var newUrl = selectedPhotoLink.attr('href').split('/');
    newUrl[newUrl.length - 1] = newPhotoId;

    selectedPhotoLink.attr("href", newUrl.join('/'));
    userName.html(newUserName);
    userName.attr("href", newUserUrl);
	
    $(".selected").attr('class', '');
    thumb.parent().attr('class', 'selected');

    photo.attr('src', $(".selected").children()[0].src.replace('_thumb', ''));
	
    //clean up height:
    
    $("#selectedPhotoTitle").text(newTitle);
    $("#selectedPhotoViewCount").text(viewCount);
    $("#selectedPhotoCommentCount").text(commentCount);

}
function cleanUpPhotoHeights(containerId) {
    // Loop through photos in the grid (by tag img) and resize them
    // algorithm should be:
    //  ASPECT_RATIO = height/width
    // CORRECTION = height/100
    // height = 100
    // width = width/correction
	
    var photos = $("#" + containerId + " img");
	
    for (var i = 0, photo; photo = photos[i]; i++) {
        if (photo.height > 96) {
            var correction = photo.height/96;
            photo.height = 96;
            photo.width = photo.width/correction;
        }
    }
	
    //clean up selected photo height
    var selectedPhoto = $("#selectedPhoto");
	
    if (selectedPhoto.height() > 400) {
		
        var shrink = selectedPhoto.height()/400;
        selectedPhoto.attr('height', 400);
        selectedPhoto.attr('width', selectedPhoto.width()/shrink);
    }
}
function cleanUpFeaturedPhotographer(id) {
    var photo = $("#" + id);

    if (photo.height() > 180) {
        var shrink = photo.height()/180;
        photo.attr('height', 180);
        photo.attr('width', photo.width()/shrink);
    }
}
function cleanUpFeaturedPhoto(id) {
    var photo = $("#" + id);

    if (photo.height() > 350) {
        var shrink = photo.height()/350;
        photo.attr('height', 350);
        photo.attr('width', photo.width()/shrink);
    }
}
function cleanUpThumbsHeights(containerId) {
    // Loop through photos in the grid (by tag img) and resize them
    // algorithm should be:
    //  ASPECT_RATIO = height/width
    // CORRECTION = height/100
    // height = 100
    // width = width/correction

    var photos = $("#" + containerId + " img");

    for (var i = 0, photo; photo = photos[i]; i++) {
        if (photo.height > 80) {
            var correction = photo.height/80;
            photo.height = 80;
            photo.width = photo.width/correction;
        }
    }
}
function cleanUpEditorsHeights(containerId) {

    var photos = $("#" + containerId + " img");

    for (var i = 0, photo; photo = photos[i]; i++) {
        if (photo.height > 130) {
            var correction = photo.height/130;
            photo.height = 130;
            photo.width = photo.width/correction;
        }
    }
}
function cleanUpPhotoHeights2(containerId) {
    // Loop through photos in the grid (by tag img) and resize them
    // algorithm should be:
    //  ASPECT_RATIO = height/width
    // CORRECTION = height/100
    // height = 100
    // width = width/correction

    var photos = $("#" + containerId + " img");

    for (var i = 0, photo; photo = photos[i]; i++) {
        if (photo.height > 60) {
            var correction = photo.height/60;
            photo.height = 60;
            photo.width = photo.width/correction;
        }
    }
}
