/* standaard js file */

defaultStatus = "";
var gv_default_js_loaded = true;


/*------------------------------------------------------------------------------
	Methode om CSS background-image cache te forceren voor IE6
------------------------------------------------------------------------------*/

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

//window.onload = init;
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}


function onLoad(formTokens)
{

	if (typeof $.validator == 'object')
	{
// Obsolite: since 03-02-2010 using jquery.validation.en.js
//		$.validator.messages.error = {
//			required: 	'Please fill out the required field',
//			requiredby: 'Please fill out the required field',
//			postcode: 	'Invalid zipcode',
//			email: 		'Invalid e-mail address',
//			emailunique:'E-mail address already registered',
//			select: 	'Please select an option',
//			date: 		'Invalid date',
//			mindate:	'Invalid date',
//			maxdate:	'Invalid date',
//			phone:		'Invalid phone number',
//			minlength: 	'Value is to short',
//			maxlength:	'Value is to long',
//			minvalue:	'Expected bigger value',
//			maxvalue:	'Expected smaller value',
//			pattern:	'Invalid value',
//			password:	'Passwords are not equal',
//			passwordft:	'Password is invalid',
//			user:		'Username is invalid',
//			website:	'Invalid website address. The website address has to start with http://',
//			ajax:		'Some went wrong while validating this field. Please try again. If you keep receiving this error, please contact the webmaster.'
//		};

		//load tokens if specified
	    if(typeof formTokens == 'object') {
	    	$.validator.tokenEnable(formTokens);
	    }
	}
}

/*------------------------------------------------------------------------------
	Popup scripts
------------------------------------------------------------------------------*/

/* pop up algemeen */
function popUp(location,popupname,width,height) {
	window.open(location, popupname,'width='+width+',height='+height+',status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,top='+((screen.height-height)/2)+',left='+((screen.width-width)/2)+'');
}

/* pop up voor images */
function popUpImage(location,popupname,width,height) {
	window.open(location, popupname, 'width='+width+',height='+height+',status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,top=0,left=0');
}

/* strip spaties en bepaal lengte */
function str_length(value) {
	str = new String(value);
	str = str.replace(/^[\s]+/g,"");
	return  str.length;
}

/*------------------------------------------------------------------------------
	Dynamisch dropdownmenu
------------------------------------------------------------------------------*/

function totalElementOffset(obj) {

	var yOffset = (obj.offsetTop ? obj.offsetTop : 0);
	var xOffset = (obj.offsetLeft ? obj.offsetLeft : 0);
	var elParent = obj.offsetParent;

	while (elParent) {
		yOffset += (elParent.offsetTop ? elParent.offsetTop : 0);
		xOffset += (elParent.offsetLeft ? elParent.offsetLeft : 0);
		elParent = elParent.offsetParent;
	}

	this.y = yOffset;
	this.x = xOffset;

}
/*

	Shows a layer at a specific position.
	id is the layer id, parent is the object to align
	the layer to and the pos and align arguments specify
	the layer's position relative to the parent object;
	The vAlign/hAlign side of the layer will be placed at the
	vPos/hPos coordinate of the parent object:

	          ----------------  <- [vPos 0]
			 |     PARENT     |
	          ----------------  <- [vPos 1]
             ^                ^
	      [hPos 0]         [hPos 1]


	          --------------  <- [vAlign 0]
			 |     LAYER     |
	          --------------  <- [vAlign 1]
             ^               ^
	     [hAlign 0]      [hAlign 1]

*/

function showLayer(id, parent, hPos, vPos, hAlign, vAlign, hOffset, vOffset) {

	var layer = document.getElementById(id);
	var offset = new totalElementOffset(parent);
	var spacer = document.getElementById('navMainSpacer');
	var left = offset.x;
	var top = offset.y;

	if (!layer) return;

	if(navigator.appVersion.indexOf('MSIE ') != -1)
	{
		hOffset++;
		vOffset++;
	}

	if (hPos == 1) left += parent.offsetWidth;
	if (vPos == 1) top += parent.offsetHeight;
	if (hAlign == 1) left -= layer.style.width.replace(/px/,'');
	if (vAlign == 1) top -= layer.style.height.replace(/px/,'');
	if (hOffset) left += hOffset;
	if (vOffset) top += vOffset;

	layer.style.display = '';
	layer.style.left = left+'px';
	layer.style.top = top+'px';
}

/*
 * Keep track of opened menu items and default images
 *
 */

var selectedSubMenus = new Array();
var imageState = new Array();

/*
 * Hide open submenu
 */
function hideSub(id) {

	showState(id);

	if (document.getElementById('sub_mnu_'+id)) {

		document.getElementById('sub_mnu_'+id).style.display = 'none';

	}
	var parent = document.getElementById('mnu_'+id);
	var link   = document.getElementById('mnu_itm'+id);

	if (link.className!='navMenuAct')
	{
		link.className = 'navMenuOut';
		parent.className = 'navMenuOut2';

	}


}


/*
 * Show sub menu at disired position
 */
function showMenuLayer(id, level, hPos, vPos, hAlign, vAlign, hOffset, vOffset) {

	var child  = 'sub_mnu_'+id;
	var parent = document.getElementById('mnu_'+id);
	var link   = document.getElementById('mnu_itm'+id);

	if (link.className!='navMenuAct')
	{
		link.className = 'navMenuOpen';
		if (level==2) {link.className = 'navMenuParent'}
		parent.className = 'navMenuOpen2';
	}

	showState(id);
	updateMenu(id,level);
	selectedSubMenus[level]= id;
	showLayer(child, link, hPos, vPos, hAlign, vAlign, hOffset, vOffset);
}

/*
 * No submenu and update current menu state
 */
function updateMenu(id,level)
{

	for(i=1;i<=selectedSubMenus.length;i++)
	{
		if (i >= level) {
			if (selectedSubMenus[i])
			{

				if(!document.getElementById('mnu_itm'+id).src && selectedSubMenus[i]==id && (document.getElementById('mnu_itm'+id).className=='navMenuOpen' || document.getElementById('mnu_itm'+id).className=='navMenuParent') )
				{
					//leave selected when current selected item and not an grafic menu

				}
				else
				{

					hideSub(selectedSubMenus[i]);
				}
				selectedSubMenus[i]='';
			}
		}

	}

}

/*
 * Hide all menus
 */
function hideMenu() {
	if (selectedSubMenus.length > 0)
	{
		for(i=1;i<=selectedSubMenus.length;i++)
		{
			if (selectedSubMenus[i])
			{
				hideSub(selectedSubMenus[i]);
				selectedSubMenus[i]='';
			}
		}
	}
}
/*
 * No submenu but set default image
 */
function showState(id)
{
	var link   = document.getElementById('mnu_itm'+id);

	if(link.src && !imageState[id])
	{
		imageState[id] = link.src;

		var suffix = link.src.substring(link.src.lastIndexOf('.'));
		var imgsrc;



		link.src = link.src.replace(/b=(1|3)/, 'b=2');

			/*
		if (link.src.lastIndexOf("_o")+2 == link.src.lastIndexOf('.') || link.src.indexOf("_s")+2 == link.src.lastIndexOf('.')  )
		{
			imgsrc = link.src.substring(0,link.src.lastIndexOf('.')-2) + "_o" + suffix;
		}
		else
		{
			imgsrc = link.src.substring(0,link.src.lastIndexOf('.')) + "_o" + suffix;
		}*/

		//link.src = imgsrc;

	}
	else
	{
		link.src = imageState[id];
		imageState[id] = '';
	}
}

function set_preview(ssrc,att)
{
	if (navigator.appName != 'Netscape')
	{
	if(ssrc=='')
	{
		ssrc = '/images/shared/blanco.gif';
	}
	else
	{
		ssrc = 'file:///'+ssrc
	}
	document.getElementById('preview'+att).src = ssrc;
	document.getElementById('preview'+att).style.display = 'block';
	}
}

function checkCookies(inline,strMelding)
{
	var cookieName = 'testCookie' + (new Date().getTime());

	document.cookie = cookieName + '=cookieValue';
	var cookiesEnabled = document.cookie.indexOf(cookieName) != -1;
	if (!cookiesEnabled)
	{
		if (inline)
		{
			document.write('<div class="loginerror">' + strMelding + '</div>');
			return true;
		}
		else
		{
			alert(strMelding);
			return false;
		}
	}
}

function debug(ln,reset)
{

	if(document.getElementById('js_debug').style.display == 'none') document.getElementById('js_debug').style.display = 'block';
	if(reset) document.getElementById('js_debug').innerHTML = null;
	document.getElementById('js_debug').innerHTML = document.getElementById('js_debug').innerHTML + ln + '<br/>';

}


addLoadEvent(function()
{
	var img, src;
	var imgs = document.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++)
	{
		if (imgs[i].id.indexOf("mnu_itm") == 0 )
		{
			src = imgs[i].src;
			img = new Image();
			img.src = src.replace(/b=\d/, 'b=1');

			img = new Image();
			img.src = src.replace(/b=\d/, 'b=2');

			img = new Image();
			img.src = src.replace(/b=\d/, 'b=3');
		}
	}
});


function updateNewsIndex(pub_Id, page)
{
	$.get('/dynamics/getnews.php',{ pub_Id: pub_Id, page: page },
		function(msg) {
			document.getElementById('news_index').innerHTML=msg;
			init();
		});
}


/*------------------------------------------------------------------------------
	Overlay
------------------------------------------------------------------------------*/

var overlayLoaded = false;
var popupVisible = false;
var popupImgs = new Array();


function openPopup(url, width, height)
{
	showOverlay();

	var content = '<a href="javascript:window.open(document.getElementById(\'popupFrame\').src, \'buttonWindow\'); hidePopup();" id="popupOpenWindow">Open in new window</a><a href="javascript:hidePopup();" id="popupClose"></a><iframe id="popupFrame" name="popupFrame" width="'+(width-18)+'" height="'+(height-36)+'" src="'+url+'" align="center" frameborder="no" scrolling="auto">';

	showPopup(width, height, content);
}

function showOverlay()
{
	if (!overlayLoaded)
	{
		overlayLoaded = true;

		// hide select box (ie6 bug)
		if ($.browser.msie) $('input').hide();

		// show overlay
		$('<div id="overlay"><div id="overlay_mask"></div><div id="overlay_load"></div><div id="overlay_popup"><div id="popup"></div></div></div>').appendTo('body');

		$('#overlay_mask').click(function(){ removeOverlay(); });

		// stretch to full height & width
		$('#overlay_mask')
			.height(document.documentElement.scrollHeight)
			.width(document.documentElement.scrollWidth);
		$('#overlay')
			.css('padding-top', document.documentElement.scrollTop+'px');
	}
}

function removeOverlay()
{
	if (overlayLoaded)
	{
		overlayLoaded = false;
		popupVisible = false;

		// remove overlay
		$('#overlay').remove();
		if ($.browser.msie) $('input').show();
	}
}

function showPopup(pWidth, pHeight, pContent)
{
	$('#popup').html(pContent);

	var imgs = $('#popup').find('img[@rel=loading]');
	var imgCnt = imgs.length;
	var imgNr = 0;
	var images = new Array();

	imgs.each(function()
	{
		images[imgNr] = this.src;
		this.src = '/images/shared/loadingPopup.gif';

		// vars used inside image object
		var nr = imgNr;
		var imgOb = this;

		// generate new image object
		var img = new Image();
		img.onload = function() {
			this.onload = null;

			// loading finished replace loading image with real image
			imgOb.src = images[nr];
		}
		img.src = images[imgNr];

		// increase image count
		imgNr++;
	});

	$('#overlay_load').hide();
	animatePopup(pWidth, pHeight);
}

function animatePopup(pWidth, pHeight)
{
	if (!popupVisible)
	{
		popupVisible = true;

		var marginTop = parseInt(($('html').height() - pHeight) / 2);

		// set margin top
		$('#overlay_popup').css({top: marginTop+'px'});

		// resize container
		$('#overlay_popup').height(pHeight).width(5);

		// show popup
		$('#overlay_popup').animate({
			height: 'toggle'
		}, null, null, function(){
			$('#overlay_popup').animate({width: pWidth}, null, null, function()
			{
				$('#popup').show();
			});
		});
	}
	else
	{
		$('#popup').show();
	}
}

function hidePopup()
{
	$('#popup').hide();
	$('#overlay_popup').animate({
		height: 0,
		width: 0
	}, null, null, function(){
		removeOverlay();
	});
}
