// JavaScript Document
	jQuery.fn.fadeToggle = function(speed, easing, callback) 
	{
	    return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	
	function setspam(divID, ID, qID, mode)
	{
		$(".tipsy").css("visibility","hidden");
		var spamdiv = '#spam_' + divID;
		var votediv = '#votes_' + divID;
		var imgdiv = '#aimg'+divID;
		var viddiv = '#av'+divID;
		$(spamdiv).attr("onclick", "");
		$(spamdiv).unbind('click');
		var rankdiv = '#rank_' + divID;
		var currentrank = $(rankdiv).html();
		if (mode == 1)
		{
			//updateranks(currentrank, 'spam');
			$(spamdiv).attr('title', 'Remove flag');
		
			$(spamdiv).click(function() 
			{
				doCallAjax('SPAMD',ID,qID,divID);
			});
			
			$('#addedby_'+divID).fadeOut("fast");
			$('#vid'+divID).hide();
			$('#upl'+divID).hide();
			$('#sh_'+divID).html('show');

			fade_show_hide('vup_'+divID);
			fade_show_hide('vd_'+divID);
			fade_show_hide('sh_'+divID);
			fade_show_hide('i'+divID);

			$(rankdiv).removeClass('list_rank');
			$(rankdiv).addClass('list_rank_hid');
			
			$(votediv).removeClass('thevotes');
			$(imgdiv).addClass('hide');
			$(viddiv).addClass('hide');
		}
		else
		{
			//updateranks(currentrank,'unspam');
			$(spamdiv).attr('title', 'Flag');
			
			$(spamdiv).click(function() 
			{
				doCallAjax('SPAMA',ID,qID,divID);
			});
			$('#addedby_'+divID).fadeIn("fast");

			fade_show_hide('vup_'+divID);
			fade_show_hide('vd_'+divID);
			fade_show_hide('sh_'+divID);
			fade_show_hide('i'+divID);

			$(rankdiv).removeClass('list_rank_hid');
			$(rankdiv).addClass('list_rank');

			$(votediv).addClass('thevotes');
			$(imgdiv).removeClass('hide');
			$(viddiv).removeClass('hide');
		}

	}
	
	function updatevotes(votetype, ID, qID, divID) 
	{
		
		somediv = '#votes_' + divID;
		numbersdiv = '#lst_' + divID;
		var plusvotes = 0;
		var minusvotes = 0;
		var totalplus = 0;
		var totalminus = 0;
		var total = 0;
		
		$(somediv).children().map(function() 
		{
        	var child = $(this);
			//changing the visual values
			if ($(child).is('.plusvotes'))
			{
				plusvotes = $(child).html();
				if ((votetype == 'VOTE') || (votetype == 'VOTEC'))
				{
					plusvotes++;
					$(child).html(plusvotes);
				}
				else if ((votetype == 'VOTECA')||(votetype == 'VOTER'))
				{
					plusvotes = plusvotes - 1;
					$(child).html(plusvotes);
				}
				totalplus = $(child).html();
			}
			
			if ($(child).is('.minusvotes'))
			{
				minusvotes = $(child).html();
				if ((votetype == 'VOTEA') || (votetype == 'VOTECA'))
				{
					minusvotes++;
					$(child).html(minusvotes);
				}
				else if ((votetype == 'VOTEC')||(votetype == 'VOTERA'))
				{
					minusvotes = minusvotes - 1;
					$(child).html(minusvotes);
				}
				totalminus = $(child).html();
			}
			
			$(numbersdiv).attr('title', 'Positive: ' + totalplus + '<br />Negative: ' + totalminus);
			
			total = totalplus - totalminus;
			
			if ($(child).is('.score'))
			{
				$(child).html(total);
			}
			
			//changing the buttons
			if ($(child).is('.voteup'))
			{
				if ((votetype == 'VOTE') || (votetype == 'VOTEC'))
				{
					$(child).removeClass('voteup');
					$(child).addClass('votedup');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					var divid = $(child).attr('id'); 
					document.getElementById(divid).onclick = function (){doCallAjax('VOTER',ID,qID,divID);};
					$(child).attr("title", "Click to remove your vote");
				}
				else if (votetype == 'VOTEA')
				{
					$(child).attr("onclick", "");
					$(child).unbind('click');
					$(child).click(function() 
					{
						doCallAjax('VOTEC',ID,qID,divID);
					});
				}
				else if (votetype == 'VOTERA')
				{
					$(child).attr("onclick", "");
					$(child).unbind('click');
					var divid = $(child).attr('id'); 
					document.getElementById(divid).onclick = function (){doCallAjax('VOTE',ID,qID,divID);};
					$(child).attr("title", "Click to vote up");
				}

			}
			else if ($(child).is('.votedown'))
			{
				if ((votetype == 'VOTEA') || (votetype == 'VOTECA'))
				{
					$(child).removeClass('votedown');
					$(child).addClass('voteddown');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					var divid = $(child).attr('id'); 
					document.getElementById(divid).onclick = function (){doCallAjax('VOTERA',ID,qID,divID);};
					$(child).attr("title", "Click to remove your vote");
				}
				else if (votetype == 'VOTE')
				{
					$(child).attr("onclick", "");
					$(child).click(function() 
					{
						doCallAjax('VOTECA',ID,qID,divID);
					});
				}
				else if (votetype == 'VOTEC')
				{
					$(child).attr("onclick", "");
					$(child).unbind('click');
				}
				else if (votetype == 'VOTER')
				{
					$(child).attr("onclick", "");
					$(child).unbind('click');
					var divid = $(child).attr('id'); 
					document.getElementById(divid).onclick = function (){doCallAjax('VOTEA',ID,qID,divID);};
					$(child).attr("title", "Click to vote down");
				}
			}
			else if ($(child).is('.votedup'))
			{
				if ((votetype == 'VOTEA') || (votetype == 'VOTECA'))
				{
					$(child).removeClass('votedup');
					$(child).addClass('voteup');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					$(child).click(function() 
					{
						doCallAjax('VOTEC',ID,qID,divID);
					});
					$(child).attr("title", "Click to vote up");
				}		
				else if (votetype == 'VOTER')
				{
					$(child).removeClass('votedup');
					$(child).addClass('voteup');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					var divid = $(child).attr('id'); 
					document.getElementById(divid).onclick = function (){doCallAjax('VOTE',ID,qID,divID);};
					$(child).attr("title", "Click to vote up");
				}
			}
			else if ($(child).is('.voteddown'))
			{
				if ((votetype == 'VOTE') || (votetype == 'VOTEC'))
				{
					$(child).removeClass('voteddown');
					$(child).addClass('votedown');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					$(child).click(function() 
					{
						doCallAjax('VOTECA',ID,qID,divID);
					});
					$(child).attr("title", "Click to vote down");
				}
				else if (votetype == 'VOTERA')
				{
					$(child).removeClass('voteddown');
					$(child).addClass('votedown');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					var divid = $(child).attr('id'); 
					document.getElementById(divid).onclick = function (){doCallAjax('VOTEA',ID,qID,divID);};
					$(child).attr("title", "Click to vote down");
				}
			}
			return null;
		});
	}
	

	function updateRevVotes(votetype, ID, qID, divID) 
	{
		
		somediv = '#' + divID;
		var plusvotes = 0;
		var minusvotes = 0;
		var totalplus = 0;
		var totalminus = 0;
		var total = 0;

		$(somediv).children().map(function() 
		{
        	var child = $(this);
			//changing the visual values
			if ($(child).is('.pvotes'))
			{
				plusvotes = $(child).html();
				if ((votetype == 'VOTEREV') || (votetype == 'VOTEREVC'))
				{
					plusvotes++;
					$(child).html(plusvotes);
				}
				else if (votetype == 'VOTEREVCA')
				{
					plusvotes = plusvotes - 1;
					$(child).html(plusvotes);
				}
				totalplus = $(child).html();
			}
			
			if ($(child).is('.tvotes'))
			{
				total = $(child).html();
				if ((votetype == 'VOTEREVA') || (votetype == 'VOTEREV'))
				{
					total++;
					$(child).html(total);
				}
			}

			if ($(child).is('.voterevup'))
			{
				if ((votetype == 'VOTEREV') || (votetype == 'VOTEREVC'))
				{
					$(child).removeClass('voterevup');
					$(child).addClass('votedrevup');
					$(child).attr("onclick", "");
					$(child).unbind('click');
				}
				else if (votetype == 'VOTEREVA')
				{
					$(child).attr("onclick", "");
					$(child).unbind('click');
					$(child).click(function() 
					{
						doCallAjax('VOTEREVC',ID,qID,divID);
					});
				}
			}
			else if ($(child).is('.voterevdown'))
			{
				if ((votetype == 'VOTEREVA') || (votetype == 'VOTEREVCA'))
				{
					$(child).removeClass('voterevdown');
					$(child).addClass('votedrevdown');
					$(child).attr("onclick", "");
					$(child).unbind('click');
				}
				else if (votetype == 'VOTEREV')
				{
					$(child).attr("onclick", "");
					$(child).click(function() 
					{
						doCallAjax('VOTEREVCA',ID,qID,divID);
					});
				}
				else if (votetype == 'VOTEREVC')
				{
					$(child).attr("onclick", "");
					$(child).unbind('click');
				}
			}
			else if ($(child).is('.votedrevup'))
			{
				if ((votetype == 'VOTEREVA') || (votetype == 'VOTEREVCA'))
				{
					$(child).removeClass('votedrevup');
					$(child).addClass('voterevup');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					$(child).click(function() 
					{
						doCallAjax('VOTEREVC',ID,qID,divID);
					});
				}		
			}
			else if ($(child).is('.votedrevdown'))
			{
				if ((votetype == 'VOTEREV') || (votetype == 'VOTEREVC'))
				{
					$(child).removeClass('votedrevdown');
					$(child).addClass('voterevdown');
					$(child).attr("onclick", "");
					$(child).unbind('click');
					$(child).click(function() 
					{
						doCallAjax('VOTEREVCA',ID,qID,divID);
					});
				}
			}
			return null;
		});
	}
	
	

	function loadPicture(placeholder, picture) 
	{
		var img = new Image();
		$(placeholder).addClass('loading');
		$(img)
		.load(function (picture) 
		{
			$(this).hide();
			$(placeholder)
			.removeClass('loading')
			.html(this);
			$(this).fadeIn();
		})
		.error(function () 
		{
		  // notify the user that the image could not be loaded
		})
		.attr('src', picture);
	}
	
	function update_ranks(currentRank)
	{

		var arr = new Array();
		arr = document.getElementsByName('rank');
		for(var i = 0; i < arr.length; i++)
		{
			var obj = document.getElementsByName('rank').item(i);
			var rank = obj.innerHTML;
	
			if (rank > currentRank)
			{
				rank = rank - 1;
				obj.innerHTML = rank;
			}
		}
	}
	
	function updateranks(currentrank, spamtype)
	{
		var therank = 0;
//		alert(currentrank);
		
		$('#availableAnswers').children().each(function() 
		{
        	var child = $(this);
			child.children().each(function() 
			{
	        	var subchild = $(this);
				if (subchild.hasClass('list_rank')||subchild.hasClass('list_rank_hid'))
				{
					//alert (subchild.attr("id"));
					if ((spamtype == 'spam') && (therank > currentrank))
					{
						//alert (therank);
						therank--;
						$(this).html(therank + 'bla');
					}
					else
					{
						therank++;
						subchild.html(therank);
					}
				}
				return null;
			});

			
			return null;
		});


	}


	function showPhoto(photo, layer_ref, thumb_layer)
	{
		
		var orig_ref = layer_ref;
		
		$("#ai-" + layer_ref + " img").removeClass('imgActive');

		layer_ref = 'playvid' + layer_ref;
						 
		var placeholder =  '#' + layer_ref;
		var timage = '#' + 'i' + thumb_layer;
		var thumblayer = '#' + 'thm' + thumb_layer;
		
		if ($(timage).hasClass('imgActive'))
		{
			$(timage).removeClass('imgActive');
			$(placeholder).fadeOut('fast');
		}
		else
		{
			var photoobject = "<img src=\"" + photo + "\" alt=\"Image\" />";
			var parentdiv = '#answer-' + orig_ref;
			var somediv = '#ai-' + orig_ref;

			config(somediv);
			loadPicture (placeholder, photo);
			$(timage).addClass('imgActive');
			$(placeholder).show();
		}
	}

	function playVideo(video, layer_ref, thumb_layer)
	{
		
		var orig_ref = layer_ref;
		
		layer_ref = 'playvid' + layer_ref;
		var placeholder =  '#' + layer_ref;
		var timage = '#' + 'i' + thumb_layer;
		var thumblayer = '#' + 'thm' + thumb_layer;
		
		if ($(timage).hasClass('imgActive'))
		{
			$(timage).removeClass('imgActive');
			$(placeholder).fadeOut('fast');
			$(placeholder).html('');
		}
		else
		{
			var videoobject = "<object width=\"450\" height=\"277\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+video+"&hl=en_US&fs=1&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+video+"&hl=en_US&fs=1&rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"450\" height=\"277\"></embed></object>";
			
			var parentdiv = '#answer-' + orig_ref;
			var somediv = '#ai-' + orig_ref;

			config(somediv);
			
			$(placeholder).html(videoobject);
			$(timage).addClass('imgActive');
			$(placeholder).fadeIn('fast');
		}
	}
	
	function playVideoMain(video, layer_ref, thumb_layer)
	{
		
		var orig_ref = layer_ref;
		
		layer_ref = 'playvid' + layer_ref;
		var placeholder =  '#' + layer_ref;
		var timage = '#' + 'i' + thumb_layer;
		var thumblayer = '#' + 'thm' + thumb_layer;
		
		if ($(timage).hasClass('imgActive'))
		{
			$(timage).removeClass('imgActive');
			$(placeholder).fadeOut('fast');
			$(placeholder).html('');
		}
		else
		{
			var videoobject = "<object width=\"310\" height=\"191\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+video+"&hl=en_US&fs=1&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+video+"&hl=en_US&fs=1&rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"310\" height=\"191\"></embed></object>";
			
			var parentdiv = '#answer-' + orig_ref;
			var somediv = '#ai-' + orig_ref;

			config(somediv);
			
			$(placeholder).html(videoobject);
			$(timage).addClass('imgActive');
			$(placeholder).fadeIn('fast');
		}
	}

	function config(somediv) 
	{

    	$(somediv).children().map(function() 
		{
        	var child = $(this);

			if ( $("span[id^='thm']"))
			{
				var thisimage =	child.find("img");

				if ( thisimage.hasClass('imgActive'))
				{
					thisimage.removeClass('imgActive');	
				}
			}
			
			return null;
		});
	}
	
	function showAddTags()
	{
		document.getElementById('addsometags').style.display = "block";
		document.getElementById('spanaddsometags').style.display = "none";
	}
	
	function toggleshow(divID)
	{
		show_hide('addedby_' + divID);
		if ($('#sh_'+divID).html() == 'show')
		{
			$('#sh_'+divID).html('hide');
		}
		else $('#sh_'+divID).html('show');
		
	}
	
	function manageUpdates(uid, qid)
	{
		if ($('#chkfollow').is(':checked'))
		{
			doCallAjax('QFOLLOW',uid,qid,'');
		}
		else
		{
			doCallAjax('QUNFOLLOW',uid,qid,'');
		}
	}
	
	function showdiv (id) 
	{
		id = '#'+id;

		if ($(id).css('display')=='none')
		{
			$(id).slideDown("fast");
		}
	};

	function hidediv (id) 
	{
		id = '#'+id;
		if ($(id).css('display')=='block')
		{
			$(id).slideUp("fast");
		}
	};
	
	
	function editdesc()
	{
		var descdiv = '#itemdesc';
		var edittextarea = '#eddescription';
		var itemid = '#aid';
		var description = doCallAjax('GETITEMDESC', $(itemid).val(), edittextarea,descdiv);
	}

	function canceldesc()
	{
		var descdiv = '#itemdesc';
		var editdiv = '#edit_itemdesc';
		$(editdiv).hide();
		$(descdiv).show();
	}
	
	
	(function($){$.fn.replaceText=function(b,a,c){return this.each(function(){var f=this.firstChild,g,e,d=[];if(f){do{if(f.nodeType===3){g=f.nodeValue;e=g.replace(b,a);if(e!==g){if(!c&&/</.test(e)){$(f).before(e);d.push(f)}else{f.nodeValue=e}}}}while(f=f.nextSibling)}d.length&&$(d).remove()})}})(jQuery);
