var friends={};var viewer_friend_ids=[];var owner_friend_ids=[];var profile_viewer_friends=null;var profile_owner_friends=null;var last_view={};var view_table={main:"ps_main",profile:"ps_profile",edit:"ps_edit",friends:"ps_friends",profile_pic_chooser:"ps_profile_pic_chooser",album_pics:"ps_album_pics"};APP_HOST="http://sociablepet.com/";_uacct="UA-992554-4";var init_attempts=0;var APP_URL=null;function init_canvas(){surface="canvas";fetch_bulletin();init();if(container=="myspace.com"){APP_URL="http://profile.myspace.com/Modules/Applications/Pages/Canvas.aspx?appId=105432"}else{if(container=="hi5.com"){APP_URL="http://sociablepet.hi5.com/friend/apps/entry/www.sociablepet.com/hi5.xml?from=ownprofile&";adjustHeight()}else{if(container=="orkut.com"){APP_URL="http://sandbox.orkut.com/Application.aspx?appId=356738141236";adjustHeight()}}}}function fetch_bulletin(){var A=APP_HOST+"fetch_bulletin";var B={};sendGet(A,B,display_bulletin)}function display_bulletin(A){$("bulletin").innerHTML=A.text}function init_profile(){surface="profile";if(container=="hi5.com"){setSkin()}init(loadProfilePage);p=new PeriodicalExecuter(showNextPet,8)}function loadProfilePage(A){profile_owner=A.get("owner");fetchFirst(true)}function fetchFirst(A){if(profile_owner!=undefined){var B=APP_HOST+"users/"+container.replace(".com","")+"/show_profile_page/"+profile_owner.getData().getId();var C={};sendGet(B,C,showPage,A)}}function showNextPet(){var A=null;if($("pets").childElements().length==1){return }$("pets").childElements().each(function(B){if(B.visible()){A=B;Effect.Fade(A.id);var C=A.id.replace("pet_","");var D=parseInt(C)+1;if(D==$("pets").childElements().length){D=0}Effect.Appear("pet_"+D,{delay:1})}})}function setSkin(){document.write('<style type="text/css">');document.write(".main {");bgColor=gadgets.skins.getProperty(gadgets.skins.Property.BG_COLOR);if(bgColor){document.write("background-color:"+bgColor+";")}bgImage=gadgets.skins.getProperty(gadgets.skins.Property.BG_IMAGE);if(bgImage){document.write("background-image: url("+bgImage+");")}bgPosition=gadgets.skins.getProperty(gadgets.skins.Property.BG_POSITION);if(bgPosition){document.write("background-position:"+bgPosition+";")}bgRepeat=gadgets.skins.getProperty(gadgets.skins.Property.BG_REPEAT);if(bgRepeat){document.write("background-repeat:"+bgRepeat+";")}fontColor=gadgets.skins.getProperty(gadgets.skins.Property.FONT_COLOR);if(fontColor){document.write("color:"+fontColor+";")}document.write("}");anchorColor=gadgets.skins.getProperty(gadgets.skins.Property.ANCHOR_COLOR);if(anchorColor){document.write(".main a, .main a:hover, .main a:visited { color:"+anchorColor+";}")}document.write("</style>")}function init(A){if(A==undefined){A=setupUsers}show_view(view_table.main);$(view_table.main).innerHTML="Lettin the pets out...";req=opensocial.newDataRequest();req.add(req.newFetchPersonRequest("VIEWER"),"viewer");req.add(req.newFetchPeopleRequest("VIEWER_FRIENDS"),"viewer_friends");req.add(req.newFetchPersonRequest("OWNER"),"owner");req.add(req.newFetchPeopleRequest("OWNER_FRIENDS"),"owner_friends");req.send(A)}function setupUsers(A){profile_owner=A.get("owner");if(profile_owner!=undefined){profile_viewer=A.get("viewer");profile_viewer_friends=A.get("viewer_friends");if(profile_viewer_friends!=undefined&&profile_viewer_friends.getData()!=undefined){profile_viewer_friends.getData().each(function(B){viewer_friend_ids.push(B.getId())})}profile_owner_friends=A.get("owner_friends");if(profile_owner_friends!=undefined&&profile_owner_friends.getData()!=undefined){profile_owner_friends.getData().each(function(B){owner_friend_ids.push(B.getId())})}params={owner_id:profile_owner.getData().getId(),network:container,owner_name:profile_owner.getData().getDisplayName()};if(profile_viewer!=undefined&&profile_viewer.getData()!=undefined){params.viewer_id=profile_viewer.getData().getId();params.viewer_name=profile_viewer.getData().getDisplayName()}url=APP_HOST+"accounts/fetch_viewer_owner.json";sendPost(url,params,postSetup)}else{retryInit()}}function retryInit(){if(init_attempts<5){init();init_attempts+=1}else{var A="Unable to initialize SociablePet after multiple attempts. ";A+="Please try refreshing your browser. If the problem persists please email us at contact@sociablepet.com";A+="<p>Apologies for any inconvenience and we'll get your pets socializing as quickly as possible</p>";$(view_table.main).innerHTML=A}}function grabData(A){if(typeof A.data=="string"){return parse_json(A.data)}else{return A.data}}function postSetup(A){results=grabData(A);if(results!=undefined){owner_account=results.owner_account;viewer_account=results.viewer_account}if(owner_account==undefined){url=APP_HOST+"accounts.json";params={"account[network_uid]":profile_owner.getData().getId(),"account[network]":container,"account[name]":profile_owner.getData().getDisplayName()};sendPost(url,params,postInstall)}else{init_display()}}function postInstall(A){owner_account=grabData(A);if(owner_account==undefined){retryInit()}else{if(viewer_account==undefined){viewer_account=owner_account}init_display()}}function init_display(){loadPetsRequest(owner_account)}function home(){if(viewer_account){loadPetsRequest(viewer_account)}else{loadPetsRequest(owner_account)}}function loadPetsRequest(B){var A=APP_HOST+"users/"+B.user_id+"/pets";var C={};sendGet(A,C,showPage)}function removeItem(A){results=grabData(A);Element.remove(results.type+"_"+results.id);$(current_busy_id).hide()}function updateItem(A){results=grabData(A);$(results.id).innerHTML=results.text;$(current_busy_id).hide()}function addComment(A){updateHTMLResult("comment_list",A);hideBusy();current_busy_id=busy_id}function showProfileFeed(A){updateHTMLResult("profile_feed_items",A);hideBusy();current_busy_id=busy_id}function showMainFeed(A){updateHTMLResult("main_feed_items",A);hideBusy();current_busy_id=busy_id}function showBirthdays(A){updateHTMLResult("birthdays",A);hideBusy();current_busy_id=busy_id}function showNewMembers(A){updateHTMLResult("new_members",A);hideBusy();current_busy_id=busy_id}function showFriends(A){updateHTMLResult("friend_list",A);hideBusy();current_busy_id=busy_id}function showPage(A){showViewWithData(view_table.main,A)}function DisplayPropertyNames(C){var B="";for(var A in C){B+=A+"\n"}alert(B)}var target_friend_network=null;var target_friend_profile=null;var target_friend_network_uid=null;var target_friend_owner_name=null;var target_pet_name=null;var viewer_pet=null;function preFriendAdd(C,E,D,B,F,A,G){viewer_pet=$("friend_source").options[$("friend_source").selectedIndex].text;target_friend_network=D;target_friend_profile=B;target_friend_network_uid=F;target_pet_name=G;target_friend_owner_name=A;sendForm(C,E,postFriendAdd)}function postFriendAdd(E){showProfile(E);if(!isOwnerAndViewer()&&container=="hi5.com"){var D={};viewer_url=profile_viewer.getData().getField(opensocial.Person.Field.PROFILE_URL);viewer_name=profile_viewer.getData().getDisplayName();owner_url=target_friend_profile;owner_name=target_friend_owner_name;var G=viewer_pet+' (Owned by <a href="'+viewer_url+'">'+viewer_name+"</a>) is now friends with ";G+=target_pet_name+' (Owned by <a href="'+owner_url+'">'+owner_name+"</a>)";G+=' (<a href="'+APP_URL+'">Click here to create your pet and get sociable</a>)';D[opensocial.Activity.Field.TITLE]=G;D[opensocial.Activity.Field.BODY]="";D[opensocial.Activity.Field.STREAM_FAVICON_URL]=APP_HOST+"images/sociable_pet_icon.jpg";var F=opensocial.newActivity(D);opensocial.requestCreateActivity(F,opensocial.CreateActivityPriority.LOW);var B=viewer_pet+' (Owned by <a href="'+viewer_url+'">'+viewer_name+"</a>) is now friends with ";B+="your pet "+target_pet_name;B+=' (<a href="'+APP_URL+'">Go say "Hi!" to '+viewer_pet+"</a>)";var H={};H[opensocial.Message.Field.TYPE]=opensocial.Message.Type.NOTIFICATION;var C=opensocial.newMessage(G,H);var A=null;opensocial.requestSendMessage(viewer_friend_ids.without(target_friend_network_uid),C);if(target_friend_network==container){if(target_friend_network_uid==owner_account.network_uid){opensocial.requestSendMessage(owner_friend_ids,C);A=opensocial.newMessage(B,H);opensocial.requestSendMessage(opensocial.DataRequest.PersonId.OWNER,A)}else{A=opensocial.newMessage(B,H);opensocial.requestSendMessage([target_friend_network_uid],A)}}}}function showProfile(A){showViewWithData(view_table.profile,A)}function showProfileEdit(A){showViewWithData(view_table.edit,A)}function hideBusy(){var A=$(current_busy_id);if(A!=undefined){A.hide()}}function showViewWithData(A,B){updateHTMLResult(A,B);show_view(A)}function updateHTMLResult(A,C){var B=document.getElementById(A);B.innerHTML=C.text}function show_view(A,B){if(undefined==B){B=true}$("ps_views").childElements().each(function(C){if(C.visible()&&C.id&&B){last_view[A]=C.id}C.hide()});hideBusy();$(A).show()}function show_last_view(){var A=false;$("ps_views").childElements().each(function(B){if(B.visible()&&B.id&&!A){show_view(last_view[B.id],false);A=true}})}function profile_pic_select(B){var A=opensocial.newDataRequest();addMainUserDataToRequest(A);A.add(MyOpenSpace.MySpaceContainer.newFetchPhotosRequest(profile_owner.getData().getId()));A.send(profile_pic_viewer)}function profile_pic_viewer(A){$(view_table.profile_pic_chooser).innerHTML=A}function response(A){document.getElementById("received").innerHTML=A}function gotoCanvas(){var A=gadgets.views.getSupportedViews()["canvas"];gadgets.views.requestNavigateTo(A)}function showFlashResponse(A){updateHTMLResult("flash",A);hideBusy()}var current_dog_id;function get_photos(D){var B=opensocial.Container.get();var C=B.newDataRequest();$(view_table.profile_pic_chooser).innerHTML="";show_view(view_table.profile_pic_chooser);current_dog_id=D;if(container=="hi5.com"){C.add(hi5.newFetchAlbumsRequest("VIEWER"),"ownerAlbums");C.send(show_albums_hi5)}else{if(container=="myspace.com"){var A=C.newFetchAlbumsRequest(opensocial.DataRequest.PersonId.VIEWER,{});C.add(A,"ownerAlbums");$(current_busy_id).show();C.send(check_myspace_photo_permissions)}else{show_web_fetch(D)}}}function check_myspace_photo_permissions(A){if(A.hadError()){if(A.get("ownerAlbums").getErrorCode()==opensocial.ResponseItem.Error.UNAUTHORIZED){opensocial.requestPermission(["accesstopublicvideosphotos"],"In order to set your pet's profile picture you need to allow access to your photos!",checkPhotoPermissionResponse)}}else{show_albums(A)}}function checkPhotoPermissionResponse(A){if(A==null){alert("We can't access your photos unless you let us :(");show_last_view()}else{if(A.accesstopublicvideosphotos){get_photos(current_dog_id)}else{alert("We can't access your photos unless you let us :(");show_last_view()}}}function show_web_fetch(B){var A="";A+='<div class="yui-g">';if(container!="orkut.com"){A+="<br/>OR "}A+="Fetch a photo from the web. <br/><br/>";A+='<div id="pic_status"></div>';A+='For example: <img src="'+APP_HOST+'images/bailey_thumb.jpg"/><br/>';A+=APP_HOST+"images/bailey.jpg<br/></p></div>";A+="You can paste any web address that points to a JPG or PNG image.<br/>";A+='<form id="pic_form">';A+='Enter image URL: <input size="30" type="text" name="image_url" id="image_url"/>';A+="<input onclick=\"if($('image_url').value.length > 0) {$('pic_status').innerHTML = ''; select_profile_pic($('image_url').value, '', '"+B+"');} else {$('pic_status').innerHTML = 'Please provide an image address';} return false;\" type=\"button\" value=\"Submit\" />";A+="</form><p>";A+="<b>Note: The URL *must* end in .jpg or .png or else it will not import.</b><br/>";if(container!="orkut.com"){A+="For most users we recommend uploading photos to your "+container+" photo album and fetching a profile photo from the album section above.<br/>"}update_ui("profile_pic_chooser",A);update_ui("profile_pic_chooser",'<a href="#" onClick=show_last_view();return false;\'>Cancel</a>')}var hi5_albums={};function show_albums_hi5(D){hideBusy();var A=D.get("ownerAlbums").getData();var C='<div class="yui-g"><div class="header">Set profile picture</div><br/>Fetch a photo from one of your Hi5 photo albums<br/><br/>';C+='<div id="pic_status"></div>';C+='<form id="pic_form">';C+='<select id="albums" onChange="show_hi5_photos(hi5_albums[$(\'albums\').value]); return false;">';var B=1;A.each(function(E){hi5_albums[E.getId()]=E;name=E.getName();if(name==""){name="Unnamed album "+B}C+='<option value="'+E.getId()+'">'+name+"</option>"});C+="</select>";C+="</form><p>";C+='<div id="ps_album_pics"></div>';update_ui("profile_pic_chooser",C);if(A.size()>0){show_hi5_photos(A.asArray()[0])}show_web_fetch(current_dog_id)}function show_hi5_photos(A){var C=current_dog_id;$(view_table.album_pics).innerHTML="";var B=1;photos=A.getPhotos();photos.each(function(E){if(B>21){return }var G=E.getUrl();var D=E.getDescription();var F='<a style="padding: 2px;" href="#" onClick=\'select_profile_pic("'+G+'","'+D+'","'+C+"\"); return false;'>";F+='<image src="'+G+'" height="75" width="75"/>';F+="</a>";if(B%7==0){F+="<br/>"}B+=1;update_ui("album_pics",F,false)})}function show_albums(C){hideBusy();var A=C.get("ownerAlbums").getData();var B='<div class="yui-g"><div class="header">Set profile picture</div><br/>Fetch a photo from one of your MySpace photo albums<br/><br/>';B+='<div id="pic_status"></div>';B+='<form id="pic_form">';B+='<select id="albums" onChange="fetch_photos($(\'albums\').value); return false;">';A.each(function(D){B+='<option value="'+D.getField("ALBUM_ID")+'">'+D.getField("TITLE")+"</option>"});B+="</select>";B+="</form><p>";B+='<div id="ps_album_pics"></div>';update_ui("profile_pic_chooser",B);show_web_fetch(current_dog_id);fetch_photos($("albums").value)}var current_album=null;var current_page=null;var per_page=20;function fetch_photos(A,C){$(current_busy_id).show();current_album=A;if(C==undefined){C=1}current_page=C;var B=opensocial.Container.get();var D=B.newDataRequest();if(container=="hi5.com"){D.add(hi5.newFetchAlbumsRequest("OWNER"),"ownerAlbums");D.send(show_photos_hi5)}else{if(container=="myspace.com"){var E={};E[MyOpenSpace.DataRequest.PhotoRequestFields.ALBUM_ID]=A;E[opensocial.DataRequest.PeopleRequestFields.FIRST]=(current_page-1)*per_page;E[opensocial.DataRequest.PeopleRequestFields.MAX]=per_page;var F=D.newFetchPhotosRequest(opensocial.DataRequest.PersonId.OWNER,E);D.add(F,"ownerAlbum");D.send(show_photos_myspace)}}}function show_photos_myspace(D){$(view_table.album_pics).innerHTML="";var F=D.get("ownerAlbum").getData();var E=current_dog_id;var C=1;F.each(function(H){var J=H.getField(MyOpenSpace.Photo.Field.IMAGE_URI);var G=H.getField(MyOpenSpace.Photo.Field.CAPTION);var I='<a style="padding: 2px;" href="#" onClick=\'select_profile_pic("'+J+'","'+G+'","'+E+"\"); return false;'>";I+='<image src="'+J+'" height="75" width="75"/>';I+="</a>";if(C%7==0){I+="<br/>"}C+=1;update_ui("album_pics",I,false)});update_ui("album_pics","<br/>",false);if(current_page>1){var B="";B="<a href=\"#\" onClick='fetch_photos(current_album, current_page - 1); return false;'>Previous</a>";update_ui("album_pics",B,false)}if((F.getTotalSize()-((current_page-1)*per_page))>per_page){var A="";A='<a href="#" style="padding-left: 500px;" onClick=\'fetch_photos(current_album, current_page + 1); return false;\'>Next</a>';update_ui("album_pics",A,false)}hideBusy()}function select_profile_pic(E,A,D){var B=APP_HOST+"users/"+viewer_account.user_id+"/pets/"+D+"/pictures/import";var C={};C.photo_url=E;C.caption=A;sendPost(B,C,init_canvas)}function friends_with_app(){var A=APP_HOST+"viewer_friends/with_app/"+viewer_account.user_id}function show_friends_with_app(A){show_view(view_table.friends);$(view_table.friends).innerHTML=A}function update_ui(C,B,A){if(A==undefined){A=true}if(A){B+="<br/>"}document.getElementById(view_table[C]).innerHTML+=B}function browse_pets(){var A=APP_HOST+"pets/all?pic_only=1";sendGet(A,{},showPage)}var profile_viewer=null;var profile_owner=null;var owner_data=null;var viewer_data=null;var owner_account=null;var viewer_account=null;var container=opensocial.getEnvironment().getDomain();var surface=null;var current_page="the function that will load the current page";var busy_id="busy";var current_busy_id=busy_id;function isOwnerAndViewer(){return profile_owner.getData().getId()==profile_viewer.getData().getId()}function parse_json(B){var A=B;if(container!="myspace.com"){A=gadgets.util.unescapeString(B)}A=gadgets.json.parse(A);return A}function stringify_json(B){var A=B;if(container=="myspace.com"){A=gadgets.JSON.stringify(B)}else{A=gadgets.json.stringify(gadgets.util.unescapeString(B))}return A}function sendForm(A,D,C){var B=$(D).serialize(true);sendPost(A,B,C)}function sendPost(A,B,D){$(current_busy_id).show();if(profile_viewer!=undefined&&profile_viewer.getData()!=undefined){B.viewer_id=profile_viewer.getData().getId()}B.network=container;var C=[];C[gadgets.io.RequestParameters.METHOD]=gadgets.io.MethodType.POST;C[gadgets.io.RequestParameters.POST_DATA]=gadgets.io.encodeValues(B);C[gadgets.io.RequestParameters.CONTENT_TYPE]=gadgets.io.ContentType.JSON;C[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;gadgets.io.makeRequest(A,D,C)}function sendGet(C,B,D,A){$(current_busy_id).show();C=appendViewer(C,A);B[gadgets.io.RequestParameters.METHOD]=gadgets.io.MethodType.GET;B[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;gadgets.io.makeRequest(C,D,B)}function sendDelete(B,A,C){if(container!="myspace.com"){if(B.indexOf("?")==-1){B+="/delete"}else{B=B.replace("?","/delete?")}}$(current_busy_id).show();B=appendViewer(B);A[gadgets.io.RequestParameters.METHOD]=gadgets.io.MethodType.DELETE;A[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;return gadgets.io.makeRequest(B,C,A)}function appendViewer(B,A){var C="?";if(B.indexOf("?")>-1){C="&"}if(profile_viewer!=undefined&&profile_viewer.getData()!=undefined){B+=C+"viewer_id="+profile_viewer.getData().getId();C="&"}B+=C+"network="+container;C="&";if(A!=true){B+=C+"nocache="+new Date().getTime()}return B}function adjustHeight(){if(container=="myspace.com"){}else{if(container=="hi5.com"){gadgets.window.adjustHeight(2000)}else{gadgets.window.adjustHeight(1000)}}}function myspaceAdjustHeight(){var B;var F=gadgets.window.getViewportDimensions().height;var A=document.body;var E=document.documentElement;if(document.compatMode=="CSS1Compat"&&E.scrollHeight){B=E.scrollHeight!=F?E.scrollHeight:E.offsetHeight}else{var C=E.scrollHeight;var D=E.offsetHeight;if(E.clientHeight!=D){C=A.scrollHeight;D=A.offsetHeight}if(C>F){B=C>D?C:D}else{B=C<D?C:D}}gadgets.window.adjustHeight(B)};