/**
 * Product Photo Enlargement
 */
(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-con"><div class="arrow-left" /><div><div class="photo300"><a href="#"><img src="/images/loading_nail.gif" alt="" /></a></div></div><a href="#" class="prevDetail">Click for Details</a></div>').find('.enlarge-con').hide();
        var EnlargeImgTimer = null;
        var loadingImg = new Image();
        loadingImg.src = '/images/loading_nail.gif';
        var newPhoto = new Image();
        newPhoto.src = '/images/spacer.gif';
        newPhoto.onload = function(){
            var cssImg = {
                'width': newPhoto.width + 'px',
                'height': newPhoto.height + 'px'
            };
            if (newPhoto.width > 300 || newPhoto.height > 300) {
                if (newPhoto.width >= newPhoto.height) {
                    cssImg.height = parseInt(300 * newPhoto.height / newPhoto.width) + 'px';
                    cssImg.width = 300 + 'px';
                }
                else {
                    cssImg.width = parseInt(300 * newPhoto.width / newPhoto.height) + 'px';
                    cssImg.height = 300 + 'px';
                }
            }
            if (!+'\v1' && !window.XMLHttpRequest) {
                jQuery('.enlarge-con .photo300 > a img').replaceWith('<img src="' + newPhoto.src + '" style="width:' + cssImg.width + ';height:' + cssImg.height + '" />').show();
            }
            else {
                setTimeout(function(){
                    jQuery('.enlarge-con .photo300 > a img').hide().css(cssImg).attr('src', newPhoto.src).show();
                }, 50);
            }
        };
        var imgOverFunc = function(){
            clearTimeout(EnlargeImgTimer);
            var qThis = jQuery(this);
            EnlargeImgTimer = setTimeout(function(){
                var qPhotoL = qThis.parents(photoCon);
                var photoSRC = qPhotoL.find(photoOri + ' img').attr('src');
                photoSRC = '.Photo160' != photoOri ? photoSRC.replace(/\/image\/3f2/, '/image/2f0').replace(/\/image\/8f4/, '/image/8f6') : photoSRC.replace(/\/image\/2f1/, '/image/2f0').replace(/\/image\/8f5/, '/image/8f6');
                var qImg = jQuery('.enlarge-con .photo300 > a img');
                if (qImg.attr('src').indexOf(photoSRC) >= 0) {
                    jQuery('.enlarge-con').show();
                    return;
                }
                var conTop = parseInt(qPhotoL.position().top);
                var conLeft = parseInt(qPhotoL.position().left);
                conLeft = '.Photo160' != photoOri ? conLeft + 104 : conLeft + 192;
                var viewWidth = ((document.documentElement && (document.documentElement.clientWidth + document.documentElement.scrollLeft)) || (document.body && (document.body.clientWidth + document.body.scrollLeft)))
                var bChangeArrow = '.image' == photoOri ? true : (qPhotoL.position().left - 322 > 0 && (conLeft + 300) > viewWidth);
                var photoAlt = qPhotoL.find(photoOri + ' img').attr('alt');
                var photoHref = qPhotoL.find(photoOri + ' a').attr('href');
                if (!+'\v1' && !window.XMLHttpRequest) {
                    qImg.hide();
                }
                else {
                    qImg.attr({
                        'alt': photoAlt,
                        'src': loadingImg.src
                    }).css({
                        'width': 'auto',
                        'height': 'auto'
                    });
                }
                var cssArrow = {
                    'left': 'auto',
                    'right': 'auto'
                };
                cssArrow.top = '97px';
                if (bChangeArrow) {
                    conLeft = qPhotoL.position().left - 322;
                    cssArrow.right = '-27px';
                }
                else {
                    cssArrow.left = '-27px';
                }
                jQuery('.enlarge-con').find('.arrow-left,.arrow-right').toggleClass('arrow-left', !bChangeArrow).toggleClass('arrow-right', bChangeArrow).css(cssArrow).end().find('a').attr({
                    'href': photoHref,
                    'title': photoAlt
                }).end().css({
                    'left': conLeft + 'px',
                    'top': conTop + 'px'
                }).show();
                newPhoto.src = photoSRC;
            }, 100);
        };
        var conOverFunc = function(){
            clearTimeout(EnlargeImgTimer);
        };
        var conOutFunc = imgOutFunc = function(){
            clearTimeout(EnlargeImgTimer);
            EnlargeImgTimer = setTimeout(function(){
                jQuery('.enlarge-con').hide();
            }, 100);
        };
        if (!+'\v1' && !window.XMLHttpRequest) {
            jQuery('.enlarge-con').append(window.zMaskIframe);
            jQuery('.enlarge-con .photo300').css('background', '#fff url(/images/loading_nail.gif) no-repeat center');
            try {
                document.execCommand('BackgroundImageCache', false, true)
            } 
            catch (e) {
            }
        }
        /*
         if(jQuery('.useIcons').length>0){
         jQuery(photoOri + ' img[class!="pia"]:not([src*="/safe_image"]):not([src*="/no_photo"])').parents(photoCon).find('.useIcons').append('<img class="enlarge-icon" title="enlarge" alt="enlarge" src="/images/enlarge.gif" />');
         }
         else{
         jQuery(photoOri + ' img[class!="pia"]:not([src*="/safe_image"]):not([src*="/no_photo"])').parents(photoCon).append('<div class="enlarge-icon"><img title="enlarge" alt="enlarge" src="/images/enlarge.gif" /></div>');
         }
         */
        jQuery('.enlarge-icon').hover(imgOverFunc, imgOutFunc);
        jQuery('.enlarge-con').hover(conOverFunc, conOutFunc);
    };
    if (jQuery('.Photo100').length > 0) {
        EnlargeImgFunc('.PhotoL', '.Photo100');
    }
    else {
        if (jQuery('.Photo160').length > 0) {
            EnlargeImgFunc('.item', '.Photo160');
        }
        else {
            if (jQuery('.image').length > 0) {
                jQuery('.image img[class!="pia"]:not([src*="/safe_image"]):not([src*="/no_photo"])').parents('.pro_show').append('<img class="enlarge-icon" title="enlarge" alt="enlarge" src="/images/enlarge.gif" />');
                EnlargeImgFunc('.pro_show', '.image');
            }
        }
    }
})();
