      (function($) {
        var cache = [];
        // Arguments are image paths relative to the current page.
        $.preloadImages = function(imgs) {
          for (var i = imgs.length; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = imgs[i];
            cache.push(cacheImage);
          }
        }
      })(jQuery)
    
      clicks = 0;
      function catchclicks() {
        clicks++;
        if(clicks== 2) { 
          alert('We are still processing your CC.\n' + 'Thank you for your patience.');
        }
        if(clicks> 1) { return false; }
        return true;
      }
    
      $(function() {
        var preloads = [];
        var nonROSource;
$('img.rollover').each(function(i) {
        nonROSource = $(this).attr('src');
        preloads.push(nonROSource.substring(0,nonROSource.lastIndexOf('.'))+'-on'+nonROSource.substr(nonROSource.lastIndexOf('.')));
      }).bind('mouseover',function(event) {
        nonROSource = $(event.target).attr('src');
        $(event.target).attr('src',nonROSource.substring(0,nonROSource.lastIndexOf('.'))+'-on'+nonROSource.substr(nonROSource.lastIndexOf('.')));
      }).bind('mouseout',function(event) {
        nonROSource = $(event.target).attr('src');
        $(event.target).attr('src',nonROSource.substring(0,nonROSource.lastIndexOf('-'))+nonROSource.substr(nonROSource.lastIndexOf('.')));
      });

	$('select.jumpmenu').bind('change',function(event) {
	  url = $(event.currentTarget)[0].value;
	  if (url.substr(0,4) == "http") {
	    window.open(url,'_blank');
	  } else {
	    window.location = url; 
	  }
	});
        $.preloadImages(preloads);
      });
      
    
    function setupFancyComments(id) {
    	$('#cmt-form').unbind('submit');    	
		$('#cmt-form').submit( function() {
			$.fancybox.showActivity();
			if($.postingComment == true) {
				return false;
			} else {
				$.postingComment = true;
				$('#cmt-form').unbind('submit');
				$('#cmt-form').submit( function() {
					return false;
				});
			}
			
			if($('#tie_to').val() == '') {
				var obj_id = id.substr(8);
				$('#tie_to').val(obj_id);
			} else {
				var obj_id = $('#tie_to').val();
			}
			
			var inline_form = false;
			if($('#cmt_form_type').val() == 'inline') {
				inline_form = true;	
			}
			
	    	$.post("comments.ajax", $("#cmt-form").serialize(), function(msg) {
	    		$('#view-cmt-'+obj_id).trigger('click');
	    		$.postingComment = false;
	    		
				$('#cmt-form textarea').each(function() {
					$(this).val('');
				});
	    		
	    		$.post("comments.ajax", {action: "getnum", content_id: obj_id }, function(data) {
	    			$('#num-cmt-'+obj_id).html(data);
	    		});
	    		
	    		if(inline_form == true && msg != 'false') {
	    			$.post("comments.ajax", {action: "gethtml", cmt_id: msg.cmt_id }, function(data) {
	    				$.fancybox.hideActivity();
						$('#inline-comments p').remove();
	    				$('#inline-comments').append(data.html);
	    				$('#single-cmt-'+msg.cmt_id).fadeIn();
	    				setupFancyComments(id);
	    			}, 'json');
	    		}
	    	}, 'json');

	    	return false;
	    });
	}
	
	

	function setupCommentsLinks() {
		$(".view_comments").fancybox({
			'autoScale'      : true,
			'width'          : 515,
			'height'		 : 'auto',
			'scrolling'      : 'auto',
			'overlayOpacity' : 0.7,
			'transitionIn'	 : 'elastic',
			'transitionOut'	 : 'elastic',
			'overlayColor'   : '#333333',
			'autoDimensions' : false,
			'centerOnScroll' : true,
			'onComplete'     : function() {
			    $("#fancybox-content > div").attr('id','fancy-scroll');
			    $("#fancy-scroll").height($("#fancybox-content").height());
			    setupCommentsLinks();
			}
		});
		
		
		$(".login-required").fancybox({
			'autoScale'      : true,
			'width'          : 515,
			'height'		 : 'auto',
			'scrolling'      : 'auto',
			'overlayOpacity' : 0.7,
			'transitionIn'	 : 'elastic',
			'transitionOut'	 : 'elastic',
			'overlayColor'   : '#333333',
			'autoDimensions' : false,
			'centerOnScroll' : true
		});
		
		
		$(".post_comments").each( function() {
			$(this).fancybox({
				'autoScale'      : true,
				'width'          : 515,
				'height'		 : 'auto',
				'scrolling'      : 'auto',
				'overlayOpacity' : 0.7,
				'transitionIn'	 : 'elastic',
				'transitionOut'	 : 'elastic',
				'overlayColor'   : '#333333',
				'autoDimensions' : false,
				'centerOnScroll' : true,
				'this'			 : $(this),
				'onComplete'     : function(links, index, obj) {
				    $("#fancybox-content > div").attr('id','fancy-scroll');
				    $("#fancy-scroll").height($("#fancybox-content").height());
				    setupFancyComments(obj['this'].attr('id'));
				}
			});
		});
		
		$( "#emotion-slider" ).slider({
			value: 2,
			min: -2,
			max: 2,
			step: 1,
			slide: function( event, ui ) {
				$( "#emotion_level" ).val( ui.value );
			}
		});
	}
	
	
	
	function setupReportSpamLinks() {
		$('.report-spam').click( function() {
			var obj_id = $(this).attr('id').substr(12);
			
			if($(this).hasClass('report-spam-cmt')) {
				var item_type = "cmt";
			} else if($(this).hasClass('report-spam-forum')) {
				var item_type = "forum";
			}
			
			$.post("comments.ajax", {action: "reportspam", item_id: obj_id, item_type: item_type }, function(data) {
    			$('#spam-status-cmt-'+obj_id).html('<em>Marked as spam</em> | ');
    		}, 'json');
		});

	}


	
	$(function() { setupCommentsLinks(); setupReportSpamLinks(); });
