// site.js

$(function(){
	// get user info  
	gigya.services.socialize.getUserInfo(conf,{callback:renderUI});       

	// register for connect status changes  
	gigya.services.socialize.addEventHandlers(conf,   
	 { onConnect:renderUI, onDisconnect:renderUI}   );
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function shareGigya(message, title, desc, link)
{
	// Constructing a UserAction Object
	var act = new gigya.services.socialize.UserAction();

	// Setting the User Message
	act.setUserMessage(message);

	// Setting the Title
	act.setTitle(title);

	// Adding a Link Back
	act.setLinkBack(link);

	// Setting the Description
	act.setDescription(desc);

	// Adding a Media (image)
	act.addMediaItem( { 
		type: 'image',      // Type of the media (image/flash/mp3)
		src: 'http://www.budgetyourtrip.com/images/bytlogosmallwhite.gif',   // URL to the image source
		href: link    // URL to redirect the user when he clicks the image
	 });

	// Activate the Share Widget
	gigya.services.socialize.showShareUI(conf, {  userAction:act });
}

function shareCountryCost(country)
{
	var message = "Check this out. ";
	var title = "Travel costs for " + country;
	var desc = "Going somewhere? Check out these travel costs at BudgetYourTrip.com";
	var link = "http://www.budgetyourtrip.com/estimate/" + encodeURI(country);
	shareGigya(message, title, desc, link);
}
function shareCityCost(geonameid, cityname, countryname)
{
	var message = "Check this out. ";
	var title = "Travel costs for " + cityname;
	var desc = "Going somewhere? Check out these travel costs at BudgetYourTrip.com";
	var link = "http://www.budgetyourtrip.com/estimate/" + encodeURI(countryname) + "/" + encodeURI(geonameid);
	shareGigya(message, title, desc, link);
}
function shareMyTrip(tripid, title)
{
	var message = "Check out my trip. ";
	var title = title;
	var desc = "View my trip at BudgetYourTrip.com";
	var link = "http://www.budgetyourtrip.com/mytripdetails.php?trip_id=" + tripid;
	shareGigya(message, title, desc, link);
}
function shareTrip(tripid, title)
{
	var message = "Check out this trip. ";
	var title = title;
	var desc = "View this trip at BudgetYourTrip.com";
	var link = "http://www.budgetyourtrip.com/mytripdetails.php?trip_id=" + tripid;
	shareGigya(message, title, desc, link);
}


var user = null;
function renderUI(res)
{
	
	if(res.user!=null && res.user.isConnected)
	{   
		user = res.user ;    
		// Show friend-selector component
		document.getElementById("friends").style.display = "block";
		var params =
		{
			containerID:"friends",
			onSelectionDone:onSelectionDone,
			width: 300,
			height: 300,
			UIConfig: '<config><body><background background-color="Transparent" frame-color="Transparent"></background></body></config>',
			showTermsLink: 'false',
			hideGigyaLink: 'true'
		}
		gigya.services.socialize.showFriendSelectorUI(conf, params);
		
	} else {
		document.getElementById("friends").innerHTML = "";
		document.getElementById("friends").style.display = "none";
	}			
}

// If the user clicked "OK" in the friend-selector component
// Send a notification to the selected friends.
function onSelectionDone(response)
{
	var subject = "A message from " + user.nickname;
	var body = "Check out Budget Your Trip: http://www.budgetyourtrip.com";
	if (response.friends.getSize() > 0)
	{
		var params = 
		{
			callback:sendNotification_callback,
			subject:subject,
			body:body,
			recipients:response.friends
		};
		gigya.services.socialize.sendNotification(conf, params)
	}
	document.getElementById("friends").innerHTML = "";
	document.getElementById("friends").style.display = "none";
}

// Display a status message according to the response from sendNotification
function sendNotification_callback(response)
{
	switch (response.status)
	{
		case 'OK':
			//document.getElementById('status').style.color = "green";
			$('#status').addClass("confirm");
			$('#status').html("Notification sent.");
			break;
		default:
			//document.getElementById('status').style.color = "red";
			$('#status').addClass("error");
			$('#status').html("Unable to send notification. status=" + response.status);
	}
	$('#status').css("display","block");
}