/*
 * $Id: wdie.js 91071 2009-10-01 19:34:13Z kbauman $
 * $URL: https://build/subversion/DestinationSearch/wdie/tags/wdieds-2.21.5.4/src/main/webapp/js/wdie.js $
 */

LMI.WDIE = ( function() {
	 // shortcut vars
    var $E = LMI.BrowserEvent,
        _E = LMI.Element,
        $ = _E.getOne,
        $$ = _E.getAll,
        $L = LMI.Reporting;
    var W = {

        decodeWrap: function( enc ) {
            return ( enc.substring( 0, 2 ) == '_e' ? LMI.WD.Base64.decode( enc.substring( 2 ) ) : enc );
        },

        print: function()	{
            if (window.print )	{
                window.print();
                //LMI.WD.Moniforce.createBeacon("mfinfo.page=" + LMI.Data.state.pageName + "&mfinfo.linktype=printpage");
            } else	{
                alert(LMI.Strings.getString('js.printerr'));
            }
        },

        sendEmail: function( evt ) {
        	var e = new LMI.BrowserEventObject( evt, window.event, this ),
                p = new LMI.Url.VoidParser( e.getCurrentTarget().href ),
                a, id, data;

            if ( p.parsed ) {
                a = p.getOneValue( 'url' );
                id = p.getOneValue( 'id' );
            } else {
                a = e.getCurrentTarget().innerHTML;
            }

            if( a.substring( 0, 2 ) == '_e' ) {
                a = LMI.WD.Base64.decode( a.substring( 2 ) );
            }
            /*
             * Not setting web beacons on this click event since there's one set on the email page.
             */
            //data = "mfinfo.listingId="+id+"&mfinfo.name="+name+"&mfinfo.url="+a+"&mfinfo.linktype="+LMI.Strings.getString( 'js.mf.linktype.email' );

            //LMI.WD.Moniforce.createBeacon( data );
            
            if( LMI.Data.state.pageType == 'WHITE' ) {
                e.preventDefault();
                document.location.href = 'mailto:' + a;
            } else {
                LMI.Data.form.go( LMI.Urls.get('displayemailform.ds'), 0, "emailListingId", id, "emailBusiness", "true");
                e.preventDefault();
                return false;
            }
        },

        fixBPPLinkUrl: function( a ) {
            var p = new LMI.Url.VoidParser( a.href );
            if( p ) {
                if( LMI.Data.state.pageType == 'WHITE' ) {
                    a.href = LMI.Urls.get('detailssearch.ds') + '&detailsListingId=' + p.getOneValue( 'detailsListingId' );
                } else {
                    a.href = LMI.Urls.get('detailssearch.ds') + '?detailsListingId=' + p.getOneValue( 'detailsListingId' ) + '&tab=' + p.getOneValue( 'tab' );
                }
            }
        },
        // add to my places links
        addInMyPlacesText: function( ip, parent ) {
            var s = _E.create( 'a', null, { textValue: LMI.Strings.getString( 'js.listings.inmypages' ), 'class':'inlist', href: LMI.Urls.get('displaymywww.ds') } );
            if( ip ) {
                parent.insertBefore( s, ip );
            } else {
                parent.appendChild( s );
            }
        },
        /* This function is defined in wd_poi and it appears
         * the version in this file is never called.
         * 
         * addToMyPlacesFromFlyout: function( evt ) {
            var e = new LMI.BrowserEventObject( evt, window.event, this ),
                li = DOMNode.findAncestor( e.getCurrentTarget(), 'li' );
            if( li )  {
                LMI.StyleSheet.removeClass( li, 'addToPgs' );
                LMI.StyleSheet.addClass( li, 'inPages' );
            }
            W.addToMyPlacesFromLink( evt );
        },*/
        addToMyPlacesFromLink: function( evt ) {
            var e = new LMI.BrowserEventObject( evt, window.event, this ),
                a = e.getCurrentTarget() || e.getTarget(),
                p = new LMI.Url.VoidParser( a.href ),
                parent = a.parentNode,
                ip = a.nextSibling,
                pageName = LMI.Data.state.pageName,
                wtEvent,
                id;
            if( p ) {
                parent.removeChild( a );
                W.addInMyPlacesText( ip, parent );
                id = p.getOneValue( 'id' );
                LMI.AjaxController.addToMyList( function(){}, [id], [] );
                
                if( pageName === 'Search|Detail Page' ) {
                    wtEvent = 'detailsAddToPgs';
                }
                else if( pageName.match( /lp/ ) ) {
                    wtEvent = 'lpAddToPgs';
                }
                else {
                    wtEvent = 'addToPgs';
                }
                $L.trackEvent( wtEvent, id );  
            }
        },
        vCard: function ( evt ) {
            var e = new LMI.BrowserEventObject( evt, window.event, this ),
                a = e.getCurrentTarget() || e.getTarget();            
                a.href = LMI.Urls.get( 'vcarddetailssearch.ds' ) + '?detailsListingId=' + LMI.Data.listings[0].id;
        },
        emailBusHandler: function ( evt ) {
            var e = new LMI.BrowserEventObject( evt, window.event, this ),
                a = e.getCurrentTarget() || e.getTarget(); 
                e.preventDefault();
                //add the title as a param to the url of the popup 
                window.open( a.href + '&email=' + W.decodeWrap( a.title ), 'emailWin', 'location=1,status=1,scrollbars=1,resizable=1,width=600,height=600' );
        }
    };
       
    function urlReplace ( url ) {
        var m = LMI.FindOnMapSize.getDimensions();     
        return url.replace( /mapwidth%3d([\d]+)/i, 'mapWidth%3d' + m.width ).replace( /mapheight%3d([\d]+)/i, 'mapHeight%3d' + m.height );
    }

    function initMapTab( ) {
        var t = document.getElementById( 'mapLink' );
            t.href = urlReplace( t.href );
    }
    
    LMI.Init.addFunction( function() {
        if( ( LMI.Data.state.pageType == 'YELLOW' && ( LMI.Data.state.pageName == 'Search|Search Results' || LMI.Data.state.pageName == 'mapResults' ) ) ) {
            initMapTab();
        }
    });

    LMI.Init.addFunction( function() {
        LMI.Lang.forEach( $$( '.enc, td.email a.eml, td.internetLinks a.email, ul.others a.email' ), function(o ) {
            o.firstChild.nodeValue = W.decodeWrap( o.firstChild.nodeValue );
            if( o.title ) {
                o.title = W.decodeWrap( o.title );
            }
        } );

        window.setTimeout( function() {
            try {
                if( LMI.Data.state.forward == 'forward.home' && typeof( doRunCMD == 'function' ) ) {
                    LMI.Data.form.setVar( "hasSkypeToolbar", "true" );
                }
            } catch( e ) { }
        }, 1000 );
    } );

    LMI.LinkBehavior.add( 'wdPrint', W.print );
    LMI.LinkBehavior.add( 'sendEmail', W.sendEmail );
    LMI.LinkBehavior.add( 'bppost', W.postLink, W.fixBPPLinkUrl );
    LMI.LinkBehavior.add( 'addToMyPlaces', W.addToMyPlacesFromLink );
    LMI.LinkBehavior.add( 'vCard', W.vCard );
    LMI.LinkBehavior.add( 'emailBusiness', W.emailBusHandler );
    
    return W;

} )();


function GetIcon( val, iconSet ) {
    var l;
    var lets = LMI.Strings.getString( 'js.letters' );
    if( val == -1 ) {
        l = 'star';
    } else if( typeof( val ) == "number" ) {
        if ( val > 26 ) {
			l = val - 26;
		} else {
			l = lets.charAt( val - 1 );
		}
    } else if( typeof( val ) == "string" && val.match( /\w/ ) ) {
        l = val.toLowerCase();
    }
    if( iconSet == 'findOnMap' ) {
        return LMI.Urls.getImg( 'map_nodes/findonmap/map_icon_' + l + '.png' ); 
    } else if ( l ) {
		if ( iconSet == 'green' ) { 
			return LMI.Urls.getImg( 'map_nodes/green/map_icon_' + l + '.png' );
		} else {
        	return LMI.Urls.getImg( 'map_nodes/yellow/map_icon_' + l + '.png' );
		}
    } else {
        return LMI.Urls.getImg( 'map_icon_blank.png' );
    }
}