﻿// create element
var j = 1;
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 ShowModalboxCore(src, count, code,pos) {

       j = pos;     
       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)";
       div.appendChild(maindiv);



  

       //
       var mess = document.createElement("div");
       mess.id = 'mess';
       mess.style.position = 'absolute';
       mess.style.backgroundColor = '#fff';
       mess.style.left = (windowInnerSize().width - 250) / 2 + 'px';
       mess.style.top = (windowInnerSize().height - 150) / 2 + getScrollY() + 'px';
       div.appendChild(mess);



       //03.02.2011

       //    mess.style.border = 'solid 1px red';

       var imClose = document.createElement("img");
       imClose.src = '/img/close.jpg';
       imClose.id = "imgClose";
       imClose.style.zIndex = 2;
       imClose.style.cursor = "hand";
       imClose.style.cursor = "pointer";
       imClose.alt = "Закрыть";
       imClose.align = "right";
       imClose.style.float = "right";
       imClose.style.margin = "10px";
       imClose.style.verticalAlign = "top"
       imClose.onclick = function () { document.getElementById("mess2").style.display = "none"; document.getElementById("mess").style.display = "none"; document.getElementById("maindiv").style.display = "none"; };
       mess.appendChild(imClose);

       //end 03.02.2011
    


       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.id = "pic";
       im.style.zIndex = 1;
       im.style.cursor = "hand";
       im.style.cursor = "pointer";
       im.alt = "Закройте окно, нажав на него";
       // im.style.filter = "alpha(opacity=0)";
       // im.style.opacity = 0;

       im.onclick = function () { document.getElementById("mess2").style.display = "none"; document.getElementById("mess").style.display = "none"; document.getElementById("maindiv").style.display = "none"; };

       mess.appendChild(im);

       im.style.maxHeight = "400px";
       im.style.maxWidth = "600px";


     

       // 03.02.2011 доп. фото

       var mess2 = document.createElement("div");
       mess2.id = 'mess2';
       mess2.style.backgroundColor = '#fff';
       //  mess2.style.border = 'solid 1px red';
       mess.appendChild(mess2);

       if (count > 0) {
           var imSmall = document.createElement("img");
           imSmall.src = '/itemsimages/' + code + '.jpg';
           imSmall.alt = "Увеличить";
           imSmall.style.cursor = "hand";
           imSmall.style.cursor = "pointer";
           imSmall.style.border = 'solid 1px #EFEFEF';
           imSmall.style.margin = "10px";
           mess2.appendChild(imSmall);

           imSmall.onclick = function () { ShowModalboxCore(this.src, count, code,1); };
           imSmall.style.maxHeight = "50px";
           imSmall.style.maxWidth = "50px";

       }
       for (var i = 2; i <= count + 1; i++) {

           var imSmall = document.createElement("img");
           imSmall.src = '/itemsimages/' + code + '/' + code + '_' + i + '.jpg';
           imSmall.id = i;
           imSmall.alt = "Увеличить";
           imSmall.style.cursor = "hand";
           imSmall.style.cursor = "pointer";
           imSmall.style.border = 'solid 1px #EFEFEF';
           imSmall.style.margin = "10px";
           mess2.appendChild(imSmall);

           imSmall.onclick = function () {ShowModalboxCore(this.src, count, code,this.id); };
           imSmall.style.maxHeight = "50px";
           imSmall.style.maxWidth = "50px";
       }
       // end 03.02.2011

       // new 19.01.2012 стрелки
       if (count > 1) {
           var imprev = document.createElement("img");
           imprev.src = '/App_Themes/SkinFile/img/btnNewScrollerPreviousArrowPasive.png';
           imprev.id = "imprev";
           imprev.style.zIndex = 2;
           imprev.style.cursor = "hand";
           imprev.style.cursor = "pointer";
           imprev.alt = "Назад";
           imprev.align = "center";
           // imprev.style.float = "left";
           imprev.onclick = function () {
               j--;
               if (j < 1) j = a.length - 1;
               var tmpsrcleft = a[j];
               ShowModalboxCore(tmpsrcleft, count, code, j);
           };
           mess.appendChild(imprev);
           //
           var imnext = document.createElement("img");
           imnext.src = '/App_Themes/SkinFile/img/btnNewScrollerNextArrowPasive.png';
           imnext.id = "imnext";
           imnext.style.zIndex = 2;
           imnext.style.cursor = "hand";
           imnext.style.cursor = "pointer";
           imnext.alt = "Вперед";
           imnext.align = "center";
           //    imnext.style.float = "right";
           imnext.onclick = function () {


               j++;
               if (j >= a.length) j = 1;
               var tmpsrc = a[j];
               ShowModalboxCore(tmpsrc, count, code, j);

           };
           mess.appendChild(imnext);


       }
       //end 19.01.2012

       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);
       }

      
       }


       // view picture prepare
       function ShowModalbox(src, count, code, arr) {
           var tmpsrc = src;           
           var postmp;
           src = '/itemsimages/' + code + '.jpg';  
           a = new Array(count);            
           for (var j1 = 1; j1 <= count + 1; j1++) {
               a[j1] = src;
               var j2 = 1 + j1;
               src = '/itemsimages/' + code + '/' + code + '_' + j2 + '.jpg';
           }
           for (var j1 = 1; j1 <= count + 1; j1++) {            
               if (tmpsrc.indexOf(a[j1]) + 1)
              {
                  ShowModalboxCore(a[j1], count, code, j1)                          
               }
           }
       }

// 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;
    }
}
