/**
 * Some general routines which might be useful in different parts of the webapp.
 */


/**
 * Mechanism for splitting long sentences in edition/publication attribute
 * values view.
 */
function splitLongSentences(){
	//select old view
	var text = $("span.attribute > a");
	 for(var i = 0 ; i < text.length ; i++){
	  if(text[i].id == ""){
	  	 continue;
	  }
	  var anchor = $("#"+text[i].id);
	  var funcName = (text[i].id).replace("attr_anch_","");
	  var results = anchor.text();
	  results = results.match(/.*?(\.|,|\!|\?|\(|\)|$)/g);
	  var splitted = "";
	  if(results != null){
	   //create new view - with multiple anchors
	   for(var j = 0; j < results.length; j++){
	     splitted += ("<a class='splittedAttrValue' id='pmcA_"+funcName+"_"+j+"' href='#'>"+results[j]+"</a>");
	   }
	   anchor.replaceWith(splitted);
	   for(j = 0 ; j < results.length ; j++){
	    //assign new action to each anchor 
		 $("#pmcA_"+funcName+"_"+j).live("click",function(){
	      var txt = $(this).text();
	  	    var idInfo = $(this).attr('id').replace("pmcA_","");
	  	    idInfo = (idInfo.match(/.*?(\_)/g)[0]).replace("_","");
	  	    submitQuery(idInfo, txt.replace(/(\.|,|\!|\?|\(|\)|)/g,""));  
	  	   });
	  	  }
	   }
	 }
}

function fitMiniatures(){
	var arrImg = $(".minImg");
	_fitMiniature(0,arrImg,arrImg);
}

function _fitMiniature(index,arrImg){
	if(index >= arrImg.length) return;
	var img = arrImg[index];
	var cW = parseInt($(".minContainer").css('width'));
	var cH = parseInt($(".minContainer").css('height'));
	if(img.complete){
		var perc = calculateFit(cW,cH,img.width,img.height);
		resizeAndFit(perc,img,cW,cH);
		_fitMiniature(index+1,arrImg);
	}
	else{
		$(img).load(function(){
			var perc = calculateFit(cW,cH,img.width,img.height);
			resizeAndFit(perc,img,cW,cH);
			_fitMiniature(index+1,arrImg);
			
		});
	}
	
}

function fitMiniature(id,url){
	$("#"+id).replaceWith("<img id=\""+id+"\" src=\""+url+"\" class=\"minImg\"/>");
	var img = $("#"+id)[0];
	var cW = parseInt($(".minContainer").css('width'));
	var cH = parseInt($(".minContainer").css('height'));
	if(img.complete){
		var perc = calculateFit(cW,cH,img.width,img.height);
		resizeAndFit(perc,img,cW,cH);
	}
	else{
		$(img).load(function(){
			var perc = calculateFit(cW,cH,img.width,img.height);
			resizeAndFit(perc,img,cW,cH);
		});
	}
}

/**
 * Resizes loaded miniature image and fits
 * it to container width/height.
 * 
 * @param percentage level of resize
 * @param img - image to resize
 * @param conWidth - container width
 * @param conHeight - container height
 * 
 */
function resizeAndFit(percentage, img, conWidth, conHeight){
	var pWidth = parseInt(img.width*(percentage/100));
	var pHeight = parseInt(img.height*(percentage/100));
	var marginLeft = 0;
	var marginTop = 0;
	marginLeft = parseInt((conWidth - pWidth)/2);
	marginTop = parseInt((conHeight - pHeight)/2);
	$(img).css({
		'width' : pWidth,
		'height' : pHeight,
		'margin-left' : marginLeft,
		'margin-top' : marginTop
	});
}

/* 
   Calculating percents of fit option : how many percents
   are needed to fit an image into a container. 
   initX,initY : container size parameters
   currentWidth, currentHeight : to fit object size
*/
function calculateFit(initX, initY, currentWidth, currentHeight){
	var l_cont;
	var l_img;
	var perc;
	var perc2;
	l_img = currentWidth;
	l_cont = initX;
	perc = ((l_cont*100)/l_img);
	perc = parseFloat(perc.toFixed(1));
	l_img = currentHeight;
	l_cont = initY;
	perc2 = ((l_cont*100)/l_img);
	perc2 = parseFloat(perc2.toFixed(1));
	if(perc < perc2)
		return perc;
	else
		return perc2;
}

function getRequestParam(contentUrl, paramName) 
{
	var requestArg = paramName+"=";
	var paramIndex = contentUrl.indexOf(requestArg);
	if ( paramIndex == -1 ) return null;
	
	var nextAmp = contentUrl.indexOf("&", paramIndex);
	if ( nextAmp == -1 ) 
	{
		return contentUrl.substring(paramIndex+requestArg.length, contentUrl.length);
	}
	return contentUrl.substring(paramIndex+requestArg.length, nextAmp);
}

/**
 * Removes action parameter from page URL and send redirect to
 * page without action param.
 */
function safePageRefresh() 
{
   var refreshingUrl = removeAction(window.location.href);
   var hashIndex = refreshingUrl.indexOf("#"); 
   if (  hashIndex != -1 ) {
	   refreshingUrl = refreshingUrl.substring(0,hashIndex);
   }

   var showContentParam = "showContent=true";
   var hasShowContent = refreshingUrl.indexOf(showContentParam);
   if ( hasShowContent != -1 ) {
	   refreshingUrl = refreshingUrl.replace(showContentParam, "showContent=false");
   }
   
   window.location.href = refreshingUrl;
}

/** Function is cutting too long names */
var NAME_MAX_LENGTH = 100;

function shorterNames(){
		$('.dlibra_shortNames a').each(function () {
				var pub_name = this.innerHTML;
				if(pub_name.length > NAME_MAX_LENGTH){
					pub_name = pub_name.substring(0,(NAME_MAX_LENGTH-3));
					this.innerHTML = pub_name + "...";
				}
		
		});
}

function _shorterNames(selector, maxLength){
	$(selector).each(function(){
		var name = $(this).text();
		if(name.length > maxLength){
			name = name.substring(0,maxLength - 3);
			$(this).text(name + "...");
			if(this.id == ""){
				this.id = "dL_shorter";
			}
		}
	});
}

function _cutString(value){
	return cutString(value,500);
}

function cutString(value, maxLenght){
	if(value.length > maxLenght){
		value = value.substring(0,(maxLenght-3));
		value = value + "...";
	}
	return value;
}

function isInteger (s)
{
    var i;

     if (isEmpty(s))
     if (isInteger.arguments.length == 1) return 0;
     else return (isInteger.arguments[1] == true);

     for (i = 0; i < s.length; i++)
     {
        var c = s.charAt(i);

        if (!isDigit(c) ){
	         return c == "-";
        }
     }

    return true;
}

/** You must specify value of DOM element */
function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
     return ((c >= "0") && (c <= "9"))
}

/** dom element as argument*/
function isVisible( c ) {
  return c.style.display != 'none';
}

/**
* checks whether given email address is valid.
*/
function isEmailValid(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(email);
}


function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.ie6 = (this.version.indexOf('MSIE 6')>0)
	this.ie7 = (this.version.indexOf('MSIE 7')>0)
	this.ff  = (navigator.userAgent.indexOf("Firefox")>0)
	this.ff2 = (navigator.userAgent.indexOf("Firefox/2")>0)
	this.min = (this.ns||this.ie)
}
is = new BrowserCheck()

var stat

function statBar(txt,time) {
   clearTimeout(stat);
   window.status = txt;
   stat=setTimeout("statErase()",time);
}

function statErase() {
   window.status="dLibra - biblioteka cyfrowa";
}

function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}

function changeImage(layer,imgName,imgObj) {
	if (document.images) {
		if (document.layers && layer!=null) eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src')
		else document.images[imgName].src = eval(imgObj+".src")
	}
}

function setCookie(name, value, expires, path, domain)
{
    var exp = new Date();
    var ex = new Date(exp.getTime()+expires);
    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + ex.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "");
    document.cookie = curCookie; 
} 

function getCookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function hideLinks( whichSelect, link, val )
{
   if(document.forms.collections[whichSelect].value == val)
   {
     document.getElementById(link).style.visibility = 'hidden';
   }
   else
   {
     document.getElementById(link).style.visibility = 'visible';
   }
}

/*
  hide all select boxes on page.
*/
function hideSelect(){
	$("select").hide();
}

/*
 shows all select boxes on page
*/
function unhideSelect(){
	$("select").show();
}


function showPopup(homepageUrl, subPage, windowName) {
  pictureWindow = window.open(homepageUrl + subPage,windowName,'scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,height=500,width=470');
  pictureWindow.focus();
}

function showHelp(helpId, homepageUrl) {
  showPopup(homepageUrl, "/dlibra/help?id=" + helpId,"dlibra_help_window");
}

function showContact(homepageUrl) {
  showPopup(homepageUrl, "/dlibra/contact","dlibra_contact_window");
}


/**
 prepares box with ajax please wait dialog
 @param message - message which will be displayed.
 @param baseUrl - base url of webpage, you must specify this when 
                  using this method from main page. 
*/
function getAjaxWarning(message, baseUrl) 
{
     if (  baseUrl == null )
		return  message + '<br/> <img src="../style/common/img/pleasewait.gif" alt="[||--]" />';
 	 else
		return  message + '<br/> <img src="'+baseUrl+'/style/common/img/pleasewait.gif" alt="[||--]" />';
		
}


/**
* puts message into dom, and hides it after some time
  @param message - message which will be displayed.
  @param isSuccess - if true success image will be used
  @param messageDomId - dom id of message object which will be created.
  @param statusDomId - dom id of object which will be appended with messageDom object.
  @param timeout - optional time, default value is 2000;
  @param cssClass - optional css class name which will be assigned to message.
  @param baseUrl - base url of webpage, use this always when invoking this method
   on main webpage.
*/
function statusMessage(message, isSuccess, messageDomId, statusDomId, timeout, cssClass, baseUrl)
{
  if ( cssClass == null )   {
	  cssClass = 'error';
	  if ( isSuccess ) cssClass = 'success';
  }  
  if ( timeout == null ) 
      timeout=2000;
      
  if  ( baseUrl == null )
	  baseUrl = '../'; 
      
  var imageUrl = baseUrl+'/style/common/img/icons/statusok2.gif';
  if ( !isSuccess ) {
      imageUrl = baseUrl+'/style/common/img/icons/statuserror2.gif';
  }    
  $('#'+statusDomId).html('<span id="'+messageDomId+'" class="'+cssClass+'"><img src="'+imageUrl+'" alt="&nbsp" style="vertical-align:bottom"/>&nbsp;'+message+'</span>');
  setTimeout('$("#'+messageDomId+'").hide("medium")', timeout);
}

/**
  This function removes action parameter and its value 
  from given url. 
  Use this function always when you want to send dlibra url to
  external system.
  @param url - page url
*/
function removeAction(pageUrl) {
    var url = new String(pageUrl);
	var result = url;
	var startIndex = url.indexOf("?action=");
	if (startIndex == -1) {
		startIndex = url.indexOf("&action=");
	}
	if (startIndex != -1) {
		var secondPart = url.substring(startIndex + 1);
		var endPart = secondPart.indexOf('&');
		if (endPart == -1) {
			result = url.substring(0, startIndex);
		}
		else {
			result = url.substring(0, startIndex + 1)
					+ secondPart.substring(endPart + 1);
		}
	}
	return result;
}

function replaceAll( str, searchTerm, replaceWith, ignoreCase )	{
	var regex = "/"+searchTerm+"/g";
	if( ignoreCase ) regex += "i";
	return str.replace( eval(regex), replaceWith );
}
