/* STATUS: FINAL */

/*
*	GPR Javascript DIV sort 
*
*	Functions for sorting li layers, containing module names, on a page and saving there positions in a Cookie on every move
*
*	This script is used in the personalisation page in access manager 
*
*	USAGE 
*	
*	Create LI items containing a DIV wich contains a checkbox, the module typename and up and down arrows.
*	Thease LI items will ba sortable by the user and checkable to determain how and if the modules should 
*	be displayed on the site.
*
*		<li id="picture_gallery_teaser"><div> ... </div></li>
*	
*	Each DIV sould have two arrows with the CLASS arrowUp and arrowDown.  thease link should be children of the DIV
*
*		<a class="arrowUp" onclick="moveDivUp(this.parentNode.parentNode)"> Up arrow </a>
*		<a class="arrowDown" onclick="moveDivDown(this.parentNode.parentNode)"> Down arrow </a>
*	
*	On load read the sorted list from a Cookie and parse the container DIV in which all LI's that need sorting are available
*
*		readListFromCookie(document.getElementById('Containing_DIV'));
*
*	<ul id="moduleList">
*		<li id="picture_gallery_teaser">
*			<div class="clearfix" style="border: 1px solid #ff0000;">
*				<div style="border: 1px solid #00ff00; float: left;"><input name="chooseModuleGroup" id="picture_gallery_teaser_checkbox" type="checkbox"/>Picture gallery teaser</div>
*				<div style="border: 1px solid #ff00ff; float: right;" class="arrowContainer">
*					Sortieren<a name="arrowUp" class="arrowUp" onclick="moveDivUp(getElementById('picture_gallery_teaser'))"> Up </a>
*					<a name="arrowDown" class="arrowDown" onclick="moveDivDown(getElementById('picture_gallery_teaser'))"> Down </a>
*			</div></div></li>		
*
*
*
*	Structure
*
*		Containing_DIV 
*			LI name="type1"
*				moduleType_DIV
*					checkbox
*					module name
*					arrowUp - arrowDown
*
*			LI name="type2"
*				moduleType_DIV
*					checkbox
*					module name
*					arrowUp - arrowDown
*
*			LI name="type3"
*				moduleType_DIV
*					checkbox
*					module name
*					arrowUp - arrowDown
*
*/

function updateList(){
	removeTextNodesFromDiv(document.getElementById('moduleList'));
	showUpAndDownArrow(document.getElementById('moduleList'));
	hideFirstAndLastArrow(document.getElementById('moduleList'));
}

function moveDivUp(currentDivObject){
	divsContainer = currentDivObject.parentNode
	for (i=0; i <= divsContainer.childNodes.length-1; i++){
		if(divsContainer.childNodes[i].id == currentDivObject.id){
			// IE loses the check status when the div is moved in the tree
			var checkedBox1 = false;
			var checkedBox2 = false;

			var checkBoxObject1 = document.getElementById(divsContainer.childNodes[i].id + "_checkbox");
			var checkBoxObject2 = document.getElementById(divsContainer.childNodes[i-1].id + "_checkbox");

			// if the checkbox of this list item is checked
			if(checkBoxObject1.checked){
				checkedBox1 = true;
			}
			if(checkBoxObject2.checked){
				checkedBox2 = true;
			}
			
			// switch the LI items
			divsContainer.insertBefore(divsContainer.childNodes[i], divsContainer.childNodes[i-1]);
			
			checkBoxObject2.checked = checkedBox2;
			checkBoxObject1.checked = checkedBox1;
			
			updateList();
			break;
		}
	}
}

function moveDivDown(currentDivObject){
	divsContainer = currentDivObject.parentNode
	for (i=0; i < divsContainer.childNodes.length; i++){
		if(divsContainer.childNodes[i].id == currentDivObject.id){
			// IE loses the check status when the div is moved in the tree
			var checkedBox1 = false;
			var checkedBox2 = false;
			
			var checkBoxObject1 = document.getElementById(divsContainer.childNodes[i].id + "_checkbox");
			var checkBoxObject2 = document.getElementById(divsContainer.childNodes[i+1].id + "_checkbox");
			
			// if the checkbox of this list item is checked
			if(checkBoxObject1.checked){
				checkedBox1 = true;
			}
			if(checkBoxObject2.checked){
				checkedBox2 = true;
			}
			
			// switch the LI items
			divsContainer.insertBefore(divsContainer.childNodes[i+1], divsContainer.childNodes[i]);
			
			checkBoxObject2.checked = checkedBox2;
			checkBoxObject1.checked = checkedBox1;
			
			updateList();
			break;
		}
	}
}

function removeTextNodesFromDiv(divModulesContainer){
	var section_prio1_container = divModulesContainer;
	// remove text nodes between modules
	for (k=0; k < section_prio1_container.childNodes.length; k++){
		if(section_prio1_container.childNodes[k].nodeName == "#text"){
			section_prio1_container.removeChild(section_prio1_container.childNodes[k]);
		}
	}
}


function writeListToCookie(divModulesContainer){
	//
	// Depricated cookies for sorting the modules are now set using a paresonalisationpage in the access manager
	//
	alert("writing list to cookie");
	var section_prio1_container = divModulesContainer;
	var galleryList = new Array();
	var divCounter = 0;
	for (i=0; i < section_prio1_container.childNodes.length; i++){
		if(section_prio1_container.childNodes[i].nodeName != null  && section_prio1_container.childNodes[i].nodeName == "LI"){
			galleryList[divCounter] = new Array();
			galleryList[divCounter][0] = section_prio1_container.childNodes[i].id;
			
			//alert(document.getElementById(section_prio1_container.childNodes[i].id + "_checkbox").checked);
			galleryList[divCounter][1] = document.getElementById(section_prio1_container.childNodes[i].id + "_checkbox").checked;
			divCounter++;
		}
	}
	createCookie("sortedGalleryList",galleryList,1000);
}

function readListFromCookie(divModulesContainer){
	updateList();

	var section_prio1_container = divModulesContainer;
	if(readCookie("sortedGalleryList") != null){
		var galleryList = readCookie("sortedGalleryList").substring(1,readCookie("sortedGalleryList").length-1).split(',');

		// for every EVEN item in the galleryList get the node and append it to the container to sort the itmes
		for (i=0; i < galleryList.length; i+=2){
			for (j=0; j < section_prio1_container.childNodes.length; j++){
				if(galleryList[i] == section_prio1_container.childNodes[j].id){
					section_prio1_container.appendChild(section_prio1_container.childNodes[j]);
				}
			}
		}
		
		// for every ODD item in the galleryList get the checked state
		for (i=1; i < galleryList.length; i+=2){
			for (j=0; j < section_prio1_container.childNodes.length; j++){
				if(galleryList[i-1] == section_prio1_container.childNodes[j].id){
					if(galleryList[i] == "true"){
						document.getElementById(galleryList[i-1] + "_checkbox").checked = true;
					}else{
						document.getElementById(galleryList[i-1] + "_checkbox").checked = false;
					}
				}
			}
		}
	}
	
	for (i=0; i < section_prio1_container.childNodes.length; i++){
		if(section_prio1_container.childNodes[i] != null  && section_prio1_container.childNodes[i].nodeName == "DIV"){
			section_prio1_container.childNodes[i].style.display = 'inline';
		}
	}
	updateList();
}

function hideFirstAndLastArrow(divModulesContainer){
	removeTextNodesFromDiv(divModulesContainer);
	
	var listItems = divModulesContainer.childNodes
	
	var firstArrows = listItems[0].getElementsByTagName('A');
	var lastArrows = listItems[listItems.length-1].getElementsByTagName('A');
	
	for (i=0; i < firstArrows.length; i++){
		if(firstArrows[i].name == 'arrowUp'){
			firstArrows[i].style.display = 'none';
		}
	}
	
	for (i=0; i < lastArrows.length; i++){
		if(lastArrows[i].name == 'arrowDown'){
			lastArrows[i].style.display = 'none';
		}
	}
}

function showUpAndDownArrow(divModulesContainer){
	var upArrows = document.getElementsByName('arrowUp');
	var downArrows = document.getElementsByName('arrowDown');
	
	for(i=0; i < upArrows.length; i++){
		upArrows[i].style.display = 'inline';
	}
	
	for(i=0; i < downArrows.length; i++){
		downArrows[i].style.display = 'inline';
	}
}

/*
*	Sort modules on page function
*		
*	This function is used for displaying sorted modules on the webpage. It works by copying modules in the order stored in 
*	a cookie to a temporary div and when done copying the inner html from this Div over the modulebar content.
*
*	usage:
*		
*		sortModulesOnPage(divModulesContainer,debugModuleList)
*
*		divModulesContainer		- Should contain the name of the rightColumn
*		divTempModuleContainer	- The name of the hidden div in the page to copy the modules to		
*
*	debug version
*		
*		sortModulesOnPage(divModulesContainer,divTempModuleContainer,debugModuleList,divMainColumn)
*
*		divModulesContainer		- Should contain the name of the rightColumn
*		divTempModuleContainer	- The name of the hidden div in the page to copy the modules to	
*		debugModuleList			- Default false, when true it displays debug information for this function		
*		divMainColumn			- The name of the output div for debugging information
*
*	requires: the following divs
*			
*		rightColumn 			- This is the div with the modules
*		tempModuleDiv			- A hidden div to copy the modules to
*		mainColumn	(1)			- The main div of the page to copy the debug information to	
*		
*		(1) optional 
*/

// Default version
function sortModulesOnPage(divModulesContainer,divTempModuleContainer){
	sortModulesOnPage(divModulesContainer,divTempModuleContainer,false,null);
}

// Default version and Debug version
function sortModulesOnPage(divModulesContainer,divTempModuleContainer,debugModuleList,divMainColumn){
	var rightModuleContainer = document.getElementById(divModulesContainer);
	var tempModuleContainer = document.getElementById(divTempModuleContainer);
	var mainColumn = document.getElementById(divMainColumn);
	
	if(readCookie("sortedGalleryList") != null){
		var sortLog = readCookie("sortedGalleryList");
	
		var galleryList = readCookie("sortedGalleryList").substring(1,readCookie("sortedGalleryList").length-1).split(',');

		
		
		// remove comments and tekstnodes
		for (j=0; j < rightModuleContainer.childNodes.length; j++){
			if(rightModuleContainer.childNodes[j].nodeName != "DIV"){
				try{
					rightModuleContainer.removeChild(rightModuleContainer.childNodes[j]);
				}catch(err){}
			}
		}

		sortLog += "<h2>Hide and make visible</h2><br>"
		
		// for every ODD item in the galleryList get the checked state
		for (i=1; i < galleryList.length; i+=2){
			for (j=0; j < rightModuleContainer.childNodes.length; j++){
				if(rightModuleContainer.childNodes[j].nodeName == "DIV"){
					moduleType = rightModuleContainer.childNodes[j].id.split('_sep_');
					if(galleryList[i-1] == moduleType[1]){
						if(galleryList[i] == "true"){
							sortLog += rightModuleContainer.childNodes[j].id + " to visible<br>"
							rightModuleContainer.childNodes[j].style.display = "block";
						}else{
							sortLog += rightModuleContainer.childNodes[j].id + " to hidden<br>"
							rightModuleContainer.childNodes[j].style.display = "none";
						}
					}
				}
			}
		}
		sortLog += "<br><h2>Sort modules according to cookie</h2>"

		// for every EVEN item in the galleryList get the node and append it to the container to sort the itmes
		var bannerPositionCounter = 0;
		for (i=0; i < galleryList.length; i+=2){
			sortLog += "<br>Check " + galleryList[i] + "<br>";
			try{
				for (j=0; j < rightModuleContainer.childNodes.length; j++){
					if(rightModuleContainer.childNodes[j].nodeName == "DIV"){
						moduleType = rightModuleContainer.childNodes[j].id.split('_sep_');
						sortLog += "Check " + galleryList[i] + " == " + moduleType[1] + "<br>"
						if(galleryList[i] == moduleType[1]){
							sortLog += "Moving " + rightModuleContainer.childNodes[j].id + "<br>";
							
							// add the first banner 
							// Node needs to be cloned becouse otherwhise the sorting ot the elements will fail because the amount of objects won't be correct.
							
							// put the banner above the right site column for sorting in IE
							if(bannerPositionCounter == 0){
								// alert('printing banner');
								bannerPositionCounter++;
								if(document.getElementById('banner_1')){
									//tempModuleContainer.appendChild(document.getElementById('banner_1').cloneNode(true));
								}	
								
								/*
								if(typeof(adlink_randomnumber)=="undefined") var adlink_randomnumber=Math.floor(Math.random()*1000000000000);
								//var bannerTag = document.write('<'+'script type="text/javascript" src="http://ad.de.doubleclick.net/adj/Welt/home;tile=2;sz=120x600;ord=' + adlink_randomnumber + '?"><'+'/script>');

								var myElement = document.createElement('script'); 
								//myElement.innerHTML = 'http://ad.de.doubleclick.net/adj/Welt/home;tile=2;sz=120x600;ord=' + adlink_randomnumber + '?';

								
								myElement.src = 'http://ad.de.doubleclick.net/adj/Welt/home;tile=2;sz=120x600;ord=' + adlink_randomnumber + '?';
								
								
								tempModuleContainer.appendChild(myElement);
								
								
								myElement.innerHTML = 'http://ad.de.doubleclick.net/adj/Welt/home;tile=2;sz=120x600;ord=' + adlink_randomnumber + '?';
								tempModuleContainer.appendChild(myElement);
								*/
								
							}
							
							
							var	textNode = rightModuleContainer.childNodes[j].cloneNode(true);
							tempModuleContainer.appendChild(textNode);
							
							
							if(rightModuleContainer.childNodes[j]){
								if(rightModuleContainer.childNodes[j].style.display == "block"){
									bannerPositionCounter++;
								}
							}
							
							// add the second banner (Not needed becouse currently only the homepage will be personalised)
							// Node needs to be cloned becouse otherwhise the sorting ot the elements will fail because the amount of objects won't be correct.
							/*
							if(bannerPositionCounter == 2){
								if(document.getElementById('banner_2')){							
									tempModuleContainer.appendChild(document.getElementById('banner_2').cloneNode(true));
								}
							}
							*/
						}
					}
				}
			}catch(err){
				alert("error checking Div: " + err.description);
			}
		}
		
		// Print the remaining modules if they haven't been printed in between the modules, 
		// this might accure when there are only a few modules desked or selected in the personalisation.
		
		// add the second banner (Not needed becouse currently only the homepage will be personalised)
		/*
		if(bannerPositionCounter < 2){
			if(document.getElementById('banner_2')){	
				tempModuleContainer.appendChild(document.getElementById('banner_2'));
			}
		}
		*/

		if(divMainColumn != null && debugModuleList == true){
			mainColumn.innerHTML = "<h1>Javascript module sort debug</h1><br>" + sortLog;
		}
		rightModuleContainer.innerHTML = tempModuleContainer.innerHTML

		// Clear the data in the temp madule container 
		tempModuleContainer.innerHTML = "";		
	}
}


// This is where the flash objects are executed. This function needs to be called after the sorting routine, 
// because executing thease objects before sorting will result in  blank flash objects in IE.
function executePodcastObjectsInModules(podcastList){
	// More then one podcast article can be desked so this loop executes all the corresponing articles
	for(i=0;i < podcastList.length;i++){
		var mp3Player = eval('mp3Player_'+ podcastList[i]);
		mp3Player.addParam("allowScriptAccess", "sameDomain");
		mp3Player.addParam("menu", "false");
		mp3Player.addParam("quality", "high");
		mp3Player.addParam("align", "middle");
		mp3Player.addParam("wmode", "opaque");
		mp3Player.addVariable("beginPlaying", false);
		mp3Player.addVariable("mp3Path", eval('mp3PlayerMediaURL_'+ podcastList[i]));
		mp3Player.write('flashMp3Player_' + podcastList[i]);
	}
}

// This is where the flash objects are executed. This function needs to be called after the sorting routine, 
// because executing thease objects before sorting will result in  blank flash objects in IE.	
function executeVideoObjectInModules(so){
	// there can only be one video module for the time being. 
	so.addParam( "allowScriptAccess", "sameDomain" );
	so.addParam( "menu", "false" );
	so.addParam( "quality", "high" );
	so.addParam( "id", "mediaplayer" );
	so.addParam( "align", "middle" );
	so.addParam( "wmode", "opaque" );
	so.addVariable( "libraryPath", soLibraryPath );
	so.addVariable( "defaultMediaAssetPath", soDefaultMediaAssetPath );
	so.addVariable( "bwCheckUrl", soBwCheckUrl );
	so.addVariable( "eaeLoggerPath", soEaeLoggerPath );
	so.addVariable( "eaeLoggerPubId", soEaeLoggerPubId );
	so.addVariable( "eaeLoggerType", "video" );
	so.addVariable( "mediacliplist", soMediacliplist);
	so.addVariable( "ivwStatsFunction", "ivwLogVideo" );
	so.write( "teaservideo" );
}


