﻿// create element
function elem(name, attrs, style) {
    var e = document.createElement(name);
    if (attrs) {
        for (key in attrs) {
            if (key == 'id') {
                e.id = attrs[key];
            } else {
                e.setAttribute(key, attrs[key]);
            }
        }
    }
    if (style) {
        for (key in style) {
            e.style[key] = style[key];
        }
    }   
    return e;
}

// set opacity 
function setOpacity(opacity) {
     document.getElementById("pic").style.opacity = opacity/100;
     document.getElementById("pic").style.filter = 'alpha(opacity=' + opacity + ')';
     return;
}

//browser window position 
function windowInnerSize() {
    var windowWidth;
    var windowHeight;
        
    if (typeof(window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }    
    return { width: windowWidth, height: windowHeight };
}

// view picture
function ShowModalbox(src) {   

     while(document.getElementById('main')==null)   
     {
        var parent = document.body;           
        parent.appendChild(elem('div',
        {'id': 'main'}       
        ));
     }
    var div = document.getElementById('main');
    while( div.hasChildNodes() ){
    div.removeChild(div.firstChild);
    div.removeChild(div.lastChild);
    }
    var maindiv = document.createElement("div" );
    maindiv.id = 'maindiv'; 
    maindiv.style.position = 'absolute';
    maindiv.style.color = '#fff';
    maindiv.style.backgroundColor = '#000';
    maindiv.style.left = '0px';
    maindiv.style.top = '0px';    
    maindiv.style.width = document.body.scrollWidth+'px';
    maindiv.style.height = document.body.scrollHeight + 'px';
    
	maindiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)"; /* для IE5.5—7 */
	maindiv.style.opacity = 0.5;
	maindiv.style.filter = "alpha(opacity=50)";
	
//    maindiv.style.filter = "alpha(opacity=50)";
//    maindiv.style.opacity = 0.5;    
    div.appendChild(maindiv);
      
    var mess = document.createElement("div" );
    mess.id = 'mess';    
    mess.title = "Закройте окно, нажав на него"; 
    mess.style.position = 'absolute';
    mess.style.backgroundColor = '#000';   
    mess.style.left = (windowInnerSize().width-250)/2+'px'; 
    mess.style.top = (windowInnerSize().height-150)/2+getScrollY()+'px'; 
    mess.style.width = '250px';
    mess.style.height = '150px';
    mess.onclick = function(){document.getElementById("mess").style.display = "none";document.getElementById("maindiv").style.display = "none";};      
    div.appendChild(mess);         

    var im = document.createElement("img" );    
    if    (navigator.appName != "Microsoft Internet Explorer" || (navigator.appName != "Microsoft Internet Explorer" && parseFloat(navigator.appVersion) > 4.0))    
    {
        im.onload =  function(){getW();};
    }    
    im.src = src; 
    im.onerror = function(){document.getElementById("pic").src = "/itemsimages/noimage.jpg";};      
    im.id = "pic";    
    im.style.cursor = "hand";    
    im.style.cursor = "pointer";
    im.style.filter = "alpha(opacity=0)";      
    im.style.opacity = 0;
    mess.appendChild(im);
  
  while(document.getElementById('pic').height > 600)   
    {     
        document.getElementById('pic').height = document.getElementById('pic').height / 2;
        document.getElementById('pic').width = document.getElementById('pic').width / 2;
    }
 
   
          
    if    (navigator.appName == "Microsoft Internet Explorer" &&
    parseFloat(navigator.appVersion) <= 4.0) 
    {
        mess.style.width = im.width;
        mess.style.height = im.height;
      
        mess.style.position = 'absolute';
        mess.style.left = (windowInnerSize().width-im.width)/2+'px'; 
        mess.style.top = (windowInnerSize().height-im.height)/2+getScrollY()+'px'; 
          for (var i=0;i<100;i++)
		    setTimeout('setOpacity('+i+')',3*i);  		        
    }
    	
}
// scroll Y position
function getScrollY() 
{
    scrollY = 0;    
    if (typeof window.pageYOffset == "number") {
        scrollY = window.pageYOffset; 
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;       
    }  else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop;         
    } else if (window.scrollY) {
        scrollY = window.scrollY;        
    }
    return scrollY;
}
//size and position 
function getW() 
{

    var browser_name = navigator.appName;

    if    (navigator.appName != "Microsoft Internet Explorer" || (navigator.appName != "Microsoft Internet Explorer" && parseFloat(navigator.appVersion) > 4.0))   
    {
        var picture = document.getElementById("pic");
        var picW = picture.width;
        var picH = picture.height;   

        var message = document.getElementById("mess");
        message.style.width = "";    
        message.style.height = "";
        message.style.width = picW;
        message.style.height = picH;
        message.style.position = 'absolute';
        message.style.left = (windowInnerSize().width-picW)/2+'px'; 
        message.style.top = (windowInnerSize().height-picH)/2+getScrollY()+'px'; 
          for (var i=0;i<100;i++)
		        setTimeout('setOpacity('+i+')',3*i);  
		        return; 
    }
}