
    /**

        Salute v2.2.1

        © 2009 Алексей Забродин

    */

    

	Salute = {

		preloader: '/core/js/salute/loader.gif',

		autoScale: true,

		anchors: new Array(),

		temp: new Array(),

        text: {

            close: 'Закрыть',

            prev: 'Предыдущая',

            next: 'Следующая'

        },

        event: function(o, e, l)

		{

			if (o.attachEvent)

			{

				o.attachEvent('on' + e, l);

			}

			else

			{

				o.addEventListener(e, l, false);

			}

		},

		style: function(o, s)

		{

			for (var i in s)

			{

				o.style[i] = s[i];

			}

		},

		createElement: function(o, n, s)

		{

			var e = document.createElement(n);

			if (s) this.style(e, s);

			o.appendChild(e);

			return e;

		},

		each: function(o, f)

		{

			for (var i = 0; i < o.length; i++)

			{

				f.call(o[i], i);

			}

		},

		align: function(e)

		{
			e.style.top = ((document.documentElement.clientHeight / 2) - (e.clientHeight / 2)) + 'px';
            e.style.left = '0';
            e.style.left = ((document.body.clientWidth / 2) - (e.clientWidth / 2)) + 'px';
		},

		fade: function(e)

		{

			function s(v)

			{

				e.style.opacity = (v / 10);

				e.style.filter = 'alpha(opacity=' + (v * 10) + ')';

                if (v == 10) e.style.filter = null;

			}

			function f(v)

			{

				s(v);

				if (v <= 9)

				{

					setTimeout(function()

					{

						f(v + 1);

					}, 28);

				}

			}

			f(0);

		},

		init: function()

		{

			var $ = Salute, i = 0;

			$.each(document.getElementsByTagName('a'), function()

			{

				if ((this.href) && (this.href != encodeURI(location.toString())) && this.rel == 'salute')

				{

					$.anchors.push({

                        href: this.href

                    });

                    this.index = i;

                    this.onclick = $.show;

                    i++;

				}

			});

			

			$.blind = $.createElement(document.body, 'div', {

                display: 'none',

                backgroundColor: '#000',

                position: 'fixed',

                top: '0',

                left: '0',

                width: '100%',

                height: '100%',

                zIndex: '2',

                opacity: '0.7',

                filter: 'alpha(opacity=70)'

            });

            $.blind.onclick = $.hide;

            $.container = $.createElement(document.body, 'div', {

                display: 'none',

                background: '#FFF url(' + $.preloader + ') no-repeat center center',

                border: '1px solid #444',

                padding: '10px',

                position: 'fixed',

                top: '50%',

                left: '50%',

                zIndex: '3'

            });

            $.img = $.createElement($.container, 'img', {

            	margin: '0'

            });

            $.img.src = $.preloader;

            

            var bs = {

                display: 'none',

                backgroundColor: '#FFF',

                fontFamily: 'Arial, Helvetica, sans-serif',

                color: '#000',

                fontSize: '13px',

                position: 'absolute',

                top: '10px',

                margin: '10px',

                padding: '0.6em',

                zIndex: '4',

                opacity: '0.7',

                filter: 'alpha(opacity=70)'

            };

            $.navBtn = $.createElement($.container, 'div', bs);

            $.navBtn.style.left = '10px';

            $.closeBtn = $.createElement($.container, 'div', bs);

            $.closeBtn.style.right = '10px';

            

            function hideBtns()

            {

            	$.closeBtn.style.display = 'none';

                $.navBtn.style.display = 'none';

            }

            function showBtns()

            {

            	if($.temp['img'].complete)

                {

                    $.closeBtn.style.display = '';

                    clearTimeout($.temp['btnTimeout']);

                    if ($.anchors.length > 1) $.navBtn.style.display = '';

                }

            }

            function shBtns()

            {

            	showBtns();

                clearTimeout($.temp['btnTimeout']);

                $.temp['btnTimeout'] = setTimeout(function()

                {

                    hideBtns();

                }, 1000);

            }

            

            $.img.onmouseout = hideBtns;

            $.img.onmousemove = shBtns;

            $.closeBtn.onmouseover = showBtns;

            $.navBtn.onmouseover = showBtns;

		},

		hide: function()

		{

			var $ = Salute;

            $.blind.style.display = 'none';

            $.container.style.display = 'none';

            $.closeBtn.style.display = 'none';

            $.navBtn.style.display = 'none';

		},

		show: function(s)

		{

			var $ = Salute, i;

			

			if ((s == 'prev') || (s == 'next'))

            {

                i = $.temp['index'];

                if((s == 'prev') && (i > 0))

                {

                    i--;

                }

                else if((s == 'prev'))

                {

                    return false;

                }

                if((s == 'next') && (i < $.anchors.length - 1))

                {

                    i++;

                }

                else if((s == 'next'))

                {

                    return false;

                }

            }

            else

            {

                i = this.index;

            }

            $.temp['index'] = i;

            $.temp['onload'] = false;

			

			$.closeBtn.innerHTML = '<a href="javascript:;" onclick="Salute.hide()" style="color: #000;">' +

                $.text.close + '</a>';

            var prev = '&larr; <a href="javascript:;" onclick="Salute.show(\'prev\')" style="color: #000;">' +

                $.text.prev + '</a>';

            var prevDis = '<span style="color: #AAA;">&larr; ' +

                $.text.prev + '</span>';

            var next = '<a href="javascript:;" onclick="Salute.show(\'next\')" style="color: #000;">' +

                $.text.next + '</a> &rarr;';

            var nextDis = '<span style="color: #AAA;">' +

                $.text.next + ' &rarr;</span>';

            var space = '&nbsp; &nbsp;';

			$.navBtn.innerHTML = prev + space + next;

            if (!i) $.navBtn.innerHTML = prevDis + space + next;

            if (i == ($.anchors.length - 1)) $.navBtn.innerHTML = prev + space + nextDis;

			

			$.event(document, 'keyup', function(e)

            {

                if($.temp['onload'] && $.blind.style.display != 'none')

                {

                    e = e || event;

                    if (e.keyCode == '37') $.show('prev');

                    if (e.keyCode == '39') $.show('next');

                    if (e.keyCode == '27') $.hide();

                }

            });

			

			function resize()

			{	

    			var w = document.body.clientWidth;

				var h = document.body.clientHeight;

				var aw = $.temp['img'].width;

				var ah = $.temp['img'].height;

    			

    			$.img.width = aw;

    			$.img.height = ah;

    			

    			function sw()

				{

					$.img.width = w - 50;

					$.img.height = ($.img.width * ah) / aw;

				}

				function sh()

				{

					$.img.height = h - 50;

					$.img.width = ($.img.height * aw) / ah;

				}

    			

    			if (($.img.width + 32) > w)

				{

					sw();

				}

				if (($.img.height + 32) > h)

				{

					sh();

				}

			}

			function topBlind()

			{



			}

			

			$.event(window, 'resize', function()

            {

                $.align($.container);

                topBlind();

                setTimeout(function()

				{

					resize();

               		$.align($.container);

				}, 1000);

            });

			

			$.img.style.visibility = 'hidden';

            $.container.style.display = '';

            $.align($.container);

            $.align($.container);

            $.blind.style.display = '';

            topBlind();

			

			$.temp['img'] = new Image;

            $.temp['img'].src = $.anchors[i].href;

            $.temp['img'].interval = setInterval(function()

            {

                if ($.temp['img'].complete)

                {

                    $.img.src = $.temp['img'].src;

                    $.img.style.visibility = '';

                    $.img.width = $.temp['img'].width;

                    $.img.height = $.temp['img'].height;

                    

                    if ($.autoScale)

                    {

                    	resize();

                   	}

                   	

                    $.fade($.img);

                    $.align($.container);

                    $.align($.container);

					

                    $.temp['onload'] = true;

                    clearInterval($.temp['img'].interval);

                }

            }, 10);

			

			return false;

		}

	};

	Salute.event(window, 'load', function()

	{

		Salute.init();

	});
