﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />
if (typeof Crispulo == "undefined") {
    Crispulo = {};
}
Crispulo.Footer = {
    showPreviews: true,
    thumbDetails: {
        data: {},
        hasData: false,
        url: ""
    },
    getThumbDetails: function(e) {
        $.ajax({
            type: "POST",
            url: "/footer/GetFooterThumbDetails",
            async: true,
            dataType: "json",
            success: function(response) {
                //debugger;
                Crispulo.Footer.thumbDetails.data = response.details;
                Crispulo.Footer.thumbDetails.url = response.path;
                Crispulo.Footer.thumbDetails.hasData = true;
                Crispulo.Footer.renderThumbs(response.details);
            },
            error: function(response) { /*debugger;*/ }
        });
    },
    renderThumbs: function(imageDetails) {
        $(imageDetails).each(function(index, img) {
            var aspectRatio = img.AspectRatio;
            if (aspectRatio > 1) {
                img.ThumbWidth = 55;
                img.ThumbHeight = 55 / aspectRatio;
                img["MarginTop"] = ((55 - img.ThumbHeight) / 2) + "px";
            }
            else {
                img.ThumbHeight = 55;
                img.ThumbWidth = 55 * aspectRatio;
            }
        });
        $("#ajax-loader").remove();
        $(crispulo.footer.thumbs.render({
            imageDetails: imageDetails,
            url: Crispulo.Footer.thumbDetails.url
        })).css({ opacity: 0 }).appendTo($("#footer_thumbs")).fadeTo(280, 0.6);
        $("#thumb-list li:last img").css("margin", "0 0 0 0");
    },
    previewThumb: function(e, elem) {
        var mouse = Crispulo.Footer.getMouseCoords(e);
        var imageId = Crispulo.Footer.extractId(elem);
        var imageDetails = Crispulo.Footer.getPreviewImageDetails(imageId);

        var preview = $(crispulo.footer.thumbs.preview({
                id: imageId,
                src: Crispulo.Footer.thumbDetails.url + imageDetails.Name,
                category: imageDetails.Category,
                title: imageDetails.Title,
                width: imageDetails.Width,
                height: imageDetails.Height
            }))
            .css({
                position: "absolute",
                opacity: 0
            })
            .appendTo(document.body);
        var position = Crispulo.Footer.EdgeDetection.compensate(mouse, imageDetails, preview);
        preview.css({
                top: position.top + "px",
                left: position.left + "px",
                zIndex: 2000
            })
            .fadeTo(380, 1.0);

        $("#image_" + imageId + "_preview-panel").mouseleave(function() {
            $(this).fadeOut(200, function() { $(this).remove(); });
        });
    },
    previewInGallery: function(e, elem) {
        e.preventDefault();
        elem = $(elem);
        var image = elem.parent().prev();
        var id = Crispulo.Footer.extractId(image);
        $("#image_" + id + "_preview-panel").trigger("mouseleave");
        document.cookie = "footerPreviewRedirectImageId=" + id + "; path=/";
        window.location = $(elem).attr("href").split("#")[0];
    },
    extractId: function(elem) {
        return /^footer_(?:thumb|preview)-([0-9]+)$/i.exec($(elem).attr("id"))[1];
    },
    getPreviewImageDetails: function(id) {
        var images = Crispulo.Footer.thumbDetails.data;
        return (function() {
            for (var i = 0, len = images.length; i < len; i++) {
                if (images[i].ID == id) { return images[i]; }
            }
        })();
    },
    getMouseCoords: function(e) {
        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }
        return {
            x: posx,
            y: posy
        };
    }
};

Crispulo.Footer.EdgeDetection = {
    window: {},
    getWindowEdges: function() {
        var win = $(window);
        Crispulo.Footer.EdgeDetection.window.size = {
            w: win.width(),
            h: win.height()
        };
        Crispulo.Footer.EdgeDetection.window.scroll = {
            top: win.scrollTop(),
            left: win.scrollLeft()
        };
    },
    compensate: function(mouse, imageDetails, previewPanel) {
        var position = {
            top: mouse.y - (imageDetails.Height + 28),
            left: mouse.x - ((imageDetails.Width / 2) + 16)
        };
        var previewSize = {
            w: $(previewPanel).width(),
            h: $(previewPanel).height()
        };
        var win = Crispulo.Footer.EdgeDetection.window;
        if (position.left < win.scroll.left) {
            position.left = win.scroll.left;
        }
        else if ((position.left + previewSize.w) > (win.scroll.left + win.size.w)) {
            position.left = (win.scroll.left + win.size.w) - (previewSize.w + 2);
        }
        if (position.top < win.scroll.top) {
            position.top = win.scroll.top;
        }
        else if ((position.top + previewSize.h + 28) > (win.scroll.top + win.size.h)) {
            position.top = (win.scroll.top + win.size.h) - (previewSize.h + 28);
        }
        return position;
    }
};

$(document).bind("initializeFooter", function(e) {
    Crispulo.Footer.getThumbDetails();
    $("#footer_thumbs").html(crispulo.footer.thumbs.ajax_loader());
    if (Crispulo.Footer.showPreviews) {
        $("#thumb-list li img").live("mouseover", function(e) { Crispulo.Footer.previewThumb(e, this); });
        $(".footer-hover p a").live("click", function(e) { Crispulo.Footer.previewInGallery(e, this); });
        Crispulo.Footer.EdgeDetection.getWindowEdges();
    }
});

$(document).ready(function() {
    $(document).trigger("initializeFooter");
    $(window).bind("resize scroll", Crispulo.Footer.EdgeDetection.getWindowEdges);
});
