/********************************************************+
 *
 * place for all necessary JavaScript-Functions...
 * Bayer UK/Ireland Tools page
 ******************/

var container = new Array();

function setIndex(p_strValue, array_index) {
	if(container[array_index]["p_strIndex"] != p_strValue) {//make new gallery visible
	 container[array_index]["p_strIndex"] = p_strValue;
	 document.getElementById("divimagebox_" + container[array_index]["p_strIndex"]).style.display = 'block';
  }										

  if(container[array_index]["oldIndex"] != '' && container[array_index]["oldIndex"] != p_strValue) {
	 document.getElementById("divimagebox_" + container[array_index]["oldIndex"]).style.display = 'none';
  }
  container[array_index]["oldIndex"] = p_strValue;										
}

function changeImage(p_strAction, array_index) {
	
  switch(p_strAction) {
	 case "next" : {
	 
		if(container[array_index]["maxCount"] > (container[array_index]["actPos"] + 1)) {
		  if(container[array_index]["intStatus"] == 0)
			 document.getElementById("imagebox_" + container[array_index]["p_strIndex"]).src = container[array_index]["arrayImages"][container[array_index]["actPos"] + 1];
		  else
			 document.getElementById("imagebox_" + container[array_index]["p_strIndex"]).src = container[array_index]["arrayImagesZoomPart"][container[array_index]["actPos"]  + 1];

		  document.getElementById("imagebox_" + container[array_index]["p_strIndex"]).alt = container[array_index]["arrayAltTexts"][container[array_index]["actPos"] + 1];
		  if(document.getElementById("imageboxcaption_" + container[array_index]["p_strIndex"]))
			 document.getElementById("imageboxcaption_" + container[array_index]["p_strIndex"]).innerHTML = container[array_index]["arrayCaptionTexts"][container[array_index]["actPos"] + 1];
		  document.getElementById("imageboxcount_" + container[array_index]["p_strIndex"]).innerHTML = (container[array_index]["actPos"] + 2) + '/' + container[array_index]["maxCount"];        
		  container[array_index]["actPos"]++;
		  if(container[array_index]["actPos"] == container[array_index]["maxCount"] - 1) {
			 document.getElementById("nextImage_" + container[array_index]["p_strIndex"]).src = container[array_index]["srcTransImage"];
		  } else {
			 document.getElementById("nextImage_" + container[array_index]["p_strIndex"]).src = container[array_index]["srcNextImage"];
		  }
		  document.getElementById("prevImage_" + container[array_index]["p_strIndex"]).src = container[array_index]["srcPrevImage"];
		}
		break;
	 } case "previous" : {
	 	
		if(container[array_index]["actPos"] > 0) {
		  if(container[array_index]["intStatus"] == 0)
			 document.getElementById("imagebox_" + container[array_index]["p_strIndex"]).src = container[array_index]["arrayImages"][container[array_index]["actPos"] - 1];
		  else
			 document.getElementById("imagebox_" + container[array_index]["p_strIndex"]).src = container[array_index]["arrayImagesZoomPart"][container[array_index]["actPos"] - 1];

		  document.getElementById("imagebox_" + container[array_index]["p_strIndex"]).alt = container[array_index]["arrayAltTexts"][container[array_index]["actPos"] - 1];
		  if(document.getElementById("imageboxcaption_" + container[array_index]["p_strIndex"]))
			 document.getElementById("imageboxcaption_" + container[array_index]["p_strIndex"]).innerHTML = container[array_index]["arrayCaptionTexts"][container[array_index]["actPos"] - 1];        
		  document.getElementById("imageboxcount_" + container[array_index]["p_strIndex"]).innerHTML = (container[array_index]["actPos"]) + '/' + container[array_index]["maxCount"];                
		  container[array_index]["actPos"]--;
		  if(container[array_index]["actPos"] == 0) {
			 document.getElementById("prevImage_" + container[array_index]["p_strIndex"]).src = container[array_index]["srcTransImage"];
		  } else {
			 document.getElementById("prevImage_" + container[array_index]["p_strIndex"]).src = container[array_index]["srcPrevImage"];
		  }
		  document.getElementById("nextImage_" + container[array_index]["p_strIndex"]).src = container[array_index]["srcNextImage"];
		}    
		break;
	 }
	 default : break;
  }
}

function zoomImage(p_strIndex, array_index) {
  if(container[array_index]["intStatus"] == 0) {
	 document.getElementById("imagebox_" + p_strIndex).src = container[array_index]["arrayImagesZoomPart"][container[array_index]["actPos"]];
	 document.getElementById("zoomicon_" + p_strIndex).src = container[array_index]["arrayZoomIcon"][1];
	 document.getElementById("zoomicon_" + p_strIndex).alt = altTextzoomOut;
	 document.getElementById("divimagebox_" + p_strIndex).className = container[array_index]["arrayZoomClass"][1];
	 container[array_index]["intStatus"] = 1;
  } else {
	 document.getElementById("imagebox_" + p_strIndex).src = container[array_index]["arrayImages"][container[array_index]["actPos"]];
	 document.getElementById("zoomicon_" + p_strIndex).src = container[array_index]["arrayZoomIcon"][0];
	 document.getElementById("zoomicon_" + p_strIndex).alt = altTextzoomIn;
	 document.getElementById("divimagebox_" + p_strIndex).className = container[array_index]["arrayZoomClass"][0];
	 container[array_index]["intStatus"] = 0;
  }
}

function jumpTo(array_index) {
	var count = document.getElementById("jump_"+array_index).value;
	if(container[array_index]["maxCount"] <= count) {
	 count = container[array_index]["maxCount"];
	 document.getElementById("nextImage_"+array_index).src = container[array_index]["srcTransImage"];
	} else {
	 document.getElementById("nextImage_"+array_index).src = container[array_index]["srcNextImage"];
	}
	if(count <=  1) {
	 count = 1;
	 document.getElementById("prevImage_"+array_index).src = container[array_index]["srcTransImage"];
	} else {
	 document.getElementById("prevImage_"+array_index).src = container[array_index]["srcPrevImage"];
	}

	document.getElementById("imagebox_"+array_index).src = container[array_index]["arrayImagesZoomPart"][count - 1];
	document.getElementById("imageboxcount_"+array_index).innerHTML = count + "/" + container[array_index]["maxCount"];

	container[array_index]["actPos"] = count - 1;
};

/*****************************+
*
* presentation js
*
***************/

var bodyBackgroundImage = "";
function toggleBGImage(){
	bg = document.body.style.backgroundImage;
	if(bg != ""){
		bodyBackgroundImage = bg;
		document.body.style.backgroundImage = "";
	}
	else{
		document.body.style.backgroundImage = bodyBackgroundImage;
		bodyBackgroundImage = "";
	}
}

function openPopup(href, pWidth, pHeight){
	width = (pWidth) ? pWidth : 550;
	height = (pHeight) ? pHeight : 700;
	var popup = window.open(href, "popup", "width="+width+",height="+height+",scrollbars=yes,resizable=yes,menubar=yes,location=no,status=no");
	if(popup.focus)
		popup.focus();
	return false;
}


window.onload = function(){};
function registerOnload(newFunction){
	var curWinOnload = window.onload;

	if(typeof window.onload != 'function'){
		window.onload = newFunction;
	}
	else{
		window.onload = function(){
			curWinOnload();
			newFunction();
		};
	}
}

// retreiving stylesheets (cross browser)
function getAllSheets() {
  if( !window.ScriptEngine && navigator.__ice_version ) { return document.styleSheets; }
  if( document.getElementsByTagName ) { var Lt = document.getElementsByTagName('link'), St = document.getElementsByTagName('style');
  } else if( document.styleSheets && document.all ) { var Lt = document.all.tags('LINK'), St = document.all.tags('STYLE');
  } else { return []; } for( var x = 0, os = []; Lt[x]; x++ ) {
    var rel = Lt[x].rel ? Lt[x].rel : Lt[x].getAttribute ? Lt[x].getAttribute('rel') : '';
    if( typeof( rel ) == 'string' && rel.toLowerCase().indexOf('style') + 1 ) { os[os.length] = Lt[x]; }
  } for( var x = 0; St[x]; x++ ) { os[os.length] = St[x]; } return os;
}

/* ===-- FontSizer --=== */
function FontSize(idx, cssLinkId, controlImgSrc){
	this.idx = idx;
	this.cssLinkId = cssLinkId;
	this.controlImgSrc = controlImgSrc;
	this.cssLink = null;
	FontSize.prototype.initialize = function(){
		this.cssLink = document.getElementById(this.cssLinkId);
	}
	FontSize.prototype.enable = function(){
        //alert(getAllSheets());
		// alert("FontSize.enable:\nlink: " + this.cssLink);
        //alert(this.idx);
        
        // loop trough all available styleseets and simple disable - no switch of rel!!!
        for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) {
             if( ss[x].title ) {
//                alert(ss[x].title);
                ss[x].disabled = true;
                //ss[x].rel = "alternate stylesheet";
             }
        }
        
         // now active the correct one, only way it to make it run in safari too,
         // all other browsers work like they did before...
         for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) {
             if( ss[x].title ) {
                if( ss[x].title == this.cssLinkId) {
                    //alert(ss[x].title);
                    ss[x].disabled = false;
                    //ss[x].rel = "stylesheet";
                }
             }
        }
        
        return false;
        
		if(this.cssLink != null){
			this.cssLink.removeAttribute("disabled");
			this.cssLink.disabled = false;
			this.cssLink.rel = "stylesheet";
		}
		//this.debug();
	};
	FontSize.prototype.disable = function(){
        return;
		//alert("FontSize.disable:\nlink: " + this.cssLink);
		if(this.cssLink != null){
			this.cssLink.setAttribute("disabled", "disabled");
			this.cssLink.disabled = true;
			this.cssLink.rel = "alternate stylesheet";
		}
		//this.debug();
	};
	FontSize.prototype.debug = function(){
		alert(
				"FoneSize(" + this.idx + "):\n" +
				"disabled: " + this.cssLink.getAttribute("disabled") + "\n" +
				"disabled: " + this.cssLink.disabled + "\n" +
				"rel: " + this.cssLink.rel + "\n" +
				"\n" +
				"caller: " + FontSize.prototype.debug.caller
		);
	};
}
var FontSizer = {
	currentFontSizeIdx: 100,
	CurrentFontSize: null,
	FontSizes: new Array(),
	controlId: null,
	init: function(FontSizes, currentFontSizeIdx, controlId){
		this.FontSizes = FontSizes;
		this._initilizeFontSizes();
		this.currentFontSizeIdx = currentFontSizeIdx;
		this.controlId = controlId;
		this.set(this.currentFontSizeIdx);
	},
	add: function(dif){
		var newFontSizeIdx = this.currentFontSizeIdx + dif;
		this.set(newFontSizeIdx);
	},
	set: function(fontSizeIdx){
		
		// alert("set fontSizeIdx="+ fontSizeIdx);
		var NewFontSize = this._getFontSizeByIdx(fontSizeIdx);
		if(this.currentFontSizeIdx != fontSizeIdx && NewFontSize != null) {
			
			if(this.CurrentFontSize != null){
				
				if(this.CurrentFontSize.idx < NewFontSize.idx){
					NewFontSize.enable();
					this.CurrentFontSize.disable();
				} else{
					this.CurrentFontSize.disable();
					NewFontSize.enable();
				}
			} else{
				NewFontSize.enable();
			}
			this.currentFontSizeIdx = fontSizeIdx;
			this.CurrentFontSize = NewFontSize;
			
			var myImg = "";
			if(document.getElementById("imgFontsizeSession")) {
				myImg = document.getElementById("imgFontsizeSession");
				myImg.src = "/scripts/include/fontsize/fontsizeSession.inc.php?newfontsize="+ NewFontSize.idx +"&anticache="+ Math.random();
			}
			
			// alert("1 NewFontSize="+ NewFontSize.idx +" myImg="+ myImg.src);	

			this._adjustControl();
		}
		//this.debug();
	},
	_initilizeFontSizes: function(){
		for(var i = 0; i < this.FontSizes.length; i++){
			this.FontSizes[i].initialize();
		}
	},
	_getFontSizeByIdx: function(idx){
		var _FontSize = null;
		for(var i = 0; i < this.FontSizes.length; i++){
			if(this.FontSizes[i].idx == idx){
				_FontSize = this.FontSizes[i];
				break;
			}
		}
		return _FontSize;
	},
	_adjustControl: function(){
		var Control = document.getElementById(this.controlId);
		if(Control) {		
			Control.src = this.CurrentFontSize.controlImgSrc;
		}
	},
	debug: function(){
		for(var i = 0; i < this.cssLinks.length; i++){
			var link = this.cssLinks[i];
			alert("id: " + link.id + "\ndisabled: " + link.getAttribute("disabled"));
		}
	}
}

var fontSize1 = new FontSize(1, "size1", "/html/images/main/fontsize_module_control_size1.gif");
var fontSize2 = new FontSize(2, "size2", "/html/images/main/fontsize_module_control_size2.gif");
var fontSize3 = new FontSize(3, "size3", "/html/images/main/fontsize_module_control_size3.gif");

function initFontSizer(){
	FontSizer.init(new Array(fontSize1,fontSize2,fontSize3), 1, "fontsizerControl");
}


function printonload() {
	window.print();
}

function selectLink(selObj, taregtself) {
	 a = Math.random();
	 a = 100*a;
	 a = Math.round(a);
	 
	 redir = '/scripts/pages/en/redirect.php?url='+selObj.options[selObj.selectedIndex].value;
	 
	 if(taregtself) {
	 	location.href=selObj.options[selObj.selectedIndex].value
	 } else {
		 //popUp = window.open(selObj.options[selObj.selectedIndex].value,'NewWindow'+a,'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes');	 
		 popUp = window.open(redir,'NewWindow'+a,'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes');	 
	}
	 selObj.selectedIndex=0;
}

registerOnload(initFontSizer);