function setSource(text){
	$('source').value+=text
}
function loadPage(quilt){
	buildQuilt(quilt);
	hideElement('grow');
	hideElement('generate');
	hideElement('help');
}
function randomWriter(){
	// get value of find and replaceWith textboxes
	// var source = new String();
	var source = document.getElementById('source').value;
	var userseed = document.getElementById('userseed').value;
	var seed = parseInt(document.getElementById('seed').value);
	var length = parseInt(document.getElementById('length').value);
	var hash = new Object()
	var index;
	var spacer = 0;
	var output = new String();
	var oldList = new Object();
	var list = new Array()
	var added = new Boolean(false);
	var seedString = new String();

	for (var j = 0; j < (source.length - seed); j++) {
		seedString = source.substring(j, j + seed);

		// find all chars that follow instances of the seed string
		if(!hash.hasOwnProperty(seedString)){
			list = new Array()
			list.push(source.charAt(j + seed));
			hash[seedString] = list;
		}else{
			oldList = hash[seedString]
			oldList.push(source.charAt(j + seed))
			hash[seedString] = oldList
		}
	}
	// pick start point
	var userSuccess = "";
	if(userseed != "" && hash.hasOwnProperty(userseed.substring(0, seed))){
	    console.log("found it")
	    seedString = userseed.substring(0, seed)
	    userSuccess = userseed;
	}
    if(userSuccess == ""){
        console.log("didnt find it")
        do{	
            do {
                index = Math.floor(Math.random() * source.length);
            } while ((index + seed) > source.length-1);
            // generate seed string
            seedString = source.substring(index, index + seed);
        } while(!isUpper(seedString[0]))
    }
			var quote = false;
			(userSuccess == "") ? output+=seedString : output+=userSuccess;
			var output_length = 0;
			if(seedString.search(' ') != -1)
			output_length = 1;
			while (output_length < length) {
				// get the list
				listOfChars = hash[seedString];
				// randomly choose a char from the list
				var num = Math.floor(Math.random() * listOfChars.length);
				var ch = listOfChars[num];
				if(ch != '"'){
					output += ch;
					if(ch== ' ')
					output_length++
				}
				// append the new char to the seed
				seedString += ch;
				// cut the first letter from the seed and add the new one to the
				// output string
				seedString = seedString.substring(1);

				if (seedString == source.substring(source.length-seed-1, source.length-1)){
					do {
						index = Math.floor(Math.random() * source.length );
						} while ((index + seed) > source.length-1);
						// generate seed string
						seedString = source.substring(index, index + seed);
					}
				}
				output = output.substring(0, output.length-1)
				if(output[output.length-1] != "."){
					if(	output[output.length-1] == "," || 
					output[output.length-1] == ";"){
						output = output.substring(0, output.length-2)	
					}
					output+= '.'
				}
				// rewrite the search area with the new text
				document.getElementById('receptacle').innerHTML = output;
			}
			function isUpper(parm) {
				var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
				if (parm == "") return false;
				for (i=0; i<parm.length; i++) {
					if (upr.indexOf(parm.charAt(i),0) == -1) return false;
				}
				return true;
			}
			function growQuilt(){
				// quilt.push({"id":quilt.length, "contents":[ $('name').value, $('website').value, $('submission').value]})
				$('basket').innerHTML = ""
				buildQuilt();
				
			}
			var leftovers = new Object()
			function buildQuilt(thisquilt){
				// columns = new Array('one', 'two', 'three', 'four', 'five')
				min_width = 80;
				savegrade1="0.6";
				savegrade2="0.25";
				savegrade3="0.667";
				savegrade4="0.333";
				bin = new Array(5)
				slot = 0;
				
				bin[0] = DIV({'id':'slot_one', 'class':'slot'})
				bin[1] = DIV({'id':'slot_two', 'class':'slot'})
				bin[2] = DIV({'id':'slot_three', 'class':'slot'})
				bin[3] = DIV({'id':'slot_four', 'class':'slot'})
				bin[4] = DIV({'id':'slot_five', 'class':'slot'})
				
				for (n in quilt) {
					id = thisquilt[n]["contents"][0]
					name = thisquilt[n]["contents"][1]
					site = thisquilt[n]["contents"][2]
					line = thisquilt[n]["contents"][3]
					pos = thisquilt[n]["contents"][4]
					neg = thisquilt[n]["contents"][5]
					
					/* find the colors */
					r = Math.floor(Math.random() * 255)
					g = Math.floor(Math.random() * 255)
					b = Math.floor(Math.random() * 255)

					grade1=PutInRange(parseFloat(savegrade1),0.0,1.0);
					grade2=PutInRange(parseFloat(savegrade2),0.0,1.0);
					grade3=PutInRange(parseFloat(savegrade3),0.0,1.0);
					grade4=PutInRange(parseFloat(savegrade4),0.0,1.0);

					ra=Math.round(r+(255-r)*grade1);
					ga=Math.round(g+(255-g)*grade1);
					ba=Math.round(b+(255-b)*grade1);

					rb=Math.round(r+(255-r)*grade2);
					gb=Math.round(g+(255-g)*grade2);
					bb=Math.round(b+(255-b)*grade2);

					rc=Math.round(r*grade3);
					gc=Math.round(g*grade3);
					bc=Math.round(b*grade3);

					rd=Math.round(r*grade4);
					gd=Math.round(g*grade4);
					bd=Math.round(b*grade4);

					/* build the element */
					do{
						max_width = Math.floor(Math.random() * 300)
						}while(max_width < min_width)

						div = DIV({'id': id, 'class':'pillow'})
						divbot = DIV({'class':'nameplate'})
						if(max_width % 5 == 0){

							text = 'rgb('+rb+','+gb+','+bb+')'
							bg = 'rgb('+rc+','+gc+','+bc+')'
						}else{
							if(max_width % 3 == 0){
								text = 'rgb('+rc+','+gc+','+bc+')'
								bg = 'rgb('+rb+','+gb+','+bb+')'
							}else{
								if(max_width % 2 == 0){
									text = 'rgb('+rb+','+gb+','+bb+')'
									bg = 'rgb('+rd+','+gd+','+bd+')'
								}else{
									text = 'rgb('+rc+','+gc+','+bc+')'
									bg = 'rgb('+ra+','+ga+','+ba+')'
								}
							}
						}
						setStyle(div, {'color': text, 'background-color': bg})
						setStyle(div, {'width': '118px'})
						setStyle(divbot, {'color': text})
						divbot.innerHTML = '<a style="font-weight:bold;text-decoration:underline;color:'+ text +'"href="'+ site +'" >'+ name +'</a>'
						// divbot.innerHTML += '<br><table style="font-size:12px;color:'+text+'" align="center"><tr><td><div class="tri" style="border-bottom: 10px solid '+ text +';border-top: none;border-right: 5px solid '+ bg +';border-left: 5px solid '+ bg +';"></div></td><td>' + pos + '</td><td>&nbsp;&nbsp;|&nbsp;&nbsp;</td><td>' + neg + '</td><td><div class="tri" style="border-top: 10px solid '+ text +';border-bottom: none;border-right: 5px solid '+ bg +';border-left: 5px solid '+ bg +';"></div></td></tr></table>'; 
						// setStyle(div, {'max-width': max_width})
						// setStyle(div, {'min-width': min_width})
						setNodeAttribute(div, {'id':n})
						if(line.length > 300)
							line = compressInit(div, n, line)
						div.innerHTML = line
						appendChildNodes(div, divbot)
						appendChildNodes(bin[slot], div)
						
						if(slot==4)
							slot=0
						else
							slot++
					}
					for(n in bin)
					appendChildNodes('basket', bin[n])
				}
				function PutInRange(n,l,h) { return (n<l)?l:((n>h)?h:n); }
				function compressInit(elem, id, txt){
					leftovers['id'] = txt.substring(298, txt.length-1)
					// log(leftovers['id'])
					txt = txt.substring(0, 297) + "..."
					// log(txt)
					// line += '<br><a style="text-align:right;color:'+ text + '" href="#" onclick="expand(this);return false;">v</a>'
					updateNodeAttributes(elem, {'onclick':'expand(this);return false;', 'style': {'cursor':'pointer'}})
					return txt
				}
				function compress(elem){
					log(elem.parent)
					id = getNodeAttribute(elem.parent, "id")
					getNodeAttribute(elem.parent)
					txt = elem.parent.innerHTML
					txt = txt.substring(0, 297) + "..."
					// line += '<br><a style="text-align:right;color:'+ text + '" href="#" onclick="expand(this);return false;">v</a>'
					updateNodeAttributes(elem.parent, {'onclick':'expand(this);return false;'})
				}
				function expand(elem){
					id = getNodeAttribute(elem.parent, "id")
					elem.parent.innerHTML = leftovers['id']
					updateNodeAttributes(elem.parent, {'onclick':'compress(this);return false;'})
					
				}

