//Copyright 2009 by Rolfe R. Schmidt, http://rolfeschmidt.com
// Licensed under Creative Commons Attribution 3.0, http://creativecommons.org/licenses/by/3.0/us/

var rainbowColorSequence = [new RGBColor('red'),
    new RGBColor('orange'),
    new RGBColor('yellow'),
    new RGBColor('green'),
    new RGBColor('blue'),
    new RGBColor('purple')];
	
var blackColorSequence = [new RGBColor('black')];
var whiteColorSequence = [new RGBColor('white')];
	
var colorSequence = rainbowColorSequence ;
	
var currColor = 0;

var markedPrimes = [];

function isPrime(n) {
    var j;
    for(j = 2;j < Math.sqrt(n) + 1; j++) {
        if (n%j == 0 && n > 2) {
            return false;
        }
    }

    return true;

}
function mixColors(c1, c2) {
    var color1 = new RGBColor(c1);
    var color2 = new RGBColor(c2);
    color1.mix(color2);

    return color1.toHex();
}

function markAsComposite(cells, number) {
    var theCell = cells[number - 1];
    theCell.composite = 'true';
    theCell.numPrimeFactors = theCell.numPrimeFactors ? parseInt(theCell.numPrimeFactors) + 1 : 1;
}
function color() {
	return colorSequence[(currColor-1) % colorSequence.length];
}
	
function markAsPrime(cells, number, colorscheme, skipConfirm) {
    var proceed = true;
    if (!skipConfirm && !isPrime(number)) {
        proceed = confirm("The number " + number + " is not prime, are you sure you want to mark it?");
    }
    if(proceed) {
        var theCell = cells[number - 1];
        theCell.prime = 'true';
        cells[number - 1].style.fontWeight= 'bold';
        cells[number - 1].style.backgroundColor = (colorscheme == 1) ? (new RGBColor('white')).mix(color(), 0.8).toHex() : 'white';
        cells[number - 1].style.color= 'black';
        cells[number - 1].style.border= $('borderwidth').value + ' solid black';
    }
	
	return proceed;

}

function allPrimesFound(cells) {
    rt = Math.sqrt(cells.length + 1);
	
    var i;
    for(i = 1; i < rt; ++i) {
        if(cells[i].composite != 'true' &&  cells[i].prime != 'true') {
            return false; // there is an unmarked cell
        }
    }
    return true;
}


function markCell(cell, fast, colorscheme) {
	if(colorscheme == 3) {
		cell.style.color = "white";
	} else {
		var bg = new RGBColor(cell.style.backgroundColor);
		var f = parseInt(cell.numPrimeFactors);
		var wt = f/(1+f);
		var mix = $('mixcolors').checked ? bg.mix(color(), wt).toHex() : color().toHex();
		if(colorscheme == 2)
			cell.style.color = 'white';
			
			
		if(fast){
			cell.style.backgroundColor = mix;
			cell.style.borderColor = mix;
		} else {
			cell.style.borderColor = mix;
			new Effect.Highlight(cell, {duration: 0.1, startcolor: color().toHex(), endcolor: mix, restorecolor: mix, queue: 'end'});
		}
	}
}
function setColorSequence() {

	var colorscheme = parseInt($('colorscheme').value);
	if(colorscheme == 1) {
		colorSequence = rainbowColorSequence;
	} else if (colorscheme == 2) {
		colorSequence = blackColorSequence;
	} else if (colorscheme == 3) {
		colorSequence = whiteColorSequence;
	}
	return colorscheme;
}

function markAllPrimes(tableId) {
	var colorscheme = setColorSequence();
	var histcell;
	cells = $$('#' + tableId + ' td.tablecell');
	
	for(var i=1; i < cells.length; i++) {
		if(cells[i].prime != 'true' && cells[i].composite != 'true') {
			currColor++;
			markAsPrime(cells, i+1, colorscheme, true);
			step = parseInt(cells[i].innerHTML);
			for(var j=2*step-1;j<cells.length;j += step) {
				//adjust histogram
				theCell = $('cell' + (j+1));
				if(!theCell.composite) {
					histcell = $('rowhist' + (j+1));
					countcell = $('rowcount' + parseInt(j/parseInt($F('width'))));
					if(histcell) {
						histcell.style.display='none';
						countcell.innerHTML = parseInt(countcell.innerHTML) - 1;
					}
				}
				//theCell.title += (", " + step);
				theCell.composite = 'true';
				markCell(theCell, true, colorscheme);
				
			}

		} 
	}
}

function removeFromHistogram(cellnum, fast) {
	countcell = $('rowcount' + parseInt((cellnum-1)/parseInt($F('width'))));
	if(countcell) {
		countcell.innerHTML = parseInt(countcell.innerHTML) - 1;
	
		if(fast) {
			$('rowhist' + cellnum).style.display = 'none';
		} else {
			Effect.Squish('rowhist' + cellnum);
		}
	}
}

function markTable(step) {
	//set the color sequence
	currColor++;
	var colorscheme = setColorSequence();
    var i=0;
    var cells = $$('#sievetable td[rscountcell]');
	var fast = (cells.length > 200);
    if (cells.length > step) {
        markAsPrime(cells, step, colorscheme);
    }
    for(i=2*step-1;i<cells.length;i += step) {
		if(!cells[i].composite) {
			removeFromHistogram(i+1, fast);
		}
		//cells[i].title += (", " + step);
        cells[i].composite = 'true';
        markCell(cells[i], fast, colorscheme);
    }
    if (allPrimesFound(cells)) {
        var markAll = confirm("All primes on this table have been found.  Mark all of them?");

        if(markAll) {
            markAllPrimes('sievetable');
        }
    }
}

function cellMouseOver(id) {
    $(id).className = 'tablecellmouseover';
}
function cellMouseOut(id) {
    $(id).className = 'tablecell';
}

function toggleAdvancedOptions() {
	$('advancedopts').style.display = ($('advancedopts').style.display == 'none') ? '' : 'none';
	$('toggleopts').innerHTML = ($('toggleopts').innerHTML == 'More...') ? 'Hide...' : 'More...';
}


