// (C)2006-2009 hnldesign.nl
// This work is licensed under a Creative Commons License. http://creativecommons.org/licenses/by-nc-nd/3.0/




/// Popup ///
function popupform(myform, windowname)
{
if (! window.focus)return true;
window.open('', windowname, "height=480,width=440,scrollbars=no");
myform.target=windowname;
return true;
}
/// End Popup ///




/// Uppercase function ///

function sCase(){
  val=document.getElementById('cliche').innerHTML;
  result=new Array();
  result2='';
  count=0;
  endSentence=new Array();
  for (var i=1;i<val.length;i++){
  if(val.charAt(i)=='.'||val.charAt(i)=='!'||val.charAt(i)=='?'){
    endSentence[count]=val.charAt(i);
    count++
  }
}

var val2=val.split(/[.|?|!]/);

if(val2[val2.length-1]=='')val2.length=val2.length-1;

for (var j=0;j<val2.length;j++){
  val3=val2[j];

if(val3.substring(0,1)!=' ')val2[j]=' '+val2[j];

var temp=val2[j].split(' ');
var incr=0;

if(temp[0]==''){
  incr=1;
}

temp2=temp[incr].substring(0,1);
temp3=temp[incr].substring(1,temp[incr].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[incr]=temp2+temp3;

for (var i=incr+1;i<temp.length;i++){
  temp2=temp[i].substring(0,1);
  temp2=temp2.toLowerCase();
  temp3=temp[i].substring(1,temp[i].length);
  temp3=temp3.toLowerCase();
  temp[i]=temp2+temp3;
}

if(endSentence[j]==undefined)endSentence[j]='';
  result2+=temp.join(' ')+endSentence[j];
}

if(result2.substring(0,1)==' ')result2=result2.substring(1,result2.length);
  document.getElementById('cliche').innerHTML=result2;
  document.getElementById('holdtext').innerHTML=""+result2+"";
}

/// End Uppercase function ///




/// 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, 02, 00, 00); // Begin en eindpunt van de zin-loops over tijd. Boven is startpunt, 
var Endvals   = new Array(03, 05, 00, 00); // onder is eindpunt. Nu dus steeds 8 loops met 1 ms tussenpoze.
var Speed = 70; // 'scroll'snelheid. Hoe lager, hoe sneller het genereren (verschilt per pc)
var Flashcolor = '#ffc000'; // kleur die gebruikt wordt tijdens het scrollen

// 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;
				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+"&rdquo;"; // Laatste punt en quote toevoegen aan mail a friend text
					Tweet_it(''+huidig2+'');
				}
			}
	}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, debug_combo){
		huidig='&ldquo;';
		huidig2='&ldquo;';
		Knopje();
		Int1 = setInterval ( 'ClicheVar('+combo[0]+', Int1, 0)', Speed );
		Int2 = setInterval ( 'ClicheVar('+combo[1]+', Int2, 1)', Speed );
		Int3 = setInterval ( 'ClicheVar('+combo[2]+', Int3, 2)', Speed );
		Int4 = setInterval ( 'ClicheVar('+combo[3]+', Int4, 3)', 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);
	debug_combo="combo"+ rand; // DEBUG
	eval("Generate(combo" + rand + ", debug_combo)");
}

/// End GENERATOR ///
