// JavaScript Document

currentHomeNewsGroup = 1;
function switchNewsItems(type){
	if(type == 'next'){
		document.getElementById('newsGroup'+currentHomeNewsGroup).style.display = 'none';
		
		currentHomeNewsGroup = currentHomeNewsGroup + 1;
		document.getElementById('newsGroup'+currentHomeNewsGroup).style.display = 'block';
	}else if(type == 'previous'){
		document.getElementById('newsGroup'+currentHomeNewsGroup).style.display = 'none';
		
		currentHomeNewsGroup = currentHomeNewsGroup - 1;
		document.getElementById('newsGroup'+currentHomeNewsGroup).style.display = 'block';
	}
	// make buttons for next and previous
	makeHomeNewsButtons();
	// expand first news item in group
	expandNews(firstNewsItems[currentHomeNewsGroup]['id'], firstNewsItems[currentHomeNewsGroup]['count'], firstNewsItems[currentHomeNewsGroup]['image'], firstNewsItems[currentHomeNewsGroup]['title'])
}

function makeHomeNewsButtons(){
	if(currentHomeNewsGroup == totalHomeNewsGroups){
		buttons = '<a href="javascript:switchNewsItems(\'previous\')"><img src="'+imgPath+'btn_up_active.gif" alt="" /></a><img src="'+imgPath+'btn_down_inactive.gif" alt="" />';
	}else if(currentHomeNewsGroup == 1){
		buttons = '<img src="'+imgPath+'btn_up_inactive.gif" alt="" /><a href="javascript:switchNewsItems(\'next\')"><img src="'+imgPath+'btn_down_active.gif" alt="" /></a>';
	}else{
		buttons = '<a href="javascript:switchNewsItems(\'previous\')"><img src="'+imgPath+'btn_up_active.gif" alt="" /></a><a href="javascript:switchNewsItems(\'next\')"><img src="'+imgPath+'btn_down_active.gif" alt="" /></a>';
	}
	
	document.getElementById('newsNavigation').innerHTML = buttons;
}

function switchImage(image, title, content){
	document.getElementById('projectHeaderTitle').innerHTML = '';
	document.getElementById('projectHeaderText').innerHTML = '';
	
	document.getElementById('projectHeaderTitle').innerHTML = title;
	//document.getElementById('projectHeaderTitle2').innerHTML = title;
	document.getElementById('projectHeaderText').innerHTML = content;
	document.getElementById('projectHeaderCurrentImage').src = image;

	sifrProjectHeaderTitle();
}

// Load google maps
function loadGoogleMaps() {
	//var mapPosition = new google.maps.LatLng(51.378916, 5.870524);
	var mapPosition = new google.maps.LatLng(51.334619, 5.979567);
	var mapOptions = {
		zoom: 11,
		center: mapPosition,
		disableDefaultUI: true,
		navigationControl: true, 
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var image = new google.maps.MarkerImage(imgPath + 'fancom.png',
	// This marker is 20 pixels wide by 32 pixels tall.
	new google.maps.Size(58, 49),
	// The origin for this image is 0,0.
	new google.maps.Point(0,0),
	// The anchor for this image is the base of the flagpole at 0,32.
	new google.maps.Point(49, 49)); 
	var googleMaps = new google.maps.Map(document.getElementById("contentHeaderImage"), mapOptions);
	var marker = new google.maps.Marker({
		position: mapPosition,
		map: googleMaps,
		icon: image,
		title: "Fancom"
	});
}

function loadDistributersMap() {
	var mapPosition = new google.maps.LatLng(51.378916, 5.870524);
	var mapOptions = {
		zoom: 4,
		center: mapPosition,
		disableDefaultUI: true,
		navigationControl: true, 
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var googleMaps = new google.maps.Map(document.getElementById("distributorsMap"), mapOptions);
	
	// get it from the ajax
	$.ajax({
		type: "GET",
	  	url: pagesPath+"functions.ajax.php",
	  	data: "action=getMarkers",
	  	success: function(msg){
	  		// split the returned message on | -> so it creates an array
	  		returnArray = msg.split('|');
	  		for (var i in returnArray){
	  			markerArray = returnArray[i].split('~');
				addMarker(googleMaps, markerArray[0], markerArray[1], markerArray[2], markerArray[3], markerArray[4], markerArray[5], markerArray[6], markerArray[7], markerArray[8], markerArray[9], markerArray[10]);
			}
	  	}
	});
}

function addMarker(map, lat, long, name, address, zipcode, city, phone, fax, website, email, type) {
	var contentString = '<div><b>'+name+'</b><br />'+address+'<br />'+zipcode+'<br />'+city+'<br /><br /><b>T</b> '+phone+'<br /><b>F</b> '+fax+'<br /><b>E</b> '+email+'<br /><b>W</b> <a href="http://'+website+'" target="_blank">'+website +'</a></div>';
	var infowindow = new google.maps.InfoWindow({
		content: contentString
	});
	
	if(type == 'centre'){
		imageName = 'marker_green.png';
	}else{
		imageName = 'marker_default.png';
	}
	
	var image = new google.maps.MarkerImage(imgPath + imageName,
			// This marker is 20 pixels wide by 32 pixels tall.
			new google.maps.Size(20, 34),
			// The origin for this image is 0,0.
			new google.maps.Point(0,0),
			// The anchor for this image is the base of the flagpole at 0,32.
			new google.maps.Point(20, 20)
		); 
	
	var markerLocation = new google.maps.LatLng(lat, long);
	var marker = new google.maps.Marker({
		position: markerLocation,
		map: map,
		icon: image,
		title: name
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker); 
	});
}

/* EVENT LISTENERS */
addEvent(window, 'load', init);

function init(){	
	try{
		fillSearch();
		
		var search = document.getElementById('searchword');
		addEvent(search, 'focus', emptySearch);
		addEvent(search, 'blur', fillSearch);
	}catch(e){ }
	
	try{
		fillContact();
		
		var contact = document.getElementById('street');
		addEvent(contact, 'focus', emptyContact);
		addEvent(contact, 'blur', fillContact);
	}catch(e){ }
}

function addEvent(obj, evType, fn){ 
	try{
		if (obj.addEventListener){ 
			obj.addEventListener(evType, fn, false); 
			return true; 
		} else if (obj.attachEvent){ 
			var r = obj.attachEvent("on"+evType, fn); 
			return r; 
		} else { 
			return false; 
		} 
	}catch(e){ }
}

function emptyValue(elm, tekst){
	if(document.getElementById(elm).value==tekst){
		document.getElementById(elm).value="";
	}
}
function returnValue(elm, tekst){
	if(document.getElementById(elm).value==""){
		document.getElementById(elm).value=tekst;
	}
}

function emptySearch(){
	emptyValue('searchword', searchFieldText);	
}
function fillSearch(){
	returnValue('searchword', searchFieldText);	
}

function emptyContact(){
	emptyValue('street', contactFieldText);	
}
function fillContact(){
	returnValue('street', contactFieldText);	
}

function popUp(link , width , height){
	window.open(link, 'venster', 'width=' + width + ',height=' + height + ',scrollbars=yes,toolbar=no,location=no,status=no,resize=no');
}

function expandAll(startDiv) {
	var divObj = document.getElementById(startDiv);
	if (divObj == "object") {
		for (var i=0;i<divObj.childNodes.length;i++) {
			if (divObj.childNodes[i].nodeName == "DIV" && divObj.childNodes[i].id.substr(0,6) == "folder") {
				toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
				expandAll(divObj.childNodes[i].id);
			}
		}
	}
}

function closeAll(startDiv) {
	expandAll(startDiv);
	var divObj = document.getElementById(startDiv);
	for (var i=0;i<divObj.childNodes.length;i++) {
		if (divObj.childNodes[i].nodeName == "DIV" && divObj.childNodes[i].id.substr(0,6) == "folder") {
			toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
			expandAll(divObj.childNodes[i].id);
		}
	}
}

function expandItem(theID, startDiv) {
	var divObj = document.getElementById(startDiv);
	var found = false;
	for (var i=0;i<divObj.childNodes.length;i++) {
		if (divObj.childNodes[i].nodeName == "DIV" && divObj.childNodes[i].id.substr(0,6) == "folder") {
			found = expandItem(theID, divObj.childNodes[i].id);
			if (divObj.childNodes[i].id == "folder" + theID) {
				// current element found
				toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
				return true;
			}
			if (found) {
				toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
				return true;
			}
		}
	}
	return found;
}

function toggleFolder(id, imageNode) {
	var folder = document.getElementById(id);
	var l = imageNode.src.length;
	if (imageNode.src.substring(l-21,l)=="tree_folderclosed.png" || imageNode.src.substring(l-19,l)=="tree_folderopen.png") {
		imageNode = imageNode.previousSibling;
		l = imageNode.src.length;
	}
	if (folder == null) {
	} 
	else if (folder.style.display == "block")  {
		if (imageNode != null) {
			imageNode.nextSibling.src = imgPath+"sitemap/tree_folderclosed.png";
			if (imageNode.src.substring(l-14,l) == "tree_mnode.png") {
				imageNode.src = imgPath+"sitemap/tree_pnode.png";
			}
			else if (imageNode.src.substring(l-18,l) == "tree_mlastnode.png") {
				imageNode.src = imgPath+"sitemap/tree_plastnode.png";
			}
		}
		folder.style.display = "none";
	} 
	else {
		if (imageNode != null) {
			imageNode.nextSibling.src = imgPath+"sitemap/tree_folderopen.png";
			if (imageNode.src.substring(l-14,l) == "tree_pnode.png") {
				imageNode.src = imgPath+"sitemap/tree_mnode.png";
			}
			else if (imageNode.src.substring(l-18,l) == "tree_plastnode.png") {
				imageNode.src = imgPath+"sitemap/tree_mlastnode.png";
			}
		}
		folder.style.display = "block";
	}
	//updateRowColor();
}


function getObjectsByClassname(clsName) {
	var retVal = new Array();
	var elements = document.getElementsByTagName("*");
	
	for(var i = 0;i < elements.length;i++) {
		if(elements[i].className.indexOf(" ") >= 0) {
			classes = elements[i].className.split(" ");
			for(var j = 0;j < classes.length;j++){
				if(classes[j] == clsName) {
					retVal.push(elements[i]);
				}
			}
		}
		else if(elements[i].className == clsName) {
			retVal.push(elements[i]);
		}
	}
	return retVal;
}


function RemoveClassName(objElement, strClass) {
	// if there is a class
	if ( objElement.className ) {
	
	   // the classes are just a space separated list, so first get the list
	   var arrList = objElement.className.split(' ');
	
	   // get uppercase class for comparison purposes
	   var strClassUpper = strClass.toUpperCase();
	
	   // find all instances and remove them
	   for ( var i = 0; i < arrList.length; i++ )
	      {
	
	      // if class found
	      if ( arrList[i].toUpperCase() == strClassUpper )
	         {
	
	         // remove array item
	         arrList.splice(i, 1);
	
	         // decrement loop counter as we have adjusted the array's contents
	         i--;
	
	         }
	
	      }
	
	   // assign modified class name attribute
	   objElement.className = arrList.join(' ');
	
	   }
	// if there was no class
	// there is nothing to remove
}

function AddClassName(objElement, strClass, blnMayAlreadyExist) {
	
	// if there is a class
	if ( objElement.className )
	   {
	
	   // the classes are just a space separated list, so first get the list
	   var arrList = objElement.className.split(' ');
	
	   // if the new class name may already exist in list
	   if ( blnMayAlreadyExist )
	      {
	
	      // get uppercase class for comparison purposes
	      var strClassUpper = strClass.toUpperCase();
	
	      // find all instances and remove them
	      for ( var i = 0; i < arrList.length; i++ )
	         {
	
	         // if class found
	         if ( arrList[i].toUpperCase() == strClassUpper )
	            {
	
	            // remove array item
	            arrList.splice(i, 1);
	
	            // decrement loop counter as we have adjusted the array's contents
	            i--;
	
	            }
	
	         }
	
	      }
	
	   // add the new class to end of list
	   arrList[arrList.length] = strClass;
	
	   // add the new class to beginning of list
	   //arrList.splice(0, 0, strClass);
	   
	   // assign modified class name attribute
	   objElement.className = arrList.join(' ');
	
	   }
	// if there was no class
	else
	   {
	
	   // assign modified class name attribute      
	   objElement.className = strClass;
	
	   }
}

function expandNews(id, totalItems, image, title) {
	var foldLi = document.getElementById('newsItem_'+id);
	var imageId = document.getElementById('newsImage');
	
	for (i = 0; i < totalItems; i++) {
		collapseNews(i);
	}
	
	if (foldLi != null) {
		var currentClass = foldLi.className ;
		foldLi.className = 'expanded';
	}
	if (imageId != null) {
		imageId.src = image;
		imageId.alt = title;
	}
}

function collapseNews(id) {
	var foldLi = document.getElementById('newsItem_'+id);
	
	if(foldLi != null) {
		var currentClass = foldLi.className ;
		foldLi.className = 'collapsed';
	}
}

function gotoMaps(adres, langcode){
	// adres = adres.replace(" ", "+");
	if(langcode == 'uk'){
		window.open("http://maps.google.com/maps?f=d&hl=nl&saddr="+adres+"&daddr=Industrieterrein+34,+Panningen,+Nederland");
	}else{
		window.open("http://maps.google.nl/maps?f=d&hl=nl&saddr="+adres+"&daddr=Industrieterrein+34,+Panningen,+Nederland");
	}
	
}