spsupport = {};
spsupport.p = { // params
    sfDomain :  superfish.b.pluginDomain,
    cdnUrl : superfish.b.cdnUrl,
    appVersion : superfish.b.appVersion,
    clientVersion : superfish.b.clientVersion,
    site :  superfish.b.pluginDomain,
    isIEQ : ( +document.documentMode == 5 ? 1 : 0 ),
    sfIcon : {
        maxSmImg : {
            w : 88,
            h : 70
        },
        icons : [0, 0, 0, 0],
        big : {
            w : 64,
            h : 48
        },
        small : {
            w : 42,
            h : 38
        },
        prog : {
            time : 1000,
            node : 0,
            color : "#398AFD",
            opac  : "0.3",
            w : [ 62, 40 ],
            h : 13
        }
    },
    suAnim : 0,
    dlsource : superfish.b.dlsource,
    w3iAFS : superfish.b.w3iAFS,
    CD_CTID: superfish.b.CD_CTID,
    userid: superfish.b.userid,
    statsReporter: superfish.b.statsReporter,
    minImageArea : ( 60 * 60 ),
    aspectRatio : ( 1.0/2.0 ),
    supportedSite : 0,
    frameLoaded : 0,
    frameExLoading: 0,
    overIcon: 0,
    alreadySent : 0,
    //    iconsReplace: 0, // ? not in use
    icons: 0,
    partner : ( superfish.b.partnerCustomUI ? superfish.b.images + "/" : "" ),

    // Result state - identical (first), similar (second)
    rState: [ 0, 0 ],
    prodPage: {
        s: 0,   // sent - first request
        i: 0,   // images count
        p: 0,   // product image
        e: 0,   // end of session slideup session
        d: 210, // dimension of image
        l: 1000 // line - in px from top
    },
    before : -1  // Close before
};
spsupport.api = {
    getDomain: function(){
        var dD = document.location.host;
        var dP = dD.split(".");
        var len = dP.length;
        if ( len > 2 ){
            var co = ( dP[ len - 2 ] == "co" ? 1 : 0 );
            dD = ( co ? dP[ len - 3 ] + "." : "" ) + dP[ len - 2 ] + "." + dP[ len - 1 ];
        }
        return dD;
    },
    validDomain: function(){
        try{
            var d = document;
            if( d == null || d.domain == null ||
                d == undefined || d.domain == undefined || d.domain == ""
                || d.location == "about:blank" || d.location == "about:Tabs"
                || d.location.toString().indexOf( "superfish.com/congratulation.jsp" ) > -1  ){
                return false;
            }else{
                return (/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,5}$/).test( document.domain );
            }
        }catch(e){
            return false;
        }
    },

    init: function(){
        var spp = spsupport.p;
        if( window.sufio )
            return; 

        if( !spsupport.api.validDomain() )
            return;

        //  if(spp.clientVersion == null)
        if( !spp.clientVersion )
            spp.clientVersion  = "unknown";

        spsupport.api.dojoReady = false;

        if ( ! top.djConfig ){
            djConfig = {}
        }
        djConfig.afterOnLoad = true;
        // djConfig.baseUrl = spp.cdnUrl;
        djConfig.baseScriptUri = spp.cdnUrl;
        djConfig.useXDomain = true;
        djConfig.scopeMap = [ ["dojo", "sufio"], ["dijit", "sufiw"], ["dojox", "sufix"] ];
        djConfig.require = ["dojo.io.script" ,"dojo._base.html", "dojo.window"];
        djConfig.modulePaths =  {
            "dojo": spp.cdnUrl + "dojo",
            "dijit": spp.cdnUrl + "dijit",
            "dojox": spp.cdnUrl + "dojox"
        };

        superfish.b.inj(  spp.cdnUrl + "dojo/dojo.xd.js",
            1,1,
            function(){
                sufio.addOnLoad(function(){
                    spsupport.api.dojoLoaded();
                });
            });

    },
    gotMessage: function( param ){
        if ( param ){
            var prep = param.split( "|" );
            param = ( +prep[ 0 ] );
        }

        if( param == 101 ){ // sys down
            superfish.util.sysDown();
        }else{
            if( param == 0 ){ // init
                spsupport.p.frameLoaded = 1;
                if( superfish.util.standByData != 0 ){
                    superfish.util.sendRequest( superfish.util.standByData );
                    superfish.util.standByData = 0;
                }
            }else if( param >= 2000 ){
                // 200 -  ( slideup paused )
                // 2100 - ( idrentical only - true, failure )
                // 2101 - ( idrentical only - true, identical is empty, similar not empty )
                // 2110 - ( idrentical only - true, identical is not empty, similar is empty )
                // 2111 - ( idrentical only - true, identical is not empty, similar is not empty )
                // 2000 - ( idrentical only - false, failure )
                // 2001 - ( idrentical only - false, identical is empty, similar not empty )
                // 2010 - ( idrentical only - false, identical is not empty, similar is empty )
                // 2011 - ( idrentical only - false, identical is not empty, similar is not empty )
                var st = spsupport.p.rState;
                st[ ( param / 21 < 100 ? 1 : 0 ) ] = param ;

                if( st[ 0 ] > 2100 || ( st[ 0 ] && st[ 1 ] == 2000 ) || st[ 1 ] > 2000 ){
                    superfish.util.hideFishPreload();
                    spsupport.p.before = 0;
                }

                if( st[ 0 ] > 2100 || st[ 1 ] > 2000 ){
                    if( superfish.b.suEnabled == 11 ){
                        if( spsupport.p.prodPage.s && !spsupport.p.prodPage.e ){
                            if( prep[ 1 ] ){
                                superfish.b.slideUpTab( prep[ 1 ] );
                            }
                        }
                    }else{
                        if( spsupport.p.prodPage.s && !spsupport.p.prodPage.e ){   // slide up
                            var vp = sufio.window.getBox();
                            var sl = superfish.util.bubble();
                            if ( spsupport.p.isIEQ ) {
                                sl.style.top = vp.t + vp.h + 10;
                                sl.style.left = vp.l + vp.w - superfish.p.width - 4;
                            }
                            else {
                                sl.style.top = vp.h + 10;
                                sl.style.left = vp.w - superfish.p.width - 4;                                              
                            }

                            spsupport.p.suAnim = sufio.animateProperty({
                                node:  sl,
                                duration: 800,
                                properties: {
                                    top: ( parseInt( superfish.util.bubble().style.top ) /*+ 3000*/ - superfish.p.height - 14 )
                                },
                                onEnd: function(){
                                    spsupport.p.suAnim = 0;
                                }

                            });
                            //superfish.util.setPosition({}, 1 );
                            spsupport.p.suAnim.play(5);
                        }
                    }
                    if( st[ 0 ] > 2100 || ( st[ 0 ] > 0 && st[ 1 ] > 0 ) ){
                        spsupport.p.prodPage.e = 1;
                        spsupport.p.prodPage.s = 0;
                    }

                }else{
                    if( st[ 0 ] > 0 && st[ 1 ] > 0 && !spsupport.p.prodPage.e &&  spsupport.p.prodPage.s){
                        superfish.util.bCloseEvent( document.getElementById("SF_CloseButton"), 2 );
                        spsupport.p.prodPage.e = 1;
                        spsupport.p.prodPage.s = 0;
                    }
                }
            }else if( param == 10 ){
                superfish.util.bCloseEvent( document.getElementById("SF_CloseButton"), 4);
            }else if( param == 11 ){
                superfish.util.bCloseEvent( document.getElementById("SF_CloseButton"), 5);
            }else if( param == 20 ){ // Slideup paused
                superfish.util.bCloseEvent( document.getElementById("SF_CloseButton"), 2 );
            }

        }
    },

    dojoLoaded: function()  {
        spsupport.api.dojoReady = true;
        spsupport.api.userIdInit();
        if( window.sufio && window.sufio.isIE && window.spMsiSupport ){
            if( !this.isOlderVersion( '1.2.1.0', spsupport.p.clientVersion ) ){
                spMsiSupport.validateUpdate();
            }
        }
    },

    userIdInit: function(){
        var spp = spsupport.p;
        var data = {
            "dlsource":spp.dlsource
        }
        if(spp.w3iAFS != ""){
            data.w3iAFS = spp.w3iAFS;
        }

        if( spp.CD_CTID != "" ){
            data.CD_CTID = spp.CD_CTID;
        }

        // if we already have a user id, go straight to the initUser callback
        if(spp.userid != "" && spp.userid != undefined){
            spsupport.api.onUserInitOK({
                userId: spp.userid,
                statsReporter: spp.statsReporter
            });
        } else { // widget
            spsupport.common.jsonpRequest(
                spp.sfDomain + "initUserJsonp.action",
                data,
                spsupport.api.onUserInitOK,
                spsupport.api.onUserInitFail,
                "superfishInitUserCallbackfunc"
                );
        }
    },

    onUserInitOK: function(obj) {
        if(!obj || !obj.userId || (obj.userId == "")){
            spsupport.api.onUserInitFail();
        } else{
            spsupport.p.userid = obj.userId;
            spsupport.p.statsReporter = obj.statsReporter;
            spsupport.api.isURISupported( document.location );
        }
    },


    ASU_OK : function( obj ){
        if( !obj ){
            spsupport.api.AS_Fail();
        } else{ }
    },
    ASU_Fail : function(){},



    isURISupported: function(url){
        spsupport.p.merchantName = "";
        spsupport.common.jsonpRequest(
            spsupport.p.sfDomain + "getSupportedSitesJSON.action?ver=" + superfish.b.wlVersion,
            0,
            spsupport.api.isURISupportedCB,
            spsupport.api.isURISupportedFail,
            "SF_isURISupported");
    },

    isURISupportedCB: function(obj) {
        var sS = {};
        if( superfish.b.ignoreWL ){
            sS.imageURLPrefixes = "";
            sS.merchantName = superfish.b.dlsource;
            spsupport.api.injectIcons( sS );
        }else if( !obj || !obj.supportedSitesMap ){
        //      spsupport.api.checkURISupportedError();
        }else{
            spsupport.p.totalItemCount = obj.totalItemCount;
            sS = obj.supportedSitesMap[ spsupport.api.getDomain() ];
            if( sS && !spsupport.api.isBLSite(obj)){
                spsupport.api.injectIcons( sS );
            }else{
                setTimeout("spsupport.api.saveStatistics()", 400);
            }
        }
    },

    isURISupportedFail: function(obj) {
    },

    isBLSite: function(obj){
        var isBL = 0;
        if ( obj.blockedSubDomains ){
            for (var s = 0 ; s < obj.blockedSubDomains.length && !isBL ; s++ ){
                var loc = top.location + "";
                if (loc.indexOf(obj.blockedSubDomains[s]) >= 0){
                    isBL = 1;
                }
            }
        }
        return isBL;
    },
    injectIcons: function(sS) {
        spsupport.p.supportedSite = 1;
        spsupport.p.supportedImageURLs = sS.imageURLPrefixes;
        spsupport.p.merchantName = sS.merchantName;
        spsupport.api.careIcons( 0 );
    },
    addSuperfishSupport: function(){
        superfish_xd_messanger.init(
            spsupport.api.gotMessage,
            ( sufio.isIE == 7 || (+document.documentMode) == 7 ? 200 : 0 ) );

        if( !top.superfishMng ){
            top.superfishMng = {};
        }
        if( !top.superfish ){
            top.superfish = {};
        }

        if( !top.superfish.p ){ // params
            top.superfish.p = {
                site : spsupport.p.site,
                totalItemsCount: spsupport.p.totalItemCount,
                cdnUrl : spsupport.p.cdnUrl,
                appVersion : spsupport.p.appVersion
            };
        }

        if( !top.superfish.util ){
            superfish.b.inj( "js/sf.js?version=" + spsupport.p.appVersion, 1,0 );
        }

        if( !spsupport.api.pluginDiv() ) {
            sufio.place("<div id='superfishPlugin'></div>", sufio.body());
        }
        sufio.place("<div id='sfiframesync'></div>", sufio.body());
    },
    careIcons: function( rep ){
        spsupport.p.icons = this.startDOMEnumeration();
        if( spsupport.p.icons > 0  ){
            superfish.b.inj(
                "js/postMessage" +
                ( ( +sufio.isIE == 7 ) || ( +document.documentMode == 7 ) ? "_IE7" : "" ) + ".js?ver=" + spsupport.p.appVersion,
                1, 0,
                function(){
                    spsupport.api.addSuperfishSupport();
                });

                
            spsupport.domHelper.addOnresizeEvent(function() {
                spsupport.api.setPopupInCorner();
                spsupport.api.startDOMEnumeration();
            });
            spsupport.domHelper.addFocusEvent(spsupport.api.startDOMEnumeration);
            spsupport.domHelper.addUnloadEvent(spsupport.api.unloadEvent);
            spsupport.domHelper.addEListener( document, spsupport.api.onDOMSubtreeModified, "DOMSubtreeModified");
            spsupport.api.vMEvent();

            sufio.addOnLoad(function(){
                setTimeout( function(){
                    spsupport.sites.care();
                    spsupport.api.wRefresh( 150 );
                    setTimeout("spsupport.api.saveStatistics()", 850)
                }, 500 );
            });
        }else{
            if( rep == 15 ){
                spsupport.api.saveStatistics();
            }else{
                setTimeout("spsupport.api.careIcons( " + ( rep + 1 ) + ");", ( 1400 + rep * 500 ) ) ;
            }
        }

        return spsupport.p.icons;
    // spsupport.domHelper.addEListener(spsupport.api.pluginDiv(),spsupport.api.blockDOMSubtreeModified, "DOMSubtreeModified");
    },

    vMEvent : function(){
        var pDiv = spsupport.api.pluginDiv();
        if( pDiv ){
            spsupport.domHelper.addEListener( pDiv, spsupport.api.blockDOMSubtreeModified, "DOMSubtreeModified");
        }else{
            setTimeout( "spsupport.api.vMEvent()", 500 );
        }

    },

    puPSearch : function(){
        if( !spsupport.p.prodPage.s ){
            if( superfish.b.suEnabled ){
                setTimeout(
                    function(){
                        try{
                            if( !spsupport.p.prodPage.s ){
                                spsupport.api.superfish().util.openPopup(
                                    spsupport.api.getItemJSON( spsupport.p.prodPage.p ),
                                    spsupport.p.appVersion, 1 );
                                spsupport.p.prodPage.s = 1;
                            }
                        }catch(e){
                            setTimeout("spsupport.api.puPSearch()", 80);
                        }
                    }, 80 );
            }
        }
    },

    isSuperfishReady : function(){
        try{
            return( + document.getElementById("sfiframesync").getAttribute("codeReady") );
        }catch(ex){ }
        return 0;
    },
    onDOMSubtreeModified: function(e,elName){
        if(spsupport.api.DOMSubtreeTimer)
            clearTimeout(spsupport.api.DOMSubtreeTimer);
        spsupport.api.DOMSubtreeTimer = setTimeout("spsupport.api.onDOMSubtreeModifiedTimeout()",2000);
    },
    onDOMSubtreeModifiedTimeout: function(){
        clearTimeout(spsupport.api.DOMSubtreeTimer);
    },
    blockDOMSubtreeModified: function(e,elName){
        e.stopPropagation();
    },
    createImg : function(src) {
        var img = new Image();
        img.src = src;
        return img;
    },
    loadIcons : function() {
        var sps = spsupport.p;
        var cust = ( sps.dlsource == "similarweb" || sps.dlsource == "ddfinder" ? sps.dlsource + "/" : "" );
        for (var i = 0; i < 4; i++) {
            sps.sfIcon.icons[ i ] = spsupport.api.createImg( sps.sfDomain + "images/" + cust + "i" + i + ".png?v=" + sps.appVersion);
        }
    },
    startDOMEnumeration: function( saveStats ){
        if( spsupport.api.sfButtons() != null ){
            document.body.removeChild( spsupport.api.sfButtons() );
        }
        var imSpan = sufio.place("<span id='sfButtons' display='none'></span>", sufio.body());
        var images = document.images;
        var isImgSupported = 0;
        var found = 0;

        for( var i = 0; i < images.length; i++ ){
            isImgSupported = spsupport.api.isImageSupported( images[ i ] );
            if(isImgSupported){
                if (!found) {
                    spsupport.api.loadIcons();
                }
                spsupport.api.addSFImage( imSpan, images[ i ], spsupport.api.sfIPath(isImgSupported ), isImgSupported, i );
                found++;
            }
        }

        if( found > 0 ){
            spsupport.api.addSFProgressBar( imSpan );
            sufio.style( "sfButtons","opacity","0");
            sufio.fadeIn({
                node: imSpan,
                duration: 300
            }).play();

            setTimeout(
                function(){
                    if( ! spsupport.p.alreadySent ){
                        spsupport.api.saveStatistics();
                        spsupport.p.alreadySent = 1;
                    }
                }, 700);
        }
        return found;
    },

    imageSupported: function( src ){
        if( src.indexOf( "amazon.com" ) > -1  && src.indexOf( "videos" ) > -1 ){
            return 0;
        }

        var sIS = spsupport.p.supportedImageURLs;
        if( sIS.length == 0 )
            return 1;

        for( var i = 0; i < sIS.length; i++ ){
            if( src.indexOf( sIS[ i ] ) > -1 ){
                return 1;
            }
        }
        return 0;
    },

    isImageSupported: function(img){
        var src = "";
        try{
            src = img.src.toLowerCase();
        }catch(e){
            return 0;
        }
        var sps = spsupport.p;

        for( var z = 0; z < 4; z ++ ){
            if( src.substring( sps.sfIcon.icons[ 0 ] ) > -1 ){
                return 0;
            }
        }

        if( src.length < 4 )
            return 0;
        var ext = src.substring(src.length - 4,src.length);
        if((ext == ".gif") || (ext == ".png"))
            return 0;

        var iW = img.width;
        var iH = img.height;

        if( ( iW * iH ) < sps.minImageArea )
            return 0;

        var ratio = iW/iH;
        if( ( iW * iH > 2 * sps.minImageArea ) &&
            ( ratio < sps.aspectRatio || ratio > ( 1 / sps.aspectRatio ) ) )
            return 0;

        if( !this.imageSupported( img.src ) )
            return 0;

        if( !spsupport.api.isVisible( img ) ){
            return 0;
        }
        if(( iW <= sps.sfIcon.maxSmImg.w ) || ( iH <= sps.sfIcon.maxSmImg.h ) ) {
            return 2;
        }
        else {
            return 1;
        }

    },

    setPopupInCorner : function () {
        if( superfish.p.onAir == 2 && spsupport.p.before == 0 ){
            if (spsupport.p.suAnim) {
                spsupport.p.suAnim.stop();
            }
            var vp = sufio.window.getBox();
            var sl = superfish.util.bubble().style;
            if(superfish.b.suEnabled != 11 || (superfish.b.suEnabled == 11 && superfish.b.slideUpOn)){
                var slL = (vp.w - superfish.p.width - 4);
                var slT = (vp.h - superfish.p.height - 4);
                if (spsupport.p.isIEQ) {
                    slL = slL + vp.l;
                    slT = slT + vp.t;
                } 
                sl.left = slL + "px";
                sl.top =slT + "px";
            }
            var pSU = superfish.util.preslideup();
            if( pSU ){
                slL = vp.w - parseInt( pSU.style.width ) - 111;
                slT = vp.h - parseInt( pSU.style.height );
                if (spsupport.p.isIEQ) {
                    slL = slL + vp.l;
                    slT = slT + vp.t;
                }
                pSU.style.left = slL + "px";
                if (superfish.b.slideUpOn) {
                    pSU.style.top = (parseInt(sl.top) - 26) + "px";
                }
                else {
                    pSU.style.top = slT + "px";
                }
            }
        }
    },

    fixPosForIEQ : function( e ) {
        spsupport.api.setPopupInCorner();
        var oS = superfish.util.overlay().style;
        oS.left = document.body.scrollLeft;
        oS.top = document.body.scrollTop;
    },


    wRefresh : function( del ){ // Widget Refresh

        sufio.fadeOut({
            node: spsupport.api.sfButtons(),
            duration: del ,
            onEnd: function() {
                setTimeout("spsupport.api.startDOMEnumeration()", del * 2 );
            }
        }).play();
    //        setTimeout("spsupport.api.startDOMEnumeration()", del );
    },


    isViewable: function ( vP, obj ){
        return (   vP.scrollLeft < ( obj.offsetLeft  + obj.offsetWidth ) &&
            ( obj.offsetLeft + obj.offsetWidth - vP.scrollLeft < vP.offsetWidth )  );
    },

    isVisible: function( obj ){
        if( obj == document ) return 1;
        if( !obj ) return 0;
        if( !obj.parentNode ) return 0;

        if( obj.style ){
            if( obj.style.display == 'none' ||
                obj.style.visibility == 'hidden' ){
                return 0;
            }
        }

        if( window.getComputedStyle ){
            var style = window.getComputedStyle(obj, "");
            if( style.display == 'none' ||
                style.visibility == 'hidden'){
                return 0;
            }
        }
        // Computed style using IE's silly proprietary way
        var cS = obj.currentStyle;
        if( cS ){
            if( cS['display'] == 'none' ||
                cS['visibility'] == 'hidden'){
                return 0;
            }
        }
        return spsupport.api.isVisible( obj.parentNode );
    },

    sfIPath: function( iType ){ // 1 - large, 2 - small
        var sps = spsupport.p;
        var icn = ( iType == 2  ?  2  :  0 );
        return( {
            r : sps.sfIcon.icons[ icn ].src,
            o : sps.sfIcon.icons[ icn + 1 ].src
        } );
    },

    addSFImage: function( parent, img, icPath, iType,  imID ){
        var nI = document.createElement("img");
        nI.id = "sf_icon_" + imID;
        nI.title = " See Similar ";
        nI.pW = spsupport.p.sfIcon.prog.w[ iType - 1 ];
        var sps = spsupport.p;
        var iProp = ( iType == 2  ?  sps.sfIcon.small  :  sps.sfIcon.big );

        var anim = sufio.animateProperty({
            node: spsupport.p.sfIcon.prog.node,
            duration: spsupport.p.sfIcon.prog.time,
            properties: {
                width: {
                    start: "0",
                    end:  spsupport.p.sfIcon.prog.w[ iType - 1 ],// parseInt( spsupport.p.overIcon.pW ),//
                    unit: "px"
                }
            },
            onEnd : function() {
                if( nI == spsupport.p.overIcon ){
                    nI.onmousedown();
                }
            }
        });

        var resetPBar = function() {
            var pBar = spsupport.p.sfIcon.prog.node;
            if( pBar ){
                anim.stop();
                sufio.style(
                    pBar ,{
                        width : "0px",
                        display : "none"
                    });
            }
        };

        sps.sfIcon.prog.node.onmousedown = function(e){
            spsupport.p.overIcon.onmousedown();
        };

        sps.sfIcon.prog.node.onmouseout = function(e){
            if (!e) {
                var e = window.event;
            }
            var relTarget = ( e.relatedTarget ? e.relatedTarget : e.toElement );
            if( !relTarget.id || ( relTarget.id && ( relTarget.id != spsupport.p.overIcon.id ) ) ){
                if( spsupport.p.overIcon ){
                    spsupport.p.overIcon.onmouseout( e );
                }
            }
        };


        nI.onmouseout = function(e){
            if (!e) {
                var e = window.event;
            }
            var relTarget = ( (e.relatedTarget) ? e.relatedTarget : e.toElement );
            if( relTarget != spsupport.p.sfIcon.prog.node ){
                this.src = icPath.r;
                spsupport.p.overIcon = 0;
                resetPBar();
            }
        };

        nI.onmouseover  = function(e){
            if( !window.superfish || !window.superfish.p || !window.superfish.p.onAir ){
                if (!e) {
                    var e = window.event;
                }
                var relTarget = ( (e.relatedTarget) ? e.relatedTarget : e.fromElement );
                if ( relTarget != spsupport.p.sfIcon.prog.node) {
                    this.src = icPath.o;
                    spsupport.p.overIcon = this;
                    if (spsupport.p.sfIcon.prog.node ) {
                        var dif = iProp.h - spsupport.p.sfIcon.prog.h;
                        sufio.style(
                            spsupport.p.sfIcon.prog.node ,{
                                display : "block",
                                top : parseInt( nI.style.top ) + dif - 2 + "px",
                                left : parseInt( nI.style.left ) + 2 + "px"
                            });
                        anim.play();
                    }
                }
            }
        //            setTimeout(
        //                ( function( icn ){
        //                    return function(){
        //                        if( icn == spsupport.p.overIcon ){
        //                            icn.onmousedown();
        //                        }
        //                    }
        //                } ) ( nI ), 1400 );
        };

        nI.onmousedown = ( function(){
            return function( e ){
                var evt = e || window.event;
                try{
                    if( evt.button == 2 )
                        return;
                }catch(e){}
                spsupport.p.overIcon = 0;
                this.src = icPath.r;
                resetPBar();
                try{
                    spsupport.api.superfish().util.openPopup(
                        spsupport.api.getItemJSON( img ),
                        spsupport.p.appVersion );
                    spsupport.p.prodPage.e = 1;
                }catch(e){
                    setTimeout(
                        ( function( ic ){
                            return function(){
                                ic.onmousedown();
                            }
                        } )( this ), 300 );
                }
            }
        })( img, icPath );

        nI.src = icPath.r;
        nI.style.position = "absolute";
        var zindex = img.style.zIndex;
        if((zindex == "auto") || (zindex == "0") || (zindex == "") ){
            zindex = "3";
        }
        nI.style.zIndex = ( +zindex ) + 12002;
        var imgPos = spsupport.api.getImagePosition(img);
        nI.style.top = "" + parseInt( imgPos.y + img.height - iProp.h + 3 ) + "px";
        nI.style.left = "" + parseInt( imgPos.x - 2 ) + "px";
        nI.style.cursor = "pointer";
        // BUG IN IE
        nI.style.width = "" + iProp.w + "px";
        nI.style.height = "" + iProp.h + "px";
        // ^^^
        parent.appendChild( nI );

        if( !sps.prodPage.s ){
            if( img.width > spsupport.p.prodPage.d &&
                img.height > spsupport.p.prodPage.d &&
                parseInt( nI.style.top ) < spsupport.p.prodPage.l &&
                sps.prodPage.p != img ){
                sps.prodPage.i ++;
                sps.prodPage.p = img;

                //             if( +document.documentMode != 5 ){ // IE QUIRKS
                setTimeout("spsupport.api.puPSearch()", 50);
            //             }
            }
        }
    },

    addSFProgressBar: function(iNode){
        var bProp = spsupport.p.sfIcon.prog;
        if( !bProp.node ) {
            bProp.node = sufio.place("<div id='sfIconProgressBar'></div>", iNode, "after" );
            bProp.node.setAttribute('style', '-moz-border-radius : 4px; -webkit-border-radius : 4px; border-radius: 4px;');
            sufio.style( bProp.node ,{
                position : "absolute",
                overflow: "hidden",
                width : "0px",
                height : bProp.h + "px",
                zIndex : "12008",
                cursor : "pointer",
                backgroundColor : bProp.color,
                opacity : bProp.opac
            });            
        }
    },

    pluginDiv: function(){
        return document.getElementById("superfishPlugin");
    },
    sfButtons : function(){
        return document.getElementById("sfButtons");
    },
    getImagePosition : function(img) {
        return( sufio.coords(img, true) );
    },

    getTextOfChildNodes : function(node){
        var txtNode = "";
        var ind;
        for( ind = 0; ind < node.childNodes.length; ind++ ){
            if( node.childNodes[ ind ].nodeType == 3 ) { // "3" is the type of <textNode> tag
                txtNode = sufio.trim( txtNode + " " + node.childNodes[ ind ].nodeValue );
            }
            if( node.childNodes[ ind ].childNodes.length > 0 ) {
                txtNode = sufio.trim( txtNode +
                    " "  + spsupport.api.getTextOfChildNodes( node.childNodes[ ind ] ) );
            }
        }
        return txtNode;
    },

    getRelatedText : function( node ){
        if( node ){
            var lNode = (
                node.nodeName.toUpperCase() == "A" ? node :
                ( node.parentNode.nodeName.toUpperCase() == "A" ? node.parentNode :
                    ( node.parentNode.parentNode.nodeName.toUpperCase() == "A" ? node.parentNode.parentNode : 0 ) ) );
            if ( lNode ){
                var lTitle = lNode.getAttribute("title");
                lTitle = ( lTitle ? lTitle : "" );
                var url = lNode.href;
                var ebLV = (  spsupport.p.merchantName.toLowerCase() == "ebay" &&
                    ( document.location.href.indexOf("&_dmd=1") > 10 ||
                        sufio.query("a.lav").length > 0 ) ? 1 : 0 ); // ebay list view
                var txt = "";
                var pUrl = "";
                if( url.indexOf( "javascript" ) == -1 && !ebLV ){
                    if( spsupport.p.merchantName.toLowerCase() == "google" ){
                        var gPref = "http://www.google.com/url?";
                        var pInd = url.indexOf(gPref);
                        if( pInd > -1 ){
                            var pSign = url.indexOf("=");
                            if( pSign > -1 ){
                                url = url.substr( pSign + 1, url.length );
                            }
                        }
                        try{
                            url = decodeURIComponent( url );
                        }catch(e){
                        // not encoded
                        }
                        var prm = url.indexOf("&");
                        if( prm > -1 ){
                            url = pUrl = url.substr(0, prm);
                        }
                    }
                    url = url.replace(/http:\/\//g, "");
                    if( spsupport.p.merchantName.toLowerCase() != "sears" ){
                        url = url.replace( document.domain, "");
                    }

                    var plus = url.lastIndexOf( "+", url.length - 1 );
                    var _url = ( plus > -1 ? url.substr( plus + 1, url.length - 1 ) : "" );
                    sufio.query( 'a[href *= "' + ( _url != "" ? _url : url ) + '"]' ).
                    forEach(
                        function( node ) {
                            if( ( _url !="" && node.href.indexOf( url, 0) > -1 ) || _url ==""  ){
                                txt += ( " " + spsupport.api.getTextOfChildNodes( node ) );
                            }
                        });
                    return {
                        prodUrl : ( pUrl != "" ? pUrl : lNode.href ),
                        iText : sufio.trim( lTitle + " " + txt )
                    };
                }else{
                    if( spsupport.p.merchantName.toLowerCase() == "ebay" ){
                        var ref = "";
                        if( ebLV ){
                            var iT = "";
                            var row = "";
                            try{
                                row = lNode.parentNode.parentNode.parentNode.parentNode.getAttribute('r');
                                iT = spsupport.api.getTextOfChildNodes( sufio.query("table[r=" + row + "] td div.ttl")[0] );
                                ref = sufio.query("table[r=" + row + "] td div.ttl .vip")[0].getAttribute("href");
                            }catch(e){}
                            return {
                                prodUrl : ref,
                                iText : iT
                            };
                        }else{
                            var p = lNode.getAttribute("r");
                            if( p && p != "" ){
                                sufio.query( 'a[r = "' + p + '"]' ). forEach(
                                    function( node ) {
                                        if( node != lNode ){
                                            ref = node.getAttribute("href");
                                            ref = ( ref.indexOf( "javascript" ) == -1 ? ref : "" );
                                        }
                                        txt += ( " " + spsupport.api.getTextOfChildNodes( node ) );
                                    });
                            }
                            return {
                                prodUrl : ref,
                                iText : sufio.trim( lTitle + " " + txt )
                            };
                        }
                    }
                }
            }
        }
        return 0;
    },

    vTextLength : function( t ) {
        if( t.length > 1000 ){
            return "";
        }else if( t.length < 320 ){
            return t;
        }else{
            if( sufio.isIE == 7 || (+document.documentMode) == 7 ){
                return t.substr(0, 320);
            }
            return t;
        }
    },

    getItemJSON : function( img ) {
        var spa = spsupport.api;
        var spp = spsupport.p;
        var imgPos = spa.getImagePosition( img );
        var x = imgPos.x;
        var y = imgPos.y;
        var iURL = "";
        try{
            iURL = decodeURIComponent( img.src );
        }catch(e){
            iURL = img.src;
        }
        var relData = spa.getRelatedText( img.parentNode );
        var jsonObj = {
            imageURL: encodeURIComponent( iURL ),
            x: x,
            y: y,
            w: img.width,
            h: img.height,
            userid: encodeURIComponent( spp.userid ),
            merchantName: encodeURIComponent( spa.merchantName() ),
            dlsource: spp.dlsource ,
            appVersion: spp.appVersion,
            imageTitle: encodeURIComponent( sufio.trim( img.title + " " + img.alt ) ),
            imageRelatedText: ( relData ? encodeURIComponent( spa.vTextLength(  relData.iText  ) ) : "" ),
            productUrl: ( relData ? encodeURIComponent( relData.prodUrl ) : "" ),
            documentTitle: encodeURIComponent( document.title )
        };
        return jsonObj;
    },
    merchantName: function()  {
        return  spsupport.p.merchantName;
    },
    superfish: function(){
        return window.top.superfish;
    },

    s2hash: function( str ){
        var res = "";
        var l = str.length;
        for ( var i = 0; i < l; i++){
            res += "" + str.charCodeAt(i);
        }
        return res;
    },

    sendMessageToExtenstion: function(msgName, data){
        var d = document;
        // msgName is the event name the the listner in the extention is listening to
        // data is an object that continas fields that the listener expected
        if (sufio){
            var jsData = sufio.toJson(data);
            if (sufio.isIE) {
                try {
                    window.sendMessageToBHO(jsData, msgName); // The bho get the parameters in a reverse order
                } catch(e) {}
            } else {
                var element = d.getElementById("sfMsgId");
                if (!element){
                    element = d.createElement("sfMsg");
                    element.setAttribute("id", "sfMsgId");
                }
                element.setAttribute("data", jsData );
                d.body.appendChild(element);
                var evt = d.createEvent("Events");
                evt.initEvent(msgName, true, false);
                element.dispatchEvent(evt);
            }
        }
    },
    saveStatistics: function() {
        if( document.domain.indexOf("superfish.com") > -1 ||
            spsupport.p.dlsource == "conduit" ||
            spsupport.p.dlsource == "pagetweak" ||
            spsupport.p.dlsource == "similarweb"){
            return;
        }

        var imageCount = 0;
        var sfButtons = spsupport.api.sfButtons();
        if( sfButtons != null ){
            imageCount = sfButtons.children.length;
        }


        var data = {
            "imageCount" : imageCount,
            "ip": superfish.b.ip
        }

        if( this.isOlderVersion( '1.2.0.0', spsupport.p.clientVersion ) ){
            data.Url = document.location;
            data.userid = spsupport.p.userid;
            data.versionId = spsupport.p.clientVersion;
            data.dlsource = spsupport.p.dlsource;

            if( spsupport.p.CD_CTID != "" ) {
                data.CD_CTID = spsupport.p.CD_CTID;
            }
            spsupport.common.jsonpRequest( spsupport.p.sfDomain + "saveStatistics.action", data );
        } else  {
            this.sendMessageToExtenstion("SuperFishSaveStatisticsMessage", data);
        }
    },
    isOlderVersion: function(bVer, compVer) {
        var res = 0;
        var bTokens = bVer.split(".");
        var compTokens = compVer.split(".");

        if (bTokens.length == 4 && compTokens.length == 4){
            var isEqual = 0;
            for (var z = 0; z <= 3 && !isEqual && !res ; z++){
                if (+(bTokens[z]) > +(compTokens[z])) {
                    res = 1;
                    isEqual = 1;
                } else if (+(bTokens[z]) < +(compTokens[z])) {
                    isEqual = 1;
                }
            }
        }
        return res;
    },
    nofityStatisticsAction :function(action) {
        var browser = navigator.userAgent;

        var data = {
            "action" : action,
            "userid" : spsupport.p.userid,
            "versionId" : spsupport.p.clientVersion,
            "dlsource" : spsupport.p.dlsource,
            "browser": browser
        }
        if(spsupport.p.w3iAFS != "") {
            data.w3iAFS = spsupport.p.w3iAFS;
        }
        if(spsupport.p.CD_CTID != "") {
            data.CD_CTID = spsupport.p.CD_CTID;
        }
        spsupport.common.jsonpRequest( spsupport.p.sfDomain + "notifyStats.action", data );
    },
    unloadEvent : function(){
    }
};

spsupport.sites = {
	 care: function(){
	        var site = spsupport.api.getDomain();
	        site = site.substr(0, site.indexOf(".") ) ;
	        site = eval( "spsupport.sites." + site );
	        if( site ){ site.care() };
    		},
    google : {
        care : function(){
        //            this.events();
        },
        events : function(){
        //            var btn = sufio.query('.lsb');
        //            if( btn.length > 0 ){
        //                setTimeout(
        //                    function(){
        //                        sufio.forEach(
        //                            btn,
        //                            function( b ) {
        //                                sufio.connect( b, "onmouseup", function(){
        //                                });
        //                                sufio.connect( b, "onmousedown", function(){
        //                                });
        //                                sufio.connect( b, "onclick", function(){
        //                                });
        //                            });
        //                    }, 350 );
        //            }
        }
    },

    amazon : {
        care : function(){
            this.foxlingo();
            this.nextPage();
            this.widget();
        },
        foxlingo : function(){
            if( !sufio.isIE &&
                spsupport.p.dlsource == "foxlingo" ){
                setTimeout("superfish.b.currency.addCurrency('$', '" + superfish.b.curRequested + "')", 1000);
            }
        },
        nextPage : function(){
            var pgn = sufio.query('.pagnLink, .pagnPrev, .pagnNext');
            if( pgn.length > 0 ){
                setTimeout(
                    function(){
                        sufio.forEach(
                            pgn,
                            function( lnk ) {
                                var tDel = 750;
                                sufio.connect( lnk, "onmouseup", function(){
                                    spsupport.api.wRefresh( tDel );
                                    setTimeout( "spsupport.sites.amazon.nextPage()", tDel * 3 );
                                } );
                            });
                    }, 1400);
            }
        },
        widget : function(){
            if( sufio.query('div[class = "shoveler"]').length > 0 ){
                setTimeout(
                    function(){
                        sufio.query('.back-button, .next-button').forEach(
                            function( btn ) {
                                sufio.connect( btn, "onmouseup", function(){
                                    spsupport.api.wRefresh(450);
                                } );
                            });
                    }, 1400);
            }
        }
    },
    sears : {
        care : function(){
            this.widget();
        },
        widget : function(){
            if( sufio.query('div[id *= "rr_placement_"]').length > 0 ){
                sufio.query('div[class = "previous-disabled"]').forEach(
                    function( btn ) {
                        sufio.connect( btn, "onmouseup", function(){
                            spsupport.api.wRefresh(1000);
                        } );
                    });
                sufio.query('div[class *= "next"]').forEach(
                    function( btn ) {
                        sufio.connect( btn, "onmouseup", function(){
                            spsupport.api.wRefresh(1000);
                        } );
                    });
            }
        }
    }
};

spsupport.domHelper = {
    addOnresizeEvent : function(func){
        //        if( sufio.isIE == 7 || (+document.documentMode) == 7 ) {
        //            if ( typeof window.onresize == 'function' ) {
        //                var oldonresize = window.onresize;
        if (typeof window.onresize != 'function'){
            window.onresize = func;
        } else {
            var oldonresize = window.onresize;
            window.onresize = function() {
                if (oldonresize) {
                    if ( sufio.isIE ) {
                        oldonresize();
                    }
                    else {
                        setTimeout(oldonresize,350);
                    }
                }
                if ( sufio.isIE ) {
                    func();
                }
                else {
                    setTimeout(func, 200);
                }
            }
        }
    //            }
    //        }else{
    //            if ( typeof window.onresize == 'function') {
    //                var wRS =  window.onresize;
    //                window.onresize = null;
    //                window.onresize =  (
    //                    function( wEvent ){
    //                        spsupport.p.wEvent = wEvent;
    //                        return function(){
    //                            if( spsupport.p.wEvent ) {
    //                                spsupport.p.wEvent();
    //                            }
    //                            setTimeout("spsupport.api.startDOMEnumeration()",100);
    //                            setTimeout("window.onresize = " + arguments.callee , 350);
    //                        }
    //                    })( wRS );
    //
    //            } else {
    //                window.onresize = null;
    //                window.onresize = function(){
    //                    window.onresize = null;
    //                    setTimeout("spsupport.api.startDOMEnumeration()",100);
    //                    setTimeout("window.onresize = " + arguments.callee, 350);
    //                };
    //            }
    //        }
    },
    addFocusEvent : function(func){
        var oldonfocus = window.onfocus;
        if (typeof window.onfocus != 'function') {
            window.onfocus = func;
        }else{
            window.onfocus = function()  {
                if (oldonfocus) {
                    oldonfocus();
                }
                func();
            }
        }
    },
    addScrollEvent : function(func){
        var oldonscroll = window.onscroll;
        if (typeof (window.onscroll) != 'function') {
            window.onscroll = func;
        }else{
            window.onscroll = function()  {
                if (oldonscroll) {
                    oldonscroll();
                }
                func();
            }
        }
    },

    addUnloadEvent : function(func){
        var oldonunload = window.onunload;
        if (typeof window.onunload != 'function'){
            window.onunload = func;
        } else {
            window.onunload = function() {
                if (oldonunload) {
                    oldonunload();
                }
                func();
            }
        }
    },

    addEListener : function(node, func, evt){
        if( window.addEventListener ){
            node.addEventListener(evt,func,false);
        }else{
            node.attachEvent(evt,func,false);
        }
    }
};

spsupport.common = {
    jsonpRequest: function(url, data, successFunc, errorFunc, callBack){
        try{
            if( callBack == null ){
                var date = new Date();
                callBack = "superfishfunc" + date.getTime();
            }
            window[callBack] = function(json) {
                if(successFunc != null)
                    successFunc(json);
            };
            sufio.io.script.get({
                url: url + ( url.indexOf("?") > -1 ? "&" : "?" ) + "callback=" + callBack,
                content: data,
                load : function(response, ioArgs) {
                    window[callBack]  = null;
                },
                error : function(response, ioArgs) {
                    window[callBack]  = null;
                    if(errorFunc != null)
                        errorFunc( response, ioArgs);
                }
            });
        }
        catch(ex){
        }
    }
};

spsupport.api.init();

//function getTime(){
//    var __SFD = new Date();
//    return( __SFD.getMinutes() + ":" + __SFD.getSeconds());
//}


function scriptStart(){
}
function scriptStop(){
    if( spsupport.p.supportedSite) {
        if( sufio.isIE ) {
            window.location.reload();
        } else {
            window.location.href = window.location.href;
        }
    }
}


