// General JavaScript functions

// Open in a new window function

// Hieronder nieuwe urls toevoegen, niet de hele url is nodig, dus geen http://!!!
// De laatste url moet NIET eindigen met een komma.
var newLinks = new Array(

	'u-cat.leidenuniv.nl',
	'openaccess.leidenuniv.nl',
	'ublive.ub.leidenuniv.nl',
	'disc.leidenuniv.nl',
	'ublive.bibliotheek.leidenuniv.nl',
	'digitallibrary.leidenuniv.nl',
	'www.digitallibrary.leidenuniv.nl',
	'132.229.7.185/ub/info/?c=10',
	'132.229.7.185/ub/info/?c=11',
	'132.229.7.185/ub/info/?c=12',
	'132.229.7.185/ub/info/?c=13',
	'catalogus.leidenuniv.nl',
	'132.229.7.185/learningguide/OFFICE2003_NL_NL/index.htm',
	'132.229.7.185/learningguide/OFFICE2003_NL_UK/index.htm',
	'132.229.7.185/learningguide/OFFICE2003_US_US/index.htm'

); // Vanaf hier nergens aankomen!

// Checker for JQuery
function rnd_check_jquery()
{
	if ( typeof( jQuery == 'undefined' ) ) window.setTimeout( 'rnd_init()', 200)
		else $( document ).ready( rnd_init() );
}

// Check jQuery status
rnd_check_jquery();
rnd_linkWalk();

// Document initialisation
function rnd_init()
{
	// Add links to the tabs of employee items
	$( '.tab_link' ).each( function ( i ) { $( this ).bind( 'click', rnd_tab_switch ) } );
	
	// Add the pagination function to the pagination button
	$( '.pagination-button' ).bind( 'click', rnd_paginate_start );
}

// Employee tab switch
function rnd_tab_switch( event )
{
	var $name = this.href.substring( this.href.indexOf( '#' ) + 1 );

	// Show the correct content
	$( '.emptab' ).each(
		function (i) {
			if ( this.id != 'div_' + $name ) $( this ).addClass( 'invisible' );
				else $( this ).removeClass( 'invisible' );
		}
	);
	
	// Change the appearance of the tabs
	$( 'li[id^=tab_]' ).each(
		function (i) {
			if ( this.id == 'tab_' + $name ) $( this ).addClass( 'active' );
				else $( this ).removeClass( 'active' );
		}
	);
	

	return false;
}

// Open links in a new window
function rnd_linkWalk()
{
	var colA = document.getElementsByTagName( 'A' );
	var linkL = newLinks.length;
	var colAL = colA.length;

	for ( var i = 0; i < linkL; i++ )
	{
		for ( var j = 0; j < colAL; j++ )
		{
			if ( colA[j].href.indexOf( newLinks[i] ) > 0 ) colA[j].target = "_blank";

			// check for pdf, etc links just once...
			if ( i == 0 )
			{
				if ( gwoemul !== null && gwoemul.substring( 0, 7 ) == '132.229' )
				{
					if ( 
						colA[j].href.lastIndexOf(".pdf") > 0 || 
						colA[j].href.lastIndexOf(".doc") > 0 || 
						colA[j].href.lastIndexOf(".ppt") > 0 || 
						colA[j].href.lastIndexOf(".xls") > 0 
					) colA[j].target = "_blank";
				}
			}
		}
	}
}

// Pagination functions
function rnd_paginate_start( event )
{
	// First hide the button
	$( '.pagination-button' ).hide();
	$( '.pagination-spinner' ).show();
	$.get( '/_fetch/page/' + event.target.name + document.location.pathname, rnd_paginate_process )
}

function rnd_paginate_process( data )
{
	// Loading is complete, hide the spinner
	$( '.pagination-spinner' ).hide();

	// Append the result to the content
	if ( data.indexOf( '<!--next' ) > 0 ) $( '.pagination-div' ).before( data );

	// Determine whether there is more content to get
	var match = data.match( /--next:([0-9]*?)--/gi ).toString();
	match = match.replace( /[^0-9]/gi, '' );
	if ( match != '-1' )
	{
		$( '.pagination-button' ).attr( "name", match );
		$( '.pagination-button' ).show();
	}
}


// Searchbox functions
function searchfrm()
{
	// formulier
	frm = document.getElementById("searchform");
	// kijk welke optie is geselecteerd
	if (document.getElementById("onderwerp").checked == true) {
	// wanneer er gezocht wordt op onderwerp dan via google zoeken
		
		frm.action = "http://icsmedia.plexus.leidenuniv.nl/zoek/";
		frm.method = "get";
		document.getElementById("searchbox").name = "q";
	 }
	 else {
		// wanneer er gezocht wordt op persoon dan via ulcn zoeken		
		frm.action = "http://www.zoekenopnaam.leidenuniv.nl/index.php";
		frm.method = "post";
		document.getElementById("searchbox").name = "term"; // nieuwe naamgeving
		document.getElementById("language").value = "nl"; // nieuw veld voor language

		// check if there is a searchterm

		if (document.getElementById("searchbox").value.search(/[^\s]/gi) == -1) {
			alert("Vul een zoekterm in");
			return false;
		}
		else {
			// submit form
			return true;
		}
	}
}

function searchfrm_english()
{
	// formulier
	frm = document.getElementById("searchform");
	// kijk welke optie is geselecteerd
	if (document.getElementById("onderwerp").checked == true) {
		// wanneer er gezocht wordt op onderwerp dan via google zoeken
		
		frm.action = "http://icsmedia.plexus.leidenuniv.nl/zoek/en/";
		frm.method = "get";
		document.getElementById("searchbox").name = "q";
	}
	else {
		// wanneer er gezocht wordt op persoon dan via ulcn zoeken
		frm.action = "http://www.zoekenopnaam.leidenuniv.nl/index.php";
		frm.method = "post";
		document.getElementById("searchbox").name = "term"; // nieuwe naamgeving
		document.getElementById("language").value = "en"; // nieuw veld voor language

		// check if there is a searchterm
		if (document.getElementById("searchbox").value.search(/[^\s]/) == -1) {
			alert("No search term provided");
			return false;
		}
		else {
			// submit form
			return true;
		}
	}
}

function savePreference(NameOfCookie, value, expiredays)
{
}
