/// Collection of javascript functions to be used across the site anarchius.org. See site for copyright information.

//////////------------------- Generic Reusable Code -------------------------------//////////////
//function changecss(theClass,element,value)
//function getURLParameter( name )
//function getURLParameterNames( )

// Function ChangeCSS. This function changes the css attributes of a particular class
// Adapted from initial version at http://www.shawnolson.net/scripts/public_smo_scripts.js
// Target support: FF, IE and Opera

function changecss(theClass,element,value) 
{
    var cssRules;
    theClass = "."+theClass; // Removes requirement of defining classnames with preceding "." selector
    
    if( document.styleSheets && document.styleSheets[0] ) {
        if( document.styleSheets[0].cssRules ) {
            cssRules = "cssRules";
        } else if (document.styleSheets[0].rules) {
            cssRules = "rules";
        } else {
            return;
       }
    } else {
        return;
    }
    
    for (var S = 0; S < document.styleSheets.length; S++){
        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
                document.styleSheets[S][cssRules][R].style[element] = value;
            }
        }
    }
}

// Following two function are courtesy http://www.netlobo.com/url_query_string_javascript.html
// Function to parse and extract the parameters from a URL using regular expressions in Javascript

function getURLParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// Function returns an array of all the parameter names in the URL. This can be used to parse URL parameters in the calling function

function getURLParameterNames( )
{
	var params = new Array( );
	var regex = /[\?&]([^=]+)=/g;
	while( ( results = regex.exec( window.location.href ) ) != null )
		params.push( results[1] );
	return params;
}

//////////------------------- Specific Site Implementation Code -------------------------//////////////

// Function to filter the different articles in the writings page. The writingType can be articles, fiction, poetry, travel

function filterWritings(writingType) 
{
	// make all the classes visible
	changecss("writings-fiction","display","block");
	changecss("writings-articles","display","block");
	changecss("writings-travel","display","block");
	changecss("writings-poetry","display","block");
	
	// Based on the arguments, disable the corresponding sections
	if( writingType == "fiction" )
	{
		changecss("writings-articles","display","none");
		changecss("writings-travel","display","none");
		changecss("writings-poetry","display","none");
	}
	else if ( writingType == "articles" )
	{
		changecss("writings-fiction","display","none");
		changecss("writings-travel","display","none");
		changecss("writings-poetry","display","none");
	}
	else if ( writingType == "travel" )
	{
		changecss("writings-fiction","display","none");
		changecss("writings-articles","display","none");
		changecss("writings-poetry","display","none");
	}
	else if ( writingType == "poetry" )
	{
		changecss("writings-fiction","display","none");
		changecss("writings-articles","display","none");
		changecss("writings-travel","display","none");
	}
}

// Function is designed run on load, and identify the appropriate filter type to call

function filterWritingsOnLoad()
{
	// Get the parameter in the URL
	var param = getURLParameter('show');
	
	if (param == "")
	{
	}
	else
	{
		filterWritings(param);
	}
}

// This function is used by the About Me page to show a message if the user has successfully been able to
// mail me. The success is through a URL parameter that is redirected to from the script.
function showContactMeSuccess()
{
	// Get the parameter in the URL
	var param = getURLParameter('status');
	
	if (param == "success")
	{
		changecss("contactmesuccess","display","block")
	}
}