var ktgScript={

	current_elem:0,
	tooltip_open:0,
	intvall:0,
	
	
/**************************************************************************************************
** Initialize Scripts - is this a browser that understands DOM?
**************************************************************************************************/
	scriptInit:function() {
		if (!document.getElementById) {
			return false;
		}else{
			return true;
		}
	},

/************************************************************************************************
** Set up Event Listener - the script that allows us to use the addEvent call below
*************************************************************************************************/

	addEvent:function(elm, evType, fn, useCapture) {
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
/************************************************************************************************
** Log for debug
*************************************************************************************************/
	
	log:function(string){
		if (typeof(console)=="undefined"){
        	//alert(string);
		}else{
			console.log(string);
	    }
	},
	
/***********************************************************************************************
** helper functions:
** $: get object
** getElementsByClass: does what the function name says
** changeClass: add, remove, swap, check classes for an object
************************************************************************************************/

	$:function() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string'){
				if(document.getElementById(element)){
					element = document.getElementById(element);
				}
			}
			if (arguments.length == 1){
				if (typeof element == 'object'){
					return element;
				}else{
					return false;
				}
			}
			if (typeof element == 'object'){
				elements.push(element);
			}
		}
		return elements;
	},
	
	getElementsByClass:function(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	},
	
/*****************************************************************************************
** This function takes four parameters:
** a defines the action you want the function to perform.
** O the object in question.
** c1 the name of the first class
** c2 the name of the second class
**
** Possible actions are:
** swap replaces class c1 with class c2 in object o.
** add adds class c1 to the object o.
** remove removes class c1 from the object o.
** check tests if class c1 is already applied to object o and returns true or false.
******************************************************************************************/
	
	changeClass:function(a,o,c1,c2){
		switch(a){
			case 'swap':
				o.className=!ktgScript.changeClass('check',o,c1)?o.className.replace(c2,c1) : o.className.replace(c1,c2);
			break;
			case 'add':
				if (!ktgScript.changeClass('check',o,c1)){o.className+=o.className ? ' ' + c1 : c1;}
			break;
			case 'remove':
				var rep=o.className.match(' ' + c1) ? ' ' + c1 : c1;
				o.className = o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp('\\b' + c1 + '\\b').test(o.className)
			break;
		}
	},

/********************************************************************************
** take url, extract get parameters to put together iframe url
********************************************************************************/
	receiveGetString:function(){
		var query = self.location.search;
		var href = document.location.href;
		ktgScript.isFamily = (href.indexOf("/familie/")>-1);
		//ktgScript.log("isFamily: " + ktgScript.isFamily);
		var iframeContainer = ktgScript.$('content');
		
		newIframe=document.createElement('iframe');
		newIframe.setAttribute('id','myframe');
		newIframe.setAttribute('class','resizable');
		newIframe.setAttribute('width','100%');
		newIframe.setAttribute('height','1000');
		newIframe.setAttribute('scrolling','no');
		newIframe.setAttribute('frameborder','0');
		newIframe.setAttribute('style','overflow: hidden; border: 0px;');
		newIframe.setAttribute('allowtransparency','true');
		
		var iframeObj = ktgScript.$('myframe');
		sammlung = new Array();
		//ktgScript.log("hasTarifSelection: " + window.hasTarifSelection);
		if (window.hasTarifSelection == true && query == ''){
			//ktgScript.log("kein OAM");
		}else{
			if (query != ''){
				query = query.substr(1, query.length - 1);
				query = query.replace(/%26/,'&');
				teile = query.split('&');
				for (i = 0; i < teile.length; i++){
					teile[i] = teile[i].split('=');
					window[teile[i][0]] = teile[i][1];
					//ktgScript.log("Name: " + teile[i][0]);
					//ktgScript.log("Wert: " +window[teile[i][0]]);
					//ktgScript.log("test: " + window['appid']);
				}
				if (teile[0][0] != "tarifWahl"){ // parameter is nicht tarifWahl, also wird OAM mit parametern aufgerufen
					//ktgScript.log("document.location.href: " + document.location.href);
					window.iframeUrl = "/oam/" + tarif + appid + "/antragsteller.do;jsessionid="+session;
					//ktgScript.log("test: " + iframeUrl);
				}else{
					//  parameter tarifWahl gefunden, also wird regularer Iframe für ARE oder AVF aufgerufen
					if (teile[0][1] == "AVF") window.iframeUrl = "/oam?appid=10205&ta=ARE&keyword=auslandsreiseversicherung&rendernavi=false";
					else window.iframeUrl = "/beitragsrechner?template=start.jsp&start_template=kt.jsp&appid=114";
					//ktgScript.log("test: " + iframeUrl);
				}
				
			}else{
				if (ktgScript.isFamily){ // show AVF
					window.iframeUrl = "/oam?appid=10210&ta=AVF&keyword=auslandsreiseversicherung&rendernavi=false";
				}else { //show ARE
					window.iframeUrl = "/beitragsrechner?template=start.jsp&start_template=kt.jsp&appid=114";
				}
			}
			iframeObj.src = iframeUrl;
		//ktgScript.log(document.location.href);
		}
		
		//newIframe.setAttribute('src',iframeUrl);
		//iframeContainer.appendChild(newIframe);
		
		
	},
	
	checkSSL:function(){
		var protocol = document.location.protocol;
		if (protocol.indexOf('https:') == -1 && protocol.indexOf('http:') >-1 ){
			var pageLocation = document.location.href;
			secureUrl = pageLocation.replace('http:','https:');
			document.location.href=secureUrl;
		}
	},
/****************************************************************************
** open or close tooltip layer
****************************************************************************/
	
	open_tooltip:function (elem){
		/*console.log("open_tooltip: " + elem);*/
		ktgScript.changeClass('add',elem.parentNode,'active');
	},

	close_tooltip:function (elem){	
		/*console.log("close_tooltip: " + elem);*/
		ktgScript.changeClass('remove',ktgScript.current_elem.parentNode,'active');
		ktgScript.tooltip_open = 0;	
	},
	
/****************************************************************************
** init tooltips 
** mouseover eventhandlers for glossar and faq links
****************************************************************************/
	initTooltips:function(){
		tooltip_links = ktgScript.getElementsByClass("tooltip");				
		/*console.log("tooltip_links: " + tooltip_links);*/
		for(var i=0;i<tooltip_links.length;i++){
			tooltip_links[i].onclick= function(){												
				var self = this;
				/*console.log("self: " + self.id);*/
				if (ktgScript.current_elem != 0 && self != ktgScript.current_elem){
					clearTimeout(ktgScript.intvall);
					ktgScript.close_tooltip(ktgScript.current_elem);
				}
				if (ktgScript.tooltip_open == 0 || ktgScript.current_elem != self ){
					ktgScript.current_elem = self;	
					ktgScript.tooltip_open = 1;
					ktgScript.open_tooltip(ktgScript.current_elem);
				}
				return false;
			}
				
			tooltip_links[i].onmouseout= function(){				
				var self = this;
				if (ktgScript.tooltip_open == 1){
					ktgScript.intvall = setTimeout("ktgScript.close_tooltip(self)",1000);
				}				
			}
		}
		
		ktgScript.$('glossar_content').onmouseover = ktgScript.$('faq_content').onmouseover = function(){				
			ktgScript.tooltip_open = 1;
			clearTimeout(ktgScript.intvall);
		}
		
		ktgScript.$('glossar_content').onmouseout = ktgScript.$('faq_content').onmouseout = function(){		
			var self = this;
			if (ktgScript.tooltip_open == 1){
				ktgScript.intvall = setTimeout("ktgScript.close_tooltip(self)",1000);					
			}
		}
	},
	
/**************************************************************************
**	main init function
** add functions you want to execute on load
***************************************************************************/

	init:function(){
		if(ktgScript.scriptInit){
			if(ktgScript.$('glossar_content')) ktgScript.initTooltips();
			//ktgScript.receiveGetString();
			ktgScript.checkSSL();
		}
	}
}
ktgScript.addEvent(window, 'load', ktgScript.init, false);