// 02.03.2011
var widgetParams; 

function getNextHighestZindex(obj){  
    var highestIndex = 0;  
    var currentIndex = 0;  
    var elArray = Array();  
    if(obj){ elArray = obj.getElementsByTagName('*'); }else{ elArray = document.getElementsByTagName('*'); }  
    for(var i=0; i < elArray.length; i++){  
       if (elArray[i].currentStyle){  
          currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);  
       }else if(window.getComputedStyle){  
          currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i],null).getPropertyValue('z-index'));  
       }  
       if(!isNaN(currentIndex) && currentIndex > highestIndex){ highestIndex = currentIndex; }  
    }  
    return(highestIndex+1);  
}  
  
var swf3d = function() {      
    var div_cont_3d= document.createElement("div"); 
    var div_cont_3d_in= document.createElement("div");  
    var div_border = document.createElement("div");
    var background3db = document.createElement("div");
    var widget3db = document.createElement("div");  
    var cl3d= document.createElement("div");    
    var cl3dbg= document.createElement("div");
    var btn=1;
    var wdg3dBorderColor = "#FFFFFF";
    var wdg3dBorderWidth = "20px";
    var wdg3dBackgroundColor = "#000000";
    var wdg3dBackgroundOpacity = "50%"; 
    var wdg3dBorderOpacity = "70%";
    var wdg3dButtonURL = "http://3d-bank.com/tpl_img/h2o_button.gif";
    var wdg3dCloseButton = "http://3d-bank.com/tpl_img/vidget_close.gif";
    var wdg3dIframe = 0;
    var clbtn3db=1;
    
    function getElementsByClass (classList, node) {
        var node = node || document;
        if(typeof(document.getElementsByClassName) == "function") { 
            return node.getElementsByClassName(classList);
        } else { 
            var list = node.getElementsByTagName('DIV');
            var length = list.length;
            var classArray = classList.split(/\s+/);
            var classes = classArray.length;
            var result =new Array(); 
            for(i = 0; i < length; i++) {
                for(j = 0; j < classes; j++)  {
                    if(list[i].className.search('\\b' + classArray[j] + '\\b') != -1) {
                        result.push(list[i]);
                        break;
                    }
                }
            }   
        
            return result;
        }
    }        

function crswf(id, objId) {
var sc=id;
if(btn) {
sc=widget3db;
} 

if(typeof(objId) != "undefined") { var prm = objId; } else { prm=id.getAttribute('media'); }
    if(prm) {
        var s="http://media.3d-bank.com/swf/viewer.swf?media="+prm+"&rr="+Math.random()+"&startb=1";
        var w=640;
        var h=480;
        var u = navigator.userAgent.toLowerCase();
        var webkit = /webkit/.test(u) ? parseFloat(u.replace(/^.*webkit\/(\d+(\.\d+)?).*$/, "$1")) : false;
        if(wdg3dIframe)
        {
        sc.innerHTML='<iframe src="'+s+'" width="'+w+'" height="'+h+'" style="border: 0;"></iframe>';
        } else {
            if(!webkit)
            {
                sc.innerHTML='<object width="'+w+'" height="'+h+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="m'+prm+'"><param name="movie" value="'+s+'"></param><param name="allowscriptaccess" value="always"></param><embed src="'+s+'" type="application/x-shockwave-flash" allowscriptaccess="always" width="'+w+'" height="'+h+'" type="application/x-shockwave-flash"></embed></object>';
            } else {
                sc.innerHTML='<embed src="'+s+'" type="application/x-shockwave-flash" allowscriptaccess="always" width="'+w+'" height="'+h+'" type="application/x-shockwave-flash"></embed>';
            }
        
        } 
    }
        
}        

function cl(event) {
        div_cont_3d.style.display = "none";
        div_cont_3d_in.style.display = "none";
        background3db.style.display="none";
        cl3d.style.display="none";
        cl3dbg.style.display="none";
        div_border.style.display="none";
        widget3db.style.display="none";
}   

function generic()
{
    wdg3dZindex = getNextHighestZindex();
    if(typeof(widgetParams) != 'undefined' && widgetParams != null) 
    {
    if(widgetParams.bordercolor == "auto" || typeof(widgetParams.bordercolor)=='undefined' ) wdg3dBorderColor = "#FFFFFF"; else wdg3dBorderColor = widgetParams.bordercolor;
    if(widgetParams.borderwidth == "auto" || typeof(widgetParams.borderwidth)=='undefined' ) wdg3dBorderWidth = "5px"; else wdg3dBorderWidth = widgetParams.borderwidth;
    if(widgetParams.backgroundcolor == "auto" || typeof(widgetParams.backgroundcolor)=='undefined' ) wdg3dBackgroundColor = "#000000"; else wdg3dBackgroundColor = widgetParams.backgroundcolor;
    /* if(widgetParams.backgroundurl == "auto") wdg3dBackgroundURL = "http://3d-bank.com/tpl_img/widget_shadow_border.png"; else wdg3dBackgroundURL = widgetParams.backgroundurl;*/
    if(widgetParams.backgroundopacity == "none" || typeof(widgetParams.backgroundopacity)=='undefined') wdg3dBackgroundOpacity = "50%"; else wdg3dBackgroundOpacity = widgetParams.backgroundopacity;
    if(widgetParams.borderopacity == "none" || typeof(widgetParams.borderopacity)=='undefined') wdg3dBorderOpacity = "70%"; else wdg3dBorderOpacity = widgetParams.borderopacity;
    if(typeof(widgetParams.buttonurl) =='undefined' || widgetParams.buttonurl == "auto") wdg3dButtonURL = "http://3d-bank.com/tpl_img/h2o_button.gif"; else wdg3dButtonURL=widgetParams.buttonurl;
    if(typeof(widgetParams.closebuttonurl)=='undefined' || widgetParams.closebuttonurl == "auto") wdg3dCloseButton = "http://3d-bank.com/tpl_img/vidget_close.gif"; else wdg3dCloseButton = widgetParams.closebuttonurl;
    if(typeof(widgetParams.iframe) == 'undefined' || widgetParams.iframe == "auto" || widgetParams.iframe == 0) wdg3dIframe = 0; else wdg3dIframe = 1;
    if(typeof(widgetParams.closebuttonurl)=='undefined' || widgetParams.closebuttontext == "none") wdg3dCloseButtonText = "закрыть"; wdg3dCloseButtonText = widgetParams.closebuttontext;
    
    }
    if(wdg3dCloseButton == "none") var clbtn3db = 0; else var clbtn3db = 1 ;
    
    wdg3dNewBorderWidth = parseInt(wdg3dBorderWidth.substring(0, wdg3dBorderWidth.length-2))*2;
    wdg3dBackgroundOpacity = parseInt(wdg3dBackgroundOpacity.substring(0,wdg3dBackgroundOpacity.length-2));
    wdg3dBorderOpacity = parseInt(wdg3dBorderOpacity.substring(0,wdg3dBorderOpacity.length-2));
    if(typeof(widgetParams) != 'undefined' && widgetParams.borderwidth != "auto" && typeof(widgetParams.borderwidth)!='undefined') wdg3dBorderWidth = widgetParams.borderwidth;
    
    with(background3db.style)
    {
            position="fixed";
            width="100%";
            height="100%";
            top="0%";
            left="0%";
            backgroundColor=wdg3dBackgroundColor;
            opacity=wdg3dBackgroundOpacity/10;
            filter="alpha(opacity=" +wdg3dBackgroundOpacity *10 + ")";
            zIndex=wdg3dZindex;
            display="none";
    }
    background3db.setAttribute("id", "bg_3dbank");

    with(div_cont_3d.style){
            position="fixed";
            width="640px";
            height="480px";
            left="50%";
            top="50%";
            marginLeft="-320px";
            marginTop="-240px";
            zIndex=wdg3dZindex;
            display="none";
    } 

if(clbtn3db)
{
    var img3d = new Image();
    var imgWidth;
    var imgHeight;
    img3d.onload = function(){
        if(wdg3dNewBorderWidth/2 > this.height && wdg3dNewBorderWidth/2 > this.width)
        {
            imgWidth = wdg3dNewBorderWidth/2;
            imgHeight = wdg3dNewBorderWidth/2;
            pOffset = wdg3dNewBorderWidth/2-10;
        } else {
            imgWidth = this.width;
            imgHeight = this.height;
            pOffset = imgHeight - 10;
        }
        
        if(imgWidth <= 20) {imgWidth = imgWidth+10;}
        if(imgHeight <= 20) {pOffset = imgHeight; imgHeight = imgHeight+10; }
        pOffset = 240+pOffset;
        with(div_border.style){
                position="fixed";
                width=640+wdg3dNewBorderWidth+imgWidth+"px";
                height=480+wdg3dNewBorderWidth+imgHeight+"px";
                left="50%";
                top="50%";
                marginTop="-"+pOffset+"px";
                marginLeft="-320px"; 
                display="none";
                zIndex=wdg3dZindex;
        }
        with(cl3d.style){
                position="relative";
                cursor="pointer";
                width=imgWidth+"px";
                height=imgHeight+"px";
                backgroundImage="url("+wdg3dCloseButton+")"; 
                top="0";
                left=640+wdg3dNewBorderWidth+"px";
                marginTop="-"+imgHeight+"px";
                backgroundRepeat="no-repeat";
                backgroundPosition="center";
                display="none";
                zIndex=wdg3dZindex;
            
        }
        with(cl3dbg.style) {
                position="relative";
                width=imgWidth+"px";
                height=imgHeight+"px";
                top="0";
                left=640+wdg3dNewBorderWidth+"px";
                /* backgroundColor=wdg3dBorderColor; --todo сделать параметром */
                opacity=wdg3dBorderOpacity/10;
                filter="alpha(opacity=" +wdg3dBorderOpacity *10 + ")";
                display="none";
                cursor="pointer";
                zIndex=wdg3dZindex;
        
        }   
        cl3dbg.setAttribute("id", "close_bg_3dbank");  
        if(typeof(wdg3dCloseButtonText) != 'undefined') cl3d.title=wdg3dCloseButtonText;        
    };
    img3d.src = wdg3dCloseButton;
} 

background3db.onclick = function(event) {cl(event); }
cl3d.onclick = function(event) {cl(event); }

with(div_cont_3d_in.style){
            position="fixed";
            width=640+wdg3dNewBorderWidth+"px";
            height=480+wdg3dNewBorderWidth+"px";
            left="50%";
            top="50%";
            backgroundColor=wdg3dBorderColor;
            marginTop="-230px"; 
            marginLeft="-320px"; 
            opacity=wdg3dBorderOpacity/10;
            filter="alpha(opacity=" +wdg3dBorderOpacity *10 + ")";
            display="none";
            zIndex=wdg3dZindex;
      
      } 
div_cont_3d_in.setAttribute("id", "border_3dbank");      
with(widget3db.style){
            position="fixed";
            width=640+wdg3dNewBorderWidth+"px";
            height=480+wdg3dNewBorderWidth+"px";
            padding=wdg3dBorderWidth;
            left="50%";
            top="50%";
            marginTop="-230px"; 
            marginLeft="-320px"; 
            display="none";
            zIndex=wdg3dZindex;
        } 
widget3db.setAttribute("id", "widget_3dbank");
      
document.body.appendChild(background3db);
document.body.appendChild(div_border);
div_border.appendChild(cl3dbg);       
div_border.appendChild(cl3d);
div_border.appendChild(div_cont_3d_in);
document.body.appendChild(div_cont_3d); 
document.body.appendChild(widget3db);
      

var imgElements = document.getElementsByTagName("img");
var imgPath;
for(j =0; j < imgElements.length; j++)
{
    imgPath = imgElements[j].src;
    var image3dbank = /media\.3d-bank\.com/.test(imgPath);
    if(image3dbank)
    {
        var matches = /.*\/(\d+_\d+).*\.jpg/i.exec(imgPath);
        if(typeof(matches[1]) != "undefined")
        {
            imgElements[j].style.cursor="pointer";
            imgElements[j].onclick = function(event) {
                crswf(this, matches[1]);
                div_cont_3d.style.display = "block";
                div_cont_3d_in.style.display = "block";
                background3db.style.display="block";
                cl3d.style.display="block";
                cl3dbg.style.display="block";
                div_border.style.display="block";
                widget3db.style.display="block";
            }
        }
    }
}

var elements = getElementsByClass('media');
if(wdg3dButtonURL != "none")
{
    var btn3dImg = new Image();
    btn3dImg.onload = function()
    {
        for(i =0; i < elements.length; i++)
        {
            with(elements[i].style)
            {
                position="relative";
                backgroundImage="url("+wdg3dButtonURL+")";
                width=this.width+"px";
                height=this.height+"px";
                cursor="pointer";
            }
        }
    };
    btn3dImg.src = wdg3dButtonURL;
}
    
for(i = 0; i < elements.length; i++) {
if(btn) {
elements[i].onclick = function(event) {
    crswf(this);
    div_cont_3d.style.display = "block";
    div_cont_3d_in.style.display = "block";
    background3db.style.display="block";
    cl3d.style.display="block";
    cl3dbg.style.display="block";
    div_border.style.display="block";
    widget3db.style.display="block";
    }

} else {
crswf(elements[i]);
 }
}

};
function bindReady(handler){
 
    var called = false
 
    function ready() { 
        if (called) return
        called = true
        handler()
    }
 
    if ( document.addEventListener ) { 
        document.addEventListener( "DOMContentLoaded", function(){
            ready()
        }, false )
    } else if ( document.attachEvent ) { 
 
        // (3.1)
        if ( document.documentElement.doScroll && window == window.top ) {
            function tryScroll(){
                if (called) return
                if (!document.body) return
                try {
                    document.documentElement.doScroll("left")
                    ready()
                } catch(e) {
                    setTimeout(tryScroll, 0)
                }
            }
            tryScroll()
        }
 
        
        document.attachEvent("onreadystatechange", function(){
 
            if ( document.readyState === "complete" ) {
                ready()
            }
        })
    }
 
    
    if (window.addEventListener)
        window.addEventListener('load', ready, false)
    else if (window.attachEvent)
        window.attachEvent('onload', ready)

}

var main=function() {
bindReady(function() {
generic();

  })
  
 }();
}();
