// create namespace for helper functions
var CAST = {};
CAST.get_url_params = function() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
};
CAST.split_ul_2_columns = function(css_selector, columns, width, class_name) {
	/* TODO: jQuery plugin van maken + styling verbeteren */
	var itemindex = 0;
	var Jlistobj = null;
	var listitem = $(css_selector);

	var items_per_column = Math.ceil(listitem.children().length / columns)
	$(css_selector + ' li').each(function() {
		if (itemindex % items_per_column == 0) {
			Jlistobj = $("<ul style=\"float: left; list-style: none; width: "+width+"px;\" class=\""+class_name+"\"></ul>");
		}
		Jlistobj.append($(this));
		Jlistobj.insertBefore(listitem);
		itemindex++;
	});
	// and delete the old one
	listitem.remove();
};

$(document).ready(function() {
	// open external links in new window
	$("a[href*='http://']:not([href*='" + location.hostname + "']),[href*='https://']:not([href*='" + location.hostname + "'])")
		.addClass('external')
		.attr('target', '_blank')
		.attr('title', 'Open in nieuw venster');

	// open iframe links in fancybox
	$('a.iframe').fancybox({
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': 600,
		'speedOut': 200,
		'overlayShow': false,
		'width': 820,
		'height': 500
	});

	// multi-column lay-out for non-CSS3 capable browser
	if ($('html').hasClass('no-csscolumns')) {
		CAST.split_ul_2_columns('#brand-list', 3, 200);
		CAST.split_ul_2_columns('#productgroups-tree .overig ul.children', 3, 243, 'children');
		CAST.split_ul_2_columns('.event .suppliers ul', 3, 243, 'children');
	}

	// enlarge click area on 'contact-list' list items
	$('.contact-list').delegate('li', 'click', function() {
		window.location = $(this).find('h3 a').attr('href');
	});

	// find hyperlinks to mp4 videos and convert to VideoJS compatible HTML
	$('a[href$=".mp4"]').init_movie_player();

	// fix for attribute selectors for IE6
	var input_elements = $('.ie6 input[type="text"]');
	input_elements.addClass('input-text');

	// adjust height of vertical borders
	var main_height = $('#main').height();
	$('#wpr-content').css('height', main_height);
	if ($(document.body).hasClass('l-m-r')) {
		$('#asides-left').css('height', main_height);
	} else {
		$('#asides-right').css('height', main_height);
	}
	
	// prepare read-more links
	$('a[href$="#"]').each(function(index, value) {

		if($(this).html() == 'Lees meer')
		{
			// the a read-more link
			$(this).addClass("read-more");

			// the paragraph
			p = $(this).parent();

			// create a span element and insert it after the read-more link
			rmspan = $('<span/>').addClass('read-more').text('... ');
			rmspan.insertAfter($(this));

			//insert the read-more link in the span element
			rmspan.append($(this));

			//insert details span after span read-more
			details = $('<span/>').addClass('details');
			details.insertAfter($(rmspan));
			html = $(p).html();
			html = html.replace(/(<span class=(")?details(")?><\/span>)/i, '<span class=details>');
			$(p).replaceWith('<p>'+html+'</span></p>');
		}
	});

	// add read-more link (only for home)
	$('.home #content .content p').expander({
		slicePoint: 100,
		widow: 0,
		expandEffect: 'show',
		expandSpeed: 350,
		userCollapseText: 'Inklappen'
	});	
});
