
  /*
      © Алексей Забродин 2009 (drvhart@ya.ru)
      Скрипт для просмотра изображений Salute v2.0
  */
  
  var salute = {
      
      loader: '/core/js/salute/loader.gif', // Картинка прелоудера
      delay: 1500, // Задержка кнопок
      txtClose: 'Закрыть',
      txtLoading: 'Загрузка изображения',
      txtPre: 'Предыдущая',
      txtNext: 'Следующая',
      urls: new Array(),
      titles: new Array(),
      total: 0,
      blind: null,
      container: null,
      navBtnsDiv: null,
      closeBtn: null,
      table: null,
      imgPlace: null,
      imgTitleDiv: null,
      current: null,
      imgCache: null,
      interval: null,
      interval_2: null,
      loadStatus: null,
      eventCache: new Array(),
      
      init: function() // Инициализация
      {
         var $ = salute;
         
         // Индексируем картинки
         var a = document.getElementsByTagName('a');
         for(var i = 0; i < a.length; i++)
         {
         	 if(a[i].href && a[i].rel == 'salute')
             {
             	 $.urls[$.total] = a[i].href;
			     $.titles[$.total] = a[i].title;
			     
		         a[i].setAttribute('name', 'salute-' + $.total);
             	 a[i].onclick = $.loadImg;
             	 
             	 $.total++;
          	 }
     	 }
     	 
     	 // Формируем html элементы
     	 var divBlind = document.createElement('div');
     	 divBlind.setAttribute('id', 'saluteBlindDiv');
         $.blind = document.body.appendChild(divBlind);
         $.blind.onclick = $.close;
         
         var divContainer = document.createElement('div');
         divContainer.setAttribute('id', 'saluteContainerDiv');
         $.container = document.body.appendChild(divContainer);
         var html = '<div id="saluteNavBtnsDiv"></div>';
         html += '<div id="saluteCloseBtn"><a href="javascript:void(0)" onclick="salute.closeBtns();salute.close();">' + $.txtClose + '</a></div>';
         html += '<table id="saluteTable"><tr><td><img src="' + $.loader + '" alt="' + $.txtLoading + '" id="saluteImgPlace" /><div id="saluteImgTitle"></div></td></tr></table>';
         $.container.innerHTML = html;
         
         $.navBtnsDiv = document.getElementById('saluteNavBtnsDiv');
         $.closeBtn = document.getElementById('saluteCloseBtn');
         $.table = document.getElementById('saluteTable');
         $.imgPlace = document.getElementById('saluteImgPlace');
         $.imgTitleDiv = document.getElementById('saluteImgTitle');
         
         $.close();
         $.closeBtns();
         
         $.navBtnsDiv.onmouseover = displayBtn;
	 	 $.closeBtn.onmouseover = displayBtn;
         
         $.imgPlace.onmousemove = function()
         {
             if($.loadStatus)
             {
                 displayBtn(null, true);
             }
         }
         $.imgPlace.onmouseout = function()
         {
	         $.closeBtns();
         }
         
         function displayBtn(e, s)
         {	
             $.closeBtn.style.display = "block";
             
			 if($.total > 1)
             {
                 $.navBtnsDiv.style.display = "block";
                 clearTimeout($.interval_2);
			   
                 if(s)
                 {
                     $.interval_2 = setTimeout(function()
                     {
                        $.closeBtns();
                     }, $.delay);
                 }
             }
	     }
      },
      novigation: function(mode)
      {
      	 var $ = salute;
      	 
      	 $.close();
      	 
      	 if(mode)
         {
             $.current++;
         }
		 else
		 {
             $.current--;
         }
		  
         $.loadImg(null, $.current + 1);
      },
      loadImg: function(e, num)
      {
      	 var $ = salute;
      	 
      	 if(num)
      	 {
      	 	 $.current = num - 1;
      	 }
      	 else
      	 {
      	 	 $.current = Number(this.name.split('salute-')[1]);
      	 }
      	 
      	 $.imgCache = new Image;
	 	 $.imgCache.src = $.urls[$.current];
	 	 $.imgCache.alt = $.titles[$.current];
	 	 
	 	 // Выводим кнопки
	 	 if(!$.current)
	     {
		     $.navBtnsDiv.innerHTML = '<span style="color: #AAA;">&larr; ' + $.txtPre + '</span> &nbsp; &nbsp; <a href="javascript:void(0)" onClick="salute.novigation(1);">' + $.txtNext + '</a> &rarr;';
	     }
	     if($.current == $.total - 1)
	     {
		     $.navBtnsDiv.innerHTML = '&larr; <a href="javascript:void(0)" onClick="salute.novigation();">' + $.txtPre + '</a> &nbsp; &nbsp; <span style="color: #AAA;">' + $.txtNext + ' &rarr;</span>';
	     }
	     if($.current && $.current != $.total - 1)
	     {
		     $.navBtnsDiv.innerHTML = '&larr; <a href="javascript:void(0)" onClick="salute.novigation();">' + $.txtPre + '</a> &nbsp; &nbsp; <a href="javascript:void(0)" onClick="salute.novigation(1);">' + $.txtNext + '</a> &rarr;';
	     }
      	 
      	 $.blind.style.display = 'block';
      	 $.container.style.display = 'block';
      	 $.setCenter();
      	 
      	 $.eventCache['scroll'] = window.onscroll;
      	 $.eventCache['resize'] = window.onresize;
      	 $.eventCache['keyup'] = document.onkeyup;
      	 
      	 onscroll = $.setCenter;
      	 onresize = $.setCenter;
      	 
      	 document.onkeyup = function(e)
	     {
             e = e || window.event;
		   	  
		     if(e.keyCode == "37" && $.current)
		     {
	             $.novigation();
		     }
		     if(e.keyCode == "39" && $.current < $.total - 1)
		     {
		         $.novigation(1);
		     }
	     }
      	 
      	 $.testLoadImg();
      	 return false;
      },
      testLoadImg: function() // Проверяем загрузку изображения
      {
         var $ = salute;
         
         if($.imgCache.complete)
	     {
             clearInterval($.interval);
             
		     $.setOpacity(0, $.imgPlace);
		     $.imgPlace.src = $.imgCache.src;
		     $.imgPlace.alt = '';
		     
		     $.table.style.width = $.imgCache.width + 'px';
		     $.table.style.height = $.imgCache.height + 'px';
		     
		     for(var i = 0; i < 11; i++)
		     {
		     	 setTimeout($.setOpacity(i, $.imgPlace, true), 30*i);
		     	 if(i == 10)
   	             {
   	             	 if($.imgCache.alt)
		             {
		     	         $.imgTitleDiv.innerHTML = $.imgCache.alt;
		     	         $.imgTitleDiv.style.display = 'block';
		     	         $.setOpacity(0, $.imgTitleDiv);
		     	         
		     	         setTimeout(function()
		     	         {
		     	             for(var n = 0; n < 11; n++)
		                     {
		                 	     setTimeout($.setOpacity(n, $.imgTitleDiv, true), 30*n);
	                 	     }
	                 	 }, 350);
		             }
				     $.setCenter();
			     }
	     	 }
		     
		     $.loadStatus = true;
		     $.setCenter();
	     }
		 else if($.interval)
         {
   	         clearInterval($.interval);
             $.interval = setInterval('salute.testLoadImg();', 100);
         }
		 else
		 {
             $.interval = setInterval('salute.testLoadImg();', 100);
         }
         
      },
      close: function() // Закрываем блоки
      {
      	 var $ = salute;
      	 
      	 $.blind.style.display = 'none';
      	 $.container.style.display = 'none';
      	 $.imgPlace.src = $.loader;
      	 $.imgTitleDiv.style.display = 'none';
      	 $.imgTitleDiv.innerHTML = '';
      	 
      	 if($.loadStatus)
      	 {
             window.onscroll = $.eventCache['scroll'];
             window.onresize = $.eventCache['resize'];
   	         document.onkeyup = $.eventCache['keyup'];
   	     }
   	     
   	     $.loadStatus = false;
      },
      closeBtns: function() // Закрываем кнопки
      {
      	 var $ = salute;
      	 $.navBtnsDiv.style.display = 'none';
      	 $.closeBtn.style.display = 'none';
      },
      setOpacity: function(value, obj, r) // Устанавливает прозрачность
      {
      	 if(r)
      	 {
      	     return function()
             {
                obj.style.opacity = value/10;
                obj.style.filter = 'alpha(opacity=' + value*10 + ')';
                if(value == 10)
                {
                	obj.style.filter = null;
                }
             }
         }
         else
         {
         	 obj.style.opacity = value/10;
             obj.style.filter = 'alpha(opacity=' + value*10 + ')';
         }
      },
      setCenter: function() // Центруем блоки
      {
    	 var $ = salute;
		   
         if(document.body.scrollTop)
         {
             var scrlTop = document.body.scrollTop;
         }
		 else
		 {
             var scrlTop = document.documentElement.scrollTop;
         }
        
         $.blind.style.top = scrlTop + 'px';
		
         var top = $.blind.clientHeight / 2 - $.container.clientHeight / 2;
         $.container.style.top = scrlTop + top + 'px';
   	  
         var left = $.blind.clientWidth / 2 - $.container.clientWidth / 2;
         $.container.style.left = left + 'px';
      }
  }
  if(typeof document.attachEvent != 'undefined')
  {
      attachEvent('onload', salute.init);
  }
  else
  {
      addEventListener('load', salute.init, false);
  }