var CG = window.CG || {};
CG.active_requests = {};

CG.homepageUrl = '';
CG.totalFriends = 0;
CG.totalStories = 0;
CG.totalBookmarks = 0;

CG.initDocument = function() {};

CG.builtinCommentPictureClick = function(id_base, clicked_image, image_filename) {
	var temp = $('#' + id_base + ' input')[0].value;
	if (temp != image_filename)
	{
		$('#' + id_base + ' input')[0].value = image_filename;
		$('#' + id_base + ' img').removeClass('selected');
		$(clicked_image).addClass('selected');
	}
	else
	{
		$('#' + id_base + ' input')[0].value = '';
		$('#' + id_base + ' img').removeClass('selected');
		$(clicked_image).removeClass('selected');
	}
};

CG.toggleRelatedStoryDetails = function(story_id) {
  var story_description = $('#relatedStory' + story_id + ' .storyRelatedStoryDetails');
  var do_hide = story_description.css('display') == 'block';
  story_description.toggle('slow');
  
  if(do_hide) {
    $('#relatedStory' + story_id + ' .storyRelatedStoryHandle div').css({
      backgroundImage : "url('" + CG.homepageUrl + "/public/images/layout/handle-show.gif')"
    });
  } else {
    $('#relatedStory' + story_id + ' .storyRelatedStoryHandle div').css({
      backgroundImage : "url('" + CG.homepageUrl + "/public/images/layout/handle-hide.gif')"
    });
  } // if
  
};


CG.toggleRelatedStories = function(story_id, speed) {
  var div = $('#relatedStories' + story_id);
  var handle = $('#handle' + story_id);
  var span = $('#handle' + story_id + ' span');
  var do_hide = div.css('display') == 'block';
  if (do_hide)
  {
  	handle.css({ background:'url(' + CG.homepageUrl + '/public/images/layout/handle-show.gif' + ') no-repeat 0 1px' });
  	handle.css({ color:'#52A3E0' });
  } else {
  	handle.css({ background:'url(' + CG.homepageUrl + '/public/images/layout/handle-hide.gif' + ') no-repeat 0 1px' });
  	handle.css({ color:'#52A3E0' });
  }
	handle.bind("mouseover", function(){
	   span.css({ backgroundColor:'#52a3e0', color:"#fff"});
	 });  
	handle.bind("mouseout", function(){
	   span.css({ backgroundColor:'#fff', color:"#52a3e0"});
	 });  
  div.toggle(speed);
}

CG.hideRelatedStoryDetails = function(story_id) {
  $('#relatedStory' + story_id + ' .storyRelatedStoryDetails').hide();
};

CG.showCommentForm = function() {
  if($('#postCommentFormHeader').css('display') == 'block') {
    return;
  }
  
  $('#postCommentFormHeader').show();
  $('#postCommentForm').show('slow', function() {
    window.scrollTo(0, $("#postCommentFormHeader").offset().top);
  });
  $('div.commentsAddLink').hide();
};

CG.hideCommentForm = function(hide_form_speed) {
  $('#postCommentFormHeader').hide();
  $('#postCommentForm').hide(hide_form_speed);
  $('div.commentsAddLink').show();
};

CG.submitCommentForm = function() {
  $('#postCommentForm form').submit();
  return false;
};

CG.quoteComment = function(comment_id) {
  CG.showCommentForm();
  $('#postCommentForm textarea').val('[quote=' + comment_id + ']' + "\n\n" + $('#postCommentForm textarea').val());
};


/** Banned comments **/

CG.toggleBannedComment = function(comment_id) {
  $('#storyComment' + comment_id + ' .commentDetails').toggle();
  
  if($('#storyComment' + comment_id + ' .commentDetails').css('display') == 'block') {
    $('#storyComment' + comment_id + ' .toggleBannedCommentLink').html('Close this comment');
  } else {
    $('#storyComment' + comment_id + ' .toggleBannedCommentLink').html('View this comment');
  }
};

CG.hideBannedComment = function(comment_id) {
  $('#storyComment' + comment_id + ' .commentDetails').hide();
};

/** AJAX **/

CG.rateStory = function(story_id, the_link) {
  var request_id = 'rateStory' + story_id;
  if(!CG.active_requests[request_id]) {
    $('#story' + story_id + ' .storyDoRate').load(the_link.href + 'ajax/');
    CG.active_requests[request_id] = true;
  } // if
  return false;  
};

var changeCelebrityStatus = true;

CG.changeCelebrity = function(story_id, the_link) {
	if ( changeCelebrityStatus == false ) {
		$('#story' + story_id + ' .storyCelebrityChange').fadeIn("slow");
	} else {
		var request_id = 'changeCelebrity' + story_id;
		$('#story' + story_id + ' .storyCelebrityChange').append('<img src="' + CG.homepageUrl + '/public/story_images/loading.gif" alt="alt" /> Loading celebrities from database. Please, wait.');
		if(!CG.active_requests[request_id]) {
			$('#story' + story_id + ' .storyCelebrityChange').load(the_link.href + 'ajax/');
			CG.active_requests[request_id] = true;
		} // if
	}
	return false;  
};


CG.cancelChange = function(story_id) {
	$('#story' + story_id + ' .storyCelebrityChange').fadeOut("slow");
	changeCelebrityStatus = false;	
	return false;  
}

CG.rateComment = function(comment_id, the_link) {
  var request_id = 'rateComment' + comment_id;
  if(!CG.active_requests[request_id]) {
    $('#storyComment' + comment_id + ' .commentRate').load(the_link.href + 'ajax/');
    CG.active_requests[request_id] = true;
  } // if
  return false;  
};

CG.rateCelebrity = function(celebrity_id, the_link) {
  var request_id = 'rateCelebrity' + celebrity_id;
  if(!CG.active_requests[request_id]) {
    $('#celebrityProfile .celebrityRate').load(the_link.href + 'ajax/');
    CG.active_requests[request_id] = true;
  } // if
  return false;  
};

CG.addToBookmarks = function(story_id, the_link) {
  var request_id = 'bookmarkStory' + story_id;
  var remove_request_id = 'removeStoryFromBookmarks' + story_id;
  if(!CG.active_requests[request_id]) {
    $('#story' + story_id + ' .storyOptions').load(the_link.href + 'ajax/');
    
    CG.totalBookmarks = CG.totalBookmarks + 1;
    
    $('#myBookmarksCount').html('My Bookmarks (' + CG.totalBookmarks + ')');
    
    CG.active_requests[request_id] = true;
    CG.active_requests[remove_request_id] = false;
  } // if
  return false;
};


CG.removeFromBookmarks = function(story_id, the_link, on_bookmarks_page) {
	var request_id = 'removeStoryFromBookmarks' + story_id;
	var add_request_id = 'bookmarkStory' + story_id;
	if(!CG.active_requests[request_id]) {
		if(confirm('Are you sure that you want to remove this story from bookmarks?')) {
			$('#story' + story_id + ' .storyOptions').load(the_link.href + 'ajax/');
			
			CG.totalBookmarks = CG.totalBookmarks - 1;

			$('#myBookmarksCount').html('My Bookmarks (' + CG.totalBookmarks + ')');

			// If on bookmarks page update page title and show bookmark
			if(on_bookmarks_page) {
				$('#story' + story_id).hide('slow');
				$('#contentTitle h1').html('My bookmarks (' + CG.totalBookmarks + ')');
				if(CG.totalBookmarks < 1) {
					$('#noBookmarks').show('slow');
				}
			}

			CG.active_requests[request_id] = true;
			CG.active_requests[add_request_id] = false;
		}
	}
	return false;
};


CG.reportNotAGossip = function(story_id, the_link) {
  var request_id = 'notAGossipStory' + story_id;
  if(!CG.active_requests[request_id]) {
    if(confirm('Are you sure that you want to report that this story is not a gossip to moderators?')) {
      $('#story' + story_id + ' .storyOptions').load(the_link.href + 'ajax/');
      CG.active_requests[request_id] = true;
    }
  }
  return false;
};

CG.addToFriendsFromComment = function(comment_id, user_id, the_link) {
  var request_id = 'addToFriends' + comment_id;
  var remove_request_id = 'removeFromFriends' + comment_id;
  if(!CG.active_requests[request_id]) {
    $('#storyComment' + comment_id + ' .commentOptions').load(the_link.href + 'from_comment/' + comment_id + '/ajax/');
    $('.commentBy' + user_id).removeClass('other').addClass('friend');
    $('#myFriendsCount span.count').load(CG.homepageUrl + '/index.php?controller=ajax&action=print_friends_count');
    
    CG.active_requests[request_id] = true;
    CG.active_requests[remove_request_id] = false;
  }
  return false;
};

CG.removeFromFriendsFromComment = function(comment_id, user_id, the_link) {
  var request_id = 'removeFromFriends' + comment_id;
  var add_request_id = 'addToFriends' + comment_id;
  if(!CG.active_requests[request_id]) {
    $('#storyComment' + comment_id + ' .commentOptions').load(the_link.href + 'from_comment/' + comment_id + '/ajax/');
    $('.commentBy' + user_id).removeClass('friend').addClass('other');
    $('#myFriendsCount span.count').load(CG.homepageUrl + '/index.php?controller=ajax&action=print_friends_count');
    
    CG.active_requests[request_id] = true;
    CG.active_requests[add_request_id] = false;
  }
  return false;
};

CG.addToIgnoreFromComment = function(comment_id, user_id, the_link) {
  var request_id = 'addToIgnore' + comment_id;
  if(!CG.active_requests[request_id]) {
    $('#storyComment' + comment_id + ' .commentOptions').load(the_link.href + 'from_comment/' + comment_id + '/ajax/');
    $('#myFriendsCount span.count').load(CG.homepageUrl + '/index.php?controller=ajax&action=print_friends_count');
    
    CG.active_requests[request_id] = true;
    $('.commentBy' + user_id).hide('slow');
  }
  return false;
};

/** Validators **/

CG.checkPostCommentForm = function(default_text) {
  var comment_text = $('#postCommentForm textarea').val();
  if((comment_text == '') || (comment_text == default_text)) {
    alert('Please insert your comment');
    return false;
  }
  return true;
};

/** FAQ page **/

CG.hideFaqAnswers = function() {
  $('#faq p.answer').hide();
};

CG.faqQuestionClick = function(question_id) {
  $('#q' + question_id + ' p.answer').toggle('slow');
  return false;
};

CG.popup = function(content_url) {
  newwindow = window.open(content_url,'celebrity_picture','height=650,width=620,top=100,left=100');
  if (window.focus) {
    newwindow.focus()
  }
  return false;
};

CG.CheckFieldLength = function(fn,wn,rn,mc,min) {
  var len = fn.value.length;
  if (len > mc) {
    //	fn.value = fn.value.substring(0,mc);
	exceed = len;    
    len = mc;
    var err_msg = '<div class="errorBox">';
    err_msg += '<div class="header"></div>';
    err_msg += '<div class="content">You have exceeded maximum allowed<br /> number of characters by ' + (exceed - mc) + '.</div>';
    err_msg += '<div class="footer"></div></div>';
    $("#warning" + wn).html(err_msg);
  } else {
    $("#warning" + wn).html('');
  }
  if ((mc-len) == 0) {
  	$("#remaining" + rn).html("0");
  } else {
  	$("#remaining" + rn).html(mc - len);
  }
  if (len == 0) {
	  $('#max' + rn).text("max");
  } else {
	  $('#max' + rn).text("left");
  }
  if (min != null)
  {
	  if ((min-len) <= 0) {
	  	$("#min" + rn).html("0");
	  } else {
	  	$("#min" + rn).html(min - len);
	  }
	  if (len == 0) {
		  $('#minNote' + rn).text("min");
	  } else {
		  $('#minNote' + rn).text("to add");
	  }
  }
}

CG.restoreColors = function ()
{
	$(".fieldError label").css({ color: "black"});
	$("#submitStoryBody").css({ border: "2px solid #52A3E0"});
	$(".fieldErrorList ul li").hide();
}


CG.toggleOtherStories = function(story_id, speed) {
  var title = $('#otherStoryTitle' + story_id);
  var meta  = $('#otherStoryMeta' + story_id);
  var do_hide = meta.css('display') == 'block';
  if (do_hide)
  {
  	title.css({ background:'url(' + CG.homepageUrl + '/public/images/layout/handle-show.gif' + ') no-repeat 0 3px' });
  } else {
  	title.css({ background:'url(' + CG.homepageUrl + '/public/images/layout/handle-hide.gif' + ') no-repeat 0 3px' });
  }
	meta.bind("mouseover", function(){
	   //	span.css({ backgroundColor:'#52a3e0', color:"#fff"});
	 });  
	meta.bind("mouseout", function(){
	   //	span.css({ backgroundColor:'#fff', color:"#52a3e0"});
	 });  
  meta.toggle(speed);
}
