var BF = {};
BF.OStoken  = MyOpenSpace.MySpaceContainer.OSToken;
BF.callback = 'http://apps.befunky.com/effects/myspace';
BF.appUrl   = '';
BF.prUrl = '';
BF.loading_='<center><img src="http://http.cdnlayer.com/befunky/appimages/progress.gif"></center>';
BF.loading_1='<center><img src="http://http.cdnlayer.com/befunky/appimages/imgloader.gif"  border=0></center>';
BF.refresh_  = 'There was some problem reaching MySpace. <a href="'+BF.appUrl +'" target="_top">Please refresh browser</a>!';
BF.slowPC  = '';
BF.viewer 	= {};
BF.owner = {};
BF.sendFriend = {};
BF.options = {};
BF.efName='Charcola';//application name
BF.prsts='1';//application name
BF.QRdat = {};
BF.Page='1';
BF.countReq=0;
var resimler=new Array();
var os_;
var dataReqObj_;
var objHttpFileDataRequest = null;

function TimeAl()
{
	now = new Date();
	return now.getTime();
}
function is_int(variable)
{
	return variable.constructor === Number && Math.round(variable, 0) === variable;
}

function init(){
	var os = opensocial.Container.get();
    var dataReqObj = os.newDataRequest();
     var param = {};
    var viewerReq = dataReqObj.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER);
    dataReqObj.add(viewerReq, 'VIEWER');
   	var viewerFriendsReq = dataReqObj.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS);
    dataReqObj.add(viewerFriendsReq, 'VIEWER_FRIENDS');
    
    dataReqObj.send(function(dataResponse){
      	if (dataResponse.hadError()) {
           // var data = dataResponse.get(opensocial.DataRequest.Group.VIEWER_FRIENDS);
		  // top.location.href = BF.prUrl;
        } else {
            var viewerData = dataResponse.get(opensocial.DataRequest.PersonId.VIEWER).getData();
            BF.viewer.id = viewerData.getField(opensocial.Person.Field.ID);
            BF.viewer.name = viewerData.getDisplayName();
            BF.viewer.gender = viewerData.getField(opensocial.Person.Field.GENDER);
            BF.viewer.image = viewerData.getField(opensocial.Person.Field.THUMBNAIL_URL);
			BF.viewer.image = BF.viewer.image.replace("s_", "l_");
			BF.viewer.image = BF.viewer.image.replace("_s", "_l");
            BF.viewer.friends = [];
            BF.viewer.friend_ids = [];
			
            var friendsData = dataResponse.get(opensocial.DataRequest.Group.VIEWER_FRIENDS).getData();
            var friendStr = '';
            friendsData.each(function(friendData){
                if (friendData['getField']) {
                    var friendName = friendData.getDisplayName();
                    var friendId = friendData.getField(opensocial.Person.Field.ID);
                    var friendGender = friendData.getField(opensocial.Person.Field.GENDER);
                    var friendThumbnailUrl = friendData.getField(opensocial.Person.Field.THUMBNAIL_URL);
                    var friendBigImageUrl = friendThumbnailUrl.replace("s_", "l_");
                    BF.viewer.friends.push({
                        id: friendId,
                        name: friendName,
                        gender: friendGender,
                        image: friendThumbnailUrl,
                        big_image: friendBigImageUrl
                    });
                }
            });
        }
    });
	
}

function newsFeed()
{
	var activityParams1 = {};
    activityParams1[opensocial.Activity.Field.TITLE] =BF.viewer.name+'  created a photo '+BF.efName+' with <a href="'+BF.prUrl+'">'+BF.efName+'!</a> Have a look at it ;)';
    activityParams1[opensocial.Activity.Field.BODY]  = 'With '+BF.efName+' you can turn your photos into '+BF.efName+' and add graphics.';
	activityParams1[opensocial.Activity.Field.STREAM_FAVICON_URL]  = 'http://i1.bebo.com/044a/AppIcon/7356574107/S5332362656.png';
	
	var mediaItems1 = [];
    var mediaItem1 = '';
   	mediaItem1 = opensocial.newActivityMediaItem(opensocial.Activity.MediaItem.Type.IMAGE, 'http://i2.bebo.com/040b/13/medium/2008/07/09/14/4881016034a8260033276m.jpg');
   	mediaItem1.setField(hi5.ActivityMediaItemField.LINK, BF.prUr);
    mediaItems1.push(mediaItem1);
	activityParams1[opensocial.Activity.Field.MEDIA_ITEMS] = mediaItems1;
    var activity1 = opensocial.newActivity(activityParams1);
    opensocial.requestCreateActivity(activity1, opensocial.CreateActivityPriority.LOW);
}



function SketchCreated(){
	//init();
	if(objHttpFileDataRequest!=null)
	{
		objHttpFileDataRequest.stop();
	}
	if(BF.viewer.id=='undefined' && BF.viewer.id==null)
	{
		init();	
	}
	$("photo_bar").innerHTML='<div class="summary">Here you can see your '+BF.efName+' that you made before. Click on your '+BF.efName+' in order to zoom In,<br />send to your friend or set as your profile '+BF.efName+'.</div>';
	$("submenu").innerHTML='';
	var menu='<a onclick="SketchCreated();" class="aselected" href="#"> '+BF.efName+' Created</a>';
	menu+=' <span class="pipe">|</span> '; 
	menu+=' <a onclick="BF.SketchesRecieved();" href="#">'+BF.efName+' Recieved</a>';
	menu+=' <span class="pipe">|</span> '; 
	menu+=' <a onclick="BF.ProfileBoxGet();" href="#">Profile Box Appearance</a>';
	$("submenu").innerHTML=menu;
	if(BF.Page=='1')
	{
	$("photo_container").innerHTML='<center><b>Please wait we are loading your '+BF.efName+'.</b></center><br>'+BF.loading_;
	 
	}
	if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
	{
		postdata = 'mode=SketchCreated&userID='+BF.viewer.id+'&prsts='+BF.prsts+'&effect='+BF.efName+'&name='+BF.viewer.name+'&page='+BF.Page+'&img_='+BF.viewer.image;
		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = postdata;
		url = BF.callback + '/fnc_query.php';
	   var ret='';
	   if (MyOpenSpace && MyOpenSpace.Ajax) {
			MyOpenSpace.Ajax.activeContentConnections = [ ];
		}
		objHttpFileDataRequest = gadgets.io.makeRequest(url,  MKResponseSC, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}
};

function MKResponseSC(obj)
{
	var ret= obj.text;
	if(ret!='undefined' && ret!='false' && ret!='' && ret!='0' && ret!='<html><body><h1>Server is too busy</h1></body></html>' && ret!='Server is too busy' && ret != null && ret.substr(0,5) !="Ooops")
	{
	   BF.countReq=0;
	   if(BF.Page=='1')
	   {
		   if(ret.substr(0,6)!='&nbsp;')
		   {
				BF.Page='2';
		  		SketchCreated();
				$("photo_container").innerHTML=ret;
				$("mdetail").innerHTML=BF.loading_1; 
			}
			else
			{
				$("photo_container").innerHTML=ret;
			}
		  
		}
		else if(BF.Page=="2")
		{
			BF.Page='3';
		  	SketchCreated();
			$("mdetail").innerHTML=ret;
			$("hdetail").innerHTML=BF.loading_1;
		}
		else if(BF.Page=="3")
		{
			BF.Page='1';
			$("hdetail").innerHTML=ret;	
		}
	}
	else
	{
		BF.countReq++;
		if(BF.countReq<6)
		{
			SketchCreated();
		}
	}
};


BF.SketchesRecieved = function(){
	//init();
	$("photo_container").innerHTML=BF.loading_;
	 postdata = 'mode=SketchRecieved&prsts='+BF.prsts+'&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = postdata;
   	url = BF.callback + '/fnc_query.php';
   var ret='';
   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
	if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
	{
			gadgets.io.makeRequest(url, function(r){
			   
					ret=r.text;
					if(r.text!='undefined' && r.text!='<html><body><h1>Server is too busy</h1></body></html>' && r.text!='Server is too busy' && r.text != null)
					{
						$("photo_container").innerHTML=ret;
						
					}
					else
					{
						BF.SketchesRecieved();
					}
				
		  }, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}

};

BF.WprPhoto = function()
{
	//init();
	if(BF.viewer.image!='')
	{
		$("photo_container").innerHTML=BF.loading_;
		var img_='<a onclick="BF.SketchImageNow(\''+BF.viewer.image+'\');" href="#"><img src="'+BF.viewer.image+'" border="0" /></a><br><br><a class="abutton" href="#" onclick="BF.SketchImageNow(\''+BF.viewer.image+'\');">'+BF.efName+' this photo</a>'
		$("photo_container").innerHTML=img_;
	}
};



BF.picImage = function() {
	//init();
	$("photo_bar").innerHTML='<div class="summary">Here you can select an album or photo to '+BF.efName+'</div>';
	$("submenu").innerHTML='';
	var menu='<a class="aselected" href="#" onclick="BF.picImage();">Your Albums</a>';
	menu+=' <span class="pipe">|</span> ';
	menu+=' <a href="#" onclick="BF.WprPhoto();"> Your Profile Photo</a> ';
	$("submenu").innerHTML=menu;
	$("photo_container").innerHTML='<center><b>Please wait we are loading your photo(s).</b></center><br>'+BF.loading_;
	var os = opensocial.Container.get();
	var dataReqObj = os.newDataRequest();
	var param = {};
	var ownerPhotosOnlyKey = 'optkeygetPhotosOWNER';
	
	dataReqObj.add(os.newFetchAlbumsRequest(opensocial.DataRequest.PersonId.OWNER, param), ownerPhotosOnlyKey);
	dataReqObj.send(function(dataResponse) {
		
		if (dataResponse.hadError()) {
				if(dataResponse.get("optkeygetPhotosOWNER").getErrorCode() == opensocial.ResponseItem.Error.UNAUTHORIZED) {
				opensocial.requestPermission(["accesstopublicvideosphotos"], "Because we need your photos!", checkPhotoPermissionResponse);
			}
		} else {
				//console.log(dataResponse);
				var ownerAlbums = dataResponse.get(ownerPhotosOnlyKey).getData();
				var dataReqObj2 = os.newDataRequest();
				
				var picsStr='<table width="100%" border="0" cellpadding="0" cellspacing="0"> <tbody><tr class="photo_row">';
				var sayac=0;
				ownerAlbums.each(function(al){
					var album_id = al.ALBUM_ID;
					var param = {};
					picsStr += '<td class="photo_cell" valign="middle"><a onclick="BF.albumSelect('+al.ALBUM_ID+');" href="#"><img src="'+al.DEFAULT_IMAGE+'" border="0" /></a><br><a class="abutton" onclick="BF.albumSelect('+sayac+');" href="#">Explore this album</a><div class="title">'+ al.TITLE+'</div><div><div class="detail">('+(al.PHOTO_COUNT)+' photos)</div></div></td>';
					sayac++;
					if (is_int(sayac/3)) {
						picsStr += '<tr class="metadata_row"><td><br /></td><td><br /></td><td></td></tr><tr class="photo_row">';
					}
					
				});
				picsStr += '</tbody></table>';
		
				$("photo_container").innerHTML=picsStr;
		}
	});
};

BF.albumSelect = function(album_id) {
	//init();
	//alert(resimler[album_id]);
	$("photo_bar").innerHTML='<div class="summary">Choose a photo from your album My Album</div>';
	$("photo_container").innerHTML='<center><b>Please wait we are loading your photo(s).</b></center><br>'+BF.loading_;
	
	var os = opensocial.Container.get();
	var dataReqObj = os.newDataRequest();
	var ownerPhotosOnlyKey = 'optkeygetPhotosOWNER';
	var param = {};
	param[MyOpenSpace.DataRequest.PhotoRequestFields.ALBUM_ID] = album_id;
	dataReqObj.add(os.newFetchPhotosRequest(opensocial.DataRequest.PersonId.VIEWER, param), ownerPhotosOnlyKey);
	dataReqObj.send(picsDataLoadCallback);
};

function picsDataLoadCallback(dataResponse){
	var picsStr='<table width="100%" border="0" cellpadding="0" cellspacing="0"> <tbody><tr class="photo_row">';
	var ownerPhotosOnlyKey = 'optkeygetPhotosOWNER';
	ownerPhotos = dataResponse.get(ownerPhotosOnlyKey).getData();
	var i=1;
	ownerPhotos.each(function(ownerPhotos){
			var imgUrl = ownerPhotos.getField(MyOpenSpace.Photo.Field.IMAGE_URI);
			limgUrl = imgUrl.replace("_l", "_s"); // hack to get small image uri from large
			limgUrl = limgUrl.replace("l_", "s_");
			if ('undefined' != typeof(imgUrl) && imgUrl != '') {
				picsStr += '<td class="photo_cell" valign="middle"><a onclick="BF.SketchImageNow(\''+imgUrl+'\');" href="#"><img src="'+limgUrl+'" border="0" /></a><br><br><a class="abutton" href="#" onclick="BF.SketchImageNow(\''+imgUrl+'\');">'+BF.efName+' this photo</a></td>';
			
				if (is_int(i/3)) {
						picsStr+= '<tr class="metadata_row"><td><br /></td><td><br /></td><td></td></tr><tr class="photo_row">';
				}
				i++;
			}
		});
		picsStr += '</tbody></table>';
		$("photo_container").innerHTML=picsStr;
}

BF.SketchImageNow = function(img_url,frid,frname)
{
		//init();
		$("photo_bar").innerHTML='<div class="summary">Our artists '+BF.efName+' your photo, please choose the one you like most or Choose another photo to '+BF.efName+'</div>';
		if(BF.Page=='1')
	   {
			$("photo_container").innerHTML='<center><b>Be patient while our artist are working on your photo.</b></center><br>'+BF.loading_;
	   }
		 BF.QRdat.img_url= img_url;
		 postdata = 'mode=SketchImage&prsts='+BF.prsts+'&effect='+BF.efName+'&userID='+BF.viewer.id;
		 if(frid!=null)
		 {
			postdata +='&frid='+frid+'&frname='+frname;
			 BF.QRdat.frid= frid;
			  BF.QRdat.frname= frname;
		}
		postdata += '&page='+BF.Page+'&img_url='+img_url;
		
	if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
	{	
		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = postdata;
		url = BF.callback + '/fnc_query.php';
	  	if (MyOpenSpace && MyOpenSpace.Ajax) {
			MyOpenSpace.Ajax.activeContentConnections = [ ];
		}
		objHttpFileDataRequest=gadgets.io.makeRequest(url, MKResponseSIN, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}
};

function MKResponseSIN(obj)
{
	var ret= obj.text;
	if(ret!='undefined' && ret!='false' && ret!='0' && ret!='' && ret!='<html><body><h1>Server is too busy</h1></body></html>' && ret!='Server is too busy' && ret != null && ret.substr(0,5) !="Ooops")
	{
	   BF.countReq=0;
	   if(BF.Page=='1')
	   {
		   BF.Page='2';
		   if(BF.QRdat.frid!=null)
			{
				BF.SketchImageNow(BF.QRdat.img_url,BF.QRdat.frid,BF.QRdat.frname);
			}
			else
			{
				BF.SketchImageNow(BF.QRdat.img_url);	
			}
			$("photo_container").innerHTML=ret;
			$("mdetail").innerHTML=BF.loading_1;
		}
		else if(BF.Page=="2")
		{
			BF.Page='3';
		   if(BF.QRdat.frid!=null)
			{
				BF.SketchImageNow(BF.QRdat.img_url,BF.QRdat.frid,BF.QRdat.frname);
			}
			else
			{
				BF.SketchImageNow(BF.QRdat.img_url);	
			}
			$("mdetail").innerHTML=ret;
			$("hdetail").innerHTML=BF.loading_1;
		}
		else if(BF.Page=="3")
		{
			BF.Page='1';
			$("hdetail").innerHTML=ret;	
		}
	}
	else
	{
		BF.countReq++;
		if(BF.countReq<6)
		{
			if(BF.QRdat.frid!=null)
			{
				BF.SketchImageNow(BF.QRdat.img_url,BF.QRdat.frid,BF.QRdat.frname);
			}
			else
			{
				BF.SketchImageNow(BF.QRdat.img_url);	
			}
		}
	}
};


BF.SketchSave = function(img_name,frid,frname)
{
		//init();
		$("photo_bar").innerHTML='<div class="summary">Here is your final '+BF.efName+'. You can post it to your profile or save to an album.</div>';
		$("photo_container").innerHTML=BF.loading_;
		 postdata = 'mode=SketchSave&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&img_name='+img_name;
		  if(frid!=null)
		 {
			postdata +='&frid='+frid+'&frname='+frname;
			
		}
		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = postdata;
		url = BF.callback + '/fnc_query.php';
	   var ret='';
	   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
	if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
	{
		gadgets.io.makeRequest(url, function(r){
		 	ret=r.text;
			if(r.text!='undefined' && r.text!='<html><body><h1>Server is too busy</h1></body></html>' && r.text!='Server is too busy' && r.text != null)
			{
				$("photo_container").innerHTML=ret;
				
			}
			else
			{
				BF.SketchSave(img_name,frid,frname);	
			}
			
		
	  }, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}
};


BF.ShGet = function (img_name,pay)
{
	//init();
	$("photo_bar").innerHTML='<div class="summary">In this page you can save your '+BF.efName+' to your gallery, send it to a friend or set as your profile '+BF.efName+'.<a href="#" onclick="SketchCreated();">Back to your '+BF.efName+'</a></div>';
	$("submenu").innerHTML='';
		$("photo_container").innerHTML=BF.loading_;
		 postdata = 'mode=ShGet&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&img_name='+img_name;
		 if(pay!=null)
		 {
				 postdata +='&pay=1';
		}
		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = postdata;
		url = BF.callback + '/fnc_query.php';
	   var ret='';
	   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
		if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
		{
			gadgets.io.makeRequest(url, function(r){
					ret=r.text;
					if(r.text!='undefined' && r.text!='<html><body><h1>Server is too busy</h1></body></html>' && r.text!='Server is too busy' && r.text != null)
					{
						$("photo_container").innerHTML=ret;
						
					}
					else
					{
						BF.ShGet(img_name,pay);	
					}
						
				
			  }, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}
};

BF.ProfileBoxGet = function()
{
	//init();
	$("photo_bar").innerHTML='<div class="summary">Here you can see your '+BF.efName+' that you made before. Click on your '+BF.efName+' in order to zoom In,<br />send to your friend or set as your profile '+BF.efName+'.</div>';
	$("photo_container").innerHTML=BF.loading_;
	 postdata = 'mode=ProfileBoxGet&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&name='+BF.viewer.name;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = postdata;
   	url = BF.callback + '/fnc_query.php';
   var ret='';
   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
	if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
	{
 		gadgets.io.makeRequest(url, function(r){
      	ret=r.text;
		if(r.text!='undefined' && r.text!='<html><body><h1>Server is too busy</h1></body></html>' && r.text!='Server is too busy' && r.text != null)
			{
				$("photo_container").innerHTML=ret;
				
			}
			else
			{
				BF.ProfileBoxGet();
			}
	  }, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}
};



BF.postTo = function(target, subject, content, user) {
	var osContainer = opensocial.Container.get();
	BF.supportedTargets = osContainer.getMySpaceEnvironment().getSupportedPostToTargets();
	//["COMMENTS", "BLOG", "BULLETINS", "PROFILE", "SEND_MESSAGE"]
	// create a new message object passing in the content to be posted and then set the subject and target fields
	var message = opensocial.newMessage(content);
	message.setField(opensocial.Message.Field.TITLE, subject);
	message.setField(opensocial.Message.Field.TYPE, target);
	// show post to
	osContainer.postTo(BF.OStoken, message, user);
};



BF.inviteFriends = function(){
	//init();
	var img ='';
	if(BF.efName == 'Sunburst') { img = '<img src="http://c1.ac-images.myspacecdn.com/images02/18/l_1e20067ead19439491ca59dbee999ba8.jpg"/>'; }
	else if(BF.efName == 'LineArtopia') {img = '<img src="http://c1.ac-images.myspacecdn.com/images02/33/l_08d91dd493ec40748f710982c9a4dd08.jpg"/>';}
	else if(BF.efName == 'Patriot') {img = '<img src="http://c2.ac-images.myspacecdn.com/images02/59/l_b0ed87c55d4f42bd9eb2fce57ce57c49.jpg"/>';}
	else if(BF.efName == 'Scribbler') {img = '<img src="http://c2.ac-images.myspacecdn.com/images02/25/l_8d29ed95935d40dab95c838c158e41c1.jpg"/>';}
	else if(BF.efName == 'Stenciler') {img = '<img src="http://c2.ac-images.myspacecdn.com/images02/54/l_559dea912e98470489ed4e55c8e06e3d.jpg"/>';}
	else if(BF.efName == 'Warholizer') {img = '<img src="http://c1.ac-images.myspacecdn.com/images02/57/l_8ebf22aac53643c8898b6efba21b0520.jpg"/>';}
	else if(BF.efName == 'CartoonMe') {img = '<img src="http://c1.ac-images.myspacecdn.com/images02/7/l_c3e70750d39948c29f646c47fd0671d4.jpg"/>';}
	else if(BF.efName == 'Charcola') {img = '<img src="http://c2.ac-images.myspacecdn.com/images02/49/l_479493c3b70e4a2e9b0b6d1f78e5b129.jpg"/>';}
	else if(BF.efName == 'Inkify') {img = '<img src="http://c2.ac-images.myspacecdn.com/images02/56/l_7fbb49cb50e9405ca2e70d7c49d06229.jpg"/>';}
	
	
	BF.postTo('BULLETINS', '<b>Turn Your photo into a '+BF.efName+'</b>', '<font size="2"><a href="'+BF.prUrl+'">Click here to turn your photo into '+BF.efName+'</a><br> I created a photo with '+BF.efName+'! Have a look at it ;)</font> <br>'+img, BF.viewer.object);
	
};

BF.showFriends = function(img_link){
	//init();
	var picsStr = '<b>Send this '+BF.efName+' to your friends!</b><br>';
	for(i=0;i<BF.viewer.friends.length;i++)
	{
		var friend=BF.viewer.friends[i];
		if ('undefined' != typeof(friend.image) && friend.image != '') {
		    picsStr += ' <p><a  class="friendbutton" onclick="BF.sendNotification(\''+friend.id+'\',\''+img_link+'\',\''+fc_(friend.name)+'\');">'+friend.name+'</a></p>';
		}
	}
	
	$("friendSelect").innerHTML=picsStr;
};


BF.skShowFriends = function(){
	//init();
	$("photo_bar").innerHTML='<div class="summary">Here you can '+BF.efName+' a friend s photo</div>';
	$("submenu").innerHTML='';
	$("photo_container").innerHTML=BF.loading_;
	var picsStr = '<table width="100%" border="0" cellpadding="0" cellspacing="0"> <tbody><tr class="photo_row">';
	var ss=1;
	for(i=0;i<BF.viewer.friends.length;i++)
	{
		var friend=BF.viewer.friends[i];
		if ('undefined' != typeof(friend.image) && friend.image != '') {
			 picsStr +='<td class="photo_cell" valign="middle"><a onclick="BF.SketchImageNow(\''+friend.big_image+'\',\''+friend.id+'\',\''+fc_(friend.name)+'\');" href="#"><img src="'+friend.image+'" border="0" /></a><br>'+friend.name+'<br><a class="abutton" href="#" onclick="BF.SketchImageNow(\''+friend.big_image+'\',\''+friend.id+'\',\''+fc_(friend.name)+'\');">'+BF.efName+' this photo</a></td>'
			 if (is_int(ss/3)) {
			picsStr+= '<tr class="metadata_row"><td><br /></td><td><br /></td><td></td></tr><tr class="photo_row">';
			}
			ss++;
		}
		
	}
	picsStr+='</tbody></table>';
	$("photo_container").innerHTML=picsStr;
};

BF.ProfileBoxPost = function(img_name){
	$("prfilebox").innerHTML=BF.loading_;
	 postdata = 'mode=ProfileBoxPost&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&img_name='+img_name;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = postdata;
   	url = BF.callback + '/fnc_query.php';
   var ret='';
   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
	if(BF.viewer.id!='undefined' && BF.viewer.id!=null)
	{
		gadgets.io.makeRequest(url, function(r){
		   ret=r.text;
		   $("prfilebox").innerHTML=ret;
	  }, params);
	}
	else
	{
			$("photo_container").innerHTML= 'It seems that you did not add the application yet. Please click the "Add App" link located at the top to start turning your photos into artwork.';
	}
};

BF.SNotifCount = function(toIds,imglink){
	postdata = 'mode=NotifCount&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&name='+BF.viewer.name+'&imglink='+imglink+'&frid='+toIds;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = postdata;
   	url = BF.callback + '/fnc_query.php';
   var ret='';
   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
 	gadgets.io.makeRequest(url, function(r){
  }, params);

};

BF.sendNotification = function(toIds,imglink,frndname,frndname1) {
	//init();
	var nitification='';
	if(frndname1!=null)
	{
		nitification='Hi '+frndname1+', <br><br>'+BF.viewer.name+' sent you a '+BF.efName+' using '+BF.efName+' application <a href="'+BF.appUrl+'">Click here to see your '+BF.efName+'</a><br><a href="'+BF.appUrl+'"><img src="'+imglink+'" border="0"></a>';	
	}
	else
	{
		nitification='Hi '+frndname+', <br><br>'+BF.viewer.name+' sent you a '+BF.efName+' using '+BF.efName+' application <a href="'+BF.appUrl+'">Click here to see your '+BF.efName+'</a><br><a href="'+BF.appUrl+'"><img src="'+imglink+'" border="0"></a>';		
	}
	BF.SNotifCount(toIds,imglink);
	/*var params = new Object(); 
    params[opensocial.Message.Field.TYPE] = opensocial.Message.Type.PRIVATE_MESSAGE;
	params[opensocial.Message.Field.TITLE]='Turn Your photo into a '+BF.efName+'';*/
    
	var message = opensocial.newMessage(nitification);
	message.setField(opensocial.Message.Field.TITLE, 'Turn Your photo into a '+BF.efName);
	message.setField(opensocial.Message.Field.TYPE, 'COMMENTS');
	
	//var message = opensocial.newMessage(nitification, params); 
    opensocial.requestSendMessage(toIds, message);
	
};

BF.DelPht = function (img_name)
{
	//init();
	$("photo_container").innerHTML=BF.loading_;
	 postdata = 'mode=DelPht&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&img_name='+img_name;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = postdata;
   	url = BF.callback + '/fnc_query.php';
   var ret='';
   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
 	gadgets.io.makeRequest(url, function(r){
       ret=r.text;
	   SketchCreated();
	   BF.TabReplace('tabs1');
 	 }, params);
};

BF.FeedBack = function()
{
	$("photo_bar").innerHTML='<div class="summary">Send us what you think about '+BF.efName+' and what new features you would like to see in the future.</div>';
	$("submenu").innerHTML='';
	$("photo_container").innerHTML=BF.loading_;
	var frm="<textarea class=sendfeedbackTextbox id=feedback ></textarea><p><input class=abuttonlarge onclick=\"BF.FeedBSend(fc_($(\'feedback\').value));\" type=button value=\"Send Feedback\"/></p>";
	$("photo_container").innerHTML=frm;
};

BF.FeedBSend= function(feed)
{
	$("photo_container").innerHTML=BF.loading_;
	 postdata = 'mode=FeedBackSend&effect='+BF.efName+'&prsts='+BF.prsts+'&userID='+BF.viewer.id+'&feed='+feed;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = postdata;
   	url = BF.callback + '/fnc_query.php';
   var ret='';
   if (MyOpenSpace && MyOpenSpace.Ajax) {
		MyOpenSpace.Ajax.activeContentConnections = [ ];
	}
 	gadgets.io.makeRequest(url, function(r){
       ret=r.text;
	   $("photo_container").innerHTML=ret;
  }, params);	
};

function fc_(text) { var temp; temp = encodeURIComponent(text); return temp; }
BF.TabReplace = function (tab_)
{
	$("tabs1").className='tab-item';
	$("tabs2").className='tab-item';
	$("tabs3").className='tab-item';
	$("tabs4").className='tab-item';
	//$("tabs5").className='tab-item';
	$(tab_).className='tab-item current';
};