


function replaceAllInstances( str, searchTerm, replaceWith, ignoreCase ) {
    var regex = "/"+searchTerm+"/g";
    if( ignoreCase ) regex += "i";
    return str.replace( eval(regex), replaceWith );
}

function callserver() {
 var remoteURL = '/ajax/keepalive';
 $.get(remoteURL, function(data) { setTimeout("callserver()",10000); });
}
    
$(document).ready(function() {
 
 	setTimeout("callserver()",10000); 
    
    $.fn.jump = function() {
      $('#verticalsidebar').removeClass('greetmode').addClass('normal');
    };
    
    /*
    $.fn.delay = function(time, callback){
     jQuery.fx.step.delay = function(){};
     return this.animate({delay:1}, time, callback);
    }

    $('#greetbox').delay(8000, function() {
     $.cookie("hidegreet", "1", { expires: 7 });
     $(this).fadeOut().jump();
    });
    */
    
    $('.closegreet').click(function() {
        $.cookie("hidegreet", "1", { expires: 7 });
        $('#greetbox').hide().jump();
        return false;
    });
    


   
    $('a.toc').attr('href','javascript:void(0);');
    $('a.toc').click(function () {
        $(window)._scrollable();        
        $(window).scrollTo( '#_'+this.id, 1000, {easing:'easeInOutCirc'});
    });


    $("a.discussionlinks").each(function(){
    
         if($(this).attr("title").match('sociologically.net')===null )	{
                        $(this).addClass('external');
			$(this).after(' <img alt="Link outside of Sociologically.net" src="/graphics/link-outside.gif" />');
			$(this).fancybox({'hideOnContentClick': false});                        
         } else {
			$(this).after(' <img alt="Link inside of Sociologically.net" src="/graphics/link-inside.gif" />');
                        $(this).attr("href",$(this).attr("title"));
			
         }
    });
    
	 $('a.discussionlinks').qtip({
	  content: {
	     text: false 
	  },
	  style: {
	     name:'dark', border: {width:2, radius:6}, tip:true
	  },
	  position: {
corner: {
         target: 'topMiddle',
         tooltip: 'bottomLeft'
      }
	   
	  }
	 });	     
    
    $('a.closefancy').live('click', function (e) {
        $(this).attr('href','javascript:void(0);');
        parent.$.fn.fancybox.close();
        return false;
    });
    
	$(".sharebox img").mouseover(function(){
		$(".sharemessage").text(this.title);
	}).mouseout(function(){
		$(".sharemessage").text("");
	});
        
    $('a.sociopedia').cluetip({sticky: true, closePosition: 'bottom', activation: 'click', closeText: 'Close this box', showtitle: false, dropShadow: false, fx:{open: 'fadeIn'}});
 	
    $('.obfuscated').each(deObfuscateEmail);
		function deObfuscateEmail(i) {
		var content = $(this).text().replace(' AT ', '@').replace(' DOT ', '.');
		$(this).replaceWith($('<a href="mailto:'+content+'" title="Click to send an email to '+content+'">'+content+'</a>'));
	}    
    
    

    

    $("a#feedback").attr('href','/feedback/pop');
    $('a#feedback').fancybox({
            'hideOnContentClick': true
    });	
    
    $('#byline').hide();
    $('#byline').fadeIn('slow');

    $('img.userimg').qtip({
	  content: {
	     text:false
	  },
	  style: {
	     name:'dark', border: {width:2, radius:6}, tip:true, width: { min: 180 }
	  },
	  position: {
corner: {
         target: 'bottomMiddle',
         tooltip: 'topLeft'
      }
	   
	  }
	 });	     
    
    

 $(function(){
	$('input').keydown(function(e){
	 if (e.keyCode == 13) {
		$(this).parents('form').submit();
		return false;
	 }
	});
 });

 $("#loginform_sidebar").validate({
	errorClass: "frontinputerror"
 });

 $("#searchform_sidebar").validate({
	errorClass: "frontinputerror"
 });


  
 $('#subscribe').live('click', function (e) {
    e.preventDefault();
       var element = this;
       var subscribethread = $(element).attr('rel');         
       
       
       $('.form_loader').css('visibility','visible');

       $.ajax({
        type: "GET",
           url: "/discussions/student-research-help/91/notifiers",
           complete: function(){
              $('#subscribecontainer').load('/ajax/paintsubscribe/'+subscribethread).hide();
              $('#subscribecontainer').fadeIn('slow');
              $('.form_loader').css('visibility','hidden'); 
           }
       });
      
       return false; 
   });         
  
	$('#threadfilter').change(function() {
		window.location = '/discussions/?group='+ $(this).val();
	});  
  
	$(".sharebox img").mouseover(function(){
		$(".sharemessage").text(this.title);
	}).mouseout(function(){
		$(".sharemessage").text("");
	});

     
	$('a.reply').live('click', function (e) {
	    e.preventDefault(); 
        id = this.id;
        $('#replyingto').load('/ajax/getreplyname/'+id);        
        $('#parent').attr('value',id);        
        $(window)._scrollable();
	$(window).scrollTo( '#replyingto', 1000);
        $('#replyingto').fadeIn('slow');     
        $('#commentcontent').focus();
        return false;
    });
    

    
    var dummy = $('#replyingto').text();

    if ( dummy.length==1 || dummy.length==0 ) {
        $('#commentcontent').focus( function () {
        	 id = $('#parent').val();
             $('#replyingto').load('/ajax/getreplyname/'+id).hide();
             $('#replyingto').fadeIn('slow');           
        });
   	}  
    
    

    $('#replyform').validate({
      invalidHandler: function() {
        $('.submitloader').hide();
        $('#replyform .button').attr('value','Post your comment'); 
      }        
    });

    function animateResults(){
      $("#poll-results div").each(function(){
          var percentage = $(this).next().text();
          $(this).css({width: "0%"}).animate({
                    width: percentage}, 'slow');
      });
    }
    
    animateResults(); 
    
	      
  	       
      



});