/*
 * Made by .p.i.x.e.l. (http://www.pixel-tyumen.ru) 2009
 * Version 1.0
 */

// PUBLIC
var sScaleInitDiv  = '#pictures';
var sScaleAlt      = 'Рисунки наших детей';
var nScaleMinWidth = 145;
var nScaleMaxWidth = 250;
var nScaleForStep  = 10;
var nScaleVelocity = 10;

// PRIVATE
var oScaleOffset   = null;
var nScaleInterval;

var oInitImage = { name: '', width: nScaleMinWidth, height: 1 }
var oReImage = { name: '', width: nScaleMaxWidth, height: 1 }
var oScaleIncrement = { width: nScaleForStep, height: 0, x: 0, y: 0 };
var oScaleAnimate = { width: 0, height: 0, x: 0, y: 0 };

function make_image (name, width, height) {
    var name, width, height;
    return '<img src="' + name + '" width="' + width + '" height="' + height + '" alt="' + sScaleAlt + '">';
}

function reset () {
    $(sScaleInitDiv).empty().css({
        position: 'static',
        margin: '20px 10px 20px 0px',
        left: '',
        top: '',
        width: oInitImage.width + 'px'
    });
    $(sScaleInitDiv).html(make_image(oInitImage.name, oInitImage.width, oInitImage.height));
    reset_animate_obgect();
}

function scale_image () {
    oScaleAnimate.width += oScaleIncrement.width;
    oScaleAnimate.height += oScaleIncrement.height;
    oScaleAnimate.x -= oScaleIncrement.x;
    oScaleAnimate.y -= oScaleIncrement.y;
    if (oScaleAnimate.width >= oReImage.width) {
        clearInterval(nScaleInterval);
        nScaleInterval = false;
        return;
    }
    $(sScaleInitDiv).empty().css({
        position: 'absolute',
        margin: '0px 10px 20px 0px',
        left: oScaleAnimate.x + 'px',
        top: oScaleAnimate.y + 'px',
        width: oScaleAnimate.width + 'px'
    }).html(make_image(oReImage.name, oScaleAnimate.width, oScaleAnimate.height));
}

function reset_animate_obgect () {
    oScaleAnimate.width = oInitImage.width;
    oScaleAnimate.height = oInitImage.height;
    oScaleAnimate.x = oScaleOffset.left;
    oScaleAnimate.y = oScaleOffset.top;
}

$(document).ready( function () {
    oScaleOffset = $(sScaleInitDiv).offset();
    var oInitImg = $(sScaleInitDiv + ' img');
    oInitImage.name = oInitImg.attr('src');
    oInitImage.width = oInitImg.attr('width');
    oInitImage.height = oInitImg.attr('height');
    oReImage.height = parseInt(nScaleMaxWidth * oInitImage.height / nScaleMinWidth);
    var oPrelImg = new Image(oReImage.width, oReImage.height);
    oPrelImg.src = oReImage.name = oInitImage.name.replace('_s.jpg', '_b.jpg');
    oScaleIncrement.height = parseInt(oScaleIncrement.width * oInitImage.height / oInitImage.width);
    oScaleIncrement.y = parseInt(oScaleIncrement.height / 2);
    oScaleIncrement.x = oScaleIncrement.width;
    reset_animate_obgect();

    $(sScaleInitDiv).hover( function () {
        if (nScaleInterval) return;
        nScaleInterval = setInterval('scale_image()', nScaleVelocity);
    }, function () { reset(); });
}).click( function () { reset(); });
