function imgFit(img, maxImgWidth)
{
   if (typeof img.naturalWidth == 'undefined') {
      img.naturalHeight = img.height;
      img.naturalWidth = img.width;
   }
   if (img.width > maxImgWidth) {
      img.height = Math.round(((maxImgWidth)/img.width)*img.height);
      img.width = maxImgWidth;
      img.title = 'Нажмите для просмотра в полном размере';
      img.style.cursor = 'move';
   } else if (img.width == maxImgWidth && img.width < img.naturalWidth) {
      img.height = img.naturalHeight;
      img.width = img.naturalWidth;
      img.title = 'Нажмите для уменьшения';
   }
}

function imgFitW(img, ImgWidth, ImgHeight, maxImgWidth)
{
   if (typeof img.naturalWidth == 'undefined') {
      img.naturalHeight = ImgHeight;
      img.naturalWidth = ImgWidth;
   }
   if (img.width != maxImgWidth && img.width > maxImgWidth) {
      img.height = Math.round(((maxImgWidth)/img.width)*img.height);
      img.width = maxImgWidth;
      img.title = 'Нажмите для просмотра в полном размере';
      img.style.cursor = 'move';
   } else if (img.width == maxImgWidth) {
      img.height = img.naturalHeight;
      img.width = img.naturalWidth;
      img.title = 'Нажмите для уменьшения';
   }
}

function imgFitWi(img, ImgWidth, ImgHeight, maxImgWidth)
{
   if (typeof img.naturalWidth == 'undefined') {
      img.naturalHeight = ImgHeight;
      img.naturalWidth = ImgWidth;
   }
	if(ImgWidth > maxImgWidth)
{
   img.title = 'Нажмите для просмотра в полном размере';
   img.style.cursor = 'move';

   img.height = Math.round(((maxImgWidth)/ImgWidth)*ImgHeight);
   img.width = maxImgWidth;
}
}


function imgFitWH(img, ImgWidth, ImgHeight, maxImgWidth, maxImgHeight)
{
    if ( (maxImgWidth/ImgWidth) > (maxImgHeight/ImgHeight) ) {
        img.new_height = maxImgHeight;
        img.new_width = Math.round(((maxImgHeight)/ImgHeight)*ImgWidth);
    }
    else {
        img.new_height = Math.round(((maxImgWidth)/ImgWidth)*ImgHeight);
        img.new_width = maxImgWidth;
    }
    if ( img.width > maxImgWidth || img.height > maxImgHeight ) {
        img.height = img.new_height;
        img.width = img.new_width;
        img.title = 'Нажмите для просмотра в полном размере';
        img.style.cursor = 'move';
    }
    else if ( (img.width == maxImgWidth && img.width < ImgWidth) || (img.height == maxImgHeight && img.height < ImgHeight) ) {
        img.height = ImgHeight;
        img.width = ImgWidth;
        img.title = 'Нажмите для уменьшения';
    }
}


function imgFitWHi(img, ImgWidth, ImgHeight, maxImgWidth, maxImgHeight)
{
    if ( (maxImgWidth/ImgWidth) > (maxImgHeight/ImgHeight) ) {
        img.new_height = maxImgHeight;
        img.new_width = Math.round(((maxImgHeight)/ImgHeight)*ImgWidth);
    }
    else {
        img.new_height = Math.round(((maxImgWidth)/ImgWidth)*ImgHeight);
        img.new_width = maxImgWidth;
    }
    img.height = img.new_height;
    img.width = img.new_width;
    img.title = 'Нажмите для просмотра в полном размере';
    img.style.cursor = 'move';
}


function imgNoResize(container, imgloading, imgsrc, imgsrcalt)
{
    if(!container.tempLoadImage) {
        container.tempLoadImage             = new Image();
        container.tempLoadImage.onerror     = changeSrcEr;
        container.tempLoadImage.onload      = changeSrcOK;
        container.tempLoadImage.parentImage = container;
        container.tempLoadImage.src         = imgsrc;
        container.tempLoadImage.altsrc      = imgsrcalt;
    }
}

function changeSrcOK() {
    this.onload             = null;
    this.parentImage.onload = null;
    this.parentImage.src    = this.src;
    if (this.parentImage.complete) {
        this.parentImage.width  = this.width;
        this.parentImage.height = this.height;
    }
}

function changeSrcEr() {
    var imgTmp = new Image();
    imgTmp.src = this.altsrc;
    this.onerror            = null;
    this.parentImage.onload = null;
    this.parentImage.src    = imgTmp.src;
    if (this.parentImage.complete) {
        this.parentImage.width  = imgTmp.width;
        this.parentImage.height = imgTmp.height;
    }
    delete imgTmp, tmp;
}

function imgNoResizeHAvatar(container, imgloading, imgsrc, imgsrcalt)
{
    if(!container.tempLoadImage) {
        container.tempLoadImage             = new Image();
        container.tempLoadImage.onerror     = changeSrcErHAvatar;
        container.tempLoadImage.onload      = changeSrcOKHAvatar;
        container.tempLoadImage.parentImage = container;
        container.tempLoadImage.src         = imgsrc;
        container.tempLoadImage.altsrc      = imgsrcalt;
    }
}

function changeSrcOKHAvatar() {
    this.onload             = null;
    this.parentImage.onload = null;
    this.parentImage.src    = this.src;
    if (this.parentImage.complete) {
        this.parentImage.width  = this.width;
        this.parentImage.height = this.height;
    }
    this.parentImage.parentNode.width = this.width + 'px';
}

function changeSrcErHAvatar() {
    var imgTmp = new Image();
    imgTmp.src = this.altsrc;
    this.onerror            = null;
    this.parentImage.onload = null;
    this.parentImage.src    = imgTmp.src;
    if (this.parentImage.complete) {
        this.parentImage.width  = imgTmp.width;
        this.parentImage.height = imgTmp.height;
    }
    this.parentImage.parentNode.width = this.width + 'px';
    delete imgTmp;
}

function imgErr(img, imgsrc, altsrc) {
    var imgTmp = new Image();
    imgTmp.src = altsrc;
    img.onerror             = null;
    img.src                 = imgTmp.src;
    if (img.complete) {
        img.width               = imgTmp.width;
        img.height              = imgTmp.height;
    }
    var imgref              = document.createElement('a');
    imgref.href             = imgsrc;
    img.parentNode.appendChild(imgref);
    imgref.appendChild(img);
}


function imgLoadWH(container, imgloading, imgsrc, imgsrcalt, imgwidth, imgheight)
{
    if(!container.tempLoadImage) {
        container.tempLoadImage             = new Image();
        container.tempLoadImage.onload      = function() { changeSrcOK_WH(container.tempLoadImage, container, imgwidth, imgheight); };
        container.tempLoadImage.src         = imgsrc;
    }
}


function changeSrcOK_WH(tempLoadImage, container, imgwidth, imgheight)
{
    tempLoadImage.onload        = null;
    if ( tempLoadImage.width > imgwidth || tempLoadImage.height > imgheight) {
        if (imgheight == 0) {
            container.onload    = function() { imgFitWi(container, tempLoadImage.width, tempLoadImage.height, imgwidth); };
            container.onclick   = function() { imgFitW(container, tempLoadImage.width, tempLoadImage.height, imgwidth); };
        }
        else {
            container.onload    = function() { imgFitWHi(container, tempLoadImage.width, tempLoadImage.height, imgwidth, imgheight); };
            container.onclick   = function() { imgFitWH(container, tempLoadImage.width, tempLoadImage.height, imgwidth, imgheight); };
        }
    }
    else {
        container.onload        = null;
        container.onclick       = null;
    }
    container.src               = tempLoadImage.src;
}
