
var oInternal ='';
var tInternal ='';
var newReviewTimer = 5000;
oInternal = window.setInterval("checkForNewReviews()", newReviewTimer);
tInternal = window.setInterval("updateGMT()", 1000);
$(document).ready(function() {
	$("a[href^='http']").each(function(e){
		if ($(this).attr('href').indexOf("review-music.com/") < 0 && !$(this).hasClass('doNotOpenWindow'))
			$(this).attr('target', '_blank');
	});
	$('form').attr('autocomplete', 'off');
	
	
	if ($("#form input[name='name']").length > 0)
	{
		FB.getLoginStatus(function(response) {
		  $("#form input[name='name']").before('<div style="float:right" id="facebookConnect"></div>');
		  onStatus(response); // once on page load
		  FB.Event.subscribe('auth.statusChange', onStatus); // every status change
		});
	}
    $("textarea").blur(function(){
        $(this).val(removeMSWordChars(this.value));
    });
});

function showUSVisitorOnUKSite()
{
	$('#countryNotification h3').text('From the USA?');
  	$('#countryNotification p').html('<a href="http://us.review-music.com/">Visit our USA site</a>');
  	$('#countryNotification').effect('bounce');
}
function showUKVisitorOnUSSite()
{
	$('#countryNotification h3').text('From the UK?');
  	$('#countryNotification p').html('<a href="http://uk.review-music.com/">Visit our UK site</a>');
  	$('#countryNotification').effect('bounce');
}
function checkForNewReviews()
{
	$.getJSON("/feeds/review-notification-" + $('#body').attr('class').toLowerCase() + ".json",  function(data){
		if (data.gmt > parseInt($('#gmdate').val())-5){
			$('#newReviewNotification').css('top', ($(window).height() - 90) + 'px');
			$('#newReviewNotification').css('left', ($(window).width() - 700) + 'px');
			$('#newReviewNotification p').html("<a href='" + data.albumURL + "'>" + displayUser(data.name) + ' has just reviewed ' + data.albumTitle +  "</a>");
			clearInterval(oInternal);
			$('#newReviewNotification').effect('slide', {}, 500, function() {setTimeout(function(){$("#newReviewNotification").fadeOut(); oInternal = window.setInterval("checkForNewReviews()", newReviewTimer);}, 10000); });
		}
	});
}

function updateGMT()
{
	return $('#gmdate').val(parseInt($('#gmdate').val())+1);
}

function displayUser(user)
{
	if ( null == user || "" == user ) { 
		return 'A User';
	} else {
		return user;
	}
		
}

function shareOnFacebook()
{
	var share = {
	  method: 'stream.share',
	  u: document.location.href
	};
	
	FB.ui(share);
}


function removeMSWordChars(str) {
    var myReplacements = new Array();
    var myCode, intReplacement;
    myReplacements[8216] = 39;
    myReplacements[8217] = 39;
    myReplacements[8220] = 34;
    myReplacements[8221] = 34;
    myReplacements[8212] = 45;
    for(c=0; c<str.length; c++) {
        var myCode = str.charCodeAt(c);
        if(myReplacements[myCode] != undefined) {
            intReplacement = myReplacements[myCode];
            str = str.substr(0,c) + String.fromCharCode(intReplacement) + str.substr(c+1);
        }
    }
    return str;
}

/**
 * This assumes the user is logged in and renders their profile picture,
 * name and a logout link.
 */
function showAccountInfo() {
  FB.api(
    {
      method: 'fql.query',
      query: 'SELECT name FROM user WHERE uid='+FB.getSession().uid
    },
    function(response) {
      $('#facebookConnect').html('<img onclick="FB.logout()" style="cursor: pointer;"' +
            'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">');
      $('#image_url').val('https://graph.facebook.com/' + FB.getSession().uid + '/picture');
      $('#name').val(response[0].name);
    }
  );
}

/**
 * This assumes the user is logged out, and renders a login button.
 */
function showLoginButton() {
  $('#facebookConnect').html(
    '<img onclick="FB.login()" style="cursor: pointer;" ' +
         'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
  );
  $('#image_url').val('');
  $('#name').val('');
}

/**
 * This will be called once on page load, and every time the status changes.
 */
function onStatus(response) {
  if (response.session) {
    showAccountInfo();
  } else {
    showLoginButton();
  }
}
