$(function(){
   $('#MultiLinkr ul:first li a').click(function(){

	 if($('#' + $(this).attr('rel')).html() == ""){
	       UpdateLinks();
	 }

	 $('#MultiLinkr .sel').removeClass('sel');
	 $(this).parent().addClass('sel');
	 deselect()
	 $('#MultiLinkr div').hide();
	 $('#' + $(this).attr('rel')).show();
   });

   $('#MultiLinkr ul#SelectAll li a.select').click(function(){
      selectAll($('#MultiLinkr div:visible').attr('id'));
   });

   $('#PastedURLS').change(function(){
	UpdateLinks();
   });

   $('a.colour').click(function(){
	 setcolor($(this).attr('rel'));
   });

   var i=0;
   $('a.colour').each(function(){
	 i=i+1;
	 thid = $(this).attr('rel');
	 setTimeout('$("a.colour[rel=\'' + thid + '\']").animate({top: 0, height: \'50\'},1000);',i*200);
   });

});

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function UpdateLinks(){
$('#Links').html('');
$('#PastedURLS').val($('#PastedURLS').val().trim().replace(/\n /g,''));
d = new Date;
  $.ajax({
	type:	'POST',
	url:	'ajax.php',
	data:	{urls: $('#PastedURLS').val()},
	dataType:	'json',
	beforeSend: ShowLoader(),
	success:	function(data){
	    $('#Links').html(data[0]);
	    $('#BBCode').html(data[1]);
	    $('#HTML').html(data[2]);
		$('#trackers').html(data[3]);
	}
  });
}

function setcolor(colour){
  $.ajax({
	type:	'POST',
	url:	'ajax.php',
	data:	{setcolor: colour},
	success:	function(data){
	}
  });
}

function ShowLoader(){ }

function selectAll(el) {
      objId = el;

      deselect();
      
      if (document.selection) {
	    var range = document.body.createTextRange();
	    range.moveToElementText(document.getElementById(objId));
	    range.select();
      }
      else if (window.getSelection)
      {
	    var range = document.createRange();
	    range.selectNode(document.getElementById(objId));
	    window.getSelection().addRange(range);
      }
}

function deselect() {

      if(document.selection) {
	          document.selection.empty();
      } else if (window.getSelection){
	    window.getSelection().removeAllRanges();
      }
}

function openAllLinks(){
	$('#Links').find('a').each(function(){
		if($(this).attr('href')){
			window.open($(this).attr('href'));
		}
	});
}
