/*
	Patcher is a singleton that maintains an array of 'Patch' objects;
	javascript objects that hold an id to refer to dynamically generated
	div containing html. Once started, patcher will generate new patches
	and fade them in and out according to config settings below.
	
	The contents of the patches are defined by PTGetNewSources - 
	you *must* define this yourself before you load this script
	
	*pike / LabforCulture 200711 /
	
*/





var Patcher = {	
	lang			: "",
	word			: "",
	site			: "",
	sources 		: [],
	patches 		: [],
	maxpatches		: 50,
	fadeStep		: .05,
	patchXmin		: 5,
	patchXmax		: 70,
	patchYmin		: 0,
	patchYmax		: 75,
	defaultLink		: "http://victims.labforculture.org/site",
	fadePatches		: PTFadePatches,
	fadeInterval	: 500,
	fadeTimer		: null,
	addPatch		: PTAddPatch,
	addInterval		: 1500,
	addTimer		: null,
	delPatch		: PTDelPatch,
	applyCSS		: PTApplyCSS,
	applyFade		: PTApplyFade,
	cleanPatches	: PTCleanPatches,
	getNewSources	: PTGetNewSources,
	newResults		: PTNewResults,
	start			: PTStart,
	stop			: PTStop,
	debug			: PTDebug
}

function PTStart() {
	this.fadeTimer = setInterval("Patcher.fadePatches()",this.fadeInterval);
	this.addTimer = setInterval("Patcher.addPatch()",this.addInterval);
}

function PTStop() {
	clearTimeout(this.fadeTimer);
	clearTimeout(this.addTimer);
}

function PTFadePatches() {
	//this.debug("PTFadePatches");
	for (var pc=0; pc < this.patches.length; pc++) {
		var patch = this.patches[pc];
		if (patch) {
			if (patch.fading==1) patch.opacity+=this.fadeStep;
			if (patch.fading==-1) patch.opacity-=this.fadeStep;
			if (patch.fading==-1 && patch.opacity<=this.fadeStep) this.delPatch(pc);
			else if (patch.fading!=0) this.applyFade(patch);
			if (patch.fading==1 && patch.opacity>=1) patch.fading=0;
			
		}
		
	}
	this.cleanPatches();
}

function PTAddPatch() {
	if (this.sources.length) {
		var source = this.sources[this.sources.length-1];
		this.sources.length -= 1;
		this.patches[this.patches.length] = new Patch(source.content);
		if (this.patches.length>this.maxpatches) {
			for (var pc=0; pc < this.patches.length; pc++) {
				if (this.patches[pc].fading==0) {
					this.patches[pc].fading=-1;
					break;
				}
			}
		}
	} else {
		this.getNewSources();
	}
}



function PTNewResults() {
	if (Googler.searcher.results && Googler.searcher.results.length>0) {
		for (var i=0;i<Googler.searcher.results.length;i++) {
			this.sources[this.sources.length] = Googler.searcher.results[i];
		}
	} else {
		this.debug("no results");
	}
}

function PTDelPatch(idx) {
	this.debug("PTDelPatch "+idx);
	var pdiv = document.getElementById(this.patches[idx].id);
	//alert("del patch");
	if (pdiv) pdiv.parentNode.removeChild(pdiv);
	//alert("deld patch");
	this.patches[idx] = null;
}

function PTApplyCSS(patch) {
	var cssText = "position:absolute; ";
	cssText += "top:"+patch.top+"%; ";
	cssText += "left:"+patch.left+"%; ";
	var pdiv = document.getElementById(patch.id);
	if (pdiv) {
		pdiv.style.cssText = cssText;
		pdiv.setAttribute('style',cssText);
	} else {
		this.debug("No div for "+pc+":"+patch.id);
	}
}

function PTApplyFade(patch) {
	var pdiv = document.getElementById(patch.id);
	if (pdiv) {
		pdiv.className = "patch opacity"+Math.round(patch.opacity*20);
	} else {
		this.debug("PTApplyFade: No div for "+pc+":"+patch.id);
	}
}

function PTCleanPatches() {
	//this.debug("PTCleanPatches");
	var cPatches = new Array();
	for (var pc=0; pc < this.patches.length; pc++) {
		if (this.patches[pc]) cPatches[cPatches.length] = this.patches[pc];
	}
	this.patches = cPatches;
}

function PTDebug(msg) {
	//alert(msg);
	window.status= this.lang+" : "+this.site+" '"+this.word+"' - "+msg.toLowerCase();
	//var ddiv = document.getElementById("monitor");
	//ddiv.innerHTML = msg+"<br/>"+this.patches.length+" patches";
}

function Patch(content,link) {

	if (!link) link = Patcher.defaultLink;
	
	this.fading 	= 1; // 1,0,-1
	this.opacity 	= 0;
	this.left		= Math.round(Math.random()*(Patcher.patchXmax-Patcher.patchXmin))+Patcher.patchXmin;
	this.top		= Math.round(Math.random()*(Patcher.patchYmax-Patcher.patchYmin))+Patcher.patchYmin;
	this.id 		= Math.round(Math.random()*1000000);
	
	Patcher.debug("new Patch "+this.top+":"+this.left+":"+this.id);
	
	var newpatch = document.createElement('div');
	var csshidden = "position:absolute;  top:-1000px; left:-1000px";			
	var patchesdiv = document.getElementById("patches");
	newpatch.setAttribute("id",this.id);
	newpatch.className = "patch";
	newpatch.innerHTML=content;
	if (link) newpatch.onclick = new Function("document.location.href='"+link+"'");
	
	// temp hide this object 
	newpatch.style.cssText = csshidden;
	newpatch.setAttribute('style',csshidden);
	
	// append it to the container and apply styles
	patchesdiv.appendChild(newpatch);
	Patcher.applyFade(this);
	Patcher.applyCSS(this);
	
}
