// Greasemonkey http://greasemonkey.mozdev.org/
//
// ==UserScript==
// @name          GNN2 Readable 1.0.2
// @description   2 column layout with larger text, wider paragraphs and a few other features.
// @namespace     http://deoxy.org/gm
// @include       http://gnn.tv*
// @include       http://*.gnn.tv*
// @include       http://guerrillanews.com*
// @include       http://*.guerrillanews.com*
// ==/UserScript==
//
// Changelog
// 1.0.2 : Added download links to video pages.
// 1.0.1 : Double click anywhere to change font size.
//
// Two column layout with the yard moved to left side.
// Text size option, larger by default.
// Paragraphs are wider and allowed to flow according to window size.
// Sets the page title to the subject for forum posts.
//
// NOTE: Had to disable large edit boxes. For some reason resizing the 
// textarea really messes up the formatting inside it :-(

(function() {

  var myfontsize = GM_getValue('fontsize', 16);
  var lineheight = myfontsize;
  var fontstyle  = 'font-size:'+myfontsize+'px;line-height:'+lineheight+'px;}';
    
  // insert something like a stylesheet or script into document head
  function addGlobal(tag, type, data) {
    var head = document.getElementsByTagName('head')[0];
    if (head) {
      var text = document.createElement(tag);
      text.type = type;
      text.innerHTML = data;
      head.appendChild(text);
    }
  }

  // redefine some styles
  addGlobal('style','text/css',
  '.app {width:100%;}'+
  '#art_headline{'+fontstyle+
  '#asset_content {width:100%;}'+
  '.block     {width:100%;}'+
  '.blog      {width:100%;}'+
  '#blog_title{'+fontstyle+
  '#blogs     {width:100%;}'+
  '#body      {width:100%;}'+
  '#body     p{'+fontstyle+
  '.cat_title {'+fontstyle+
  '.comment   {width:100%;}'+
  '.comment  #reply p{width:100%;}'+
  '.comment  p{'+fontstyle+
  '#comments_wrap {width:100%;}'+
  '#comments  {width:100%;}'+
  '#right_col #blogs #comments .comment #reply p{width:100%;}'+
  '#content   {width:70%;}'+
  '#content #header {width:100%;}'+
  '#framework {width:100%;}'+
  '#hdl_headline td{width:300px;}'+
  '#hdl_headline b{'+fontstyle+
  '#homepage {width:100%;}'+
  '#homepage #left #headlines li{'+fontstyle+
  '#homepage #left .module .recent {height:100px}'+
  '#homepage #left .module .recent p{'+fontstyle+
  '#homepage #left .module li{'+fontstyle+
  '#homepage #left #article li{'+fontstyle+
  '#homepage #right #videos li{'+fontstyle+
  '#homepage #right .module li{'+fontstyle+
  '#list_page usr_name{'+fontstyle+
  '#post rpl_text{'+fontstyle+
//  '#post textarea{width:100%;height:400px;'+fontstyle+
  '#related   {width:100%;}'+
  '#replies   {width:100%;}'+
  '#reply     {width:100%;}'+
  '#reply    p{'+fontstyle+
  '#right_col {width:100%;}'+
  '#right_col #blogs h1{width:100%;}'+
  '#right_col #blogs .blog{width:100%;}'+
  '#right_col #blogs #text{width:100%;}'+
  '#rpl_text  {width:100%;}'+
  '#rpl_text p{width:100%;'+fontstyle+
  '#summary   {width:100%;}'+
  '#summary  p{'+fontstyle+
  '#text      {width:100%;}'+
  '#text     p{width:100%;'+fontstyle+
  '#thr_title {'+fontstyle+
  '#usr_image_fl {display:none;}'+
  '#content #listpage #vid_title {'+fontstyle+
  '#content #listpage #lnk_title {'+fontstyle+
  '#content #listpage .expanded .summary .desc {'+fontstyle+
  '#wrapper   {width:100%;}'
  );
  
  var loc = location.href;
  var oldleft = '';
  var move = '';
  
  // clean up the right-side
  document.body.style.background = '#ffffff';

  // get the login form or the yard and user menus
  var ctrl = document.getElementById('control');
  if (ctrl) {
    // make a copy of the yard so we can move it later
    move = '<div class=mod id=control style="background:#aa4444">'+ctrl.innerHTML+'</div>';
    ctrl.style.display = 'none';
  }
  
  // if this is a blog copy their info so we can move it too
  var leftcol = document.getElementById('left_col');
  if (leftcol) {
    oldleft = '<div class=mod>'+leftcol.innerHTML+'</div>';
    leftcol.style.display = 'none';
  }
  
  // grab all the divs
  var divs = document.getElementsByTagName('div');
  if (divs) {
    for (var i = 0; i < divs.length; i++) {
      var div = divs[i];
      if (div.style.width == '400px') {
          div.style.width  = '100%';
//      div.style.overflow = 'auto'; // optional
      }
      // put the yard under the search form
      if (i == 7) { 
        var old = div.innerHTML;
        div.innerHTML = oldleft + move + old;
      }
    }
  }

  // put subject in the page title of forum posts
  if (loc.match(/thread\.php/)) {
    var head = document.getElementById('header');
    if (head) {
      var hmm = head.innerHTML;
      hmm = hmm.replace(/\n/g, '');
      hmm = hmm.replace(/^.*?<h1[^>]*>(.*?)<\/h1>.*$/, '$1');
      hmm = hmm.replace(/<\!--.*?-->/g, '');
      hmm = hmm.replace(/<[^>]+>/g, '');
      document.title = hmm;
    }
  }

  // set up the double click
  var wrap = document.getElementById('wrapper');
  if (wrap) {
    wrap.ondblclick = function() {
      var hmm = prompt('Set font size', myfontsize);
      if (hmm.match(/^\d+$/)) {
        GM_setValue('fontsize', hmm);
        location.href = loc;
      }
    };
  }
    
  // add a download link to video popup
  if (loc.match(/viewer\.php/)) {
    var videodiv, div, media, mov;
    
    videodiv   = document.evaluate("//div[contains(@id,'ftr')]",
                 document,null, XPathResult.FIRST_ORDERED_NODE_TYPE,null);
    if (!(div = videodiv.singleNodeValue)) { return; } 
    
    media      = document.evaluate("//param[contains(@name,'src')]",
                 document,null, XPathResult.FIRST_ORDERED_NODE_TYPE,null);
    if (!(mov = media.singleNodeValue)) { return; } 
    
    var text = document.createElement('br');
    div.appendChild(text);
    
    text = document.createElement('a');
    text.setAttribute('href', mov.value);
    text.setAttribute('title', mov.value);
    text.appendChild(document.createTextNode('[download]'));
    div.appendChild(text);
  }
  
  // add download links to video pages
  if (loc.match(/\/videos\//)) {
    var div = document.getElementById('date');
    var a   = document.getElementsByTagName('a');
    var c = 0;
    if (a) {
      for (var i = 0; i < a.length; i++) {
        var hmm = a[i].getAttribute('onclick');
        if (hmm) {
          if (hmm.match(/^javascript:window\.open/)) {
            var uri = hmm.replace(/^.*?\('([^']+)'.*$/, '$1');
            uri = 'http://gnn.tv' + uri;
            // grab each video popup and find the url
            GM_xmlhttpRequest({
              method: "GET",
              url: uri,
              onload: function(doc) {
                uri = '';
                var vid = doc.responseText.replace(/[\n\r]/g, '');
                if (vid) uri = vid.replace(/^.*?<PARAM NAME="src" VALUE="([^"]+)">.*$/i, '$1');
                if (uri) {
                  c++;
                  var link = document.createElement('a');    
                  link.setAttribute('href',  uri);
                  link.setAttribute('title', uri);
                  link.appendChild(document.createTextNode(' [download'+c+'] '));
                  div.appendChild(link);
                }
              }
            });
          }
        }
      }
    }
  }
})();

