/* main.js */

/* Author: Sascha Meier \*/
/* LastChangedDate: 2010-06-24 \*/
/* LastChangedBy: SM \*/

function onLoadFunctions() {
     // add function calls here
     navHover();
	 carouselHover();
     roundedCorners('A','mainNav');
     roundedCorners('A','subMenu');
     roundedCorners('A','sitemap');
     iframeFix('UL','mainNav');
     initFontStepper();
     initHovers('TR');
     BrowserDetect.init();
     fixOldFirefox();
     loadOverlay();
}
if (window.addEventListener) {
     window.addEventListener("load", onLoadFunctions, true);
} else if (window.attachEvent) {
     window.attachEvent("onload", onLoadFunctions);
}

/* show navi items (ie 6) */
navHover = function() {
     var nav = document.getElementById("mainNav");
     if (nav) {
          var lis = document.getElementById("mainNav").getElementsByTagName("LI");
          for (var i=0; i<lis.length; i++) {
               
               lis[i].onmouseover=function() {
                    this.curentClass = this.className;
                    this.className+=" iehover";
               }
               lis[i].onmouseout=function() {
                    this.className=this.curentClass;
               }
          }
     }
}
/* show navi items (ie 6) */
carouselHover = function() {
  // check if class exists
  if($('div').hasClass(" jcarousel-skin-ie7"))
  {
   var lis = jQuery('.jcarousel-skin-ie7 li');	//build list with items
	
		  for (var i=0; i<(lis.length-3); i++) {	// get length and remove the last three elements ( empty elements from the template)
               lis[i].onmouseover=function() {
                    this.curentClass = this.className;
                    this.className+=" iehover";
               }
               lis[i].onmouseout=function() {
                    this.className=this.curentClass;
               }
          }
     }
}


// initialize hovers
function initHovers(tag,IdName) {
     // set hover
     var lis = document.getElementsByTagName(tag);
     for (var i=0; i<lis.length; i++) {
          if (!lis[i].className) {lis[i].className=' ';}

          if (lis[i].id == IdName) {
               lis[i].onmouseover=function() {
                    this.className = this.id+"Hover";
               }
               lis[i].onmouseout=function() {
                    this.className = "";
               }
          } else if (!IdName) {
               if (lis[i].className) {
                    lis[i].classNames = lis[i].className.split(" ");
                    if (lis[i].classNames.length > 0) {
                         lis[i].classes = "";
                         for (var z=0; z<lis[i].classNames.length; z++) {
                              lis[i].classes +=  " "+lis[i].classNames[z];
                         }
                    }
									}
                    lis[i].onmouseover=function() {
                         this.className = this.classes+" "+this.classNames[0]+"iehover";
                    }
                    lis[i].onmouseout=function() {
                         this.className = this.classes;
                    }
                    lis[i].onfocus=function() {
                         this.className = this.classes+" "+this.classNames[0]+"iehover";
                    }
                    lis[i].onblur=function() {
                         this.className = this.classes;
                    }
          }
     }
}


// initialize roundedCorners
function roundedCorners(tag,IdName) {
     // set corners
     if (navigator.appName == "Microsoft Internet Explorer") {
          var node = document.getElementById(IdName);
          if (node) {
               var lis = document.getElementById(IdName).getElementsByTagName(tag);
               for (var i=0; i<lis.length; i++) {
                    lis[i].className+=" roundedCorners"
                    lis[i].innerHTML='<span class="lt"></span>'+lis[i].innerHTML;
                    lis[i].innerHTML='<span class="rt"></span>'+lis[i].innerHTML;
                    lis[i].innerHTML='<span class="lb"></span>'+lis[i].innerHTML;
                    lis[i].innerHTML='<span class="rb"></span>'+lis[i].innerHTML;
               }
          }
     }
}


// initialize iFrame (ie6 fix for navi)
function iframeFix(tag,IdName) {
     // set iframeFix
     if (navigator.appName == "Microsoft Internet Explorer") {
          var node = document.getElementById(IdName);
          if (node) {
               var lis = document.getElementById(IdName).getElementsByTagName(tag);
               for (var i=0; i<lis.length; i++) {
                    lis[i].innerHTML='<iframe class="iframeFix" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="javascript:\'\';"></iframe>'+lis[i].innerHTML;
               }
          }
     }
}


// cookie functionality
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// initialize font stepper
function initFontStepper() {
     // show font stepper
     if (document.getElementById("footerFontResize")) {
          document.getElementById("footerFontResize").style.visibility = "visible";
     }
     if (document.getElementById("fontdec")) {
          document.getElementById("fontdec").onclick= function () {fontStepper(-1, false); 
          return false;
          }
     }
     if (document.getElementById("fontdef")) {
          document.getElementById("fontdef").onclick= function () {fontStepper(0, false);
          return false;
          }
          document.getElementById("fontdef").className= "footerFontsize2Active";
     }
     if (document.getElementById("fontinc")) {
          document.getElementById("fontinc").onclick= function () {fontStepper(1, false);
          return false;
          }
     }

  /* set cookie if needed*/
  if (readCookie("fontStepperValue")) {
    var fontstepperInt = Number(readCookie("fontStepperValue"));
    fontStepper(fontstepperInt);
  }
}

function fontStepper(spInc, spReset) {
     /* font stepper */
     var spEmStepWidth 	= 0.125;	// increase/decrease font every step by spEmStepWidth
     var spEmBasis 		= 1;		// font size of spArticleBody at startup
     var spEmStep 		= 0;		// counter for current step (leave as 0)
     var spEmMaxSteps 	= 2;		// maximum steps alowed
     
     // reset font size
     if (spReset)
     spEmStep = -1;
     // inside allowed steps?
     if (Math.abs(spEmStep + spInc) <= spEmMaxSteps) {
          // increase/decrease spEmStep
          spEmStep += spInc;
          // set new font size for every tag inside "spEmStep"
          spEmFontSize = spEmStep * spEmStepWidth + spEmBasis;
          //get spArticleBody
          spEmBody = document.getElementById('wrapperAll');
          // set new fot size
          spEmBody.style.fontSize = spEmFontSize + "em";
          //spEmBody.style.lineHeight = spEmFontSize+0.54 + "em";
          // save value in cookie
          step = spEmStep+1;
        if (spInc == -1) {
         if (document.getElementById("fontinc")) {
          document.getElementById("fontdec").className= "footerFontsize1Active";
          document.getElementById("fontdef").className= "footerFontsize2";
          document.getElementById("fontinc").className= "footerFontsize3";
          createCookie("fontStepperValue",spInc,"1")
        }
      }
      if (spInc == 0) {
         if (document.getElementById("fontinc")) {
          document.getElementById("fontdec").className= "footerFontsize1";
          document.getElementById("fontdef").className= "footerFontsize2Active";
          document.getElementById("fontinc").className= "footerFontsize3";
          createCookie("fontStepperValue",spInc,"1")
        }
      }
      if (spInc == 1) {
         if (document.getElementById("fontinc")) {
          document.getElementById("fontdec").className= "footerFontsize1";
          document.getElementById("fontdef").className= "footerFontsize2";
          document.getElementById("fontinc").className= "footerFontsize3Active";
          createCookie("fontStepperValue",spInc,"1")
        }
      }

     }
}

function urlEncode(rawText){
      // this converts the rawText into x-www-form-urlencoded format (and space to "%20")
      var encoded = "";
      for(var n=0; n<rawText.length; n++) {
        var c=rawText.charCodeAt(n);
        // all chars in range 0-127 => 1byte   without (A-Z, a-z, 0-9, *, -, ., _)
        if (c<128) {
            if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c >= 48 && c <= 57) || (c==42) || (c==45) || (c==46) || (c==95))
                encoded += String.fromCharCode(c);
            else 
                encoded += '%' + c.toString(16);
        }
        // all chars in range 127 to 2047 => 2byte
        else if((c>127) && (c<2048)) {
          encoded += '%' + ((c>>6)|192).toString(16);
          encoded += '%' + ((c&63)|128).toString(16);
        }
        // all chars in range 2048 to 66536 => 3byte
        else {
          encoded += '%' + ((c>>12)|224).toString(16);
          encoded += '%' + (((c>>6)&63)|128).toString(16);
          encoded += '%' + ((c&63)|128).toString(16);
        }
      }
      return encoded;
}
	

function webQuery() {
     var url = '';
     var lang = '?lang=';
     var country = '&country=';
     var query = '&query=';
          if (document.forms.search.url.value != '') url = document.forms.search.url.value;
          if (document.forms.search.lang.value != '') lang += document.forms.search.lang.value;
          if (document.forms.search.country.value != '') country += document.forms.search.country.value;
          if (document.forms.search.query.value != '') query += urlEncode(document.forms.search.query.value);
     var searchAddress = url + lang + country + query;
     window.open(searchAddress,'q','width=564,height=528,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,menubar=no,resizable=no');
     return false;
} 



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"
		}
	]

};

function fixOldFirefox () {
  if (BrowserDetect) {
    if (BrowserDetect.browser == "Firefox")
      if (BrowserDetect.version <= "3.5") {
        var getHead = document.getElementsByTagName("HEAD");
        if (getHead[0]) {
          getHead[0].innerHTML += ('<link rel="stylesheet" href="/media/_tech/layout/css/special/cssfirefoxold.css" type="text/css" media="all" charset="UTF-8" />');
        }
    }
  }
}

