var dk_win_inline = new Array();
var is_ie = (document.all)

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

function dk_common_msxml(pType) {	
	switch (pType) {
	case "HTTP":
		try {return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) {
			try {return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) {}
		}
		break;
	case "DOM":
		try {return new ActiveXObject("Msxml2.DomDocument") } catch (e) {
			try {return new ActiveXObject("Msxml.DomDocument") } catch (e) {}
		}
		break;
	default: break;
	}
}
function dk_common_httprequest(pURL,pXML) {
	try {
		if (is_ie) var xmlhttp = dk_common_msxml('HTTP')
		else var xmlhttp = new XMLHttpRequest()

		if (pXML && xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}
		xmlhttp.open("GET", pURL, false)	// Synchronous request
		xmlhttp.send(null)
		return (pXML) ? xmlhttp.responseXML : xmlhttp.responseText
	} catch (e) {
		return (pXML) ? "<root>ERR"+e.description+"</root>" : "ERR"+e.description
	}
}
function dk_common_async_httprequest(pURL, pReady, pError, pDetails) {
	try {
		if (is_ie) var xmlhttp = dk_common_msxml('HTTP')
		else var xmlhttp = new XMLHttpRequest()

		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				xmlhttp.onreadystatechange = function() {}
				pReady(xmlhttp.responseXML, pURL, pDetails);
			}
		};
		xmlhttp.open("GET", pURL, true);	// Asynchronous request
		xmlhttp.send(null)
	} catch (e) {
		pError(e, pURL, pDetails);
	}
}

var dk_common_window
function dk_common_openwin(pURL, pWidth, pHeight) {
	if (!pWidth) pWidth = 1000;
	if (!pHeight) pHeight = 600;
	
	var c=new Object();						
	if (document.all) {
		c.availHeight= document.body.clientHeight
		c.availWidth= document.body.clientWidth
	} else {
		c.availHeight = document.documentElement.scrollHeight;
		c.availWidth = document.documentElement.scrollWidth;
	}
	c.left = (c.availWidth - pWidth) / 2;

	dk_common_window = window.open(pURL,"dk_opacweb","resizable, toolbar, location, height="+pHeight+", width="+pWidth+", left="+c.left)
	dk_common_window.focus();

}
function dk_hotlist_error_block(pError, pURL, pDetails) {
	alert(pError.description)
}
function dk_hotlist_showblock(pXML, pURL, pDetails) {
	zNN = '';
	zDatabase = '';
	zNotice = '';
	zReserveMessage = '';
		
	zObj = pXML;
	
	if (!zObj.childNodes) {
		
	} else if (is_ie) {
		zRoot = zObj.childNodes[1]
		try {zNotice = zObj.selectSingleNode("/Root/readspeaker").text} catch (e) {}
		try {zNN = zObj.selectSingleNode("/Root/reserve/nn").text} catch (e) {}
		try {zDatabase = zObj.selectSingleNode("/Root/reserve/database").text} catch (e) {}
		try {zReserveMessage = zObj.selectSingleNode("/Root/reserveMessage").text} catch (e) {}
	} else {	
		zRoot = zObj.childNodes[0]
		for (var i=0; i<zRoot.childNodes.length; i++) {
		    var node = zRoot.childNodes[i]
		    switch (node.nodeName.toUpperCase()) {						
			    case 'READSPEAKER':
				    zNotice = node.firstChild.data
		            break;
		        case 'RESERVE':
		            zNN = node.getElementsByTagName("nn")[0].firstChild.data
		            zDatabase = node.getElementsByTagName("database")[0].firstChild.data
				    break;
		        case 'RESERVEMESSAGE':
		            zReserveMessage = node.firstChild.data
		            break;
		        default:
		            break;
		    }
		}

	}
	
	// Manually remove the links until there is another way to do this
	/*
	zNotice = zNotice.replace(/<a /g, '<span ')
	zNotice = zNotice.replace(/<A /g, '<span ')
	zNotice = zNotice.replace(/onmouse/g, 'xonmouse')
	zNotice = zNotice.replace(/onMouse/g, 'xonmouse')
	zNotice = zNotice.replace(/onclick/g, 'xonclick')
	zNotice = zNotice.replace(/onClick/g, 'xonclick')
	*/
	
	zNotice += '<div class="dkopacwebreserve">'

	if (zNN!='') {
		var zTitle = (pDetails.lang=='FR') ? 'R&eacute;server' : 'Reserve';
		zURL = DK_opac15+"/ajax_res_display.php?nn="+zNN		
		zNotice += '<a id="dkopacwebreserve" class="dkopacwebreserve" href="javascript:dk_url_content_window(\'dkopacwebreserve\',\''+zTitle+'\',\''+zURL+'\',200,300,50,-200)">'+zTitle+'</a>';
	} else {
		zNotice += '<span>'+zReserveMessage+'</span>'
	}
	zNotice += '</div>'
	pDetails.zDiv.innerHTML = zNotice;	
}

function dk_hotlist_toggle(pURL, pId, pLang) {
	zA = document.getElementById('dk_opacweb_anchor'+pId);

    zCollapsed = (zA.className=='dkcollapsible dkcollapsed');
					
	if (zCollapsed) {
	    zDiv = document.getElementById('dk_opacweb_res'+pId);
		zA.className = "dkcollapsible dkexpanded";
		
		zLoading = false;	
		zTemp = zDiv.getElementsByTagName("div");
		if (zTemp.length) zLoading = (zTemp[0].className=='loading');

		if (zDiv.innerHTML=='' || zLoading) {
			var zLang = (pLang) ? pLang : 'FR';
			if (zLang=='') zLang='FR';

			zDetails = new Object();
			zDetails.id = pId;
			zDetails.lang = zLang;
			zDetails.zDiv = zDiv;
			zDetails.zA = zA;
			dk_common_async_httprequest(pURL, dk_hotlist_showblock, dk_hotlist_error_block, zDetails);
		}
	} else {
		//zDiv.style.display='none'
		zA.className = "dkcollapsible dkcollapsed"
	}
}

function dk_centralpopup(pId, pTitle, pTextId, pHeight, pWidth, pOffsetLeft, pOffsetTop) {
		zHeight=(pHeight) ? pHeight : 200
		zWidth =(pWidth)  ? pWidth  : 300
		zOffsetLeft=(pOffsetLeft) ? pOffsetLeft : 0
		zOffsetTop=(pOffsetTop) ? pOffsetTop : 0
		
    var c = dk_getPosition(pId)
		c.top = c.top + zOffsetTop;
		c.left = c.left + zOffsetLeft;
		if (c.top < 0) c.top = 0;
    
		zText = document.getElementById(pTextId).innerHTML
		
		/* End of specific */
		dk_displayView(pId, zText, pTitle, zWidth, zHeight, c.left, c.top);
}

function dk_windowSize() {
	var c = new Object();
  c.width = 0, c.height = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    c.width = window.innerWidth;
    c.height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    c.width = document.documentElement.clientWidth;
    c.height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    c.width = document.body.clientWidth;
    c.height = document.body.clientHeight;
  }
	return c;
}
		
function dk_getPosition(pTag, pIgnoreScroll) {
		var c=new Object();						
		var o=document.getElementById(pTag);	
		
		if (document.all) {
			c.availHeight= document.body.clientHeight
			c.availWidth= document.body.clientWidth
		} else {
			c.availHeight = window.innerHeight
			c.availWidth = window.innerWidth
			c.availHeight = document.documentElement.scrollHeight;
			c.availWidth = document.documentElement.scrollWidth;
		}
		
		c.height=o.offsetHeight; c.width=o.offsetWidth	
		c.left=o.offsetLeft; c.top=o.offsetTop;
		while ((o=o.offsetParent) != null) { c.left += o.offsetLeft; c.top += o.offsetTop; }						
		
		if (!pIgnoreScroll) {
			c.top = c.top - document.documentElement.scrollTop
			c.left = c.left - document.documentElement.scrollLeft
		}
		c.maxWidth = c.availWidth - c.left + 1
		if (c.left < 0) c.left = 0;
		return c;
	}

  function dk_displayView(pId,pHTML,pTitle, pWidth, pHeight, pLeft, pTop) {
    if(dk_win_inline[pId+'hide']==true)return('');    
		
    dk_win_inline[pId+'win']=dhtmlwindow.open("bb"+pId, "inline", pHTML, pTitle, "width="+pWidth+",height="+pHeight+"px,left="+pLeft+"px,top="+pTop+"px,resize=1,scrolling=0", "recal")
  }
	
  function dk_hideView(pId){
    dk_win_inline[pId+'hide']=true;
		dhtmlwindow.close(dk_win_inline[pId+'win']);
  }
var inlinewin=undefined;
var hide=false;
var displayed=false;
var div_left = 0;
var div_top = 0;
var d = new Date();

function viewNotice(id_link, image,link,title,author,editor,year){
  var html = '';
  html += '<div class="listitem">';
  html +=   '<div class="smallNotiveImg" style=""><img src="'+image+'" /></div>';
  html +=   '<div class="detail">';
  html +=     '<ul class="viewNoticeUl"><li><a href="'+link+'"><b>'+title+'</b></a>';
  html +=       '<li><span class="author">'+author+'</span></li>';
  if(editor!='') html += '<li><span class="pubdate">'+editor+', '+year+'</span></li>';
  else html +=  '<li><span class="pubdate">'+year+'</span></li>';
  html +=     '</ul>';
  html +=   '</div>';
  html +=   '<div class="clear"></div>';
  html +=   '<div class="linkNotice"><a href="'+link+'">Acc&eacute;dez au d&eacute;tail de la notice <img src="'+DK_dhtmlstyle+'dhtmlwindows/images/go.png" /></a></div>';
//  html += '</div>';
  html += '</div>';
  createadview(id_link,html,title);
  return('');
}   

function checkPosition(object) {
  if (object.offsetParent != null) {
    div_left += object.offsetLeft;
    div_top += object.offsetTop;
    checkPosition(object.offsetParent);
  }
}

function createadview(id,text,title){
  hide=false;
  var fct = "displayView('"+addslashes(id)+"','"+addslashes(text)+"','"+addslashes(title)+"')";
  //alert(addslashes(text));
  setTimeout(fct,555);
}
  
function displayView(id,text,title){
  if(hide==true)return('');
  if(text==undefined)test='titre par dfaut';
  if(title==undefined)test='texte par d faut';
  div_left = 20;
  div_top = 20;    
  checkPosition(document.getElementById(id));//.offsetParent
  var left = document.documentElement.scrollLeft;//(document.all)?document.body.scrollLeft:window.pageXOffset;
  var top = document.documentElement.scrollTop;//(document.all)?document.body.scrollTop:window.pageYOffset;
  div_left = div_left - left;
  div_top = div_top - top;
  inlinewin=dhtmlwindow.open("broadcastbox", "inline", text, title, "width=300px,height=120px,left="+div_left+"px,top="+div_top+"px,resize=1,scrolling=0", "recal")
}



//Ajoute un caractre dans la chaine
// c : Caract re
// s : Chaine
// i : Indice
function addCar(c, s, i) {
  var chaine = "";
  if(i>0) {
    for(var j=0;j<i;j++) {
      chaine += s.charAt(j);
    }
  }
  chaine += c;
  for(var j=i;j<s.length;j++) {
    chaine += s.charAt(j);
  }
  return chaine;
}

function addslashes(str) {
  //str = str.replace(/\\("|'|\\)/g, "$1");
  for(var i=0;i<str.length;i++) {
    switch(str.charAt(i)) {
      //case '\' :
      //  addCar("\\", str, --i);
      case "'" :
        str = addCar("\\", str, i);
        i++;
        //alert(i+' \' : '+str);
      break;  
    }
  }
  return str;
}

function dk_rssgadget_rsslist_change(id) {
	var sel = document.getElementById('rsslist_sel_'+id);
	var view = document.getElementById('rsslist_rss_'+id);
	view.href = sel.value;
}
function dk_rssgadget_rsslist_add(id) {
	var sel = document.getElementById('rsslist_sel_'+id);
	var link = document.getElementById("edit-rssgadget-link");
	link.value = sel.value;
	//document.location.href="#";
}
