function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function truebody(){
    return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

//запускаем тень
function oShowFuzz(){
    $("body").append("<div id='ofuzz'></div>");
    $("#ofuzz").css("height", $(document).height());
    $("#ofuzz").show();
}


//Выводим  прогресс картинку
function oShowProgres(startslash){
    //lj,fdkzt
    $("body").append("<div id='oprev_progres'><img src='"+startslash+"images/fotgal/bar.gif' id='oprev_progres_img' alt='Load...'/></div>");
    oprev_progres_img_width = 100; //Ширина прогресс картинки
    oprev_progres_img_height = 100;//Высота прогресс картинки

    $("#oprev_progres").css("margin-left", (-oprev_progres_img_width/2));
    $("#oprev_progres").css("margin-top", (-oprev_progres_img_height/2));
    $("#oprev_progres").css("top", (parseInt($(window).height()/2+getBodyScrollTop())));
    $("#oprev_progres").css("width", (oprev_progres_img_width));
    $('#oprev_progres').show();
}

function clc(imgname, imgwidth, imgheight, startslash){
    
    //запускаем тень
    oShowFuzz();
    
    //Выводим  прогресс картинку
    oShowProgres(startslash);

    //грузим превью картинки
    imgPreloader=new Image();
    imgPreloader.onload=function(){
        imgPreloader.onload=null;
        var pagesize=OgetPageSize();
        var x=pagesize[0];
        var y=pagesize[1];
        var imageWidth=imgPreloader.width;
        var imageHeight=imgPreloader.height;
        if(imageWidth>x){
            imageHeight=imageHeight*(x/imageWidth);
            imageWidth=x;
            if(imageHeight>y){
                imageWidth=imageWidth*(y/imageHeight);
                imageHeight=y;
            }
        }else if(imageHeight>y){
            imageWidth=imageWidth*(y/imageHeight);
            imageHeight=y;
            if(imageWidth>x){
                imageHeight=imageHeight*(x/imageWidth);
                imageWidth=x;
            }
        }

//       imgwidth = parseInt(imgwidth, 0);
//       imgheight = parseInt(imgheight, 0);

       imgwidth = imageWidth;
       imgheight = imageHeight;

        $("body").append("<div id='omsgbox'><img src='"+imgname+"' id='oimgprv' alt='prev...'/></div>");

        $("#omsgbox").css("margin-left", (-imgwidth/2));
        $("#omsgbox").css("margin-top", (-imgheight/2));
        $("#omsgbox").css("top", ($(window).height()/2+getBodyScrollTop()));
        $("#omsgbox").css("height", (imgheight));
        $("#omsgbox").css("width", (imgwidth));
        $("#oimgprv").css("width", (imgwidth));
        $("#oimgprv").css("height", (imgheight));
        $('#oimgprv').attr('src', imgname);
        $('#omsgbox').show();


        $("#oprev_progres").remove();
        $('#omsgbox').click(function(){
            $("#ofuzz").remove();
            $("#omsgbox").remove();
        });

    }
    imgPreloader.src=imgname;
    //грузим превью картинки

    return false;
}

function OgetPageSize(){
    var de=document.documentElement;
    var w=window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth;
    var h=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight;
    arrayPageSize=[w,h];
    return arrayPageSize;
}

$(window).bind("resize", function(){
    $("#omsgbox").css("top", (truebody().clientWidth/2));
    $("#ofuzz").css("height", $(window).height());
});

