// DeviantART - CSS Nullify v0.4.2
// Made By Luke Stevenson {http://lucanos.deviantart.com}
// Distributed and Maintained via GMVC
// Last updated: 01 September 2009
//
//   This script provides the option to disable Custom CSS in deviantArt
// Journals
//
// ==UserScript==
// @name              DeviantART - CSS Nullify
// @namespace         http://gmvc.lucanos.com/
// @description       (v0.4.2) Provides a "Toggle CSS" button to allow for the Custom Journal CSS to be switched on and off.
// @include           http://*.deviantart.com/
// @include           http://*.deviantart.com/journal/*
// ==/UserScript==

function cssNullify() {
  var debug = false;
  if( debug ) { console.group( "cssNullify Executed" ); }

  var arrStyle = document.getElementsByTagName('style');
  var arrJournal = new Array();

  // It's a hack job, but it works... For the moment...
  if( arrStyle.length == 3 ) {
    arrJournal[arrJournal.length] = 0;
  }
/*  
  // Check each of the STYLE's contents for the string 'custom journal css'
  if( debug ) { console.group( "Checking Stylesheets for Custom Journal CSS" ); }
  for ( var i=0; i<arrStyle.length; i++ ) {
    if ( arrStyle[i].innerHTML.match( '\/* begin custom css *\/' ) ) {
      arrJournal[arrJournal.length] = i;
      if( debug ) { console.log( "Custom Journal CSS in Stylesheet %i" , i ); }
    } else {
      if( debug ) { console.log( "NO Custom Journal CSS in Stylesheet %i (%o)" , i , arrStyle[i] ); }
    }
  }
  if( debug ) { console.groupEnd(); }
*/

  if ( arrJournal.length>0 ) {
    // Custom Journal CSS found
    if( debug ) { console.group( "Custom Journal CSS Found - Adding Toggle Switch" ); }

    // Create the Button Container
    var CSSCont = document.createElement('div');
      CSSCont.style.right = '3px';
      CSSCont.style.position = 'absolute';
      CSSCont.style.padding = '0 2px 2px 2px';
      CSSCont.style.color = '#000000';
      CSSCont.style.backgroundColor = '#bac5ba';
      CSSCont.style.zIndex = '50';

    // Create the Button
    var CSSLink = document.createElement('a');
      CSSLink.textContent = '[ Toggle CSS ]';
      CSSLink.style.color = '#000000';

    // Set the Button Link
    CSSLink.href = "javascript:"+
                   "var cssArr=new Array('"+arrJournal.join(',')+"');"+
                   "for(var a=0;a<cssArr.length;a++){"+
                     "void(document.getElementsByTagName('style')[cssArr[a]].disabled=(document.getElementsByTagName('style')[cssArr[a]].disabled?false:true))"+
                   "};";

    // Put the Button inside the Container
    CSSCont.appendChild( CSSLink );

    // Find The Journals
    var journals = document.evaluate("//div[contains(@class,'journalbox')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

    // For Each Journal, Add the Toggle Button
    for (var i=0 ; i<journals.snapshotLength ; i++ ) {
      journals.snapshotItem(i).parentNode.insertBefore( CSSCont , journals.snapshotItem(i) );
    }

    if( debug ) { console.groupEnd(); }
  } else {
    // No Custom Journal CSS
    if( debug ) { console.log( "No Custom Journal CSS" ); }
  }

  if( debug ) { console.groupEnd(); }
}
cssNullify();
