// colors for blocks.
var colors = [['680B00'],['58585A'],['692F00'],['6A3D00'],['6C4D00'],['6D5D00'],['646400'],['485C06'],['285511'],['004F18'],['004920'],['004231'],['003C3F'],['00334C'],['092851'],['1B1347'],['27033F'],['390030'],['4B001D'],['5E0004'],['F37854'],['F68C59'],['F89E5D'],['FBB261'],['FEC966'],['FFE56B'],['F2EF6F'],['CDDF74'],['A1D078'],['6FC27A'],['42B782'],['52AE99'],['5EA6AC'],['669DBE'],['6F8DC8'],['7D79B8'],['8765AC'],['A26697'],['C26A7D'],['E26E62'],['ED2624'],['EF4023'],['F05323'],['F26322'],['F47321'],['F58220'],['F68E1F'],['F99E1C'],['FCB016'],['FFC00F'],['FFD005'],['FFE200'],['FBF003'],['E5E515'],['C7DA2C'],['A9CF38'],['8AC53F'],['68BD45'],['39B54A'],['00AD4E'],['00A652'],['00A060'],['009A70'],['00957F'],['008F8E'],['008A99'],['0083A5'],['0C7DB0'],['0F76BC'],['3867B1'],['4B59A7'],['574A9E'],['603A96'],['682D90'],['7A2C82'],['902A73'],['A42964'],['B92754'],['CC2343'],['E21E2F'],['000000'],['6D6E71'],['A7A9AC'],['ED1C24'],['FFF200'],['00AEEF'],['2E3192'],['EF3E36'],['39B54A'],['0B9444'],['056839'],['C2B59B'],['9C8679'],['716658'],['5A4A42'],['0F75BC'],['283891'],['764C28'],['3B2313']];
var Sikkens = new Class({
	initialize:function(oOpt) {
		this.colors = oOpt.colors;
		this.setBlockColors();
		this.setLinks();
		this.setRandomImages();
		this.setImageColWidths();
		this.setFirstColumns();
		this.setLanguageLinks();
		$('overlay').destroy();
	}
	
	,setBlockColors: function() {
		var me = this;
		document.getElements('.box').each(function(oEl) {
			if(oEl.getElement("img")) return;
			var col = me.colors.getRandom();
			oEl.setStyle('background-color', '#' +col);
		});
	}
	/**
	 * each <a> with a class "read_more" is removed and all elements in 
	 * the "block" get a click event hander that goes to the url
	 */
	,setLinks: function(){
		document.getElements('.read_more').each(function(oEl) {
			var block = oEl.getParent('.block');
			oEl.setStyle('display','none');
			block.getChildren().each(function(oChild) {
				oChild.addClass("cursor");
				oChild.addEvent("click", function(oEv){
					document.location = oEl.get('href'); 
				});
			});
		});
	}
	
	,setLanguageLinks: function() {
		try {
			// set selected language link.
			$('menu').getElements('a').set("class","");
			var lang = document.location.pathname.split('/')[1];
			var el = (lang == "nl" || lang == "") ? $('lang_nl') : $('lang_en'); 
			el.addClass("selected");
			
			// set links
			var base_url = document.location.protocol +"//" +document.location.hostname;
			$('lang_nl').set('href',base_url+"/nl" +document.location.pathname.split("/" +lang +"/").join("/"));
			$('lang_en').set('href',base_url+"/en" +document.location.pathname.split("/" +lang +"/").join("/"));
		}
		catch(oErr) {
			alert(oErr);
		}
	}
	
	,setRandomImages:function() {
		
		var home = document.getElement('.box_home');
		if (home) {
			try {
				var files = ['a','b','c','d','e','f','g','h'];
				//home.getElement('img').addClass("resize_to_col");
				home.getElement("img").set('src', '/assets/images/textures/text_110_' +files.getRandom() +".png");
			}
			catch(oErr) {
				
			}
		}
		try {
			var files = ['a','b','c','e','f','g'];
			$$('div.width_b img.texture').each(function(oEl) {
				oEl.set('src', '/assets/images/textures/text_150_' +files.getRandom() +".png");
			});
		}
		catch(oErr) {
		}
		try {
			var files = ['a','b','c','d'];
			$$('div.width_c img.texture').each(function(oEl) {
				oEl.set('src', '/assets/images/textures/text_230_' +files.getRandom() +".png");
			});
		}
		catch(oErr) {
		}
	}
	
	,setImageColWidths: function() {
		var max_w = $('masthead').getSize().x;
		var me = this;
		$$('.image_w').getElements('img').each(function(oEl){
			var size = oEl.getSize();
			if (!size.x && size.hasOwnProperty('length') && size.length > 0)
				oEl.getParent('div.image_w').set('width', size[0].x);
			else 
				oEl.getParent('div.image_w').set('width', size.x);
		});
		this.setRowWidth(document.getElements('div.row'));			
	}
	
	,setRowWidth: function(aRows) {
		aRows.each(function(oRow) {
			var width = 0;
			var children = oRow.getChildren('div');
			children.each(function(oEl) {
				var size = oEl.getSize();
				width += size.x +oEl.getStyle('margin-left').toInt() + 10;
			});
			oRow.setStyle('width', width +"px");
		});
		this.resizeImagesToCol();
	}
	
	,resizeImagesToCol: function() {
		$$('.resize_to_col').each(function(oEl) {
			var col = oEl.getParent('div');
			oEl.setStyle('width', col.getWidth() +"px");
		});
	}
	
	,setFirstColumns: function() {
		$$('div.row').each(function(oEl) {
			try {
				oEl.getElement(':first-child').addClass("first_col");
			} catch (oErr) {}
		});
	}
});
		
document.addEvent('domready', function(oEv) {
	var sik = new Sikkens({
		"colors":colors
	});
});


/*
var colors = [
[104,11,0]
,[88,88,90]
,[105,47,0]
,[106,61,0]
,[108,77,0]
,[109,93,0]
,[100,100,0]
,[72,92,6]
,[40,85,17]
,[0,79,24]
,[0,73,32]
,[0,66,49]
,[0,60,63]
,[0,51,76]
,[9,40,81]
,[27,19,71]
,[39,3,63]
,[57,0,48]
,[75,0,29]
,[94,0,4]
,[243,120,84]
,[246,140,89]
,[248,158,93]
,[251,178,97]
,[254,201,102]
,[255,229,107]
,[242,239,111]
,[205,223,116]
,[161,208,120]
,[111,194,122]
,[66,183,130]
,[82,174,153]
,[94,166,172]
,[102,157,190]
,[111,141,200]
,[125,121,184]
,[135,101,172]
,[162,102,151]
,[194,106,125]
,[226,110,98]
,[237,38,36]
,[239,64,35]
,[240,83,35]
,[242,99,34]
,[244,115,33]
,[245,130,32]
,[246,142,31]
,[249,158,28]
,[252,176,22]
,[255,192,15]
,[255,208,5]
,[255,226,0]
,[251,240,3]
,[229,229,21]
,[199,218,44]
,[169,207,56]
,[138,197,63]
,[104,189,69]
,[57,181,74]
,[0,173,78]
,[0,166,82]
,[0,160,96]
,[0,154,112]
,[0,149,127]
,[0,143,142]
,[0,138,153]
,[0,131,165]
,[12,125,176]
,[15,118,188]
,[56,103,177]
,[75,89,167]
,[87,74,158]
,[96,58,150]
,[104,45,144]
,[122,44,130]
,[144,42,115]
,[164,41,100]
,[185,39,84]
,[204,35,67]
,[226,30,47]
,[0,0,0]
,[109,110,113]
,[167,169,172]
,[237,28,36]
,[255,242,0]
,[0,174,239]
,[46,49,146]
,[239,62,54]
,[57,181,74]
,[11,148,68]
,[5,104,57]
,[194,181,155]
,[156,134,121]
,[113,102,88]
,[90,74,66]
,[15,117,188]
,[40,56,145]
,[118,76,40]
,[59,35,19]
];

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

*/
