//orori fb-fans
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 

var has_login=false;
function update_user_box() {

	//return false;
	if(has_login) return false;
	if($('facebook_login_button')){$('facebook_login_button').style.display='none';getStream();}
	if($('orori-friendstream')){		
		FB.XFBML.Host.parseDomTree();
		has_login=true;
	}
}

function getStream(callback){
	$('orori-friendstream').innerHTML = '<img alt=""  style="position:absolute;left:50%; top:50%;width:32px; height:32px;margin-left:-16px;margin-top:-16px;"  src="images/indicator_white_large.gif" />';

	if(typeof(callback)=='undefined')callback=function(){}
	FB_RequireFeatures(["Api"], function(){ 
		
		// Create an ApiClient object, passing app's API key and 
		// a site relative URL to xd_receiver.htm 
		FB.Facebook.init(api_key, channel_path); 
		var api = FB.Facebook.apiClient; 	
		// require user to login 

		FB.Facebook.apiClient.users_hasAppPermission('read_stream',function(res,ex){
			if( res ) {
				//api.requireLogin(function(exception){
				//result=api.stream_get(71416667187, '', '', 15, '', function(d) {document.write(dump(d))});
				result=api.stream_get(71416667187, '', '', 10, '', function(d) {
					//fromid ==> comment, actor_id ==> post		
					var stream_html = '';
					var curr_profile;
					if (d['posts'] != null) {
						posts = d['posts'];
						profiles = d['profiles'];
						for (i=0;i<posts.length;i++) {
							if (posts[i]['message'].length > 0) {
	
								for (x=0;x<profiles.length;x++) {
									if (profiles[x]['id'] == posts[i]['actor_id']) {
										curr_profile = x;
									}
								}
								var datenumber =posts[i]['created_time'];
								_date=new Date();
								_date.setTime(datenumber*1000);								
								
								stream_html += 	'<div style="width:100%" class="clearfix">';
								stream_html += 	'	<div style="width:50px; height:50px;float:left; margin:5px; position:relative;background:url('+profiles[curr_profile]['pic_square']+') center no-repeat" class="clearfix">';
								stream_html +=  '		<a href="'+profiles[curr_profile]['url']+'?ref=mf" title="'+profiles[curr_profile]['name']+'" target="_blank">';
								stream_html +=  ' 			<img src="images/spacer.gif" alt="'+profiles[curr_profile]['name']+'" style="width:50px; height:50px;"  />';
								stream_html +=  '			<img src="images/neworori/fbsmall.png" alt="" style="position:absolute;right:1px;bottom:1px;" />';
								stream_html +=  '		</a>';
								stream_html += 	'	</div>';
								stream_html +=  '   	<b>';
								if (profiles[curr_profile]['url'] != null) {stream_html +=  '    		<a href="'+profiles[curr_profile]['url']+'?ref=mf" target="_blank">'+profiles[curr_profile]['name']+'</a> : ';}else{stream_html += profiles[curr_profile]['name'];}
								stream_html +=  '  	</b>';
								stream_html +=  '	" '+posts[i]['message']+ ' " <br />';
								stream_html +=  '	<span style="color:#999; font-size:10px;">';								
								stream_html +=  			_date;
								stream_html +=  '	</span>';								
								stream_html += '</div>';
								if (posts[i]['comments']['count'] > 0)  stream_html +=  '			<div class="comment_box_nub"></div>';
								stream_html += '<div >';
	
								stream_html +=  '<div id="commentable_item_3711105_115598832187" class="commentable_item with_comments autoexpand_mode" data-comment="{&quot;source&quot;:&quot;0&quot;,&quot;viewer&quot;:&quot;537164285&quot;,&quot;target_fbid&quot;:&quot;115598832187&quot;,&quot;target_owner&quot;:&quot;71416667187&quot;,&quot;target_owner_name&quot;:&quot;ORORI Wedding Rings&quot;,&quot;item_id&quot;:&quot;3711105&quot;,&quot;type_id&quot;:&quot;100&quot;,&quot;assoc_obj_id&quot;:&quot;1654227286&quot;,&quot;check_hash&quot;:&quot;9e23a56ce39818c6&quot;,&quot;extra_story_params&quot;:[],&quot;source_app_id&quot;:&quot;&quot;}">';
								stream_html +=  '		<div class="comment_box">';
								if (posts[i]['comments']['count'] > 0)  stream_html +=  '			<div class="comment_box_nub"></div>';
	
								comment = posts[i]['comments']['comment_list'];
								for (j=0;j<posts[i]['comments']['comment_list'].length;j++) {
									//searc profile
									for (y=0;y<profiles.length;y++) {
											if (profiles[y]['id'] == posts[i]['comments']['comment_list'][j]['fromid']) {
												curr_profile = y;
											}
									}
								
									stream_html +=  '				<div id="comment_3711105_115598832187_3717496" class="ufi_section" style="width:130px;">';
									stream_html +=  '					<div class="comment_profile_pic">';
									if (profiles[curr_profile]['url'] != null) stream_html +=  '						<a href="'+profiles[curr_profile]['url']+'?ref=mf" title="'+profiles[curr_profile]['name']+'" target="_blank">';
									stream_html +=  '							<span class="UIRoundedImage UIRoundedImage_SMALL UIRoundedImage_GIRLIE">';
									stream_html +=  '								<img src="'+profiles[curr_profile]['pic_square']+'" alt="'+profiles[curr_profile]['name']+'" class="UIRoundedImage_Image">';
									stream_html +=  '								<img src="http://static.ak.fbcdn.net/rsrc.php/z821J/hash/jchlj5zu.png" alt="" class="UIRoundedImage_CornersSprite">';
									stream_html +=  '							</span>';
									if (profiles[curr_profile]['url'] != null)  stream_html +=  '						</a>';
									stream_html +=  '					</div>';
									stream_html +=  '					<div >';
									stream_html +=  '						<div class="comment_text" style="width:100%;">';
									if (profiles[curr_profile]['url'] != null) stream_html +=  '							<a href="'+profiles[curr_profile]['url']+'?ref=mf" class="comment_author" target="_blank">';
									stream_html +=  '<b>' + profiles[curr_profile]['name'] + '</b>';
									if (profiles[curr_profile]['url'] != null) stream_html +=  '							</a>';
									stream_html +=  '							: " '+comment[j].text.replace(/\n/gi, "<br /> \n") + ' "';
									stream_html +=  '						</div>';
									stream_html +=  '					</div>';
									stream_html +=  '				</div>';
	
								}
			
								stream_html +=  '		</div>';
								stream_html +=  '</div>';
								stream_html +=  '</div>			';
							}
						}
						$('orori-friendstream').innerHTML = stream_html;
					}
				});
			//}); 			
			}			
			else {
				var stream_html = '';
				 stream_html += '<div style="margin-top:10px;">';
				 stream_html += '<h1>ORORI need your permission to access your\'s stream and display it on this page</h1>';
				 stream_html += '<h1>Plase <a href="javascript:void(0);" onclick="FB.Connect.showPermissionDialog(\'read_stream\', function(){getStream()}, true, [71416667187]);">click here</a> to grant permission</h1>';
				 
				 stream_html += '<br /><br /><br /><br /><p><strong>Thank You</strong></p>';
				 stream_html += '<p><strong>ORORI</strong></p>';
				 
				 stream_html += '</div>';
				$('orori-friendstream').innerHTML = stream_html;
			}
		});	
	}); 
}
