$(document).ready(function() {
  
  // Sound off
  $(".soundOff label").inFieldLabels({fadeOpacity: 0.2});
  $(".soundOff textarea").attr("autocomplete","off");
  $(".soundOff textarea").autogrow();
  
	// Hide and Show Display Options Menu by clicking the "Display Options" link in the page title section
		$(".displayOptions").click(function(){
			var menu = $("div.displayOptionsMenu");
			if ($(menu).hasClass("invisible")) {
				$(menu).removeClass("invisible").addClass("visibleBlock");
			} else {
				$(menu).removeClass("visibleBlock").addClass("invisible");
			}
			return false;
		});
	// Hide the Display Options Menu by clicking the "close" link in the Menu
	$(".displayOptionsMenu span a").click(function(){
		$("div.displayOptionsMenu").removeClass("visibleBlock").addClass("invisible");
		return false;
	});
	
	// Hide and Show Display Options Menu by clicking the "Display Options" link in the page title section
		$("li .localTeams").hover(
			function(){
				var menu = $("div.localTeamMenu");
				if ($(menu).hasClass("invisible")) {
					$(menu).removeClass("invisible").addClass("visibleBlock");
				} else {
					$(menu).removeClass("visibleBlock").addClass("invisible");
				}
				return false;
			},
			function(){
				var menu = $("div.localTeamMenu");
				$(menu).hover(
					function() {},
					function() {
						$(menu).removeClass("visibleBlock").addClass("invisible");
						return false;
					}
				);
				
			}
		);
	// Hide the Display Options Menu by clicking the "close" link in the Menu
	$(".localTeamMenu span a").click(function(){
		$("div.localTeamMenu").removeClass("visibleBlock").addClass("invisible");
		return false;
	});

	
	// get more news
	$(".getmorenews").click(function(){
	  $.ajax({
      url: "/news/more",
      success: function(data) {
        $("#latestNews").append(data);
      }
    });
	});
	
	// getmorefantasynews
	$(".getmorefantasynews").click(function(){
	  $.ajax({
      url: "/fantasy/more",
      success: function(data) {
        $("#latestNews").append(data);
      }
    });
	});
	
});

var loginCalled = 0;
function login() {
  loginCalled = 1;
  $.ajax({
    url: '/home/login/1',
    success: function(text) {
      html = '';
      html += '<div class="createReplyForm">';
      html += text;
      html += '</div>';
      jQuery.facebox(html);
    }
  });
}

function register(timeout) {
  if(timeout == 0 && !loginCalled) {
    $.ajax({
      url: '/home/register/1',
      success: function(text) {
        html = '';
        html += '<div class="createReplyForm">';
        html += text;
        html += '</div>';
        jQuery.facebox(html);
      }
    });
  }
  else {
    setTimeout('register(0)', timeout);
  }
}

function soundoff() {
  html = '';
  html += '<div class="createReplyForm">';
  html += '<h4>Sound Off!</h4>';
  html += '<form name="soundoff" action="/soundoff/add" method="post">';
  html += '<p>Tell us what you really think.</p>';
  html += '<br>';
  //html += '<textarea placeholder="Soundoff!" class="addEmComment" name="soundoff"></textarea>';
  html += '<input type="text" name="soundoff" style="width: 300px;" />';
  //html += '<input type="submit" class="emButton" id="emAddButton" value="Submit" onclick="document.soundoff.submit()" />';
  html += '<br>';
  html += '<ul class="forumToolbarButtons">';
  html += '<li><a class="lgSubmit" onclick="document.soundoff.submit();" style="width: 200px;" title="Submit" href="#">Submit</a></li>';
  html += '</ul>';
  html += '<div class="clear"></div>';
  html += '</form>';
  html += '</div>';
  jQuery.facebox(html);
}

function shareVideo() {
  html = '<div class="createReplyForm">';
  html += '<h4>Submit a Video</h4>';
  html += '<form name="submitVideo" class="createReplyForm" action="/roll/sharevideo" method="post">';
  html += '<div class="formError">';
  html += '<p>To share a YouTube video, paste your link below.</p>';
  html += '</div>';
  html += '<p>YOUTUBE VIDEO URL: <span>(required)</span></p>';
  html += '<input type="text" name="uri" style="width: 300px;" />';
  html += '<ul class="forumToolbarButtons">';
  html += '<li><a class="lgSubmit" onclick="document.submitVideo.submit();" style="width: 200px;" title="Submit Video" href="#">Submit Video</a></li>';
  html += '</ul>';
  html += '<div class="clear"></div>';
  html += '</form>';
  html += '</div>';
  jQuery.facebox(html);
}

function shareLink() {
  html = '<div class="createReplyForm">';
  html += '<h4>Share a Link</h4>';
  html += '<form name="shareLink" class="createReplyForm" action="/roll/sharelink" method="post">';
  html += '<div class="formError">';
  html += '<p>To share a link, paste your link below.</p>';
  html += '</div>';
  html += '<p>LINK URL: <span>(required)</span></p>';
  html += '<input type="text" name="uri" style="width: 300px;" />';
  html += '<ul class="forumToolbarButtons">';
  html += '<li><a class="lgSubmit" onclick="document.shareLink.submit();" style="width: 200px;" title="Share Link" href="#">Share Link</a></li>';
  html += '</ul>';
  html += '<div class="clear"></div>';
  html += '</form>';
  html += '</div>';
  jQuery.facebox(html);
}

function validateComment(formData, jqForm, options) {  
    for (var i=0; i < formData.length; i++) { 
        if(formData[i].name == 'comment') {
          if(formData[i].value == '') {
            alert('Please submit a valid comment');
            return false;
          }
        }
    } 
    return false; 
}

function addComment(commenttype, commentid) {
  newCommentURL = '/comment/' + commenttype + '/' + commentid;
  formid = getAddCommentFormId(commenttype, commentid);
  theform = $('#'+formid);
  theform.ajaxForm( { beforeSubmit: validateComment } ); 
  var options = { 
        //dataType:  'json',
        success: addCommentSuccess
  };
  theform.ajaxSubmit(options);
}


function addCommentSuccess(text, statusText, xhr, $form) {
  mydata = JSON.parse(text);
  commenttype = -1;
  commentid = -1;
  $.each(mydata, function(i,item) {
    if(i == 0) {
      commentid = item.commented_id;
      commenttype = item.type;
    }
  });
  div_id = getCommentsDivId(commenttype, commentid);
  $(div_id).empty();
  handleCommentsDisplay(text, commenttype, commentid);
}

function getCommentsDivId(commenttype, commentid) {
  return '#comments-' + commenttype + '-' + commentid;
}

function getAddCommentDivId(commenttype, commentid) {
  return 'emAddCommentDiv-' + commenttype + '-' + commentid;
}


function getAddCommentFormId(commenttype, commentid) {
  return 'emAddCommentForm-' + commenttype + '-' + commentid;
}

function handleCommentsDisplay(text, commenttype, commentid) {
  mydata = JSON.parse(text);
  html = '';
  loggedin = 0;
  $.each(mydata, function(i,item){
    if(i == 0) {
      loggedin = item.loggedin;
    }
    else {
      html += '<div class="emComment">';
      html += '<div class="emCommentImage">';
      //html += '<img src="/profiles/images/' + item.username + '.jpg" />';
      //html += '<img src="/images/commentUserDefault.gif" />';
      html += '<img width="72" src="' + item.image + '" />';
      html += '<br>';
      html += item.username;
      html += '</div>';
      html += '<div class="emCommentText">';
      html += item.comment;
      html += '</div>';
      html += '<div class="emCommentInto">';
      html += item.datecreated;
      html += '</div>';
      html += '</div>';
    }
  });
  if(loggedin != 0)  {
    addCommentDivId = getAddCommentDivId(commenttype, commentid);
    addCommentFormId = getAddCommentFormId(commenttype, commentid);
    html += '<div id="' + addCommentDivId + '" class="emAddComment">';
    newCommentURL = '/comment/add/' + commenttype + '/' + commentid;
    html += '<form id="' + addCommentFormId + '" method="post" action="' + newCommentURL + '" onsubmit="return false;">';
    html += '<span id="emNameSpan">';
    html += '<label for="addEmName">' + 'Add Comment' + '</label>';
    html += '</span>';
    html += '<textarea placeholder="Enter Comment" class="addEmComment" name="comment"></textarea>';
    html += '<input type="hidden" name="type" value="' + commenttype + '" />';
    html += '<input type="hidden" name="commented_id" value="' + commentid + '" />';
    html += '<span class="emButton">';
    onclicktext = 'javascript:addComment(' + commenttype + ',' + commentid + ')';
    html += '<input type="submit" class="emButton" id="emAddButton" value="Submit" onclick="' + onclicktext + '" />';
    html += '</span>';
    html += '</form>';
    html += '</div>';
  }
  else {
    html += '<br>';
    html += 'Please <a href="javascript:login();">login</a> to add a comment';
    html += '<br>';
  }
  $(div_id).append(html);
  //$(div_id).append(mydata[0].comment);
  //$(div_id).append(mydata[1].comment);
  //$('.result').html(data);
  //alert('Load was performed.');
  //$(div_id).append(data);
  $(div_id).show('fast');
}

function showComments(commenttype, commentid) {
  div_id = getCommentsDivId(commenttype, commentid);
  $(div_id).empty();
  commentsurl = '/comment/get/' + commenttype + '/' + commentid;
  $.ajax({
    url: commentsurl,
    success: function(text) {
      handleCommentsDisplay(text, commenttype, commentid);
    }
  });
}
