 
function scrollRightx() {
	$('#prev').show('slow');
	var count = $('#usercount').val();
	var pos = $('#currentpos').val();
	
	var sliderpos = $('#userslider').position();
	$('#userslider').animate({left: -1*pos*80});
	$('#currentpos').val(++pos);
	if (pos+2 >= count) $('#next').hide('slow');
}

function scrollLeftx() {
	$('#next').show('slow');
	var pos = $('#currentpos').val() - 1;
	$('#userslider').animate({left: -1*(pos-1)*80});
	$('#currentpos').val(pos);
	if (pos == 1) $('#prev').hide('slow');
}


/*
Check to see if user is logged into Anywhere
*/
function anywhereConnected() {
    if (typeof(twttr) == 'undefined') return;
	twttr.anywhere(function (T) {
		if (T.isConnected()) {
			var twitter_id = storeConnectedUserTwitter(session_id, user_id, park_id, publisher_id, hash_type, hash, T.currentUser);
			document.getElementById('anywhere-twitter-id').value = twitter_id;
			initializeRetweetUsers(T.currentUser.data('id'), "0");
			initializeRetweetUsersPictures();
			hideAnywhereButton();
		} else {
			storeNotConnectedUserTwitter(session_id, user_id, park_id, publisher_id, hash_type, hash);
		}
	});
}

/*
Hide the anywhere button
*/
function hideAnywhereButton() {
	$('#anywhere-follow').hide();
	$('#retweet-users-pictures-more');
	$('#retweet-users-pictures');
}

/*
Enable hovercards.
Param @twitter_id = twitter id of current user logged in. 0 if not logged in
Param @retweetbox = if retweet box has been initialized, then true so that signin link is initialized
*/
function anywhereLinkify(twitter_id, retweetbox_initialized) {
    if (typeof(twttr) == 'undefined') return;
	twttr.anywhere(function (T) {
		T("#retweet-users").hovercards({ expanded: true });
		T("#retweet-users-pictures img").hovercards({
			expanded: true, 
  			username: function(node) { 
    			return node.alt; 
  			}
		});
		T("#hover-card").hovercards({ expanded: true });
		 		
		T("#hover-card img").hovercards({
			expanded: true, 
  			username: function(node) { 
    			return node.alt; 
  			}
		});
		T("#topbar-recentviewers img").hovercards({
			expanded: true, 
  			username: function(node) { 
    			return node.alt; 
  			}
		});
		if (twitter_id == "0") {
			if (retweetbox_initialized) {
				document.getElementById("anywhere_signin_link").onclick = function () {
					T.signIn();
				};
				document.getElementById("anywhere_signin_image").onclick = function () {
					T.signIn();
				};
			}
		}
	});
}

/*
Initialize Users in Retweet box
*/
function initializeRetweetUsers(twitter_id, facebook_id) {
	$('#img_ajax_retweet_users').show();
	if (twitter_id == "0") {
		twitter_id = document.getElementById('anywhere-twitter-id').value;
	}
	if (facebook_id == "0") {
		facebook_id = document.getElementById('facebook-id').value;
	}

	$.ajax({
		type: "GET",
		url: "/clickdatausers/getRecentViewers/?session_id="+session_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id+"&facebook_id="+facebook_id+"&dst="+dst_url,
		target: '#retweet-users',
		success: function(html){
		  $('#retweet-users').show().html(html);
		  $('#img_ajax_retweet_users').hide();
		  anywhereLinkify(twitter_id, true);
		},
		fail: function(xhr, ajaxOptions, thrownError) {
		  $('#img_ajax_retweet_users').hide();
		}
	});
}

/*
Initialize Suggestions in Retweet box
*/
function initializeRetweetSuggestions(twitter_id) {
	$('#img_ajax_retweet_relatedarticles').show();
	$.ajax({
		type: "GET",
		url: "/clickdatausers/getSuggestions/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash,
		target: '#retweet-relatedarticles',
		success: function(html){
			$('#retweet-relatedarticles').html(html);
			$('#img_ajax_retweet_relatedarticles').hide();
			anywhereLinkify(twitter_id, true);
		},
		fail: function(xhr, ajaxOptions, thrownError) {
			$('#img_ajax_retweet_relatedarticles').hide();
		}
	});
}

/*
Initialize User pictures in topbar
*/
function initializeRetweetUsersPictures() {
	$('#retweet-users-pictures').html("");
	$('#img_ajax_retweet_users_pictures').show();
	$.ajax({
		type: "GET",
		url: "/clickdatausers/getRecentViewersPictures/?session_id="+session_id+"&hash_type="+hash_type+"&hash="+hash+"&dst="+dst_url,
		target: '#retweet-users-pictures',
		success: function(html){
			$('#retweet-users-pictures').show().html(html);
			$('#retweet-users-pictures-more').show();
			$('#img_ajax_retweet_users_pictures').hide();
		},
		fail: function(xhr, ajaxOptions, thrownError) {
			$('#img_ajax_retweet_users_pictures').hide();
		}
	});
	anywhereLinkify("0", false);
}



/*
When the user clicks on a suggested link
*/
function clickOnSuggestedLink(newhash) {
	storeClickOnSuggestedLink(newhash);
}

/*
Open and close the retwet box
*/
function toggleRetweetBox(location) {
	if (document.getElementById('retweet').style.display != "none") {
		$("#moreless").html('<b>more</b>');
		$("#morearrow").attr('src', "/images/topbardown.png");
		$("#twitterimg").attr("src", "/images/topbarfbtwitter_.png");
		$('div#retweet-relatedarticles').hide();
		$('div#retweet-users').hide();
		$('div#anywhere-tweetbox').hide();
		$('div#retweet').hide('slow');
		$('div#divider').hide();
	} else {
		showRetweetBox(location);
	}
}	

function facebook_login(response) {
	// user successfully logged in
	if (response.session) {
		//console.dir(response);
		document.getElementById('facebook-login-data').value = response;
		var facebook_id = "0";
		FB.api('/me', function(response2) {
			facebook_id = storeConnectedUserFacebook(session_id, user_id, park_id, publisher_id, hash_type, hash, response2);
			document.getElementById('facebook-id').value = facebook_id;
		});
		initializeRetweetUsers("0", facebook_id);
	}
}

function isset () {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
    
    var a=arguments, l=a.length, i=0;
    
    if (l===0) {
        throw new Error('Empty isset'); 
    }
    
    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

/*
POST user details to database for Twitter
*/
function storeNotConnectedUserTwitter(session_id, user_id, park_id, publisher_id, hash_type, hash) {
	storeNotConnectedUser(session_id, user_id, park_id, publisher_id, hash_type, hash, "twitter");
}

/*
POST user details to database for Facebook
*/
function storeNotConnectedUserFacebook(session_id, user_id, park_id, publisher_id, hash_type, hash) {
	storeNotConnectedUser(session_id, user_id, park_id, publisher_id, hash_type, hash, "facebook");
}

/*
POST user details to database
*/
function storeNotConnectedUser(session_id, user_id, park_id, publisher_id, hash_type, hash, accounttype) {
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded",
		url: "/clickdatausers/addnotloggedin/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&accounttype="+accounttype,
		data: "",
		dataType: "html",
		success: function(html) {
		},
		fail: function(xhr, ajaxOptions, thrownError) {
		}
	});
}

/*
POST user details to database
*/
function storeConnectedUserTwitter(session_id, user_id, park_id, publisher_id, hash_type, hash, currentUser) {

	var profile_background_color = encodeURIComponent(currentUser.data('profile_background_color'));
	var description = encodeURIComponent(currentUser.data('description'));
	var profile_text_color = encodeURIComponent(currentUser.data('profile_text_color'));
	var followers_count = encodeURIComponent(currentUser.data('followers_count'));
	var lang = encodeURIComponent(currentUser.data('lang'));
	var time_zone = encodeURIComponent(currentUser.data('time_zone'));
	var utc_offset = encodeURIComponent(currentUser.data('utc_offset'));
	var friends_count = encodeURIComponent(currentUser.data('friends_count'));
	var profile_link_color = encodeURIComponent(currentUser.data('profile_link_color'));
	var statuses_count = encodeURIComponent(currentUser.data('statuses_count'));
	var created_at = encodeURIComponent(currentUser.data('created_at'));
	var following = encodeURIComponent(currentUser.data('following'));
	var favourites_count = encodeURIComponent(currentUser.data('favourites_count'));
	var profile_sidebar_fill_color = encodeURIComponent(currentUser.data('profile_sidebar_fill_color'));
	var contributors_enabled = encodeURIComponent(currentUser.data('contributors_enabled'));
	var notifications = encodeURIComponent(currentUser.data('notifications'));
	var protected = encodeURIComponent(currentUser.data('protected'));
	var profile_image_url = encodeURIComponent(currentUser.data('profile_image_url'));
	var geo_enabled = encodeURIComponent(currentUser.data('geo_enabled'));
	var profile_background_image_url = encodeURIComponent(currentUser.data('profile_background_image_url'));
	var profile_sidebar_border_color = encodeURIComponent(currentUser.data('profile_sidebar_border_color'));
	var url = encodeURIComponent(currentUser.data('url'));
	var location = encodeURIComponent(currentUser.data('location'));
	var name = encodeURIComponent(currentUser.data('name'));
	var screen_name = encodeURIComponent(currentUser.data('screen_name'));
	var id = encodeURIComponent(currentUser.data('id'));
	var verified = encodeURIComponent(currentUser.data('verified'));
	var profile_background_tile = encodeURIComponent(currentUser.data('profile_background_tile'));
	
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded",
		url: "/clickdatausers/addtwitteranywhere/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&id="+id+"&name="+name+"&screen_name="+screen_name+"&profile_background_color="+profile_background_color+"&description="+description+"&profile_text_color="+profile_text_color+"&followers_count="+followers_count+"&lang="+lang+"&time_zone="+time_zone+"&utc_offset="+utc_offset+"&friends_count="+friends_count+"&profile_link_color="+profile_link_color+"&statuses_count="+statuses_count+"&created_at="+created_at+"&following="+following+"&favourites_count="+favourites_count+"&profile_sidebar_fill_color="+profile_sidebar_fill_color+"&contributors_enabled="+contributors_enabled+"&notifications="+notifications+"&protected="+protected+"&profile_image_url="+profile_image_url+"&geo_enabled="+geo_enabled+"&profile_background_image_url="+profile_background_image_url+"&profile_sidebar_border_color="+profile_sidebar_border_color+"&url2="+url+"&location="+location+"&verified="+verified+"&profile_background_tile="+profile_background_tile+"&dst="+dst_url,
		data: "",
		dataType: "html",
		success: function(html) {
		},
		fail: function(xhr, ajaxOptions, thrownError) {
		}
	});
	
	return id;
}

/*
Store the retweet box being generated
*/
function storeRetweetBoxGenerated(location, session_id, user_id, park_id, publisher_id, hash_type, hash, twitter_id, facebook_id) {
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded",
		url: "/clickdatausers/addretweetbox/?transaction_type="+location+"&session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id+"&facebook_id="+facebook_id,
		data: "",
		dataType: "html",
		success: function(html) {
		},
		fail: function(xhr, ajaxOptions, thrownError) {
		}
	});
}

/*
Store the contents of the tweet
*/
function storeTweet(session_id, user_id, park_id, publisher_id, hash_type, hash, twitter_id, plaintext_tweet, html_tweet) {	
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded",
		url: "/clickdatausers/addtweetfromtweetbox/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id+"&plaintext_tweet="+encodeURIComponent(plaintext_tweet)+"&html_tweet="+encodeURIComponent(html_tweet),
		data: "",
		dataType: "html",
		success: function(html) {
		},
		fail: function(xhr, ajaxOptions, thrownError) {
		}
	});
}

/*
Store connected user in Facebook
*/
function storeConnectedUserFacebook(session_id, user_id, park_id, publisher_id, hash_type, hash, response2) {
	var fb_status = "", fb_birthday = "", fb_email = "", fb_first_name = "", fb_gender = "", 
		fb_hometown_id = "", fb_hometown_name = "", fb_id = "", fb_last_name = "", fb_link = "", fb_location_id = "", fb_location_name = "", 
		fb_name = "", fb_timezome = "", fb_updated_time = "";

	var ajax_query = "/clickdatausers/addfacebooklogin/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash;
	try {
		if (isset(response2.status)) {
			fb_status = response2.status;
			ajax_query = ajax_query+"&status="+fb_status;
		}
	}
	catch(err) {
	}
	
	try {
		if (isset(response2.birthday)) {
			fb_birthday = response2.birthday;
			ajax_query = ajax_query+"&birthday="+fb_birthday;
		}
	}
	catch(err) {
	}
	
	try {
		if (isset(response2.email)) {
			fb_email = response2.email;
			ajax_query = ajax_query+"&email="+fb_email;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.first_name)) {
			fb_first_name = response2.first_name;
			ajax_query = ajax_query+"&first_name="+fb_first_name;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.gender)) {
			fb_gender = response2.gender;
			ajax_query = ajax_query+"&gender="+fb_gender;
		}
	}
	catch(err) {
	}
	
	try {
		if (isset(response2.hometown_id)) {
			fb_hometown_id = response2.hometown_id;
			ajax_query = ajax_query+"&hometown_id="+fb_hometown_id;
		}
	}
	catch(err) {
	}
	
	try {
		if (isset(response2.hometown_name)) {
			fb_hometown_name = response2.hometown_name;
			ajax_query = ajax_query+"&hometown_name="+fb_hometown_name;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.id)) {
			fb_id = response2.id;
			ajax_query = ajax_query+"&id="+fb_id;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.last_name)) {
			fb_last_name = response2.last_name;
			ajax_query = ajax_query+"&last_name="+fb_last_name;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.link)) {
			fb_link = response2.link;
			ajax_query = ajax_query+"&link="+fb_link;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.location_id)) {
			fb_location_id = response2.location_id;
			ajax_query = ajax_query+"&location_id="+fb_location_id;
		}		
	}
	catch(err) {
	}

	try {
		if (isset(response2.location_name)) {
			fb_location_name = response2.location_name;
			ajax_query = ajax_query+"&location_name="+fb_location_name;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.name)) {
			fb_name = response2.name;
			ajax_query = ajax_query+"&name="+fb_name;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.timezone)) {
			fb_timezome = response2.timezone;
			ajax_query = ajax_query+"&timezone="+fb_timezone;
		}
	}
	catch(err) {
	}

	try {
		if (isset(response2.updated_time)) {
			fb_updated_time = response2.updated_time;
			ajax_query = ajax_query+"&updated_time="+fb_updated_time;
		}
	}
	catch(err) {
	}
	
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded",
		url: ajax_query,
		data: "",
		dataType: "html",
		success: function(html) {
		},
		fail: function(xhr, ajaxOptions, thrownError) {
		}
	});

	return fb_id;
}
