var m_width; //TP
var ThruPixels = {
    init: function() {
        var img = document.getElementById("image");

        ThruPixels.minContainerWidth = 480;
        ThruPixels.containerWidth = ThruPixels.minContainerWidth;
        ThruPixels.setContainerWidth();
        $("#exifWrap").hide();
        $("#commentWrap").hide();
        ThruPixels.exifInit();
        ThruPixels.commentsInit();
        ThruPixels.exifOn = false;
        ThruPixels.commentsOn = false;
        ThruPixels.toggleExIf();
        ThruPixels.toggleComments();
    },

    toggleExIf: function() {
        $("#view_exif").bind("click", function(event) {

            if (ThruPixels.exifOn) {     // If it's on
                $("#exifWrap").fadeTo("slow", 0.99).fadeOut();
                ThruPixels.exifOn = false;
                $("#view_exif").css("color", "#777777");
            }
            else {                    // If it's off
                $("#exifWrap").fadeIn("slow").fadeTo("slow", 0.75);
                ThruPixels.exifOn = true;
                $("#view_exif").css("color", "#CC9900");
            }

            if (event.preventDefault) {  // DOM Event Handling
                event.preventDefault();
            } else {                    // IE Event Handling
                event.returnValue = false;
            }
        });
    },

    toggleComments: function() {
        $("#view_comments").bind("click", function(event) {

            if (ThruPixels.commentsOn) { // If it's on
                $("#commentWrap").fadeTo("slow", 0.99).fadeOut();
                ThruPixels.commentsOn = false;
                $("#view_comments").css("color", "#777777");
                document.forms[0].reset();
                ResetErrMsgs();
            } else {                    // If it's off
                $("#commentWrap").fadeIn("slow").fadeTo("slow", 0.75);
                ThruPixels.commentsOn = true;
                $("#view_comments").css("color", "#CC9900");
            }

            if (event.preventDefault) {  // DOM Event Handling
                event.preventDefault();
            } else {                    // IE Event Handling
                event.returnValue = false;
            }
        });
    },

    exifInit: function() {
        var offset = $("#image").offset();
        var gap = Math.floor(($("#image").width() * 3) / 100);
        var width = Math.floor(($("#image").width() * 30) / 100);
        $("#exifWrap").css("position", "absolute");
        $("#exifWrap").css("top", offset.top);
        $("#exifWrap").css("left", offset.left);
        $("#exifWrap").css("z-index", 20);
        $("#exifWrap").css("border", "1px solid #666");
        $("#exifWrap").css("background-color", "#000000");
        $("#exifWrap").css("margin", "0em");
        $("#exifWrap").css("padding", 5 + "px");
        $("#exifWrap").width(250);
        m_width = width;
    },

    commentsInit: function() {
        var offset = $("#image").offset();
        var left = offset.left + $("#image").width() - 436;
        $("#commentWrap").css("position", "absolute");
        $("#commentWrap").css("top", offset.top);
        $("#commentWrap").css("left", left);
        $("#commentWrap").css("z-index", 30);
        $("#commentWrap").css("border", "1px solid #666");
        $("#commentWrap").css("background-color", "#000000");
        $("#commentWrap").css("padding", "21px");
        $("#commentWrap").css("padding-top", "15px");
        $("#exifWrap").css("margin", "0em");
        $("#commentWrap").width(394);
        $("#commentWrap").height($("#image").height() - 36 > 570 ? 570 : $("#image").height() - 36);
        $("#commentWrap").css("overflow", "auto");
    },

    setContainerWidth: function() {
        var pictureWidth = $("#image").width();
        ThruPixels.containerWidth = (pictureWidth < ThruPixels.minContainerWidth) ? ThruPixels.minContainerWidth : pictureWidth;
        $("#container").width(ThruPixels.containerWidth);
    }
}
$(document).ready(function() {
    ThruPixels.init();
});

window.onresize = function() {
    ThruPixels.exifInit();
    ThruPixels.commentsInit();
}

function CloseComments() {
    document.forms[0].reset();
    ResetErrMsgs();
    $("#commentWrap").fadeTo("slow", 0.99).fadeOut();
    $("#view_comments").css("color", "#777777");
    ThruPixels.commentsOn = false;
}

function CloseEXIF() {
    $("#exifWrap").fadeTo("slow", 0.99).fadeOut();
    $("#view_exif").css("color", "#777777");
    ThruPixels.exifOn = false;
}


function initImage() {
    imageId = "image";
    m_image = document.getElementById("image");
    setOpacity(m_image, 0);
    m_image.style.visibility = "visible";
    fadeInEffect(imageId, 0);
}

function fadeInEffect(objId, opacity) {
    if (document.getElementById) {
        obj = document.getElementById(objId);
        if (opacity <= 100) {
            setOpacity(obj, opacity);
            opacity += 5;
            window.setTimeout("fadeInEffect('" + objId + "'," + opacity + ")", 30);
        }
    }
}

function setOpacity(obj, opacity) {
    opacity = (opacity == 100) ? 99.999 : opacity;
    // IE/Win
    obj.style.filter = "alpha(opacity:" + opacity + ")";
    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity / 100;
    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity / 100;
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity / 100;
}

window.onload = function() {initImage()}