﻿$(document).ready(function() {
    $('img[title]').each(function() {
        var title = $(this).attr('title');
        var src = title.substring(title.indexOf("_")+1,title.length);
        $.preloadImages(src);
    });
    
    $("a[class=side-gallery-image]").boxy({ title: "Viewing Gallery Image", closeable: true, draggable: true, center: true, x: 150, y: 150, cache: true, closeText: "x", afterShow:
        function() {
            $(".red-button").mouseover(function() {
                $(this).animate({ backgroundColor: "#ce371a" }, 200);
            });

            $(".red-button").mouseout(function() {
                $(this).animate({ backgroundColor: "#c21b04" }, 200);
            });

            $("#voteGalleryImage").click(function() {
                var giId = $("#hidImageId").val();
                $("#jVoteGalleryButton").slideUp(600, function() {
                    $("#jVoteGalleryImageLoader").css("visibility", "visible");
                    $("#jVoteGalleryImageLoader").css("display", "block");
                    $("#jVoteGalleryImageLoader").slideDown(600, function() {
                        $.ajax({
                            type: "POST",
                            cache: false,
                            url: "Resource/ajax/registerGalleryImageVote.aspx",
                            data: "image=" + giId,
                            dataType: "xml",
                            success: handleGalleryImageVotePass,
                            error: handleGalleryImageVoteError
                        });
                    });
                });
            });
        }
    });
});

function handleGalleryImageVotePass(xml, textStatus) {
    if ($(xml).find("error").length < 1) {
        $("#jVoteGalleryImageLoader").slideUp(600, function() {
            var html = "<p style='text-align: right;>Thanks, your vote has been submitted.</p>";
            $("#jVoteGalleryButton").html(html);
            $("#jVoteGalleryButton").slideDown(600);
        });
    } else {
        handleGalleryImageVoteError(null, null, null);
    }
}

function handleGalleryImageVoteError(XMLHttpRequest, textStatus, errorThrown) {
    $("#jVoteGalleryImageLoader").slideUp(600, function() {
        var html = "<p style='text-align: right;'>A server error occured and your vote was not submitted. Please check you are <a href='login.aspx' onclick='javascript: Boxy.get(this).hide();' class='red'>logged in</a> and you have not already voted this month.</p>";
        $("#jVoteGalleryButton").html(html);
        $("#jVoteGalleryButton").slideDown(600);
    });
}

jQuery.preloadImages = function()
{
  // for(var i = 0; i<arguments.length; i++)
  // {
    // jQuery("<img>").attr("src", arguments[i]);
  // }
}
