//	¬© Alexander Reichstadt 2009
//	All rights reserved without prejudice, no changes without premission
//	Permission requests to areichstadt@gmail.com
//
//	Version 1.0
//	2009-09-10	[yyyy-mm-dd]
//
//	Version 1.1
//	Added support by way of Request for reading dirs and files
//	2009-10-05	[yyyy-mm-dd]
//



var site_toolpath = 'http://apfeltaste.net/lib/HTTP/';
var site_host_readdir = 'http://'+document.location.host+'/readdir.php?dir=';

inc('Request.js');

function testLibrary()
{
	alert('Library lxlib.js loaded');
	testRequests();
}

function inc(filename)
{
	var body = document.getElementsByTagName('body').item(0);
	script = document.createElement('script');
	script.src = site_toolpath+filename;
	script.type = 'text/javascript';
	body.appendChild(script)
}

//*******************************
//	Filesystem Utility Methods
//*******************************

function getFileContents(queryuri){
	 var req1 = new HTTP.Request({
        URI: queryuri
       ,asynchronous: false
    });
    return req1.transport.responseText;
    
}

function asyncFileContents(queryuri)
{
	var req = new HTTP.Request(
		{
			asynchronous:true,
			uri: queryuri,
			onSuccess: function ( transport )
			{

			},
			onComplete: function (transport2)
			{

				alert(transport2.responseText);
			}
		}
	);		
}

function getDirContents(relPath,fileType)
{
	var thePath = relPath;
	var dirGetter = encodeURI(site_host_readdir+thePath);
	var theFilesString = getFileContents(dirGetter);
	var theFiles = theFilesString.split('<br><seperate><br>');
	theFiles.pop();
	if (fileType){
		var selFiles = new Array();
		for (var i=0;i<theFiles.length;i++){
			var ftype = theFiles[i].substring(theFiles[i].length-fileType.length-1,theFiles[i].length);
			if ('.'+fileType==ftype){
				selFiles.push(theFiles[i]);
			}
		}
		return selFiles;
	}
	return theFiles;
}


//*******************************
//	HTML Utility Methods
//*******************************

function appendDivToBodyWithName(newName)
{

	var mbody = document.body;
	var mdiv = document.createElement('div');
	mdiv.id = newName;
	mdiv.name = newName;
	mdiv.className=newName;
	mdiv.style.width = '5px';
	mdiv.style.height = '5px';
	mdiv.style.position="absolute";
	mdiv.style.float="top left";
	var tt = document.createTextNode('*'+newName);
	mdiv.appendChild(tt);
	mbody.appendChild(mdiv);
	return mdiv;
}

function makeSelectorStringFromArray(inArray,idName,methodName,keyMethodName)
{

	var mstring = "\r<form method=\"get\" onchange=\"javascript:"+keyMethodName+"()\">\r<select id=\""+idName+"\" onchange=\"javascript:"+keyMethodName+"()\">\r";

	for(var i=0;i<inArray.length;i++){
		mstring = mstring + '<option onmouseup="'+methodName+'(\''+inArray[i]+'\')">' +inArray[i]+ '</option>' + "\r";


	}
	mstring = mstring + "</select>\r";
	mstring = mstring + '</form>';

	return mstring;
}


//*******************************
//	String Methods
//*******************************
function chomp()
{
	var one = this.replace(/(\n|\r|\s)+$/, '');
	one = one.replace(/^(\n|\r)/, '');
	return one;
}
String.prototype.chomp = chomp;


function substringBetweenLeftAndRight(inLeft,inRight)
{
	var divstr = inLeft
	if (this.indexOf(divstr)==-1) return "";
	var sections = this.split(divstr);
	var eos = sections[1].indexOf(inRight);
	var interim = sections[1].substring(0,eos);
	return interim.chomp();
}
String.prototype.substringBetweenLeftAndRight = substringBetweenLeftAndRight;

function getDivWithIdFromString(theDivId)
{
	var divstr = '<DIV id="'+theDivId+'">';
	if (this.indexOf(divstr)==-1) return "";
	var sections = this.split(divstr);
	var eos = sections[1].indexOf('<'+'/DIV>');
	var interim = sections[1].substring(0,eos);
	return interim.chomp();
}
String.prototype.getDivWithIdFromString = getDivWithIdFromString;

function stringToChar(inChar)
{
	var i = this.indexOf(inChar);
	if (i>-1) return this.substring(0,i);
	return '';
}
String.prototype.stringToChar = stringToChar;

function tagStringToArray()
{
	var imax = this.length;
	var currentTag = '';
	var lastTag = '';
	var currentValue = '';
	var tagMap = new Array();
	var theItems = new Array();
	var recordTag = false;
	var recordValue = false;
	var tagOpen = true;
	var tagChanged = false;
	var currentObject;
	for (var i=0;i<imax;i++){
		var tchar = this.substring(i,i+1);
		if (tchar=="<"){
			lastTag = currentTag;
			currentTag='';
			recordTag=true;
			tagOpen=true;
			continue;
		}
		if (tchar=='/'){
			recordTag=true;
			tagOpen=false;
			continue;
		}
		if (tchar=='>'){
			
			recordTag	= false;
			tagChanged	= true;
			
			if (tagOpen){
				if (tagMap.length==0){
					currentObject = new Object();
				}
				tagMap.push(currentTag);
				recordValue = true;
			} else {
				recordValue = false;
				var closingTag = tagMap.pop();
				currentObject[closingTag] = currentValue;
				if (tagMap.length==0){
					if (currentObject != null){
						theItems.push(currentObject);
					}
				}

				currentValue = '';
			}
			continue;
		}
		
		if (recordTag){
			currentTag = currentTag+tchar;
			continue;
		}
		
		if (recordValue){
			currentValue = currentValue+tchar;
		}
		
	}
	/*
	for (var i=0;i<theItems.length;i++){
		for (var j in theItems[i]){
			alert(j + " => " + theItems[i][j]);
		}
	}
	*/
	return theItems;
}		
			
function findName()
{
	for (i in document.cookie){
		alert(i);
	}
}

function AAtagStringToArray()
{
	var rawA = this.split('<');
//	var oTags = new Array();
	var tagMap = new Object();
	
	for (var i=0;i<rawA.length;i++){
		if (rawA[i].substring(0,1)=="/") continue;
		var currentTag = rawA[i].stringToChar('>');
		tagMap[currentTag]='1';
//		alert(tagMap[currentTag]);
	}
	

	
	for (var i in tagMap){
		if (i) alert(i);
		
	}
		
}
String.prototype.tagStringToArray = tagStringToArray;


//*******************************
//	Miscellaneous Utility Methods
//*******************************

function randomScreenPosition(subarea)
{
	var sp = new Object();
	var usearea=1.0;
	if (subarea)usearea=subarea;
	sp.x = Math.floor(window.innerWidth * Math.random()*usearea);
	sp.y = Math.floor(window.innerHeight * Math.random()*usearea);

	return sp;
}
