function DrawGalleryOptions(div) {

    $('.gallery').css({ width: '452px', height: '332px', overflow: 'hidden' });
    $(div).append('<div class="gallery-title"><p style="font-size:14px;">no title</p><div class="gallery-bg gallery-bg-small"></div></div>');
    $(div).append('<div class="gallery-nav"></div>');
    $('.gallery-nav').append('<div class="gallery-bg"></div><div class="gallery-controls"><p class="gall-title"><a class="gallleft" href="#galleryleft" title="">Prev</a><a class="gallright" href="#galleryright" title="">Next</a></p></div>');

    $('.gallery-title, .gallery-nav, .gallery-bg').css({ position: 'absolute', left: '0px', height: '53px', width: '452px' });
    $('.gallery-nav').css({ height: '53px', bottom: '-53px' });
    $('.gallery-bg').css({ backgroundColor: '#222222', opacity: 0.8, height: '53px', zIndex: 1000 });
    $('.gallery-title').css({ top: '-53px' });
    $('.gallery-controls').css({ position: 'absolute', zIndex: 1002 });
    $('.gallery-bg-small').css({ height: '53px' });
    $('.gallery-title p').css({ zIndex: 1002, color: '#f76f00', position: 'absolute', textAlign: 'center', fontFamiy: 'arial', fontSize: '14px', fontWeight:'bold', width: '452px', paddingTop: '15px' });
    $('.gallery-controls p').css({ posistion: 'absolute', zIndex: 1005, left: '0px', top: '0px' });
    $('a.gallleft').css({ border: 'none', display: 'block', textIndent: '0', fontSize: '11px', color: '#ffffff', top: '15px', left: '15px', position: 'absolute'});
    $('a.gallright').css({ border: 'none', display: 'block', textIndent: '0', fontSize: '11px', color: '#ffffff', top: '15px', left: '400px', position: 'absolute'});
}

function ShowGalleryOptions() {

    jQuery.easing.def = "easeOutQuint";
    $('.gallery-title').animate({ top: '0px' }, 800);
    $('.gallery-nav').animate({ bottom: '0px' }, 800);

}

function HideGalleryOptions() {

    $('.gallery-title').animate({ top: '-53px' }, 800);
    $('.gallery-nav').animate({ bottom: '-53px' }, 800);

}

function StopAll() {

    $('.gallery-title').stop();
    $('.gallery-nav').stop();

}

function MoveGallery(pic, num) {

    //$('#test2').html('>> <br />' + pic[_GalleryCurrent].src + " " + pic[_GalleryCurrent].height + " " + pic[_GalleryCurrent].width + "<br />Current: " + _GalleryCurrent);

    $('.gallery').append('<img id="gallery-current" style="display:none;" src="' + pic[num].src + '" width="' + pic[num].width + '" height="' + pic[num].height + '" alt="' + pic[num].alt + '" />');
    $('#gallery-current').css({ opacity: 0, display: 'block' });
    $('.gallery img').css({ position: 'absolute', top: '0px', left: '0px', zIndex: '400' });
    //jQuery.easing.def = "easeOutElastic";

    $('.gallery-nav, .gallery-title').css({ position: 'absolute', left: '0px', zIndex: '1100' }); // random reassignement of gallery zIndex, because IE7 doesn't behave

    $('.gallery-title p').html(pic[num].alt);

    $('#gallery-current').animate({ opacity: 1 }, 1200, function() {

        $('#gallery-last').remove();
        $('#gallery-current').attr('id', 'gallery-last');

    });
    $('.gallery-controls').css({ position: 'absolute', zIndex: 1102 });


}


function AutoPlay(pic) {

    if (boolautoplay) {
        //if (_iteration <= opt.sleepOnLoop) {

        _GalleryCurrent++;

        if (_GalleryCurrent > _GalleryMax) {
            _GalleryCurrent = 0;
        }

        MoveGallery(pic, _GalleryCurrent)//chec3k;

        setTimeout(function() { AutoPlay(pic) }, playDelay);
        //}
    }

}


$(function() {

    if ($('.gallery')) {

        var pic;
        pic = $(".gallery").children("img");
        _GalleryMax = pic.length - 1;
        _GalleryCurrent = 0;
        animTimer = 900;
        easing = "easeOutQuint";
        boolautoplay = true;
        playDelay = 3200;

        //$('#test2').html('>><br />' + pic[_].src + " " + pic[0].height + " " + pic[0].width + "<br />Current: " + _GalleryCurrent);

        $('.gallery').css({ height: 283, overflow: 'hidden', position: 'relative' });
        $('.gallery').html('');

        DrawGalleryOptions('.gallery');
        //alert(pic[_GalleryCurrent].alt + pic[_GalleryCurrent].src + pic[_GalleryCurrent].width + pic[_GalleryCurrent].height);
        $('.gallery').append('<img id="gallery-last" src="' + pic[_GalleryCurrent].src + '" width="' + pic[_GalleryCurrent].width + '" height="' + pic[_GalleryCurrent].height + '" alt="' + pic[_GalleryCurrent].alt + '" />');
        $('.gallery-title p').html(pic[_GalleryCurrent].alt);

        AutoPlay(pic);

        $('.gallery').hover(function() {

            //alert('on');
            StopAll();
            ShowGalleryOptions();

        },
        function() {

            //alert('off');
            StopAll();
            HideGalleryOptions();

        });

        $('a.gallright').click(function() {
            boolautoplay = false;
            $('#gallery-current').stop(false, true);

            //alert('boota');
            _GalleryCurrent = _GalleryCurrent + 1;
            if (_GalleryCurrent > _GalleryMax) {
                _GalleryCurrent = 0;
            }

            MoveGallery(pic, _GalleryCurrent)//check;

        });

        $('a.gallleft').click(function() {
            boolautoplay = false;
            $('#gallery-current').stop(false, true);

            //alert('boota');
            _GalleryCurrent = _GalleryCurrent - 1;
            if (_GalleryCurrent < 0) {
                _GalleryCurrent = _GalleryMax;
            }

            MoveGallery(pic, _GalleryCurrent)//check2;

        });

    }

});