function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=365;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function getCookie(name) {
 var start = document.cookie.indexOf( name + "=" );
 var len = start + name.length + 1;
 if ((!start) && (name != document.cookie.substring(0, name.length))) {
  return null;
 }
 if ( start == -1 ) return null;
 var end = document.cookie.indexOf( ';', len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}

function setOrder() {
	
  val = Sortable.serialize('order');
  setCookie('order', val);
}

function toggleMore(nr) {
  if($('more_'+nr).style.display == 'none') {
    Effect.BlindDown('more_'+nr, {duration:.5});
    $('link_'+nr).innerHTML = 'less <img src=\"http://www.originalsignal.com/images/less.gif\" alt=\"fold\" />';
  } else {
    Effect.BlindUp('more_'+nr, {duration:.5});
    $('link_'+nr).innerHTML = 'more <img src=\"http://www.originalsignal.com/images/more.gif\" alt=\"go\" />';
  }
  
  return false;
}

function toggleMoreMy(nr) {
  if($(nr).style.display == 'none') {
    Effect.BlindDown(nr, {duration:.5});
    $('link_'+nr).innerHTML = 'less <img src=\"http://www.originalsignal.com/images/less.gif\" alt=\"fold\" />';
  } else {
    Effect.BlindUp(nr, {duration:.5});
    $('link_'+nr).innerHTML = 'more <img src=\"http://www.originalsignal.com/images/more.gif\" alt=\"go\" />';
  }
  
  return false;
}

function setScript(domain,count) {
  $('script').innerHTML = "<script type=\"text/javascript\" src=\"http://"+domain+".originalsignal.com/widget?count="+count+"\"></script>";
}

function addBlock(domain,id) {
  allblocks = document.getElementsByClassName('fulllist');
  count = allblocks.length;
  if(count == 3 || count == 6 || count == 9 || count == 12 || count == 15 || count == 18) {
    new Insertion.Bottom('feedblocks', "<br />");
  }
  if($(domain+"-"+id)) {
    new Effect.Highlight(domain+"-"+id);
  } else {
    new Ajax.Updater('feedblocks', 'block.php?type='+domain+'&id='+id, {
      insertion:Insertion.Bottom, 
      onComplete:function(request){
        new Effect.Appear(domain+"-"+id,{});
      }
    }); 
  }
  return false;
}

function array_search(val, arr) {
	var i = arr.length;
	while (i--)
		if (arr[i] && arr[i] === val) break;
	return i;
}

function delBlock(domain,id) {
  blocks = getCookie('blocks');

  cookieVal = blocks.split("&blocks[]=");
  key = array_search(domain+"-"+id, cookieVal);
  cookieVal.splice(key,1);
  cookieVal = cookieVal.join("&blocks[]=");
  
  setCookie('blocks',cookieVal);
  
  new Effect.Fade(domain+"-"+id);
  
  return false;
}

function newBlock(feed) {
  allblocks = document.getElementsByClassName('fulllist');
  count = allblocks.length;
  if(count == 3 || count == 6 || count == 9 || count == 12 || count == 15 || count == 18) {
    new Insertion.Bottom('feedblocks', "<br />");
  }
  new Ajax.Updater('feedblocks', 'block.php?type=custom&id='+feed, {
    insertion:Insertion.Bottom, 
    onLoading:function(request) {
      $('indicator').style.display = '';
    },
    onComplete:function(request){
      $('indicator').style.display = 'none';
      new Effect.Appear("new-"+feed,{});
      new Effect.SlideUp('select_feeds');
    }
  }); 
  return false;
}

function popup(url) {
	var winl = (screen.width - 460) / 2;
	var wint = (screen.height - 450) / 2;
	var thevars = 'resizable=yes,scrollbars=yes,status=0,menubar=no,width=460,height=450,top=' + wint + ',left=' + winl;
	
	newwindow=window.open(url,'popup',thevars);
	if (window.focus) {newwindow.focus()}
	return false;
}
