<?xml version="1.0" encoding="UTF-8" ?>

<Module>
	<ModulePrefs author="eXoPlatform"
		title="Favorite documents"
		directory_title="Favorite documents"
		title_url="http://www.exoplatform.org"
		description="The favorite documents"
		height="227">
         <Require feature="opensocial-0.7" /> 
         <Require feature="dynamic-height"/>
	</ModulePrefs>
	<Content type="html">
	<![CDATA[
	
		<script type="text/javascript" src="/favorites/javascript/script.js">


               </script>
		
		<style type="text/css">
			body{
				font: 11px Tahoma,Verdana,Arial,Helvetica,sans-serif;
				height:100%;
				margin:0 auto;
				padding:0;
			}
			
			a {
				text-decoration: none;
			        color: #484848;
			}

                        .UIGadgetThemes{
                        }

			.UIGadgetThemes .TitGad {
				line-height: 22px;
				border-top: 1px solid #ffd400;
				border-bottom: 6px solid #ffd400;
				color: #484848;
				font-size: 12px;
				padding: 0px 15px;
				font-weight: bold;
			}
			
			.UIGadgetThemes .TitGad .IconDropDown {
				background: url('background/IconDropDown.gif') no-repeat right 7px;
				float: right;
				padding-right: 12px;
				font-size: 11px;
				color: #484848;
				display: block;
				text-decoration: none;
			}
			
			.UIGadgetThemes .TitGad .ContTit {
				margin-right: 45px;
			}
			
			.UIGadgetThemes .GadCont {
				padding: 10px 15px;
				font-size: 12px;
			}
			
			.UIGadgetThemes .GadCont .IconLink {
				background: url('background/IconLink.gif') no-repeat left center;
				display: block;
			}
			
			/** MySpace **/
			.UIGadgetThemes .MySpace .IconLink {
				padding-left: 10px;
				margin-bottom: 5px;
				color: #484848;
				text-decoration: none;
			}
			
			.UIGadgetThemes .MySpace .NoneSpace {
				padding-left: 10px;
				margin-bottom: 5px;
				color: #484848;
				text-decoration: none;
				display: block;
			}


                       .UIGadgetThemes .ItemIcon {
				border-bottom:1px solid #CCCCCC;
				line-height: 16px;
				margin: 0px 5px 0px 11px;
				padding: 0px 0 0px 11px;
				width: auto;
			}


                        .ContentGadGet .BgTitle{
				background: #dfeefa;
				width: auto;
				line-height: 16px;
				font-weight: bold;
				color: #468bcc;
			}



                       .UIGadgetThemes .DateTime{
				color: #c1c5c9;
				font-weight: normal;
                                font-size: 10px;
			}

	  </style>

          <script type="text/javascript">

                        function Favorite() { }
			

               
			var prefixUrl = location.protocol + '//' + location.hostname;
			if (location.port) {
				prefixUrl += ':' + location.port;
			}
                        var user;
  

			 Favorite.prototype.loadUser = function() {
			  var req = opensocial.newDataRequest();
			  var opts = {};
			  opts[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [
			            opensocial.Person.Field.PROFILE_URL];
			
			  req.add(req.newFetchPersonRequest("VIEWER", opts), 'viewer');
			  req.send(onLoadUser);
			
			  function onLoadUser(data) {
			    if (!data.hadError()) {
			            this.viewer = data.get('viewer').getData();
			            var profile_url =  this.viewer.getField(opensocial.Person.Field.PROFILE_URL);
			            userId = profile_url.substr(profile_url.lastIndexOf('/') + 1);
				    user = userId;
                                    favorite.init();
			    }
			  }

			}
			

                      /** Getting list chain */

 			 Favorite.prototype.init = function() {
                           accessUrl = prefixUrl + '/rest/private/cmisatom/';
                           refreshInterval = 10000;
 			   favorite.getFavoriteFolder(accessUrl);
			}


                         /** Detecting current username & getting favorite folder ID */
                         Favorite.prototype.getFavoriteFolder = function(accessUrl) {
			    var params = {};
                            var authString = encode64(user + ":" + "gtn");
			    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
			    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;

                            //TODO: Remove when OAuth could be done;
                            params[gadgets.io.RequestParameters.HEADERS] = {
                            "Authorization" : "Basic "  + authString
                            }


                            var url = accessUrl;
			    url += "collaboration";
			    url += '/objectbypath?path=';
			    url += '/Users/' + user + '/Private/Favorites/';

                            var ts = new Date().getTime();
    			    var sep = "&";
 			    if (refreshInterval && refreshInterval > 0) {
          			ts = Math.floor(ts / refreshInterval);
	 		    }
                            url = [ url, sep, "nocache=", ts ].join("");


			    gadgets.io.makeRequest(url, favorite.getCmisContent, params);
			}

                  
                        /* Getting favorite folder contents */      
                        Favorite.prototype.getCmisContent = function(response) {
                             var xmlDoc = loadXMLString(response.text);
                             x=xmlDoc.getElementsByTagName("id")[0];
                             if (!x) alert("Unable to retrieve item.");
			     y=x.childNodes[0];
			     var obj_id=y.nodeValue; 
                             if (obj_id && (obj_id.length > 0)) 
                             {
  			       var params = {};
                               var authString = encode64(user + ":" +"gtn");
   			       params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
			       params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;

                              //TODO: Remove when OAuth could be done;
                               params[gadgets.io.RequestParameters.HEADERS] = {
                                "Authorization" : "Basic "  + authString
                               }

                               var url = accessUrl;
                               url += "collaboration";
                               url += "/children/";
                               url += obj_id;

                               var ts = new Date().getTime();
        	               var sep = "?";
  			       if (refreshInterval && refreshInterval > 0) {
          			ts = Math.floor(ts / refreshInterval);
	 		       }
                               url = [ url, sep, "nocache=", ts ].join("");
                               gadgets.io.makeRequest(url, favorite.displayList, params);
                             } 
                              else 
                             {
                               alert("Unable to retrieve item.");
                             }
			}

                    
                         /* Showing list */
                         Favorite.prototype.displayList = function(resp) {
  			     var data = eval(resp.data.Entry);
                             var displayTime = new DisplayTime();
                             var editedDocList = _gel("myspaces_id");

                             for (var i = 0; i < data.length; i++) {
				var doc = data[i];
				var AppClass = document.createElement('div');
				var showHTML = "<a  style=\"cursor: pointer;\" ";
                                showHTML += " href= \" ";
                                showHTML += prefixUrl;
                                showHTML += "/portal/private/acme/editor/?path=";
                                showHTML += "/repository/collaboration/Users/";
                                showHTML += user;
                                showHTML += "/Private/Favorites/";
				showHTML += doc.Title;
                                showHTML += "\" ";
       				showHTML += "target";
       				showHTML += "=";
       				showHTML += "\"_top\"";
       				showHTML += ">";
				showHTML += doc.Title;
				showHTML += "</a>";
				showHTML += "<div class='DateTime'>";
				showHTML += "Added ";
				showHTML += displayTime.timeToPrettyString(doc.Date);
				showHTML += "</div>";
				showHTML += "<br>";

                                AppClass.innerHTML = showHTML;
				AppClass.className = 'ItemIcon';
				editedDocList.appendChild(AppClass);
                               }
				var title = _gel("BgTitle");
				if (data && (data.length > 0) && title) {
				   editedDocList.removeChild(title);
				}
			
				gadgets.window.adjustHeight();

			}
                         


                       function loadXMLString(txt) {
                         var xmlDoc = null;
			 if (window.DOMParser)
			   {
			    parser=new DOMParser();
			    xmlDoc=parser.parseFromString(txt,"text/xml");
			   }
			 else // Internet Explorer
			   {
			    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			    xmlDoc.async="false";
			    xmlDoc.loadXML(txt);
			   }
			  return xmlDoc;
			}


                         //TODO: Remove when OAuth could be done;

			 var keyStr = "ABCDEFGHIJKLMNOP" +
			                "QRSTUVWXYZabcdef" +
			                "ghijklmnopqrstuv" +
			                "wxyz0123456789+/" +
			                "=";
			  
			   function encode64(input) {
			      var output = "";
			      var chr1, chr2, chr3 = "";
			      var enc1, enc2, enc3, enc4 = "";
			      var i = 0;
			  
			      do {
			         chr1 = input.charCodeAt(i++);
			         chr2 = input.charCodeAt(i++);
			         chr3 = input.charCodeAt(i++);
			  
			         enc1 = chr1 >> 2;
			         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			         enc4 = chr3 & 63;
			  
			         if (isNaN(chr2)) {
			            enc3 = enc4 = 64;
			         } else if (isNaN(chr3)) {
			            enc4 = 64;
			         }
			  
			         output = output +
			            keyStr.charAt(enc1) +
			            keyStr.charAt(enc2) +
			            keyStr.charAt(enc3) +
			            keyStr.charAt(enc4);
			         chr1 = chr2 = chr3 = "";
			         enc1 = enc2 = enc3 = enc4 = "";
			      } while (i < input.length);

			      return output;
			   }
                       
                        var favorite = new Favorite();
      		        favorite.loadUser();
          </script>
				
	            <body>
			<div class="UIGadgetThemes">
                          <div id="more_spaces">
                           <div class="TitGad">
                            <div class="ContTit">My Favorites</div>
                           </div>
                          </div>
                          <div id="myspaces_id" class="GadCont MySpace">
                            <div id="BgTitle" class="BgTitle">
			      <div class="NotAvailableItemIcon">
				No available documents!
				</div>
			    </div>
                          </div>
	               </div>
		   </body>
	]]>
	</Content>
</Module>

 
