// (C)2006-2009 hnldesign.nl
// This work is licensed under a Creative Commons License. http://creativecommons.org/licenses/by-nc-nd/3.0/

String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild);
    return result;
} 


/// Popup ///
function popupform(myform, windowname, width, height)
{
if (! window.focus)return true;
window.open('', windowname, "height="+height+",width="+width+",scrollbars=no");
myform.target=windowname;
return true;
}
/// End Popup ///

/// Zapper ///

function hnldesign(zap) {
			if (document.getElementById) {
				var abra = document.getElementById(zap).style;
				if (abra.display == "block") {
					abra.display = "none";
					} else {
					abra.display = "block"
					}
				return false
				} else {
				return true
				}
			}

/// End Zapper ///

/// totaal mogelijke combinaties ///
function totaal (){
decimal = 0;
anynum=eval(Math.floor(
(Var1total*Var2total)+
(Var3total*Var4total)+
(Var5total*Var6total)+
(Var7total*Var8total)+
(Var9total*Var10total)+
(Var11total*Var12total)+
(Var13total*Var14total)
));
	   divider =10;
	   switch(decimal){
			case 0:
				divider =1;
				break;
			case 1:
				divider =10;
				break;
			case 2:
				divider =100;
				break;
			default:  	 //for 3 decimal places
				divider =1000;
		}

	   workNum=Math.abs((Math.round(anynum*divider)/divider));

	   workStr=""+workNum;

	   if (workStr.indexOf(".")==-1){workStr+="."}

	   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0;
	   pStr=workStr.substr(workStr.indexOf("."));

	   while (pStr.length-1< decimal){pStr+="0"}

	   if(pStr =='.') pStr ='';

	   //--- Adds a comma in the thousands place.
	   if (dNum>=1000) {
		  dLen=dStr.length;
		  dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
	   }

	   //-- Adds a comma in the millions place.
	   if (dNum>=1000000) {
		  dLen=dStr.length;
		  dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
	   }

	   //-- Adds a comma in the millions place.
	   if (dNum>=10000000000) {
		  dLen=dStr.length;
		  dStr=parseInt(""+(dNum/10000000000))+","+dStr.substring(dLen-11,dLen)
	   }

	   //-- Adds a comma in the millions place.
	   if (dNum>=100000000000000) {
		  dLen=dStr.length;
		  dStr=parseInt(""+(dNum/100000000000000))+","+dStr.substring(dLen-15,dLen)
	   }

	   retval = dStr + pStr;
	   //-- Put numbers in parentheses if negative.
	   if (anynum<0) {retval="("+retval+")";}


	//You could include a dollar sign in the return value.
	  //retval =  "$"+retval

	  totaal = retval;
		document.getElementById('poss_combis').innerHTML=totaal;
}

/// GENERATOR CODE ///

// Instellingen

var x=0; // Komt terug in de ClicheVar() functie - Niet veranderen!
var Counters = new Array(0,0,0,0); // Lege counters maken. Even veel als het aantal delen in de langste combo.
var Startvals = new Array(00, 04, 00, 00); // Begin en eindpunt van de zin-loops over tijd. Boven is startpunt,
var Endvals   = new Array(08, 12, 00, 00); // onder is eindpunt. Nu dus steeds 8 loops met 4 ms overlap.
var Speed = 50; // 'cycle'snelheid. Hoe lager, hoe sneller het genereren (verschilt per pc)
var Flashcolor = '#ffc000'; // kleur die gebruikt wordt tijdens het cyclen

// Functions

// Neem een random cliche uit ID en schrijf m in de html - Deze functie wordt GELOOPT vanuit GenerateX() functies
function ClicheVar(id, interval, count){
	if (id==undefined){ // Geen id meegestuurd? Kappen dan.
		clearInterval (eval(interval));
		Counters[count]=0;
	}else if (Counters[count]>Startvals[count]){ // Counter heeft startwaarde bereikt of is groter dan: beginnen met de functie
		x=Math.floor(Math.random() * id.length);
		zin=id[x];
		document.getElementById('cliche').innerHTML=""+huidig+"<span id=\"zin"+count+"\" style=\"color:"+Flashcolor+";\">"+zin+"</span>";
		document.getElementById('holdtext').innerHTML=""+huidig2+""+zin+""; // Mail a friend text vullen
		Counters[count]++;
			if (Counters[count]>Endvals[count]){ // Eindwaarde bereikt? Kappen met de loop
				document.getElementById("zin"+count).style.color = "#000000";
				huidig=document.getElementById('cliche').innerHTML;
				huidig2=document.getElementById('holdtext').innerHTML;
		        tweetline+=zin;
				clearInterval (eval(interval));
				Counters[count]=0;

				// eind acties
				if((Counters[0]==0)&&(Counters[1]==0)&&(Counters[2]==0)&&(Counters[3]==0)){
					Knopje(); // knop weer enablen
					document.getElementById('cliche').innerHTML=""+huidig+"&rdquo;"; // Laatste punt en quote toevoegen
					document.getElementById('holdtext').innerHTML=""+huidig2; // Laatste punt toevoegen aan mail a friend text
					document.getElementById('tweetlink').setAttribute("href", "http://twitter.com/intent/tweet/?text='"+tweetline.unescapeHtml()+"' via www.opdekant.nl");
                    Tweet_it(''+tweetline.unescapeHtml()+'');
				}
			}
	}else{
		Counters[count]++; // Als de startwaarde nog niet bereikt is, tel dan 1 op bij de counter
	}
}

// Clicheknop uitschakelen of inschakelen om op hol slaan te voorkomen :)
function Knopje(){
	if	(document.getElementById('knop').disabled==true){
		document.getElementById('knop').disabled=false;
	}else{
	document.getElementById('knop').disabled=true;
	}
}

// Herhaal de random cliche functie van hierboven om de xx milliseconden (setinterval (ScrollVar,xx) x keer (counter==x), en schrijf hem daarna -definitief- op.
function Generate(combo){
		huidig='&ldquo;';
		huidig2='';
		tweetline='';
		Knopje();
		Int1 = setInterval ( 'ClicheVar('+combo[0]+', Int1, 0)', Speed );
		Int2 = setInterval ( 'ClicheVar('+combo[1]+', Int2, 1)', Speed );// Voeg Int's toe naarmat je de combo's groter kunnen worden
		//document.getElementById(debug_combo).innerHTML++; // DEBUG
		//document.getElementById('totaal').innerHTML++; // DEBUG
		//document.getElementById('random').innerHTML=rand; // DEBUG
		}

// Belangrijkste functie: GENEREREN DIE HAP! Roept Generate aan met een combo variable random tussen 0 en 6
function Cliche(){
	rand=Math.floor(Math.random()*combocount);
	eval("Generate(combo" + rand +")");
}

/// End GENERATOR ///
