var modifiedArray = new Array();
var counter;
var freeKeywordPlaces;

// random
Math.randomize = function() {var minValue=0,maxValue=0;if(arguments.length==1){maxValue=((isNaN(Number(arguments[0])))?(0):(parseInt(arguments[0])));}else if(arguments.length==2){minValue=((isNaN(Number(arguments[0])))?(0):(parseInt(arguments[0])));maxValue=((isNaN(Number(arguments[1])))?(0):(parseInt(arguments[1])));}if(maxValue<minValue){var tmpValue=minValue;minValue=maxValue;maxValue=tmpValue;}Math.randomize.startValue=(((Math.randomize.startValue*Math.randomize.a)+Math.randomize.b)%Math.randomize.c);return((minValue==maxValue)?(Math.randomize.startValue/Math.randomize.c):(minValue+Math.floor((maxValue-minValue+1)*Math.randomize.startValue/Math.randomize.c)));};Math.randomize.a=4096;Math.randomize.b=150889;Math.randomize.c=714025;Math.randomize.startValue=(((new Date()).getTime())%Math.randomize.c);
Array.prototype.remove = function(obj) {var arr=this,i=0;while(i<arr.length){if(arr[i]===obj){arr=arr.slice(0,i).concat(arr.slice(i+1,arr.length));--i;}++i;}for(i=0;i<arr.length;++i){this[i]=arr[i];}this.length=arr.length;};

// returns an array with mixed entries
function getSequence(tempArr,num) {
	var arr = [];
 	var teaserArr = tempArr ;
 	var arrLength = num;
 	var lotteryPot = [], i;
 	for (i=0; i<teaserArr.length; ++i) {
  		 lotteryPot[lotteryPot.length] = i;
 	}
	while (arr.length < arrLength) {
   	i = lotteryPot[Math.floor(Math.randomize()*lotteryPot.length)];
   	lotteryPot.remove(i);
   	arr[arr.length] = teaserArr[i];
 	}
 	return arr;
}

function startTagCloud() {
	if(typeof(keyword)=="undefined"){
				var scriptNode1 = document.createElement("script");
				scriptNode1.src =loadTagCloudArr;
				document.body.appendChild(scriptNode1);
				window.setTimeout("startTagCloud2()", 200);
	}else{
		startTagCloud2()
	}
}

function startTagCloud2() {
	// prod -> edit   replace for edit stage tracking urls
	if(location.href.indexOf('http://wcms40.bmwgroup.com/bmwgroup_edit/') != -1) {
		for(i=0; i < keyword.length; i++) {
			keyword[i][2] = keyword[i][2].replace("http://www.bmwgroup.com/", "http://wcms40.bmwgroup.com/bmwgroup_edit/");
		}
		for(i=0; i < yes_keyword.length; i++) {
			yes_keyword[i][2] = yes_keyword[i][2].replace("http://www.bmwgroup.com/", "http://wcms40.bmwgroup.com/bmwgroup_edit/");
		}
	}
	
	// prod -> qa   replace for qa stage tracking urls
	if(location.href.indexOf('http://wcms40.bmwgroup.com/bmwgroup_qa/') != -1) {
		for(i=0; i < keyword.length; i++) {
			keyword[i][2] = keyword[i][2].replace("http://www.bmwgroup.com/", "http://wcms40.bmwgroup.com/bmwgroup_qa/");
		}
		for(i=0; i < yes_keyword.length; i++) {
			yes_keyword[i][2] = yes_keyword[i][2].replace("http://www.bmwgroup.com/", "http://wcms40.bmwgroup.com/bmwgroup_qa/");
		}
	}
	
	// prod -> qa (liintra)   replace for qa stage tracking urls
	if(location.href.indexOf('http://liintra.muc:5949/') != -1) {
		for(i=0; i < keyword.length; i++) {
			keyword[i][2] = keyword[i][2].replace("http://www.bmwgroup.com/", "http://liintra.muc:5949/");
		}
		for(i=0; i < yes_keyword.length; i++) {
			yes_keyword[i][2] = yes_keyword[i][2].replace("http://www.bmwgroup.com/", "http://liintra.muc:5949/");
		}
	}
	
	// if there are entries for manipulation
	if((no_keyword != null && no_keyword.length > 0) || (yes_keyword != null && yes_keyword.length > 0)) {
		checkNoArray();
	} else {
		// otherwise skip the matching and assign cssIndex
		evaluateRanking();
	}
}

function checkNoArray() {
	var keywordTemp = new Array();
	var pushKeyword;
	
	for(i=0; i < keyword.length; i++) {
		pushKeyword = true;
		for(j=0; j < no_keyword.length; j++) {
			// if keyword is in no list, don't push it into temp array
			if(keyword[i][0] == no_keyword[j][0]) {
				pushKeyword = false;
			}
		}
		
		// push if the keyword is not in no list
		if(pushKeyword == true) {
			keywordTemp.push(keyword[i]);
		}
	}
	
	keyword = keywordTemp;
	
	if(yes_keyword.length > 0) {
		// if yes array has entries make a comparison
		checkYesArray();
	} else {
		// otherwise skip comparison and assign cssIndex and write tag cloud
		evaluateRanking();
	}
}

function checkYesArray() {
	var keywordTemp = new Array();
	var pushKeyword;
	
	for(i=0; i < keyword.length; i++) {
		pushKeyword = true;
		for(j=0; j < yes_keyword.length; j++) {
			// if there is a duplicate, remove it from the keyword list, because then it is also written in yes list, which has priority
			if(keyword[i][0] == yes_keyword[j][0]) {
				pushKeyword = false;
				// if there is no url in yes list, then take the url of keyword list
				// the prioritisation is taken from the yes list
				if(yes_keyword[j][2] == "") {
					// write in yes list the url of keyword list
					yes_keyword[j][2] = keyword[i][2];
				}
			}
		}
		
		// only push keywords that are not twice
		if(pushKeyword == true) {
			keywordTemp.push(keyword[i]);
		}
	}
	
	keyword = keywordTemp;
	
	// match keyword list and yes list (first width css index 4)
	mergeArrays(4);
}

// merge both arrays
function mergeArrays(cssIndex) {
	// if yes array has at least one entry
	if(yes_keyword.length > 0) {
		pushYesArray(cssIndex);
	}
}

// if a keyword with the ranking i in yes list exists, write it in the new array (modifiedArray)
function pushYesArray(cssIndex) {
	counter = 0;
	
	for(i=0; i < yes_keyword.length; i++) {
		freeKeywordPlaces = 0;
		
		if(yes_keyword[i][1] == cssIndex && counter < 3) {
			modifiedArray.push(yes_keyword[i]);
			counter++;
		}
		
		freeKeywordPlaces = counter;
	}

	// the free places are for tracking keywords
	pushTrackingKeywords(cssIndex, freeKeywordPlaces);
}

function pushTrackingKeywords(cssIndex, freeKeywordPlaces) {
	var goToRanking = false;
	
	for(i=0; i < (3 - freeKeywordPlaces); i++) {
		// write the ranking
		keyword[0][1] = cssIndex;
		
		modifiedArray.push(keyword[0]);
		keyword.shift();
		
		// if no further entry exists, break up
		if(keyword.length == 0) break;
	}
	
	var nextCssIndex = --cssIndex;
	
	// if cssIndex ist larger than 0 and there are still entries, continue merging
	if(nextCssIndex > 0 && keyword.length > 0) {
		mergeArrays(nextCssIndex)
	} else {
		goToRanking = true;
	}

	// number of keywords, that are used if keywords are removed (f.e. of overlength)
	var safetyKeywords = keyword.length;
	
	// push the rest of keyword list into new array. The are used as security, if a keyword is removed
	if(nextCssIndex == 0 && safetyKeywords > 0) {
		for(i=0; i < safetyKeywords; i++) {
			// write ranking of zero (identifier for security keyword)
			keyword[i][1] = 0;
			modifiedArray.push(keyword[i]);
		}
	}
	
	// if everything is evaluated, assign rankin
	if(goToRanking) {
		keyword = modifiedArray;
		evaluateRanking();
	}
}

// assigns ranking and checks keyword length
function evaluateRanking() {
 	var cssCounter = new Array();
	cssCounter[0] = 0;
	cssCounter[1] = 0;
	cssCounter[2] = 0;
	cssCounter[3] = 0;
	cssCounter[4] = 0;
  var ranking;
  var startValue = 0;
  var cssIndex = 4;
	var cssIndexTest = 0;
	
  for(i = 0; i < keyword.length; i++) {
		// decrease css counter; start with the highest
		while(cssCounter[cssIndex] > 2 && cssIndex > 0){
			cssIndex--;
		}
		var cssIndexTemp = cssIndex;
		
		// as long as the keyword ist too long, assign a smaller cssIndex
		// if the lowest css index doesn't fit, css index 0 is assigned
		while(testKeywordLength(cssIndexTemp,keyword[i][0]) > 168 && cssIndexTemp > 0) {
			cssIndexTemp--;
		}
		
		// assing ranking
	  keyword[i][1] = cssIndexTemp;
		cssCounter[cssIndexTemp] = cssCounter[cssIndexTemp] + 1;
  }
	
	if(keyword.length > 0){
		writeTagCloud(keyword);
	}
}

function testKeywordLength(css,key){ 
 	document.getElementById('cloudContentTest').innerHTML = '<span class="tagCloud_' +css+ '"><nobr>' + key + '</nobr></span>';
	return document.getElementById('cloudContentTest').offsetWidth;
}


var keywordOrder = new Array();

function writeTagCloud(keyword) {
  var htmlContent = "";
	var keywordLimit = 12; // maximum number of shown keywords
	var keywordLength = 0;
 	var counter = 0;
	
	for(i=0; i < keyword.length; i++) {
		if(keyword[i][1] > 0 && counter < 12){
			modifiedArray[counter] = keyword[i];
			counter++;
		}
	}
	
	keyword = modifiedArray;
	
	// defines the maxiumum number of keywords
	(keyword.length < keywordLimit) ? keywordLength = keyword.length : keywordLength = keywordLimit;
	
	// write array for random mechanism
	for(i=0; i < keywordLength; i++) {
		keywordOrder[i] = i;
	}
	
	var cnt = 70;
	var tempArr = new Array;
	var tempCodeArr = new Array;
	var tempHeightArr = new Array;
	var tempHtmlContent = "";
	
	for(j=0;j < cnt;j++){
		tempHtmlContent = "";
		tempArr.push(getSequence(keywordOrder, keywordLength));
		for(i=0; i < keywordLength; i++) {
			tempHtmlContent += '<span class="tagCloud_' + keyword[tempArr[j][i]][1] + '"><nobr><a href="' + keyword[tempArr[j][i]][2] + '">' + keyword[tempArr[j][i]][0] + '</a></nobr></span> ';
	 	}
		tempCodeArr.push(tempHtmlContent);
		document.getElementById('tcb').innerHTML = tempHtmlContent;
		document.getElementById('tagCloudContainer').style.display = "block";
 		document.getElementById('tagCloudContainer').style.visibility = "hidden";
		tempHeightArr.push(document.getElementById('tagCloudContainer').offsetHeight);
		document.getElementById('tcb').innerHTML = "";
	}
	
	for(j=0;j < cnt-1;j++){
		if(tempHeightArr[0] < tempHeightArr[1] ) {
			tempHeightArr.splice(1, 1);
			tempCodeArr.splice(1, 1);
		}else{
			tempHeightArr.splice(0, 1);
			tempCodeArr.splice(0, 1);
		}
	}
	
  htmlContent += tempCodeArr[0]

  document.getElementById('tagCloudContent').innerHTML = htmlContent;
	document.getElementById('tagCloudContainer').style.display = "block";
  document.getElementById('tagCloudContainer').style.visibility = "visible";
  document.getElementById('tcb').style.display = "none";
}

