//make same height
var make_same_height = function(selector){
	var max_height = 0;
	$(selector).each(function(){
		if($(this).height() > max_height){
			max_height = $(this).height();
		}
	});
	$(selector).height(max_height);	
}

//scroll header items
var SGtmr = new Array();
var scrollHeaderItems = function(sel){
	x = $(sel + ':eq(0)').width() * -1;
	$(sel + ':eq(0)').animate({marginLeft:x + 'px'},'slow',function(){
		$(sel + ':eq(0)').css('margin-left','0px').appendTo($(sel).parent());
		$(sel + ':eq(0)').css('margin-left','0px');
		if(SGtmr[sel])
			clearTimeout(SGtmr[sel]);
		SGtmr[sel] = setTimeout('scrollHeaderItems("'+sel+'")',4000);
	});
}

//draw icons
var drawicons = function(){
	var files = new Array('ai','avi','flv','bmp','cs','dll','doc','exe','dmg','fla','gif','jpg','js','mdb','pdf','png','ppt','rdp','txt','vsd','xls','xlsx','xml','zip');
	$("a[href]").not("a[href$=php],a[href$=html],.no-icon,.noicon").each(function(){
		if($.trim($(this).text())){
			p = $(this).attr("href").split('.');
			e = p[p.length - 1].toLowerCase();
			for(i in files)
				if(files[i] === e)
					$(this).addClass('inline_icon icon_' + e);
		}
	});
}


//ticker countries
var InfiniteRotator =
{
	init: function(container)
	{
		//clearInterval;
		
		//container
		var container;
		//console.log(container);
		
		//initial fade-in time (in milliseconds)
		var initialFadeIn = 500;

		//interval between items (in milliseconds)
		var itemInterval = 4500;

		//cross-fade time (in milliseconds)
		var fadeTime = 500;

		//count number of items
		var numberOfItems = $(container).length;

		//set current item
		var currentItem = 0;

		//show first item
		$(container).eq(currentItem).fadeIn(initialFadeIn);

		//loop through the items
		var infiniteLoop = setInterval(function(){
			$(container).eq(currentItem).fadeOut(fadeTime);

			if(currentItem == numberOfItems -1){
				currentItem = 0;
			}else{
				currentItem++;
			}
			$(container).eq(currentItem).fadeIn(fadeTime);

		}, itemInterval);
	}
};


$(document).ready(function(){

	//start ticker when module is included
	if($('.quotes_module').length){
		$('.quotes_module').parents('.article').find('div.title').hide();
	}
	$('.quote').css('display','none');
	InfiniteRotator.init('.quote');


	//remove some titles
	$('#footer_links .article').each(function(){
		$('#footer_links .article .title').remove();
	});
	$('#right .articles .article.no_title').each(function(){
		$('#right .articles .article.no_title div.title').remove();
	});

  	//change the user panel
	$("ul.nav_user_panel li a").animate({"opacity": "0.7"}, {duration:0});
  	$("ul.nav_user_panel li a").mouseover(function(){
  		$(this).animate({"opacity": "1.0"}, {duration:150});
	});
	$("ul.nav_user_panel li a").mouseout(function(){
  		$(this).animate({"opacity": "0.7"}, {duration:150});
	});
	$("ul.nav_user_panel li a.buttonID_45").click(function(e){
		e.preventDefault();
		$('#external_login').css('left',$(this).offset().left);
		if($('#external_login').is(':hidden')){
			$('#external_login').slideDown('fast');
			$('#callme:visible').slideUp('fast');
		}else{
			$('#external_login').slideUp('fast');
		}
		return false;
	});
	$('#callme tr').find('td.spacer').remove('');
	$('#callme .tr_Telefoon td.value').attr('colspan',2).prepend($('#callme .tr_Telefoon .label span'));
	$('#callme .tr_Telefoon .label').remove();
	$('#callme form').submit(function(e){
		e.preventDefault();
		if(!$(this).find('input.input:eq(0)').val()){
			alert('Uw telefoonnummer is verplicht.');
		}else{
			$.post('/?module=formbuilder&formID=6&ajax=1',$(this).serialize(),function(data){
				if($(data).find('.formbuilder_done').length){
					$('#callme').html($(data).find('.formbuilder_done').html());
				}
			});
		}
		return false;
	});
	$("ul.nav_user_panel li a.buttonID_90").click(function(e){
		e.preventDefault();
		$('#callme').css('left',($(this).offset().left + $(this).width()) - $('#callme').width()).find('.submit').removeClass('medium').addClass('small');
		if($('#callme').is(':hidden')){
			$('#callme').slideDown('fast');
			$('#external_login:visible').slideUp('fast');
		}else{
			$('#callme').slideUp('fast');
		}
		return false;
	});
	$('body').click(function(e){
		if($(e.target).attr('id') != 'external_login' && !$(e.target).parents('#external_login').length && $('#external_login').is(':visible')){
			$('#external_login').slideUp('fast');
		}
		if($(e.target).attr('id') != 'callme' && !$(e.target).parents('#callme').length && $('#callme').is(':visible')){
			$('#callme').slideUp('fast');
		}
	});

	//change some menu/li items
	$('#menu ul li:first a:first').addClass('first');

	//submenu mouse over
	$("#menu > ul > li").mouseover(
		function(){
			if(!$(this).hasClass('current_buttonID') && !$(this).parents('li:last').hasClass('current_buttonID')){
				i = $(this).children('ul:eq(0)');
				if(i.is(":hidden")){
					i.show();
				}
				$('#menu > ul > li.current_buttonID').addClass('off');
				$('#menu ul li.current_buttonID ul').hide();
			}
		}
	);
	$("#menu > ul > li").mouseout(
		function(){
			if(!$(this).hasClass('current_buttonID') && !$(this).parents('li:last').hasClass('current_buttonID')){
				i = $(this).children('ul:eq(0)');
				if(i.is(":visible")){
					i.hide();
				}
				$('#menu > ul > li.current_buttonID').removeClass('off');
				$('#menu ul li.current_buttonID ul').show();
			}
		}
	);
	$('#menu ul ul li.current_buttonID').parents('li').addClass('current_buttonID').find('a:first').addClass('current_buttonID');
	$('#menu ul li.current_buttonID ul').show();


	$('input.submit').not('.button').addClass('button medium');
	
	$('#right form.form_bel_mij_formulier td.label').hide();
	$('#right form.form_bel_mij_formulier td.spacer').css('padding-right','30px');
	$('#right form.form_bel_mij_formulier td.spacer,#right form.form_bel_mij_formulier td.value').css({'padding-top': '10px', 'padding-bottom': '10px'});
	$('#right br.module_spacer').remove();

	//show layout when dom is loaded
	$('#container').css('display', 'block');

	//scroll
	scrollHeaderItems("#referenties #logos div.referenties_logos .logo");

	//move first image when 'HOME' is defined
	if($(".home").length){
		$('#left .articles .article:eq(0)').find('div.title').hide();		
		$('#left .articles .article.half > div.content').each(function(){
			o = $(this).find('img:eq(0),#mapsearch_1').not('.no-top-img');
			if(o.parent().is('a')){
				o = o.parent();
			}else{
				u = o.parent().find('.leesmeer').attr('href');
				if(u)
					o = o.wrap('<a href="' + u + '"></a>').parent();
			}
			o.prependTo($(this).parents('.article')).wrap('<div class="image clear"></div>');
			$(this).after($(this).find('a.leesmeer'));
		});
		//make same height!
		make_same_height("#left .articles .article.half > .content");
	}

	make_same_height("#left .articles .article.same_height > .content");

//	_typeface_js.renderDocument();

	drawicons();
});

