﻿function setPosition(maxWidth, maxHeight) {
    var w = winWidth();
    var h = winHeight();

    var width = maxWidth;
    var height = maxHeight;

    if (w < maxWidth + 100 || h < maxHeight + 50) {
        if (w / h > maxWidth / maxHeight) {
            height = h - 50;
            width = maxWidth / (maxHeight / height);
        }
        else {
            width = w - 100;
            height = maxHeight / (maxWidth / width);
        }
    }

    document.getElementById("wrapper").style.width = width + "px";
    document.getElementById("wrapper").style.height = height + "px";

    document.getElementById("title_img").style.height = document.getElementById("title_img").offsetHeight > height ? height - 5 + "px" : "auto";

    document.getElementById("wrapper").style.marginTop = (winHeight() - document.getElementById("wrapper").offsetHeight) / 2 + "px";

    try { resizeGallery(); } catch (e) { }
}

function winWidth() {
    if (typeof (window.innerWidth) == 'number') {
        //Netscape compliant
        return window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        //IE6 standards compliant mode
        return document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        //DOM compliant
        return document.body.clientWidth;
    }
}
function winHeight() {
    if (typeof (window.innerHeight) == 'number') {
        //Netscape compliant
        return window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE6 standards compliant mode
        return document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        //DOM compliant
        return document.body.clientHeight;
    }
}

var top_margin = 0;
var t_up, t_down;

function scroll_down(amount, wheel) {
    stop_scroll(t_up);
    
    if (amount < 0 || !wheel) {
        var x = document.getElementById("scrolls");

        if (top_margin > document.getElementById("content").offsetHeight - x.offsetHeight) {
            top_margin -= 5;
            x.style.marginTop = top_margin + "px";
            t_down = setTimeout("scroll_down(" + ++amount + "," + wheel + ")", 1);
        }
    }
    else {
        stop_scroll(t_down);
    }
}
function scroll_up(amount, wheel) {
    stop_scroll(t_down);

    if (amount > 0 || !wheel) {
        var x = document.getElementById("scrolls");

        if (top_margin < 0) {
            top_margin += 5;
            x.style.marginTop = top_margin + "px";

            t_up = setTimeout("scroll_up(" + --amount + "," + wheel + ")", 1);
        }
    }
    else {
        stop_scroll(t_up);
    }
}
function stop_scroll(t) {
    try { clearTimeout(t); } catch (e) { }
    
//    var x = document.getElementById("back");
//    var y = document.getElementById("forward");
//    var a_up = document.getElementById("link_up");
//    var a_down = document.getElementById("link_down");

//    if (top_margin == 0) {
//        x.src = "img/previous_inactive.gif";
//        a_up.style.cursor = "default";
//    }
//    else {
//        x.src = "img/previous_active.gif";
//        a_up.style.cursor = "pointer";
//    }

//    if (top_margin == (-1463)) {
//        y.src = "img/next_inactive.gif";
//        a_down.style.cursor = "default";
//    }
//    else {
//        y.src = "img/next_active.gif";
//        a_down.style.cursor = "pointer";
//    }
}




/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta) {
        if (delta < 0)
		scroll_down(delta*10,true);
        else
		scroll_up(delta*10,true);
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

