/***************************/
/* questo modulo contiene  */
/* funzioni personalizzate */
/* LB in javascript.       */
/*                         */
/* 18/2/2009               */
/* Gianluca Montorsi       */
/***************************/

/************************/
/* Funzioni generiche   */
/************************/


/* cambio immagini */
function changepic(img_name, img_src) {
    document[img_name].src = img_src;
}


/* dimensioni schermo, finestra e pagina */

function getWindowWidth() {
/*  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }*/
  return (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth; ;
}


function getWindowHeight() {
/*  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }*/
  return (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight; ;
}

/*
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
*/
function getScrollX() {

  return (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft; 

}

function getScrollY() {

  return (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop; 

}


function Viewport() { 
	this.windowX = (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth; 
	this.windowY = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight; 
	this.scrollX = (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft; 
	this.scrollY = (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop; 
	this.pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; 
	this.pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
}



function getPageWidth() {

	return (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; 
}

function getPageHeight() {
	
	return (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
}




function ResizeXY(elementId, sizeX, sizeY) {

       var tt=document.getElementById(elementId);
       if( sizeY ) 
       	tt.style.height = sizeY + 'px';
       if( sizeX ) 
       	tt.style.width = sizeX + 'px';
}


function ResizeY(elementId, size) {
	ResizeXY(elementId, -1, size);
}

function ResizeX(elementId, size) {
	ResizeXY(elementId, size, -1);
    
}


function showAlert() {

	alert('pippo');	
}


/***********************/
/* funzioni specifiche */
/***********************/


/*  BOOKS  */


function getBookSize(minWidth, minHeight, maxWidth, maxHeight) {

	var wx = getWindowWidth() - 20;
	var wy = getWindowHeight() - 20;
	var alfa = minWidth / minHeight;
	var beta = wx / wy;
	var x = 0;
	var y = 0;

	
	
	if((beta > 1) && ((alfa / beta) < beta) ) {
		x = Math.ceil(alfa * wy);
		y = wy;
	} else {
		x = wx;
		y = Math.ceil(wx / alfa);
	}
	
	
	if( (x < minWidth) || (y < minHeight) ) {
		x = minWidth;
		y = minHeight;
	}
	
	if( (x > maxWidth) || (y > maxHeight) ) {
		x = maxWidth;
		y = maxHeight;
	}
	
	 
	return [ x , y ];
}

function getBookWidth(minWidth, minHeight, maxWidth, maxHeight) {
	
	var tmp = getBookSize(minWidth, minHeight, maxWidth, maxHeight);
	return tmp[0];
}

function getBookHeight(minWidth, minHeight, maxWidth, maxHeight) {
	
	var tmp = getBookSize(minWidth, minHeight, maxWidth, maxHeight);
	return tmp[1];
}



function changeLang(newLang) {
//cambia la lingua del sito
    var url = String(window.location);
    url = url.replace('#', '');
	//var pos = url.indexOf('lang');
	//if (url.charAt(url.length-1) == '/')
	//    url = url + 'index.aspx'
	//if(pos>0) 
	//	url = url.substring(0,pos-1) + url.slice(pos+8)
    //if (url.indexOf('?') > 0) { url = url + '&lang=' + newLang; } else { url = url + '?lang=' + newLang; }
    if (newLang == 'ENG' || newLang == 'RUS') {
            url = url.replace('.eu', '.com');
            url = url.replace('.it', '.com');
            url = url.replace('.lb-group', '.lb-technology');
            url = url.replace('/ru', '');
            if (newLang == 'ENG') {
                url = url.replace('lang=rus', 'lang=eng');
                url = url.replace('lang=ita', 'lang=eng');
            } else {
                url = url.replace('.com/', '.com/ru/');
                url = url.replace('.com:8081/', '.com:8081/ru/');
                url = url.replace('lang=eng', 'lang=rus');
                url = url.replace('lang=ita', 'lang=rus');
            }
    } else {
            url = url.replace('.com', '.eu');
            url = url.replace('.drymortarplant', '.lb-technology');
            url = url.replace('/ru', '');
            url = url.replace('lang=rus', 'lang=ita');
            url = url.replace('lang=eng', 'lang=ita');
    }
    window.location = url;
}


function getURLVar(urlVarName) {
//divide the URL in half at the '?' 
var urlHalves = String(document.location).split('?');
var urlVarValue = '';
if(urlHalves[1]){
//load all the name/value pairs into an array 
var urlVars = urlHalves[1].split('&');
//loop over the list, and find the specified url variable 
for(i=0; i<=(urlVars.length); i++){
if(urlVars[i]){
//load the name/value pair into an array 
var urlVarPair = urlVars[i].split('=');
if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
//I found a variable that matches, load it's value into the return variable 
urlVarValue = urlVarPair[1];
}
}
}
}
return urlVarValue;   
}

function Scroll(id, ud, spd) {
    var obj = document.getElementById(id);
    clearTimeout(obj.to);
    if (ud) {
        obj.scrollTop = obj.scrollTop + ud;
        obj.to = setTimeout(function() { Scroll(id, ud, spd); }, spd || 10)
    }
}

function HighlightPreview(obj, onOff) {
    //alert(obj.id);
    if (onOff == 'on') {
        obj.style.backgroundImage = "url(../images/fondoliste.jpg)";
        obj.style.height = parseInt(99) + 'px';
        obj.style.marginBottom = 0;
    } else {
        obj.style.backgroundImage = "";
        obj.style.height = "auto";
        obj.style.marginBottom = parseInt(5) + 'px';
    }
}


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function getCookie(Name) { //get cookie value
    var re = new RegExp(Name + "=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
        return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
}

function setCookie(name, value, days) { //set cookie value
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = expireDate.setDate(expireDate.getDate() + parseInt(days))
    document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString() + "; path=/";
}


function CallGB(title, path, x, y) {
    var xx;
    var yy;

    if (x) { xx = x; } else { xx = 1024 }
    if (y) { yy = y; } else { yy = 768 }

    return GB_showCenter(title, path, yy, xx);
}

//setCookie("globalVar", 1, 30)


//if (getCookie("globalVar") != "") //if cookie exists with name = "globalVar"
//    alert(getCookie("globalVar")) //alerts "1"




