/**
 * showroom Product Photo Enlargement
 * @dingjinlong
 */
(function() {
    var EnlargeImgFunc = function(photoCon, photoOri) {
        if (!window.zMaskIframe) {
            window.zMaskIframe = '<iframe class="zMaskIframe" style="position:absolute;z-index:-1;left:0;top:0;width:330px;height:330px;filter:mask();" frameborder="0"><\/iframe>';
        }
        jQuery('body').append('<div class="enlarge-photo"><dic class="seLargeImg"/><div class="warp"/></div>').find('.enlarge-photo').hide();
        if(!jQuery('.seLargeImg').length)return;
        var EnlargeImgTimer = null;
        var newPhoto = new Image();
        newPhoto.src = '/images/loading_nail.gif';
        newPhoto.onload = function() {
          var cssImg = {
                'width': newPhoto.width + 'px',
                'height': newPhoto.height + 'px'
          };
          if (!+'\v1' && !window.XMLHttpRequest) {
            jQuery('.enlarge-photo .photo160 > a img').replaceWith('<img src="' + newPhoto.src + '" style="width:' + cssImg.width + ';height:' + cssImg.height + '" />').show();
          }
          else {
            setTimeout(function() {
              jQuery('.enlarge-photo .photo160 > a img').hide().attr('src', newPhoto.src).show()
            }, 50)
          }
        };
        var imgOverFunc = function() {
          var qThis = jQuery(this);
          if(!qThis.find(photoOri + ' img[class ="imag"]:not([src*="/safe_image"]):not([src*="/no_photo"])').length)return;
          clearTimeout(EnlargeImgTimer);
          EnlargeImgTimer = setTimeout(function() {
            if (!jQuery('.seLargeImg').length) return;
              var photoSRC = qThis.find(photoOri + ' img').attr('src');
              photoSRC = '.photo160' != photoOri ? photoSRC.replace(/\/3f2/, '/2f1') : photoSRC;
              if (photoSRC.width > 160 && photoSRC.height > 160) {
                photoSRC.height = 160 + 'px';
                photoSRC.width = 160 + 'px'
              }
              else if (photoSRC.width > 160 || photoSRC.height > 160) {
                if (photoSRC.width >= photoSRC.height) {
                  photoSRC.height = photoSRC.width + 'px';
                  photoSRC.width = 160 + 'px'
                }
                else {
                  photoSRC.width = photoSRC.width + 'px';
                  photoSRC.height = 160 + 'px'
                }
              }
              var qImg = qThis.find('.seLargeImg .photo160 > a img');//ÐÂµÄÍ¼Æ¬
              var conTop = parseInt(qThis.position().top);
              var conLeft = parseInt(qThis.position().left);
              var warpHeight = parseInt(qThis.find('.seLargeImg').height())+22;
              var warpWidth = parseInt(qThis.find('.seLargeImg').width())+23;
              var viewWidth = ((document.documentElement && (document.documentElement.clientWidth + document.documentElement.scrollLeft)) || (document.body && (document.body.clientWidth + document.body.scrollLeft)));
              var viewTop = ((document.documentElement && (document.documentElement.clientHeight + (document.documentElement.scrollTop+document.body.scrollTop))) || (document.body && (document.body.clientHeight + document.body.scrollTop)));
              var compareTop=0;
              var compareWidth=35;
              if (conTop + warpHeight > viewTop) {
                compareTop = conTop + warpHeight - viewTop-33;
              }else if(viewTop - conTop > document.documentElement.clientHeight){
                compareTop = conTop-(document.documentElement.scrollTop+document.body.scrollTop)-35;
              }
              if(conLeft + warpWidth > viewWidth){
                compareWidth = conLeft + warpWidth - viewWidth;
              }
              var photoAlt = qThis.find(photoOri + ' img').attr('alt');
              var photoHref = qThis.find(photoOri + ' a').attr('href');
              if (!+'\v1' && !window.XMLHttpRequest) {
                qImg.hide();
              }
              else {
                qImg.attr({
                  'alt': photoAlt,
                  'src': photoSRC
                });
              }
              qThis.find('.seLargeImg a').attr({
                'href': photoHref,
                'title': photoAlt
              });
              qThis.find('.seLargeImg > .more a').attr({
                'title': ''
              });
              var left = conLeft-compareWidth;
              if(!document.documentElement.scrollTop){
                left = left + 20;
              }
              var enlarge_sel = qThis.find('.seLargeImg').clone();
              var enlarge_warp = qThis.find('.warp').clone();
              jQuery('.enlarge-photo .seLargeImg').replaceWith(enlarge_sel);
              jQuery('.enlarge-photo .warp').replaceWith(enlarge_warp);
              jQuery('.enlarge-photo').find('.seLargeImg').show().end().find('.warp').show();
              jQuery('.enlarge-photo').find('.warp').css({
                  'left': '-3px',
                  'top': '-3px'
              }).end().css({
                  'left': left + 'px',
                  'top': conTop - compareTop-35 +'px',
                  'position': 'absolute'
              }).show("normal");
              newPhoto.src = photoSRC
              },50);
        };
        var conOverFunc = function() {
            clearTimeout(EnlargeImgTimer)
        };
        var conOutFunc = imgOutFunc = function() {
          clearTimeout(EnlargeImgTimer);
            EnlargeImgTimer = setTimeout(function() {
                 jQuery('.enlarge-photo').hide();
                 if(jQuery('.enlarge-photo').find('.seLargeImg').length){
                 jQuery('enlarge-photo').empty();
              }
            }, 50)
        };
        if (!+'\v1' && !window.XMLHttpRequest) {
            //jQuery('.enlarge-photo').append(window.zMaskIframe);
            jQuery('.seLargeImg .photo160').css('background', '#fff url() no-repeat center');
            try {
                document.execCommand('BackgroundImageCache', false, true);
            } 
            catch (e) {
            }
        }
        jQuery('.imgborder').hover(imgOverFunc, imgOutFunc);
        jQuery('.enlarge-photo').hover(conOverFunc, conOutFunc)
    };
    jQuery(document).ready(function() {
        if (jQuery('.imgborder').length && jQuery('.Photo100').length) {
            EnlargeImgFunc('.imgborder', '.Photo100');
        }
     });
})();






