﻿/* -----------------------  Constants -------------------- */
var debug = true;
var pageInited = false;
var cookieLifeTime = 15*60*1000; //d+h+m+s+ms



//setting cookie
function setCookie(name, value) {
	deleteCookie(name);
	expires=new Date(new Date().getTime() + cookieLifeTime);
    document.cookie= name + "=" + escape(value) + "; expires="+expires.toGMTString()+"; path=/;";
	//window.location.reload();
}

function setCookieFromFlash(name, value, isreloadpage){
	setCookie(name, value);
	/*
	changed 06.01.09. >> to reload index after registration-finished.
	
	old start
	
	if (isreloadpage != undefined && isreloadpage == true) {
		reloadPage();
	}
	else {
		window.location.href = "member-data.html";
	}
	
	old end*/
	
	var registrationCookie = getCookie("registration");
	
	if (registrationCookie==null) {
		if (isreloadpage != undefined && isreloadpage == true) {
			reloadPage();
		}
		else {
			window.location.href = "member-data.html";
		}
	} else {
		if (isreloadpage != undefined && isreloadpage == true && registrationCookie != "1") {
			reloadPage();
		} else if (isreloadpage != undefined && isreloadpage == true && registrationCookie == "1") {
			window.location.href = "index.html";
		} else {
			window.location.href = "member-data.html";
		}
		
	}
}

function reloadPage(){
	window.location.reload();
}

//setting cookie
function setFollowUpCookie(name, value) {
	deleteCookie(name);
	expires=new Date(new Date().getTime() + cookieLifeTime);
    document.cookie= name + "=" + escape(value) + "; expires="+expires.toGMTString()+"; path=/;";
}

//deleting cookie
function deleteCookie(name) {
	  expires=new Date(new Date().getTime() - 1000);
    document.cookie= name + "='0'; expires="+expires.toGMTString()+"; path=/;";
}
//reading cookie
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
//checking status of login
function checkLogin() {
	  var sId = getCookie('sessionID');
	  var fUp = getCookie('followUp');
	  if(sId != "" && sId != null)
	  {
	  	//Cookie exists, logged in
	  	setVisible('loginContainer',false);
	  	setVisible('logoutContainer',true);
		
			//If FollowUp-Cookie exists, redirect to page
			/*//EDIT 09.07.2008 - No FollowUp-Cookie needed from this time on
			if(fUp != null && fUp != "") {
				var loc = location.href.split("/")
				if(loc[loc.length-1] != fUp+".html") {
					switch(fUp) {
						case "gewinnspiel":
							location.href = "gewinnspiel.html"
							break;
					}
				}
			}
			*/
	  }
	  else
	  {
	  	//no Cookie, logged out
	  	setVisible('loginContainer',true);
	  	setVisible('logoutContainer',false);
	  }
}

//checking status of login for member-data-html
function changeMemberData() {
	  var sId = getCookie('sessionID');
	  if(sId != "" && sId != null)
	  {
	  	//Cookie exists, logged in
	  	//show member change formular
	  	window.location.href = "member-data.html"; 
	  }
	  else
	  {
	  	//no Cookie, logged out
	  	setVisible('loginContainer',true);
	  	setVisible('logoutContainer',false);
	  	
	  }
}


//logout user / delete cookie
function logout() {

	 deleteCookie('sessionID');
	 window.location.reload();
	 
}

// set layer visible
function setVisible(obj,visible)
{
	obj = document.getElementById(obj);
	if(obj){
		
		if(visible)
		{
			obj.style.visibility = 'visible';
		}
		else
		{
			obj.style.visibility = 'hidden';
		}
	}
			
}

function setVisibleWithHeight(obj,visible,clientheight) {
	setVisible(obj,visible);
	obj = document.getElementById(obj);
	if(obj){
		var arrayPageSize = getPageSize();
		obj.style.height = (arrayPageSize[1] + 'px');
		obj.style.display = 'block';
	}

}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//Flash aus dem DIV-Layer entfernen
function unloadFlash(obj) {
	obj = document.getElementById(obj);
	if(obj){
		obj.innerHTML = "";
	}
}

function openLayer(layer) {	
	switch(layer){
		case "login" :	closeLayer("all");
						//Flash laden
						var Login = { movie:"library/flash/whitelabel/login_layer.swf", width:"700", height:"500", majorversion:"8", build:"0"};
						UFO.create(Login, "layer_flash", function() {
							setVisibleWithHeight('layer1',true,document.body.clientHeight); 
							setVisible('layer_flash',true);		   	   
						});
						break;
						
		case "wapportal" :	closeLayer("all");
						var Wapportal = { movie:"library/flash/handy/swf/handy_flirt_layer.swf", width:"646", height:"447", majorversion:"8", build:"0", flashvars:"sessionID="+getCookie("sessionID")};
						UFO.create(Wapportal, "wapportal_layer_flash", function() {
							setVisibleWithHeight('wapportal',true,document.body.clientHeight); 
							setVisible('wapportal_layer_flash',true);		   	   
						});
						break;
						
		case "world_wapportal" :	closeLayer("all");				
						var Wapportal = { movie:"library/flash/handy/swf/handy_world_layer.swf", width:"646", height:"447", majorversion:"8", build:"0", flashvars:"sessionID="+getCookie("sessionID")};
						UFO.create(Wapportal, "wapportal_layer_flash", function() {
							setVisibleWithHeight('wapportal',true,document.body.clientHeight); 
							setVisible('wapportal_layer_flash',true);		   	   
						});
						break;
						
		case "postcard" :	closeLayer("all");
						var Ecard = { movie:"library/flash/handy/swf/ecard_layer.swf", width:"630", height:"450", majorversion:"8", build:"0", flashvars:"sessionID="+getCookie("sessionID")};
						UFO.create(Ecard, "postcard_layer_flash", function() {
							setVisibleWithHeight('postcard',true,document.body.clientHeight); 
							setVisible('postcard_layer_flash',true);   
						});
						break;
						
		case "wetterwidget" :	closeLayer("all");
						var Widget = { movie:"library/flash/wetterwidget/wetterwidget_layer.swf", width:"646", height:"507", majorversion:"8", build:"0"};
						UFO.create(Widget, "wetterwidget_layer_flash", function() {
							setVisibleWithHeight('wetterwidget_dl',true,document.body.clientHeight); 
							setVisible('wetterwidget_layer_flash',true);		   	   
						});
						break;
						
		case "videocall" :	closeLayer("all");
						var Widget = { movie:"library/flash/handy/swf/videocall_layer.swf", width:"646", height:"447", majorversion:"8", build:"0", flashvars:"sessionID="+getCookie("sessionID")};
						UFO.create(Widget, "videocall_layer_flash", function() {
							setVisibleWithHeight('videocall',true,document.body.clientHeight); 
							setVisible('videocall_layer_flash',true);		   	   
						});
						break;
	}
}

function closeLayer(layer) {
	switch(layer){
		case "login" :	setVisibleWithHeight('layer1',false,document.body.clientHeight); 
						setVisible('layer_flash',false);
						unloadFlash('layer_flash');	
						break;
		case "wapportal" :	setVisibleWithHeight('wapportal',false,document.body.clientHeight); 
						setVisible('wapportal_layer_flash',false);
						unloadFlash('wapportal_layer_flash');	
						break;
		case "postcard" :	setVisibleWithHeight('postcard',false,document.body.clientHeight); 
						setVisible('postcard_layer_flash',false);
						unloadFlash('postcard_layer_flash');	
						break;
		case "wetterwidget" :	setVisibleWithHeight('wetterwidget_dl',false,document.body.clientHeight); 
						setVisible('wetterwidget_layer_flash',false);
						unloadFlash('wetterwidget_layer_flash');		
						break;						
		case "videocall" :	setVisibleWithHeight('videocall',false,document.body.clientHeight); 
						setVisible('videocall_layer_flash',false);
						unloadFlash('videocall_layer_flash');		
						break;
		case "all":		closeLayer('login');
						closeLayer('wapportal');
						closeLayer('postcard');
						closeLayer('wetterwidget');	
						closeLayer('videocall');	
	}
}

function switchTabs(openTab,closeTab) {
	obj = document.getElementById(openTab);		
	obj.style.display = 'block';
	obj2 = document.getElementById(closeTab);
	obj2.style.display = 'none';
						
}

//Helper
function getURLParam(strParamName){

	var strReturn = "";	
	var strHref = window.location.href;	
	
	if ( strHref.indexOf("?") > -1 ){	
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();	
		var aQueryString = strQueryString.split("&");	
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){	
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){	
				var aParam = aQueryString[iParam].split("=");				
				strReturn = aParam[1];				
				break;	
			}	
		}	
	}	
	return strReturn;
	
}
