/* Function to test if a key word is given for the search */
var baseURL = '';  //TODOINSTALL à changer si baseURL change
var url = location.pathname;

jQuery(document).ready(function($) {
			// ON LOAD
			// Cufon : load custom font
			cufon(); 
			
			// Carousel pour les nouveautés
			$('#nouveautes-carousel').cycle({
				fx: 'fade'
			});
			
			// Menu comment à déplier sur la page d'accueil
			if ($('body').attr('id') == 'pid-accueil') {
				deployCommentMenu();
			}
			
			// Préfixe les liens vers l'OPAC
			prefixOPACLink();
			
			// Ouvrir dans une nouvelle fenêtre
			addTargetNew($('#block-menu-secondary-links li.ent a'));
			
});

// Cufon: fonction pour changer la police de certains éléments
function cufon(){
	Cufon.replace(['#site-name-wrapper', '#block-menu-menu-angers-blocs ul.sf-menu > li', 
	'#block-menu-menu-rubriques-accueil ul.menu li', 'h2.canal-title', 'span.title', 
	'h2.title', '#content-region h1.title', '.carousel .views-field-title',
	'.focus-bloc .views-field-title', '.focus-bloc .views-slideshow-controls-bottom'], {hover: true});
} 

// Fonction qui ajoute un préfixe aux URL d'accès des bases de données si l'utilisateur est HORS campus 
// Appelée directement dans le contenu de la page Base de données
function bddChangeLink(){
	prefix = "https://budistant.univ-angers.fr/";
	if ($('div.external_access').length > 0) {
		$('div.view-content td.views-field a').each(function(index) {
				$iUrl = $(this).attr('href');
				$(this).attr('href', prefix + $iUrl.replace(":/", ""));
		});
	}
}

// Fonction préfixant URL Accès distant si utilisateur hors campus
// Appelé sur Views Accordéon BDD
function accordeonChangelink(){
	prefix = "https://budistant.univ-angers.fr/";
	if ($('div.external_access').length > 0) {
		$('div.views-accordion div.views-field-field-thumbnail-fid a').each(function(index) {
				$iUrl = $(this).attr('href');
				$(this).attr('href', prefix + $iUrl.replace(":/", ""));
		});
	}
}



// Fonction préfixant URL Accès distant si utilisateur hors campus
// Appelé sur page @revue pour appel frame SFX
function framesfxChangelink(){
	prefix = "https://budistant.univ-angers.fr/";
	if ($('div.external_access').length > 0) {
		$('iframe').each(function(index) {
				$iUrl = $(this).attr('src');
				$(this).attr('src', prefix + $iUrl.replace(":/", ""));
		});
	}
}

// Fonction qui préfixe tous les liens de l'OPAC par opac/*
function prefixOPACLink(){
	if ($('div#contenu[class*="OPACembed"]').length > 0) {
		type = $('div#contenu').attr("class").replace('aleph_embed OPACembed_', '');
	}
	else type = 'search';
	
	prefix = baseURL + '/opac/'+type+'/';
			
	// HREF commencent par catalogue.univ
	$('div#content-content a[href^="http://catalogue.univ"]').each(function(index) {
			$iUrl = $(this).attr('href');
			$(this).attr('href', prefix + $iUrl);
	});
	$('div#content-content a[href^=javascript:open_window("http://catalogue.univ]').each(function(index) {
			$iUrl = $(this).attr('href');
			//$(this).attr('href', $iUrl.replace("http://catalogue", prefix + "http://catalogue").replace('");', '&con_lng=cms");'));
			$(this).attr('href', $iUrl.replace('");', '&con_lng=cms");'));
			
	});
	$('div#contenu form').each(function(index) {
			$iUrl = $(this).attr('action');
			$(this).attr('action', prefix + $iUrl);
	});
}

// Fonction qui teste si la zone de recherche est vide
function IsBlankField(field){
	var j = 0;
	var chr;

	for(j; j < field.length; j++){
			chr = field.charAt(j);

			if(chr != "" && chr != " " && chr != "\n" && chr != "\t"){
				return false;
			}
	}

	return true;
}

// Fonction qui déplie le menu Comment
function deployCommentMenu() {
	//alert('On va déplier');
	$('div#block-menu-menu-angers-scd-menu h2.collapsiblock').removeClass('collapsiblockCollapsed');
	$('div#block-menu-menu-angers-scd-menu div.content').attr('aria-hidden', 'false').attr('style', 'display: block;');
}

// Fonction qui ajoute un attribut @target="_blank"
function addTargetNew(el) {
	el.attr('target', '_blank');
}
			
// Fonction qui change la cible du formulaire en fonction du bouton de soumission choisi 
function submitTo(id){
		//shortening form elements
		var myForm      = document.getElementById('rechForm');
		var txtSearch   = document.getElementById('txtQuery');
		var srchURL;    // this variable will hold the URL of the search engine to be used
		var srchVar;    // this variable will hold the value of the text field 'name' which will hold the string to be searched 
		var hideInputs = $('input[type=hidden]');
		if(IsBlankField(txtSearch.value)){
				//if the form doesn't validate, send this alert
				alert("Veuillez saisir un terme pour la recherche!");
		} 
		else{
				if(id == "lecatalogue"){
					srchURL = "http://catalogue.univ-angers.fr/F/";
					srchVar = "request";
					//myForm.setAttribute("target", "_blank");
					$('input[id*="edit-"]').remove();
					$('input[id*="cata_"]').remove();
					$('input[id*="site_"]').remove();
					$('input[id*="schol_"]').remove();
				}
				else if(id == "cata"){
					srchURL = baseURL + "/opac/search/http://catalogue.univ-angers.fr/F/";
					srchVar = "request";
					$('input[id*="edit-"]').remove();
					$('input[id*="lecatalogue_"]').remove();
					$('input[id*="site_"]').remove();
					$('input[id*="schol_"]').remove();
				} 
				else if(id == "site"){
					srchURL = baseURL + "/search/node/"+$('input#txtQuery').val();
					srchVar = "search_block_form";
					myForm.setAttribute("target", "_self");
					$('input[id*="edit-"]').remove();
					$('input[id*="lecatalogue_"]').remove();
					$('input[id*="cata_"]').remove();
					$('input[id*="schol_"]').remove();
				} 
				else{
					srchURL = "http://scholar.google.com/scholar";
					srchVar = "q";
					//myForm.setAttribute("target", "_blank");
					$('input[id*="lecatalogue_"]').remove();
					$('input[id*="cata_"]').remove();
					$('input[id*="edit-"]').remove();
					$('input[id*="site_"]').remove();
				}

				//adding the attribute name and values to the form and text field elements
				myForm.setAttribute("action", srchURL);
				txtSearch.setAttribute("name", srchVar);
				 //this, of course, will open a new browser window
				myForm.setAttribute("method", "get");
				myForm.submit();
		}
}

// Fonction nécessaire pour l'ajout, la suppression dans le panier. Repris du site de l'OPAC
function select_record(theForm) {
	url = $('div#contenu a.blue:first').attr('href');
	baseurl = url.substring(0, url.indexOf('?'));
	if ($('select[name="folder"]').length > 0) {
		if (theForm.ckbox.checked) { 
			var loc = baseurl + "?func=myshelf-select-1&" + theForm.name + "=on"; 
		} 
		else { 
			var loc = baseurl + "?func=myshelf-select-1&" + theForm.name + "=off"; 
		}
	}
	else {
		if (theForm.ckbox.checked) { 
			var loc = baseurl + "?func=short-select-single&" + theForm.name + "=on"; 
		} 
		else { 
			var loc = baseurl + "?func=short-deselect-one&" + theForm.name + "=off"; 
		}
	}

	document.images["dummy_image"].src = loc;
} 

// Fonction reprise de l'OPAC pour les liens dans l'affichage d'une notice
function open_window (loc) { 
    self.name="main"; 
    if ((navigator.appVersion.indexOf("MSIE 5.0") != -1) && (navigator.appName.indexOf("Explorer") != -1)){ 
   var w = window.open ("","win1","height=500,width=500,scrollbars,resizable"); 
     w.document.write("<html>\n"); 
     w.document.write('<body onLoad="window.location.href='); 
     w.document.write("'" + loc + "';\n"); 
     w.document.write('"'); 
     w.document.write("</body>\n"); 
     w.document.write("</html>\n"); 
     w.document.close(); 
    } else { 
    var w = window.open (loc,"win1","height=500,width=500,scrollbars,resizable"); 
    } 
    w.focus(); 
}
// Fonction reprise de l'OPAC pour le renouvellement des prets
function replacePage (loc) {      
    window.location.replace(loc);
}
function collectData () {
	var session = $('div.OPACembed_user').attr('id');
	session = session.substring(session.indexOf('_')+1);
	var strData = baseURL + "/opac/user/http://catalogue.univ-angers.fr:80/F/"+session+"?func=bor-renew-all&renew_selected=Y&adm_library=OPD51";
	for (i=0; i<document.getElementsByTagName("input").length; i++) {
		var el = document.getElementsByTagName("input")[i];
		if (el.type == "checkbox" && el.checked)
			strData += "&" + el.name + "=" + "Y";
	}
	return strData;
}

// Fonction reprise de l'OPAC pour le panier
function setFolder(val) {
   url = $('div#contenu a.blue:first').attr('href');
   baseurl = url.substring(0, url.indexOf('?'));
   var loc = baseurl + "?func=myshelf-short&folder=" + val;
   window.location.href=loc;
} 


// Fonction RECHERCHE RAPIDE SB
function envoyer(id){
		//shortening form elements
		var myForm      = document.getElementById('rechForm');
		var txtSearch   = document.getElementById('txtQuery');
		var srchURL;    // this variable will hold the URL of the search engine to be used
		var srchVar;    // this variable will hold the value of the text field 'name' which will hold the string to be searched 
		var hideInputs = $('input[type=hidden]');
		if(IsBlankField(txtSearch.value)){
				//if the form doesn't validate, send this alert
				alert("Veuillez saisir un terme pour la recherche!");
		} 
		else{
				if(id == "catalogue"){
					srchURL = baseURL + "/rechercher";
					srchVar = "recherche";
					$('input[id*="edit-"]').remove();
					$('input[id*="site_"]').remove();
					$('input[id*="schol_"]').remove();
				}
				else if(id == "site"){
					srchURL = baseURL + "/search/node/"+$('input#txtQuery').val();
					srchVar = "search_block_form";
					myForm.setAttribute("target", "_self");
					$('input[id*="catalogue_"]').remove();
					$('input[id*="edit-"]').remove();
					$('input[id*="schol_"]').remove();
				} 
				else{
					srchURL = "http://scholar.google.com/scholar";
					srchVar = "q";
					$('input[id*="catalogue_"]').remove();
					$('input[id*="edit-"]').remove();
					$('input[id*="site_"]').remove();
				}

				//adding the attribute name and values to the form and text field elements
				myForm.setAttribute("action", srchURL);
				txtSearch.setAttribute("name", srchVar);
				 //this, of course, will open a new browser window
				myForm.setAttribute("method", "get");
				myForm.submit();
		}
}

