/**
 * $Id: global.js 425 2010-07-28 05:49:44Z dclarke $
 * 
 * @description facebookgames global javascript, yo.
 * @author Darryl Clarke
 */

$(function() {
	$('.external').attr("target", "_new");
	$('.external').click(function() {
		pageViewLink('/external/' + $(this).attr('href'));
	});

	// this is for buttons
	$('.user-action-link').hover(function() {
		$(this).addClass('ui-state-hover');
	}, function() {
		$(this).removeClass('ui-state-hover');
	});
	

	$('tr.listrow').click(function(event) {
			$('tr.listrow').removeClass('tableHighlightActive');
			$(this).addClass('tableHighlightActive');
			loadAppData(event, this); 
			});
	$('tr.listrow').hover(function() {
		$(this).addClass('tableHighlight');
	}, function() {
		$(this).removeClass('tableHighlight');
	});
	
	$('.login_link').click(function(event) {
		event.stopPropagation();
		$("#dialog-login").dialog({
			height: 240, width: 450,
			modal: true
		});
		return false;
	});
	$("input#searchTerm").autocomplete( {
		source : '/ajax/autocomplete',
		minLength : 2,
		select : function(event, ui) {
			window.location = '/index/show/game/' + ui.item.id;
			// $("input#jumpId").val(ui.item.id);
		}
	});

	$('.login_with_facebook').click(function(event) {
		event.stopPropagation();
		FB.login(function(response) {
	  	if (response.session) {
	    	// user successfully logged in
		    window.location = '/';
	  	} else {
	    	// user cancelled login
		  	window.location.reload();
	  	}
		}, {perms:'email,publish_stream'});
	});
	$('.logout').click(function() {
		FB.logout(function(response) {
	          //user is now logged out
		});
	    window.location = '/auth/logout';
	});

	// FB.init("ece19a54a85a75fa6d0888542ff90f26", "/fb/xd_receiver.html");
});

function loadAppData(event, element)
{
	event.stopPropagation();
	var gameId = element.id;
	var theurl    = '/ajax/show/game/' + gameId;
	pageViewLink(theurl);
	$.ajax({ url: theurl, cache: true,
		  success: function(html){
		    $("#rightPanel").html(html);
		  }
	});
}

function pageViewLink(page)
{
	try {
		pageTracker._trackPageview(encodeURI(page));
	} catch (e) {}
}
