var API2_PATH = '/apiv2/';
var ASSET_HOST = 'http://i.wattpad.net';
var COOKIE_DOMAIN = 'wattpad.com';
if (!/wattpad\.com/.test(location.host)) {
  ASSET_HOST = 'http://wattpad.local';
  COOKIE_DOMAIN = 'wattpad.local';
}

$(document).ready(function() {

  // when something is selected in one category dropdown, it is removed from the other category dropdown
  $('#category1, #category2').change(function() {
    var otherId = /1$/.test(this.id) ? 'category2' : 'category1';
    var otherSelect = $('#' + otherId);
    var oldValue = otherSelect[0].value;
    otherSelect.find('option').remove();
    otherSelect.html($('#categoryMaster').html());
    otherSelect.find('option[value=' + oldValue + ']').attr('selected', 'selected');
    if (this.value == 20) {
		var notWattyCats=new Array(2,10,13,16,17,19);
		for (var i=0;i<notWattyCats.length;i++)
			otherSelect.find('option[value=' + notWattyCats[i] + ']').remove();
		if (otherSelect.find('option:selected').length==0)
			otherSelect.find('option').first().attr('selected','selected');
    }
    if (this.value != 0) {
      otherSelect.find('option[value=' + this.value + ']').remove();
    }
    return false;
  });

  $('#togglebg').click(function() {
    var opacity = 0.2;
    if ($("#content_wrapper").css("opacity")!=1){
      opacity = 1;
    }
    $("#content_wrapper").fadeTo("fast", opacity, function() {});
  });

  $('#editProfile').click(function () {
    $('#profileview').toggle();
    $('#profileedit').toggle();
    return false;
  });  

  $('#changeAvatar').click(function () {
    $('#avatar1').toggle();
    $('#avatar2').toggle();
    return false;
  });
	if ($("#deleteAvatar").length > 0) {
		$('#deleteAvatar').click(function () {
			if (confirm(t("Remove your profile picture?"))) {
				document.forms['delavatarform'].submit();
			}
			return false;
		});
	}

  $('#changeBackground').click(function () {
    $('#background1').toggle();
    $('#background2').toggle();
    return false;
  });
	if ($("#deleteBackground").length > 0) {
		$('#deleteBackground').click(function () {
			if (confirm(t("Remove your background picture?"))) {
				document.forms['delbgform'].submit();
			}
			return false;
		});
	}

  $('#changeCover').click(function () {
    $('#cover2').toggle();
	$('#cover1').toggle();
    return false;
  });
	if ($("#deleteCover").length > 0) {
		$('#deleteCover').click(function () {
			if (confirm(t("Remove this cover?"))) {
				document.forms['groupform'].action = '?editcover=1';
				document.forms['groupform'].submit();
			}
			return false;
		});
	}
});

$('.fanlink.ajax, .fanlinkx.ajax').live('click', function() {
  $(this).siblings('.fan_spinner').css({ visibility: 'visible' });
  var that = this;
  if (/isfacebook/.test($(that).attr('class')) && /add/.test($(this).attr('href'))) {
    facebook_prompt_permission('publish_stream', function() { becomeFan(that); });
  } else {
    becomeFan(that);
  }
  return false;
});

function becomeFan(link) {
  var word = /member/.test($(link).attr('class')) ? 'member' : 'fan';
  $.ajax({
    type: 'POST',
    url:  $(link).attr('href'),
    success: function(response) {
      $('.fan_spinner').hide();
      $('.fan_icon').remove();
      if (response == 'added') {
        $("<span>You're a "+word+"!</span>").replaceAll('.fanlink');
        ga_track('Stories', 'Become a '+word);
      } else {
        $("<span>You're no longer a "+word+"!</span>").replaceAll('.fanlinkx');
        ga_track('Stories', 'Remove '+word);
      }
    }
  });
}

function updateCharCount(){
	$("#charcount").text($("textarea[name^=about]").val().length);
}

// translates given string
// TODO: umm...implement it
function t(str) {
  return str;
}

function ga_track(category, action, label, value) {
	_gaq.push(['_trackEvent', category, action, label, value]);
}
