/*------------------------------------------------------------------------------
	Methode om CSS background-image cache te forceren voor IE6
------------------------------------------------------------------------------*/

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


/*------------------------------------------------------------------------------
	onLoad handler
------------------------------------------------------------------------------*/

var bodyInit = false; //set initial load
var root = 'body'; //set root
var popWidth = 492;

function onLoad(loadRoot, formTokens)
{
	// set root
	if (typeof(loadRoot) == 'string') root = loadRoot;

	// one time initializations in here
	if (!bodyInit) {

		// on show event handler for overlay
		if (typeof $.overlay == 'object') $.overlay.event.bind('show', function(e,$popup,options){ onLoad('#popup_'+options.name); });

		// image rollover handler
		if (typeof $.fn.addRollOverHandler == 'function') $('.navMain img').addRollOverHandler();

		// remember intialization
		bodyInit = true;
	}

	//form validation
	if (typeof $.validator == 'object') {
	    //load tokens if specified
	    if(typeof formTokens == 'object') {
	    	$.validator.tokenEnable(formTokens);
	    } else {
	    	$(root+' form').validatorEnable();
	    }
	}

	// overlay
	if (typeof $.overlay == 'object') {
		$(root+' img[rel=zoom]').setGallery({thumbId: 4, enableImgNav: false});
		if(root == 'body') setPopWidth();		
		$(root+' a[rel=popup-inline]').openPopup({minWidth:popWidth,maxWidth:popWidth});
		$(root+' a[rel=popup-iframe]').iframePopup({width: 800, height: 600});
	}

	//search
	$('form#frm_search_side').bind("submit", createCleanSearchUrl );

}

function setPopWidth()
{
	if($('.loContent').css('width') && root == 'body') {
		cssWidth = new String($('.loContent').css('width'));
		if(cssWidth.indexOf('px') > 0) {
			popWidth = parseInt(cssWidth.substr(0,cssWidth.indexOf('px')));
		}
		else {
			popWidth = parseInt($('.loContent').css('width'));
		}
	}	
}

/*------------------------------------------------------------------------------
	AJAX handlers
------------------------------------------------------------------------------*/

function setIndex(pub_Id, page, archive)
{
	$.get(location.href,{ pub_Id: pub_Id, page: page, archive: archive, action: 'index'},
		function(msg) {
			$('#index_container').html(msg);
			onLoad('#index_container');
		}
	);
}

/*------------------------------------------------------------------------------
	Various
------------------------------------------------------------------------------*/

function checkCookies(redirect)
{
	var cookieName = 'testCookie';
	document.cookie = cookieName + '=cookieValue';
	var cookiesEnabled = document.cookie.indexOf(cookieName) != -1;
	if (!cookiesEnabled)
	{
		location.href=redirect;
	}
}

function createCleanSearchUrl()
{	
	var searchVal = $('form#frm_search_side input#search').val();
	var action = $('form#frm_search_side').attr('action');
	if ($('form#frm_search_side input#search').hasClass('errHilite') == false) {
		window.location = '../..'+action+'/'+searchVal.replace(' ', '/');
	}
	return false;
}
