//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
		else
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	}
	popEl.modal({
			onOpen: function (dialog) {
						dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('normal', function () {
							dialog.container.show('normal', function () {
								dialog.data.slideDown('normal'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('normal', function () {
						  dialog.container.hide('normal', function () {
							dialog.overlay.fadeOut('normal', function () {
							  $.modal.close(); // must call this to have SimpleModal
											   // re-insert the data correctly and
											   // clean up the dialog elements
							});
						  });
						});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}

//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 = 3000;

		//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);
	}
};


//init
$(document).ready(function(){

	$(".client_signupform_module input#ingangsdatum_per").datepicker({ minDate: '+10d'});

	$("#topmenu > ul > li").mouseover(
		function(){
			if(!$(this).hasClass('current_buttonID') && !$(this).parents('li:last').hasClass('current_buttonID')){
				$("#topmenu ul ul:visible").hide();
				i = $(this).children('ul:eq(0)');
				if(i.is(":hidden")){
					i.show();
				}
			}
		}
	);
	$("#topmenu ul li.current_buttonID").parents('li').addClass('current_buttonID');
	$('#topmenu ul ul li.current_buttonID').parents('li').addClass('current_buttonID').find('a:first').addClass('current_buttonID');
	$('#topmenu ul li.current_buttonID ul').show();
	
	
	//container home stuff
	if($('#container.home').length){
		//a = Math.ceil($('#container.home #middle .article').length / 2);
		//$('#container.home #middle .article:not(.no_margin)').height(($('#container.home #right').height() / a) - 33);
		//$('#container.home #middle .article.no_margin').height(($('#container.home #right').height() / a) - 3);
		$('#container.home #middle .article').css('height','190px');
		$('#container.home #middle .article.image').css('height','220px');
	}else{
		if($('#container #middle .article').length == 1 && $('#container #right .article').length == 3 && $('#container #middle .article').height() < $('#container #right').height() - 17){
			$('#container #middle .article').height($('#container #right').height() - 17);
		}
		$('#container #right .article:lt(3)').wrapAll('<div class="rightwrapper" />');
		if($('#container #left .article').height() != $('#container #right .rightwrapper').height() + 33){
			$('#container #left .article').height( $('#container #right .rightwrapper').height() + 33);
		}
	}
	//adjust some heights
	$('#container #right .article:eq(0)').css('height','100px');
	$('#container #right .article:eq(1)').css('height','100px');
	$('#container #right .article:eq(2)').css('height','130px');
	//quotes randomize
	if($(".acties_module").length){
		h = $('.acties_module').height(); //.parents('.article:eq(0)')
		//$('.acties_module').css('height',h);
		$(".acties_module .actie_details").each(function(){
			r = ((h - $(this).height())/2);
			$(this).css('margin-top',r)
		});
	}
	//start ticker when module is included
	$('.actie').css('display','none');
	InfiniteRotator.init('.actie');


	if($('.article.image').length){
		$('.article.image img').each(function(){
			s = $(this).attr('src');
			$(this).hide().parents('.article').css('background-image','url(' + s + ')');
		});
	}
	
	if($('span.acordion').length){
		$('span.acordion p').each(function(){
			$(this).wrapInner('<span class="acor_cont" />');
			$(this).find('b:first').addClass('expand').prependTo($(this));
		});
		$('span.acordion span.acor_cont').hide();
		$('span.acordion b.expand').click(function(){
			$('span.acordion span.acor_cont:visible').hide();
			$(this).next('span.acor_cont').show();
		});
	}

	//kill empty titles and articles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed').length){
			$(this).remove();
		}
	});
	//hide blank br
	$(".module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').css('display','none');
		}
	});
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
	
	//icons
	var files = new Array('ai','avi','bmp','cs','dll','doc','exe','fla','js','mdb','mp3','pdf','ppt','rdp','swf','swt','txt','vsd','xls','xml','zip');
	$(".article a[href]").each(function(){
		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);
	});
	
	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		i = 0;
		//change all onclick popups
		if($(this).attr('onclick')){
			i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");

			if(i){
				w = 640;
				h = 480;
				
				u = $(this).attr('href');
				e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e == 'png' || e == 'gif' || e == 'jpg'){
					var id = 'popwinlink_' + uid();
					$(this).attr('id',id);

					img[id] = new Image();
					img[id].onload = function(){
						img[id].onload = null;
						w = img[id].width;
						h = img[id].height;
	
						$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick').toString();

					h = parseInt(c.match(/height=([0-9]+)/)[1]) + 25;
					w = parseInt(c.match(/width=([0-9]+)/)[1]) + 25;
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
				}
			}
		//change all movie files
		}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
			//get filename...
			u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/nhec.nl/video/video.php?file=" + u + "',320,260);");		
		}
	});

});
