
/************* WR *************/
var WR={};

/************* Form *************/
WR.Form={
	clearFieldValue:function(Tag){
		$(Tag).val("");
	},
	isDefaultTextOrEmpty:function(Form,Fieldname,DefaultText){
		var FieldValue=$(Form).find("input[name="+Fieldname+"]").val();
		if ((FieldValue==="")||(FieldValue===DefaultText)){
			return true;
		}
		return false;
	}
};

/************* Ajax *************/
WR.Ajax = {
	Options:{
		Cache:true
	},
	LOADING:"<div class='ajax-loading'><img src='../files/website-layout/$file/ajax-loader.gif' width='31' height='31' alt='"+ttw_labels.ajax.loading+"'></div>",
	oSource:this,
	get: function(sSourceUrl, sType, sID, fCallBack){
		this.oSource.handleRequest = fCallBack;
		var loc = window.location.href;
		if(loc.indexOf("wirtschaftsrat.de")!= -1)
		{
			if(loc.indexOf("https://")!= -1)
				sSourceUrl=sSourceUrl.replace("../","https://www.wirtschaftsrat.de/wirtschaftsrat.nsf/")
			else
				sSourceUrl=sSourceUrl.replace("../","http://www.wirtschaftsrat.de/wirtschaftsrat.nsf/")
		}
		
		//alert("L:"+loc+"DATA:"+sSourceUrl);
		$("#"+sID).html(this.LOADING);
		$.ajax({
			type:"GET",
			url:sSourceUrl,
			cache:WR.Ajax.Options.Cache,
			timeout:60000,
			success: function(Data) {
					WR.Ajax.oSource.handleRequest(sID,Data);
			},
			error:function (XMLHttpRequest, textStatus, errorThrown){
				$("#"+sID).html("<div class=\"ajax-error\">"+ttw_labels.ajax.error+"</div>");
			}
		});
	},
	getHTML:function(Config){
		var Url=Config.Url;
		var HTMLTagID=Config.ID;		
		var Config_fCallBack=Config.CallBackFunction;
		if (Config_fCallBack===""){
			fCallBack=Config_fCallBack;				
		}
		else{
			fCallBack=WR.DOM.setContentByID;
		}
		var OptionCache=Config.Cache;
		if (OptionCache!==true){
			WR.Ajax.Options.Cache=OptionCache;				
		}
		this.get(Url, "html", HTMLTagID, fCallBack);
	}
};

/****************** DOM *********************/
WR.DOM = {
	setContentByID: function(sID, sContent){
		if ($("#"+sID).length>0) {
			$("#"+sID).html(sContent);
		}
	},
	appendContentByID: function(sID, sContent){
		if ($("#"+sID).length>0) {
			$("#"+sID).append(sContent);
		}
	}
};

/************* Messagebox *************/
WR.MB={
	show:function(Config){
		var BoxText=Config.Text;
		$.colorbox.init();
		$.colorbox({
			html:"<div class='colorbox-text'>"+BoxText+"</div>",
			opacity:0.7,
			innerWidth:300,
			initialWidth:300,
			innerHeight:100,
			initialHeight:100,
			close:"[ X ]"
		});
	}
};

/************* Quicksearch *************/
WR.Quicksearch={
	DefaultTextSearch:"Suchbegriff",
	validate:function(Form){
		if (WR.Form.isDefaultTextOrEmpty(Form,"qst",this.DefaultTextSearch)){
			WR.MB.show({
				Text:ttw_labels.box.quicksearch.errorsearchterm
			});
			return false;
		}
		return true;			
	}
};

/************* Login *************/
WR.Login = {
	DefaultTextUsername:ttw_labels.header.login.username,
	DefaultTextPassword:ttw_labels.header.login.password,
	validate: function(Form){
		if (WR.Form.isDefaultTextOrEmpty(Form,"Username",this.DefaultTextUsername)){
			WR.MB.show({
				Text:ttw_labels.box.login.errorusername
			});
			return false;
		}
		else if(WR.Form.isDefaultTextOrEmpty(Form,"Password",this.DefaultTextPassword)){
			WR.MB.show({
				Text:ttw_labels.box.login.errorpassword
			});
			return false;			
		}
		return true;
	},
	logout:function(URL){
		location.href=TTW.dbpath+"?logout&RedirectTo="+URL;
	}
};

/************* Change Password*************/
WR.ChangePW = {
	validate: function(Form){
		if (WR.Form.isDefaultTextOrEmpty(Form,"HTTPPassword","")){
			WR.MB.show({
				Text:ttw_labels.box.changepw.erroroldpw
			});
			return false;
		}
		else if(WR.Form.isDefaultTextOrEmpty(Form,"HTTPPasswordNew","")){
			WR.MB.show({
				Text:ttw_labels.box.changepw.errornewpw
			});
			return false;			
		}
                       else if(WR.Form.isDefaultTextOrEmpty(Form,"HTTPPasswordNewCheck","")){
			WR.MB.show({
				Text:ttw_labels.box.changepw.errorcheckpw
			});
			return false;			
		}
		return true;
	}
};

/************* Forgot Password*************/
WR.ForgotPW = {
	validate: function(Form){
		if (WR.Form.isDefaultTextOrEmpty(Form,"uid","")){
			WR.MB.show({
				Text:ttw_labels.box.forgotpw.errorusername
			});
			return false;
		}
		return true;
	}
};

/************* Tabs *************/
WR.Tabs={
	init:function(TabsID){
		$("#"+TabsID+" .tab a").bind('click',WR.Tabs.open);
	},
	resetTabNavigation:function(CurrentLinkTag){
		var ulTags=$(CurrentLinkTag).parents(".ul");
		$(ulTags).find(".tab-active").removeClass("tab-active");
	},
	setTabNavigationActiveTab:function(CurrentLinkTag){
		WR.Tabs.resetTabNavigation(CurrentLinkTag);
		$(CurrentLinkTag).parent().addClass("tab-active");
	},
	open:function(){
		WR.Tabs.setTabNavigationActiveTab(this);
		var Config={};
		Config.Url=$(this).attr("href");
		Config.ID=$(this).parents(".tabs-header").next(".tabs-content").find(".tabs-content-inner").attr("id");
		WR.Ajax.getHTML(Config);
		return false;
	}
};

/************* Homepage *************/
WR.Homepage={};

/************* Homepage Teaser *************/
WR.Homepage.Teaser = {
	CurrentNumber:1,
            MaximalNumber:1,
            Interval:null,
            IntervalSpeed:5000,
	setNavigation:function(){
		$(".teaser-nav-counter-current").html(this.CurrentNumber);		
	},
	next:function(){
		if (this.CurrentNumber===this.MaximalNumber){
			this.CurrentNumber = 0;
		}
		this.CurrentNumber++;
		this.open();
		this.setNavigation();
                        this.resetslide();
	},
	prev:function(){
		if (this.CurrentNumber===1){
			this.CurrentNumber = this.MaximalNumber + 1;
		}
		this.CurrentNumber--;
		this.open();
		this.setNavigation();
                       this.resetslide();		
	},
	open:function(){
                       $(".teaser-container-inner").hide();
                       $("#teaser"+this.CurrentNumber).show();
	},
           autoslide:function(){
                       if (this.MaximalNumber > 1)
                       {
                            if (this.CurrentNumber < this.MaximalNumber)
                            {
                                  this.CurrentNumber++;
                                  this.open();
                                  this.setNavigation();
                            }
                            else
                            {
                                  this.CurrentNumber = 1;
                                  this.open();
                                  this.setNavigation();
                            }
                       }
           },
           resetslide:function(){
                      if (this.Interval != null)
                      {
                         window.clearInterval(this.Interval);
                         this.Interval = window.setInterval('WR.Homepage.Teaser.autoslide()', this.IntervalSpeed);
                      }
           }      
           
};

/************* Log *************/
WR.Log = {
    setQuery: function(oList){
        var sReturn = "";
        for (var sEntry in oList) {
            sReturn = sReturn + "&" + sEntry + "=" + oList[sEntry];
        }
        return sReturn;
    },
    createDocument: function handleerror(msg, url, line){
        jQuery.post(TTW.dbpath + '/LogJSError?openAgent', {
            'msg': msg,
            'url': url,
            'line': line,
            'useragent': navigator.userAgent,
            'appcodename': navigator.appCodeName,
            'appname': navigator.appName,
            'appversion': navigator.appVersion,
            'applanguage': navigator.language,
            'platform': navigator.platform,
            'cookieenabled': navigator.cookieEnabled
        });
        return true;
    }
};

/************* UI *************/
WR.UI = {};

/************* UI Datepicker *************/
WR.UI.Datepicker = {
	init:function(jQuerySelector){
		var dayNamesList=ttw_labels.date.weeknames;
		var dayNamesShortList=ttw_labels.date.weeknamesshort;
		var monthNamesList=ttw_labels.date.monthames;
		$(jQuerySelector).datepicker({
			 dateFormat: "dd.mm.yy",
			 dayNames:dayNamesList.split(","),
			 dayNamesShort:dayNamesShortList.split(","),
			 dayNamesMin:dayNamesShortList.split(","),
			 monthNames:monthNamesList.split(","),
			 nextText:ttw_labels.datepicker.next,
			 prevText:ttw_labels.datepicker.prev,
			 firstDay: 1
		});		
	}
};

/************* Print *************/
WR.Print={
	openWindow:function(){
		var URL="../id/"+TTW.pageid+"?Open&setprintmode=1";
		var Properties="resizable=yes, location=no, menubar=yes, toolbar=no, scrollbars=yes, status=no, dependent=yes, width=650,height=600";
		var WindowPrint=window.open(URL,'windowprint',Properties);
		return false;
	},
	setPageElementsByID:function(aList){
		var dSource=window.opener.document;
		if (dSource){
			for(var i=0;i<=aList.length;i++){
				var Entry=aList[i];
				var IDSource=dSource.getElementById(Entry);
				if (IDSource){
					$("#"+Entry).html(IDSource.innerHTML);					
				}
			}			
		}
 	}	
};

/************* Cookie *************/
WR.Cookie={
	create:function(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=/";
	},
	read:function(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;
	}
};

/************* Switch to mobile website (m.wirtschaftsrat.de) *************/
WR.MobileSwitch={
	COOKIE_NAME:'MobileSwitch',	
	COOKIE_NAME_HIDE:'MobileSwitchHide',
	getBoxAnswer:function(){
		 return window.confirm('Möchten Sie zur mobilen Webseite wechseln?');
	},
	saveCookieBoxAnswer:function(BoxAnswer){
		$.cookie(this.COOKIE_NAME, String(BoxAnswer), {path:'/',expires: 30});
	},
	getCookieBoxAnswer:function(){
		return $.cookie(this.COOKIE_NAME);
	},
	doesCookieNotExist:function(){
		return $.cookie(this.COOKIE_NAME)===null;
	},
	showMessageBox:function(TargetPageName){
		var BoxAnswer=this.getBoxAnswer();
		if (BoxAnswer!==""){
			this.saveCookieBoxAnswer(BoxAnswer);
		}
		if (BoxAnswer===true){
			this.forwardToMobilePage(TargetPageName);	
		}
	},
	setCookieNoBoxAnswer:function(){
		$.cookie(this.COOKIE_NAME_HIDE,"1", {path:'/'});
	},
	forwardToMobilePage:function(TargetPageName){
		var pagename="";
		if (TargetPageName!==""){
			pagename=TargetPageName;
		}
		else{
			pagename="home-de-mobile";
		}
		var host=location.host;
		var hostName="";
		if (host.indexOf("wirtschaftsrat")!==-1){
			hostName="//m.wirtschaftsrat.de";
		}
		else{
			hostName=location.host;
		}
		window.location.href="//"+hostName+TTW.dbpath+"/id/"+pagename+"?open&mobile=1";
	},
	init:function(TargetPageName){
		if (this.doesCookieNotExist()){
			this.showMessageBox(TargetPageName);	
		}
		else if(this.getCookieBoxAnswer()==="true"){
			this.forwardToMobilePage(TargetPageName);
		}
	}
};

PDF={
	show:function(link){
                        $.ajax({
  url: link,
  
  success: function(data){
    $.colorbox.init();
		$.colorbox({
			html: data,
			opacity:0.7,
			innerWidth:500,
			initialWidth:500,
			innerHeight:300,
			initialHeight:300,
			close:"[ X ]"                                  
		});
	}});
return false;
}    		
		
};


$(document).ready(function(){
$(".dropdown-btn").on("click",function(){$(this).next(".ul").toggle();});
});
