/**
 * file default.js
 * contains javascript for Bouwbedrijf Barten
 * @require MooTools version 1.2.1
 */


window.addEvents({
	'domready': function() {
		initSitemapFix();
		initExternalLinks();
	},
	'load': function() {
		initEqualHeightPartners();
		initFixEqualHeightSitemap();
		initProjectGallery();
	}
});


/**
 * initProjectGallery
 *
 * shows visual in large container
 *
 * @author phison Do <phison.do{AT}efocus.nl>
 * @return void
 */
function initProjectGallery() {
	
	var elProjectGallery = document.getElement('div.projectgallerij');
	if(!elProjectGallery) return false;
	
	var drop = document.getElement('.container');
	var arrThumbLists = document.getElement('ul.item_container').getElements('li');
	var arrThumbImages = document.getElement('ul.item_container').getElements('li img');	
	
	arrThumbImages.each(function(elThumbImage){
		elThumbImage.addEvent('click', function() {
			
			drop.removeEvents();
			drop.empty();
			
			var largeImg = new Element('img', { 'src' : elThumbImage.get('rel'), 'alt' : '' });
			largeImg.inject(drop);
			
		});
	});
	
	if(!arrThumbLists[4]) return false;	arrThumbLists[4].setStyle('margin', '0');
	if(!arrThumbLists[9]) return false; arrThumbLists[9].setStyle('margin', '0');	
		
}


/**
 * initSitemapFix
 *
 * removes marginRight of every third element
 * @author phison Do <phison.do{AT}efocus.nl>
 * @return void
 */
function initSitemapFix() {
	if(!document.getElement('div.csc-sitemap')) return false;
	
	var arrSitemapListItems = $$('div.csc-sitemap>ul>li');	
	var arrSitemapListItemsLength = arrSitemapListItems.length;	
	
	var result = [];
	var count = 0;	
		
	for (var i = 2; i < arrSitemapListItemsLength; i = i + 3) {
		result[count] = arrSitemapListItems[i];	
		count++;
		arrSitemapListItems[i].setStyle('marginRight', '0');
	}
}



/**
 * initFixEqualHeightSitemap
 * Calculates equal heights to sitemap
 * 
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @return void
 */
function initFixEqualHeightSitemap() {
	if (!document.getElement('.csc-sitemap')) return;
	
	var arrSitemapLists = $$('div.csc-sitemap>ul>li');
	
	var intSitemapListHeightMax = 0;
	
	arrSitemapLists.each(function(elSitemapList){
		if (elSitemapList.getHeight() > intSitemapListHeightMax) {
			intSitemapListHeightMax = elSitemapList.getHeight();
		}
	});
	arrSitemapLists.each(function(elSitemapList){
		elSitemapList.setStyle('height', intSitemapListHeightMax);
	});
}


/**
 * setEqualHeightPartners
 * sets for each row the height equal
 * 
 * @author	Ralph Meeuws <ralph.meeuws{AT}efocus.nl>, 
 * 			Lee Boonstra <lee.boonstra{AT}efocus.nl>
 * @return void
 */
function initEqualHeightPartners() {
	if (!document.getElement('.partners ul')) return;
	
	var arrPartnerLists = document.getElement('.partners ul').getElements('li');
	var arrBiggestHeightNum = [];
	var arrHeightToSet = [];
	var intRowCounter = 0;

	//loop through all the rows and save the biggest height number into an array
	arrPartnerLists.each(function(elPartnerList){
		intRowCounter++;
		arrBiggestHeightNum.push(elPartnerList.getHeight());
		if (intRowCounter == 3) {
			intRowCounter = 0;
			getHeighestNum(arrBiggestHeightNum);
			arrHeightToSet.push(arrBiggestHeightNum[0]);
			arrBiggestHeightNum = [];	
		}
	});

	setEqualHeightListElements(arrPartnerLists, arrHeightToSet);
}


/**
 * setEqualHeightListElements 
 * Set equal heights of list elements based of height array
 * @author lee.boonstra[AT]efocus.nl
 * @param arrList - array with list elements
 * @param arrHeights - array with heights,
 * - incase fixed height, make sure arrHeights[0] is set
 * @return void
 */
function setEqualHeightListElements(arrList, arrHeights){
	var intRowCounter = 0;
	var counter = 0;
	arrList.each(function(elListItem){
	counter++;
	elListItem.setStyle('height', arrHeights[intRowCounter]);
	elListItem.setStyle('line-height', arrHeights[intRowCounter]);
		if (counter == 3) {
			intRowCounter++;
			counter = 0;
			elListItem.setStyle('marginRight', '0');
		}
	});
}


/**
 * getHeighestNum
 * sort number (not lexicographically), reverse order
 * @author - lee.boonstra[AT]efocus.nl
 * @param arr - numerical array
 * @return arr - numerical array
 */
function getHeighestNum(arr){
	if (arr.length > 2) {
		arr.sort(function(a, b){
			return b - a
		});
		return arr;
	}
}

/**
 * initExternalLinks
 * Used in order for certain links to open in a new window without 
 * the direct target attribute (so it validates properly).
 * 
 * @author lee.boonstra[AT]efocus.nl
 */
function initExternalLinks() {
	var elAnchor;
	if (!document.getElementsByTagName) return; 
	var arrAnchors = document.getElementsByTagName("a"); 
	for (var i=0; i<arrAnchors.length; i++) { 
		elAnchor = arrAnchors[i]; 
		if (elAnchor.getAttribute("rel") == "external" && elAnchor.getAttribute("href")) {
			elAnchor.target = "_blank";
		}
	}
}
