<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs 
    title="Unype Virtual World" 
    title_url="http://unype.com/opensocial/unype/unype.xml"
    description="Chat with people from Facebook, MySpace, Bebo, Hi5, Ning and Orkut on a world map! Unype is a virtual world, like Second Life, that runs on Google Maps and Google Earth. Chat, meet new people, play games, share and rate locations, photos, videos, 3D models on the world map!"
    author="Murat Aktihanoglu"
    author_email="murat@unype.com"
    author_affiliation="Holoscape Inc."
    author_location="New York, NY, USA"
    thumbnail="http://unype.com/images/UnypeButtonSmaller.png"
    screenshot="http://unype.com/images/screenshot.png"
    icon="http://unype.com/icon16x16.ico"
    height="580">
    <Require feature="opensocial-0.7" />
  </ModulePrefs>
  <Content type="html">
  <![CDATA[
  
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "UA-51114-7";
    urchinTracker();
    </script>  
  
    <div id='main'/>
    </div>
    
    <script type="text/javascript">
        
        function request() {
            var req = opensocial.newDataRequest();

            if ( document.URL.match("orkut") )
            {
                var opt_params = { };
                opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [
                                                                opensocial.Person.Field.PROFILE_URL
                                                                ];
                req.add(req.newFetchPersonRequest('VIEWER',opt_params), "viewer");
            }
            else
            {
                req.add(req.newFetchPersonRequest('VIEWER'), "viewer");
            }
            
            //req.add(req.newFetchPersonRequest('OWNER'), "owner");
            
            req.send(response);
        }

        function response(data) 
        {
            // do something with the viewer data
            var viewer = data.get("viewer").getData();

            var viewername = viewer.getDisplayName();

            //var owner = data.get("owner").getData();
            
            //var ownername = owner.getDisplayName();
            
            // get the container's name with this hack
            var idPrefix = "or";
            var containerUrl = document.URL;
            
            var networkid = viewer.getField(opensocial.Person.Field.ID);
            
            
            //var width  = gadgets.window.getViewportDimensions().width;
            //var height = gadgets.window.getViewportDimensions().height;  
            
            var width = 480;
            var height = 400;
            if ( containerUrl.match("orkut") )
            {
                idPrefix = "or";
                snName = "Orkut";
                width = 750;
                height = 600;
                
                var profile = viewer.getField(opensocial.Person.Field.PROFILE_URL);
                //alert(networkid+' - '+profile);
                
                var index = profile.indexOf('uid=');
                
                networkid = profile.substr(index+4);           
                
            }
            else if ( containerUrl.match("ning") )
            {
                idPrefix = "ni";
                snName = "Ning";
            }
            else if ( containerUrl.match("pulse") )
            {
                idPrefix = "pu";
                snName = "Pulse";
            }
            else if ( containerUrl.match("hi5") )
            {
                idPrefix = "hi";
                snName = "Hi5";
                width = 950;
                height = 600;
            }
            else
            {
                // where are we??
                alert("where are we???");
            }
            
            
            if ( networkid == null || networkid == '' || networkid == 'xn_anonymous' )
            {
                var found = false;
                // get last id from the cookie
                if (document.cookie.length>0)
                {
                    c_start=document.cookie.indexOf("unypeid=");
                    if (c_start!=-1)
                    { 
                        c_start=c_start + 8; 
                        c_end=document.cookie.indexOf(";",c_start);
                        if (c_end==-1)
                        {
                            c_end=document.cookie.length;
                        }
                        networkid = unescape(document.cookie.substring(c_start,c_end));
                        found = true;
                    } 
                 }

                if ( !found )
                {
                    // if no cookie, create an id
                    networkid = Math.floor(99999999*Math.random());
                    
                    // put this in the cookie
                    var exdate=new Date();
                    exdate.setDate(exdate.getDate()+365);
                    document.cookie="unypeid=" +escape(networkid)+ ";expires=" + exdate.toGMTString();                    
                }               
                
            }
            
            var id = idPrefix + networkid;
            
            var thumb = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
            
            if ( containerUrl.match("ning") )
            {
                thumb = viewer.pictureUrl; 
            }
             
            //var profile = viewer.getField(opensocial.Person.Field.PROFILE_URL);
            //profile = 'http://sandbox.orkut.com' + profile;

            //if ( !ownername )
            //{
            //  ownername = id;
            //}
            if ( !viewername )
            {
              viewername = id;
            }
            
            //alert('ownername:' + ownername + '-id:' + id + '-thumb:'+ thumb + '-profile:' + profile + '-viewername' + viewername );

            var html = '<iframe id="mapsframe" name="mapsframe" ' + 
                'src="http://unype.com/maps/map.php?id=' + id +
                    '&name=' + viewername + 
                    '&pic=' + thumb + 
                    '&network=' + snName + 
                    '&mode=loggedin&width=' + width + '&height=' + height + '" ' + 
                    'frameborder="0" style="width: ' + width + 'px; height: ' + height + 'px;"/>';
            
            document.getElementById('main').innerHTML = html;
        }
    
        function writeActivity() {
          var streamTitle = 'Unype Virtual World';
          var stream_params = {'url': 'http://unype.com' };
          var stream = opensocial.newStream('', streamTitle, stream_params);
          
          var title = 'Playing inside Unype Virtual World';
          var link =  'http://unype.com';
          var activity_params = {'url' : link}
          var activity = opensocial.newActivity(stream, title, activity_params)
          opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, getActivities);
        }    
        
        // http://code.google.com/apis/opensocial/docs/javascript/index.html
        function getActivities() 
        {
        }
        
        //var containerUrlHack = document.URL;
        //if ( containerUrlHack.match("orkut") )
        {
            // doesn't work in Ning
          //  gadgets.util.registerOnLoadHandler(request);
        }
        //else //if ( containerUrl.match("ning") )
        //{
            _IG_RegisterOnloadHandler(request);
        //}
        
    </script>

  ]]>
  </Content>
</Module>