/**
 * Object contains functions that Likeness uses to do viral stuff.....
 */
var MOVIES_DOMAIN = RY_URL_PATH_SHORT;
var HI5_DOMAIN = HI5_URL_PATH_SHORT;

function ViralityChannels()
{
	this.quizId        = 16 * 5 - 4;
	this.quizTitle     = "Austin Power - Spy Who Shagged Me!";
	this.quizScore     = 0;
	this.viewer        = null;
	this.viewerFriends = null;
	this.url           = "http://" + HI5_DOMAIN + "/friend/apps/entry/" + MOVIES_DOMAIN + "/google_apps/movies/view/Movies_XML.php";
	this.clickhere     = '<a href="http://' + HI5_DOMAIN + '/friend/apps/entry/' +
		MOVIES_DOMAIN + '/google_apps/movies/view/Movies_XML.php">click here</a>';

	this.friend_loaded_callback = null;
	this.targetIds = null;

	this.getQuizURL = function(quizId)
	{
		var view_params = "?view-params=";

		if(container == "orkut.com" || container == "myspace.com")
		{
			view_params = "&appParams=";
		}

		var view_params_params = escape("{\"pagename\":\"" + escape("MoviesMultiQuizPage?quizId=" + quizId) + "\"}");

		return this.url + view_params + view_params_params;
	}

	this.shareApplication = function(opt_message, opt_callback)
	{
		if(opt_message == undefined || opt_message == null || opt_message.length == 0)
		{
			opt_message = opensocial.newMessage("Select friends whose scores you want to see on the \"" + this.getQuizTitle() + "\" quiz, and click \"Invite Friends\"!  After they take this quiz, you will be notified :)");
		}
		else
		{
			opt_message = opensocial.newMessage(opt_message);
		}

		if(opt_callback != undefined && opt_callback != null)
		{
			opensocial.requestShareApp(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_message, opt_callback);
		}
		else
		{
			opensocial.requestShareApp(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_message);
		}

		return true;
	}

	this.getTrackURL = function(event, url)
	{
		return "http://www.rockyou.com/events/track.php?event=" + event + "&url=" + url;
	}

	this.sendDefaultNotification = function(from_who)
	{
        var messageParams = {};
		messageParams[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;

        var text = "";

        if(from_who != null)
        {
        	text += from_who.getDisplayName();
		}
		else
		{
			text += "Your friend";
		}

		text += " wants to compare movie tastes with you. ";
		text += "<a href=\"" +  this.getTrackURL("hi5-movies-notif-postinstall", this.url) + "\">";
		text += "Click here to compare movie tastes with ";
        
        if(from_who != null)
        {
            text += from_who.getDisplayName();
        }
        else
        {
            text += "your friend";
        }
        
        text += "</a>!";

		opt_notification_text = opensocial.newMessage(text, messageParams);

		if(this.targetIds != null)
		{
        	var notification_ids = this.targetIds.split(",");

			if(notification_ids.length > 100)
			{
            	var buckets = Array();
				var i = 0;
				while (i < notification_ids.length)
				{
					var batch = notification_ids.slice(i, (i+100));
					buckets.push(batch);
					i += 100;
				}

				for(var k = 0; k<buckets.length; k++)
				{					
					var res = opensocial.requestSendMessage(buckets[k], opt_notification_text);
				}
			}
			else
			{
				var res = opensocial.requestSendMessage(notification_ids, opt_notification_text);
			}
		}
		else
		{
			var res = opensocial.requestSendMessage(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_notification_text);
		}

		return true;
	}

	this.sendNotification = function(from_who, opt_notification_text)
	{
		var messageParams = {};
		messageParams[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;

		/*
		[quiz taker full name] just challenged your movie knowledge. <link to quiz>See if you can beat [quiz taker first name]'s score of [quiz taker's score]% on the [quiz title] quiz</link>.
		*/

        var text = "";

        if(from_who != null)
        {
        	text += from_who.getDisplayName();
		}
		else
		{
			text += "Your friend";
		}

		text += " just challenged your movie knowledge. ";
		text += "<a href=\"" +  this.getTrackURL("hi5-movies-notif-accepted", this.getQuizURL(this.getQuizId())) + "\">";
		text += "Click here to accept this challenge</a> and beat their ";
		//text += from_who.getDisplayName() + "'s";
		text += " score of " + this.quizScore + "% on the ";
		text += "<a href=\"" +  this.getTrackURL("hi5-movies-notif-accepted", this.getQuizURL(this.getQuizId())) + "\">";
		text += this.getQuizTitle() + "</a> quiz!";

		if(opt_notification_text == undefined || opt_notification_text == null ||
			opt_notification_text.length == 0)
		{
			opt_notification_text = opensocial.newMessage(text,
				 messageParams);
		}
		else
		{
			opt_notification_text = opensocial.newMessage(opt_notification_text, messageParams);
		}

		if(this.targetIds != null)
		{
			var notification_ids = this.targetIds.split(",");
            
			if(notification_ids.length > 100)
			{
            	var buckets = Array();
				var i = 0;
				while (i < notification_ids.length)
				{
					var batch = notification_ids.slice(i, (i+100));
					buckets.push(batch);
					i += 100;
				}

				for(var k = 0; k<buckets.length; k++)
				{
					var res = opensocial.requestSendMessage(buckets[k], opt_notification_text);
				}
			}
			else
			{
				var res = opensocial.requestSendMessage(notification_ids, opt_notification_text);
			}
		}
		else
		{
			var res = opensocial.requestSendMessage(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_notification_text);
		}

		return true;
	}

	this.sendEmail = function(opt_subject, opt_body, opt_receipients)
	{
        var messageParams = {};
		messageParams[opensocial.Message.Field.TYPE] = opensocial.Message.Type.EMAIL;

		if(opt_subject == undefined || opt_subject == null || opt_subject.length == 0)
		{
			opt_subject = "Take Movie Trivia Quiz";
		}

		if(opt_body == undefined || opt_body == null || opt_body.length == 0)
		{
			opt_body = "I want to see if you can beat me in <a href=\"" +
				this.getQuizURL(this.getQuizId())  + "\">" +
				this.getQuizTitle() + "</a> quiz, to take this Movie Trivia quiz to find out!";
		}

		if(opt_receipients == undefined || opt_receipients == null)
		{
			opt_receipients == opensocial.DataRequest.PersonId.VIEWER_FRIENDS;
		}

		messageParams[opensocial.Message.Field.TITLE] = opt_subject;

		var message = opensocial.newMessage(opt_body, messageParams);
		opensocial.requestSendMessage(opt_receipients, message);
	}

	this.createActivity = function(from_who, opt_title)
	{
        var activityParams = new Array();
		var mediaItems = new Array();
		var opt_body = null;

		var activity_track_code = "hi5-movies-feed-x";

		if(container != undefined && container == "orkut.com")
		{
			activity_track_code = "orkut-movies-feed-x";
		}

        if(opt_title == undefined || opt_title == null || opt_title.length == 0)
        {
        	opt_title = from_who.getDisplayName() + " just took " + this.quizScore +"% on the " + this.getQuizTitle() + " quiz.";
        	opt_body  = "<a href=\"" + this.getTrackURL(activity_track_code, this.getQuizURL(this.getQuizId())) + "\">Can you beat ";
        	opt_body += from_who.getDisplayName() + "'s score?</a>";
		}

		if(from_who == null)
		{
			from_who = this.viewer;
		}

    	activityParams[opensocial.Activity.Field.TITLE] = opt_title;
    	activityParams[opensocial.Activity.Field.BODY]  = opt_body;

    	var mediaItem = opensocial.newActivityMediaItem(opensocial.Activity.MediaItem.Type.IMAGE,
    		from_who.getField(opensocial.Person.Field.THUMBNAIL_URL));

    	var quizImage = opensocial.newActivityMediaItem(opensocial.Activity.MediaItem.Type.IMAGE,
    		"http://apps.rockyou.com/images/opensocial/movie_trivia/76/" + ((this.getQuizId() * 5) - 4) + ".jpg");

    	// Add a media item link if supported
    	try
    	{
    		if(gadgets.util.hasFeature('hi5') && opensocial.getEnvironment().supportsField(
    			opensocial.Environment.ObjectType.ACTIVITY_MEDIA_ITEM, hi5.ActivityMediaItemField.LINK))
    		{
      			//mediaItem.setField(hi5.ActivityMediaItemField.LINK, viewer.getField(opensocial.Person.Field.PROFILE_URL));
      			mediaItem.setField(hi5.ActivityMediaItemField.LINK,
      				this.getQuizURL(this.getQuizId())
        		);

        		quizImage.setField(hi5.ActivityMediaItemField.LINK,
        			this.getQuizURL(this.getQuizId())
        		);
    		}
		}
		catch(e)
		{
			/* Not on hi5, ignore */
		}

    	mediaItems.push(mediaItem);
    	mediaItems.push(quizImage);

    	activityParams[opensocial.Activity.Field.MEDIA_ITEMS] = mediaItems;
    	var activity = opensocial.newActivity(activityParams);


    	//activityParams[opensocial.Activity.Field.URL] = this.getQuizURL(this.getQuizId());

    	try
    	{
    		activity.setField(
    			opensocial.Activity.Field.URL,
    			this.getTrackURL("hi5-movies-feed-x", this.getQuizURL(this.getQuizId()) )
    		);
		}
		catch(e)
		{
			/* not supported */
		}
    	opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH);
	}

	this.loadFriends = function()
	{
    	var req = opensocial.newDataRequest();
	    req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
	    req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');
	    req.send(this.onLoadFriends.bind(this));
	}

	this.onLoadFriends = function(data)
	{
 		this.viewer = data.get('viewer').getData();
	    this.viewerFriends = data.get('viewerFriends').getData();

	    if(this.friend_loaded_callback != null)
	    {
	    	this.friend_loaded_callback();
		}
	}


	this.setQuizId = function(id)
	{
		this.quizId = id;
	}

	this.getQuizId = function()
	{
		return this.quizId; //((this.quizId * 5) - 4);
	}

	this.setQuizTitle = function(title)
	{
		this.quizTitle = title;
	}

	this.getQuizTitle = function()
	{
		return this.quizTitle;
	}

	this.setViewer = function(viewer)
	{
		this.viewer = viewer;
	}

	this.getViewer = function()
	{
		return this.viewer;
	}

	this.setViewerFriends = function(friends)
	{
		this.viewerFriends = friends;
	}

	this.getViewerFriends = function()
	{
		return this.viewerFriends;
	}

	this.setURL = function(url)
	{
		this.url = url;
	}

	this.getURL = function()
	{
		return this.url;
	}

	this.setFriend_loaded_callback = function(func)
	{
		this.friend_loaded_callback = func;
	}

	this.setQuizScore = function(quizScore)
	{
		this.quizScore = quizScore;
	}

	this.setTargetIds = function(ids)
	{
		this.targetIds = ids;
	}
}
