var instantLogo;

$(document).ready(function() {
    instantLogo = new InstantLogo(document.getElementById("instantLogoCanvas"));
	instantLogo.render();

    $("#instantLogoCanvas").mousedown(function(){
		instantLogo.render();
	});
});

//////////////////
// InstantLogo
//////////////////

// constants
InstantLogo.LOGOSIZE = 40;
InstantLogo.AREASIDE = InstantLogo.LOGOSIZE*2.5;
InstantLogo.RANDOMSEEDSNUM = 38;
InstantLogo.IS_COLOR = true;
InstantLogo.IS_SOLID_COLOR = false;

// requires: easel.js
// Required argument: a canvas element. Optionally a list of 38 seed numbers.
function InstantLogo(canvasElement, savedSeeds) {
	// privates
	var myCanvas = canvasElement, isBlack, isSquare;
	
	// easel objects
	var myStage
	var instantLogo = new Container();
	var mask = new Shape();
	var mask2 = new Shape();
	var logo = new Shape();

	var seeds = savedSeeds || [];
	var first = true;
	
	init();

	// public/getters
	this.render = function () {
		renderLogo();
	}
	
	this.getSeeds = function () {		
		return seeds;
	}
	
	this.getIsBlack = function () {
		return isBlack;
	}
	
	// private
	function init() {
		var context = myCanvas.getContext("2d");

	    myStage = new Stage(myCanvas);
		myStage.addChild(instantLogo);

		instantLogo.x=(myCanvas.width/2)-(InstantLogo.AREASIDE/2);
		instantLogo.y=(myCanvas.height/2)-(InstantLogo.AREASIDE/2);
		//instantLogo.compositeOperation="xor";
		
		instantLogo.addChild(logo);

		logo.x = InstantLogo.AREASIDE/2;
		logo.y = InstantLogo.AREASIDE/2;

		//renderLogo();	
	}
	
	function readAndSetSeeds() {		
		if (first){
			first = false;
			if (seeds.length == InstantLogo.RANDOMSEEDSNUM) return;
		}
		for (var i=0; i < InstantLogo.RANDOMSEEDSNUM; i++) {
			seeds[i] = Math.random();
		};
	}

	function renderLogo()
	{
		readAndSetSeeds();

		isBlack = 0//Math.round(seeds[0]);
		isSquare = Math.floor(seeds[1]*3);

		logo.graphics.clear();
		
		if (InstantLogo.IS_SOLID_COLOR) {
			logo.graphics.beginFill(randomColorAsString([seeds[2],seeds[3],seeds[4],seeds[5],seeds[6],seeds[7]]))
		} else {
			logo.graphics.beginLinearGradientFill(
				[randomColorAsString([seeds[2],seeds[3],seeds[4],seeds[5],seeds[6],seeds[7]]),
				randomColorAsString([seeds[8],seeds[9],seeds[10],seeds[11],seeds[12],seeds[13]])],
				[0,1],0,0,Math.floor(seeds[14]*(InstantLogo.LOGOSIZE/2))+(InstantLogo.LOGOSIZE/2),Math.floor(seeds[15]*(InstantLogo.LOGOSIZE/2))+(InstantLogo.LOGOSIZE/2)
				);
		}

		if (isSquare==0){
			logo.graphics.drawRect(-(InstantLogo.LOGOSIZE/2), -(InstantLogo.LOGOSIZE/2), InstantLogo.LOGOSIZE, InstantLogo.LOGOSIZE);
			logo.graphics.beginFill((isBlack)?"#000":"#fff");
			var side = (InstantLogo.LOGOSIZE*.25)+seeds[16]*(InstantLogo.LOGOSIZE*.75-1);
	        logo.graphics.drawRect(-(side/2), -(side/2), side, side);
		} else if (isSquare==1){
			drawEquilateralTriangle(logo, InstantLogo.LOGOSIZE*1.1, 0, InstantLogo.LOGOSIZE*.175);
			logo.graphics.beginFill((isBlack)?"#000":"#fff");
			drawEquilateralTriangle(logo, (InstantLogo.LOGOSIZE*.25)+seeds[17]*((InstantLogo.LOGOSIZE*1.1)*.75-1), 0, InstantLogo.LOGOSIZE*.175);
		} else {
			if (!InstantLogo.IS_SOLID_COLOR) {
				logo.graphics.beginRadialGradientFill(
					[randomColorAsString([seeds[18],seeds[19],seeds[20],seeds[21],seeds[22],seeds[23]]),
					randomColorAsString([seeds[24],seeds[25],seeds[26],seeds[27],seeds[28],seeds[29]])],[0,1],0,0,0,0,0,InstantLogo.LOGOSIZE/2
					);
			};
			logo.graphics.drawCircle(0, 0, InstantLogo.LOGOSIZE/2);
			logo.graphics.beginFill((isBlack)?"#000":"#fff");
	        logo.graphics.drawCircle(0, 0, ((InstantLogo.LOGOSIZE/2)*.25)+seeds[30]*((InstantLogo.LOGOSIZE/2)*.75-1));
		}
	    logo.graphics.endFill();

		isSquare = Math.floor(seeds[31]*3);

		setMask(mask, [seeds[32],seeds[33],seeds[34]]);
		setMask(mask2, [seeds[35],seeds[36],seeds[37]]);
	
		myStage.update();
	};

	function setMask(obj, seedsArr)
	{
		obj.x = seedsArr[0]*InstantLogo.AREASIDE;
		obj.y = seedsArr[1]*InstantLogo.AREASIDE;
		obj.rotation = seedsArr[2]*360;

		obj.graphics.clear();
		obj.graphics.beginFill((isBlack)?"#000":"#fff");

		if (isSquare==0){
			obj.graphics.drawRect(-(InstantLogo.LOGOSIZE/2), -(InstantLogo.LOGOSIZE/2), InstantLogo.LOGOSIZE, InstantLogo.LOGOSIZE);
		} else if (isSquare==1){
			drawEquilateralTriangle(obj, InstantLogo.LOGOSIZE*1.1, 0, InstantLogo.LOGOSIZE*.175);
		} else {
			obj.graphics.drawCircle(0, 0, InstantLogo.LOGOSIZE/2);
		}
	    obj.graphics.endFill();
		instantLogo.addChild(obj);
	};

	function drawEquilateralTriangle(obj, side, x, y)
	{
		var height = side*Math.sqrt(0.75);
		var innerRadius = (1/6)*Math.sqrt(3)*side;
		obj.graphics.moveTo((side/2)+x, innerRadius+y);
		obj.graphics.lineTo(x, -(height-innerRadius)+y);
		obj.graphics.lineTo((-side/2)+x, innerRadius+y);
		obj.graphics.lineTo((side/2)+x, innerRadius+y);
	};

	function randomColorAsString(seedsArr) {
		if(!InstantLogo.IS_COLOR) return '#000';
		var seedsArr = seedsArr || [Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),Math.random()];
		return '#'+'0123456789abcdef'.split('').map(function(v,i,a){
		  return i>5 ? null : a[Math.floor(seedsArr[i]*16)] }).join('');
	}	
}


