function galleryMain(){
    galleryMain.list=new Array;
    for(var i=0; document.getElementsByTagName('img')[i]; i++){
        if(document.getElementsByTagName('img')[i].className=='gallery'){
            galleryMain.list.push(document.getElementsByTagName('img')[i]);
        }
    }
    for(var i=0; galleryMain.list[i]; i++){
        addEvent(galleryMain.list[i],"click",function(){galleryShow(this);});
        galleryMain.list[i].id=i+"g";
    }
}
function galleryShow(elemRef){
    var newDiv=document.createElementNS?document.createElementNS("http://www.w3.org/1999/xhtml","div"):document.createElement("div");
    document.body.appendChild(newDiv);
    newDiv.style.position="fixed";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv.style.position="absolute";}
    newDiv.style.left="0px";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv.style.left=scrolllpos()+"px";}
    newDiv.style.top="0px";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv.style.top=scrollpos()+"px";}
    newDiv.style.background="#000";
    newDiv.style.opacity="0.5";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv.style.filter="alpha(opacity = 50)";}
    newDiv.style.width="100%";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv.style.width==windowW()+"px";}
    newDiv.style.height="100%";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv.style.height=windowH()+"px";}
    newDiv.style.zIndex="100";
    newDiv.id='gallery';
    var newDiv2=document.createElementNS?document.createElementNS("http://www.w3.org/1999/xhtml","div"):document.createElement("div");
    document.body.appendChild(newDiv2);
    newDiv2.style.position="fixed";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){newDiv2.style.position="absolute";}
    newDiv2.style.background="#fff";
    newDiv2.style.zIndex="110";
    newDiv2.innerHTML="<img src='"+elemRef.src.replace("preview","display")+"' style='position: relative; left: 5px; top: 5px; z-index: 140' id='"+parseInt(elemRef.id)+"d' onclick='galleryroll()'><div style='position: relative; left: 5px; top: -15px; z-index: 140' nowrap><b>"+elemRef.alt+"</b></div><table style='position: relative; top: -20px' width='100%'><tr><td><div style='z-index: 140; white-space: no-wrap' onclick='galleryprev();'><b>&lt;&lt;&lt;</b></div></td><td align='right'><div style='z-index: 140; white-space: no-wrap' onclick='gallerynext();'><b>&gt;&gt;&gt;</b></div></td></tr></table>";
    galleryAdjust(newDiv2);
    addEvent(window,"resize",function(){galleryAdjust(newDiv2);});
    addEvent(document,"scroll",function(){galleryAdjust(newDiv2);});
    addEvent(newDiv2.firstChild,"load",function(){galleryAdjust(newDiv2);});
    addEvent(window,"resize",function(){imgLoadAdjust();});
    addEvent(document,"scroll",function(){imgLoadAdjust();});
    addEvent(newDiv2.firstChild,"load",function(){imgLoadAdjust();});
    addEvent(newDiv,"click",function(){clearInterval(galleryroll.timer);galleryroll.timer=null;galleryHide(this);});
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){addEvent(window,"scroll",function(){galleryAdjust(newDiv2);});}
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){addEvent(window,"scroll",function(){imgLoadAdjust();});}
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){addEvent(window,"scroll",function(){newDiv.style.left=scrolllpos()+"px";newDiv.style.top=scrollpos()+"px";newDiv.style.width==windowW()+"px";newDiv.style.height==windowH()+"px";});}
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){addEvent(window,"resize",function(){newDiv.style.left=scrolllpos()+"px";newDiv.style.top=scrollpos()+"px";newDiv.style.width==windowW()+"px";newDiv.style.height==windowH()+"px";});}
}
function galleryHide(elemRef){
    elemRef.parentNode.removeChild(elemRef.nextSibling);
    elemRef.parentNode.removeChild(elemRef);
    galleryHideLoad();
}
function galleryAdjust(elemRef){
    if(!elemRef.firstChild.owidth){elemRef.firstChild.owidth=elemRef.firstChild.width;}
    else{elemRef.firstChild.style.width=elemRef.firstChild.owidth+"px";}
    if(!elemRef.firstChild.oheight){elemRef.firstChild.oheight=elemRef.firstChild.height;}
    else{elemRef.firstChild.style.height=elemRef.firstChild.oheight+"px";}
    if(((windowW()/100)*90-10)<elemRef.firstChild.owidth){
        elemRef.firstChild.style.width=Math.round((windowW()/100)*90-10)+"px";
        elemRef.firstChild.style.height="auto";
    }
    if(((windowH()/100)*90-25)<elemRef.firstChild.oheight){
        elemRef.firstChild.style.height=Math.round((windowH()/100)*90-25)+"px";
        elemRef.firstChild.style.width="auto";
    }
    elemRef.style.width=(parseInt(elemRef.firstChild.width)||parseInt(elemRef.firstChild.width/(elemRef.firstChild.height/elemRef.firstChild.oheight)))+10+"px";
    elemRef.style.height=(parseInt(elemRef.firstChild.height)||parseInt(elemRef.firstChild.height/(elemRef.firstChild.width/elemRef.firstChild.owidth)))+25+"px";
    elemRef.style.left=Math.round((windowW()-parseInt(elemRef.style.width))/2)+"px";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){elemRef.style.left=scrolllpos()+Math.round((windowW()-parseInt(elemRef.style.width))/2)+"px";}
    elemRef.style.top=Math.round((windowH()-parseInt(elemRef.style.height))/2)+"px";
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){elemRef.style.top=scrollpos()+Math.round((windowH()-parseInt(elemRef.style.height))/2)+"px";}
    
}
function windowH(){
    var doc=document.documentElement;
    return self.innerHeight||(doc&&doc.clientHeight)||document.body.clientHeight;
}
function windowW(){
    var doc=document.documentElement;
    return self.innerWidth||(doc&&doc.clientWidth)||document.body.clientwidth;
}
function gallerynext(){
    currRef=document.getElementById("gallery").nextSibling;
    var id=parseInt(currRef.firstChild.id);
    if(id==galleryMain.list.length-1){id=0;}
    else{id++;}
    galleryShowLoad();
    var newImg=document.createElementNS?document.createElementNS("http://www.w3.org/1999/xhtml","img"):document.createElement("img");
    newImg.src=document.getElementById(id+"g").src.replace("preview","display");
    newImg.id='tmpimg';
    newImg.style.display='none';
    addEvent(newImg,"load",function(){gallerynextdo();});
    document.body.appendChild(newImg);
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){if(newImg.complete){gallerynextdo();}else{gallerynextcheck();}}
}
function gallerynextcheck(){
    if(document.getElementById("tmpimg").complete){
        clearInterval(gallerynextcheck.timer);gallerynextcheck.timer=null;
        gallerynextdo();
    }
    else{gallerynextcheck.timer=setInterval(function(){gallerynextcheck();},300);}
}
function gallerynextdo(){
    currRef=document.getElementById("gallery").nextSibling;
    var id=parseInt(currRef.firstChild.id);
    if(id==galleryMain.list.length-1){id=0;}
    else{id++;}
    elemRef=document.getElementById('tmpimg');
    elemRef.parentNode.removeChild(elemRef);
    galleryHide(currRef.previousSibling);
    galleryShow(document.getElementById(id+"g"));
    galleryHideLoad();
}
function galleryprev(){
    currRef=document.getElementById("gallery").nextSibling;
    var id=parseInt(currRef.firstChild.id);
    if(id==0){id=galleryMain.list.length-1;}
    else{id--;}
    galleryShowLoad();
    var newImg=document.createElementNS?document.createElementNS("http://www.w3.org/1999/xhtml","img"):document.createElement("img");
    newImg.src=document.getElementById(id+"g").src.replace("preview","display");
    newImg.id='tmpimg';
    newImg.style.display='none';
    addEvent(newImg,"load",function(){galleryprevdo();});
    document.body.appendChild(newImg);
    if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){if(newImg.complete){galleryprevdo();}else{galleryprevcheck();}}
}
function galleryprevcheck(){
    if(document.getElementById("tmpimg").complete){
        clearInterval(galleryprevcheck.timer);galleryprevcheck.timer=null;
        galleryprevdo();
    }
    else{gallerynextcheck.timer=setInterval(function(){gallerynextcheck();},300);}
}
function galleryprevdo(){
    currRef=document.getElementById("gallery").nextSibling;
    var id=parseInt(currRef.firstChild.id);
    if(id==0){id=galleryMain.list.length-1;}
    else{id--;}
    elemRef=document.getElementById('tmpimg');
    elemRef.parentNode.removeChild(elemRef);
    galleryHide(currRef.previousSibling);
    galleryShow(document.getElementById(id+"g"));
    galleryHideLoad();
}
function galleryShowLoad(){
    if(document.getElementById('loadimg')){}
    else{
        var loadImg=document.createElementNS?document.createElementNS("http://www.w3.org/1999/xhtml","img"):document.createElement("img");
        loadImg.src='http://oxigen.ru/img/loading_b.gif';
        loadImg.id='loadimg';
        loadImg.style.position="fixed";
        if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){loadImg.style.position="absolute";}
        loadImg.style.zIndex="150";
        loadImg.style.left="0px";
        if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){loadImg.style.left=scrolllpos()+"px";}
        loadImg.style.top="0px";
        if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){loadImg.style.top=scrollpos()+"px";}
        document.body.appendChild(loadImg);
        imgLoadAdjust();
    }
}
function imgLoadAdjust(){
    if(document.getElementById("loadimg")){
        loadImg=document.getElementById("loadimg");
        loadImg.style.left=Math.round((windowW()-119)/2)+"px";
        if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){loadImg.style.left=Math.round((windowW()-119)/2)+scrolllpos()+"px";}
        loadImg.style.top=Math.round((windowH()-34)/2)+"px";
        if(navigator.userAgent.match("MSIE 6") || navigator.userAgent.match("MSIE 7")){loadImg.style.top=Math.round((windowH()-34)/2)+scrollpos()+"px";}
    }
}
function galleryHideLoad(){
    elemRef=document.getElementById('loadimg');
    elemRef.parentNode.removeChild(elemRef);
}
function galleryroll(){
    if(galleryroll.timer){clearInterval(galleryroll.timer);galleryroll.timer=null;}
    else{galleryroll.timer=setInterval(function(){gallerynext();},3000);}
}

function scrollpos(){
    scrollpos.scroll=document.body.scrollTop;
    if (scrollpos.scroll==0){
        if(window.pageYOffset){
            scrollpos.scroll=window.pageYOffset;}
        else{
            scrollpos.scroll=(document.body.parentElement)?document.body.parentElement.scrollTop:0;
        }
    }
    return scrollpos.scroll;
}
function scrolllpos(){
    scrolllpos.scroll=document.body.scrollLeft;
    if (scrolllpos.scroll==0){
        if(window.pageXOffset){
            scrolllpos.scroll=window.pageXOffset;}
        else{
            scrolllpos.scroll=(document.body.parentElement)?document.body.parentElement.scrollLeft:0;
        }
    }
    return scrolllpos.scroll;
}
