var base_url = 'http://cncsite.nl/';
var myfield = 'sidebarlogin';
var debug_var = true;
var debug_console = true;
var debug_what = '';
var e = e;
var header_timeout = null;

function SelectLanguage(language) {
	
	var form = language;
	
	document.forms[form].submit();	
}

function addMenuFunctions()
{
	var ulMenu = document.getElementById('menuTop');
	var liItems = ulMenu.getElementsByTagName('li');
		
	for(var i=0; i<liItems.length; i++)
	{
		if(liItems[i].childNodes.length > 1)
		{
			addEvent(liItems[i], 'click', _menuToggle);
			var ulItems = liItems[i].getElementsByTagName('ul');
			
			for(var j=0; j<ulItems.length; j++)
			{
				ulItems[j].className='hide';
			}
		}
	}	
}

function _menuToggle(ev, obj)
{
	var ul = this.getElementsByTagName('ul');

	if(ul[0].className == 'hide')
	{
		ul[0].className='show';
	}
	else
	{
		ul[0].className='hide';
	}
	
	calculateSize();
	
	stopSomething(ev);
}

function addStyleSheetFunctions()
{
	var printCss = document.getElementById('print');
	var previewCss = document.getElementById('preview');
	var smallCss = document.getElementById('small');
	var normalCss = document.getElementById('normal');
	var bigCss = document.getElementById('big');
	var inverseCheckboxes = document.getElementById('inversecheckbox');
	var sign_guestbook = document.getElementById('sign_guestbook');
	var objClose = document.getElementById('closebutton');
	var header = document.getElementById('header_image');
	var albumlist = document.getElementById('albumlist');
	
	if(printCss != null)
	{
		// attatch event for printer css
		addEvent(printCss, 'click', _addprintFunctions);
	}
	
	if(previewCss)
	{
		// attatch event for printer css
		addEvent(previewCss, 'click', _addpreviewFunctions);
	}
	
	if(smallCss != null)
	{
		// attatch event for small css
		addEvent(smallCss, 'click', _addsmallFunctions);
	}
	
	if(normalCss != null)
	{
		// attatch event for normal css
		addEvent(normalCss, 'click', _addnormalFunctions);
	}
	
	if(bigCss != null)
	{
		// attatch event for big css
		addEvent(bigCss, 'click', _addbigFunctions);
	}
	
	if(inverseCheckboxes != null)
	{
		// attach inversecheckboxes
		addEvent(inverseCheckboxes, 'click', inverse_checkboxes);
	}
	
	if(sign_guestbook != null)
	{
		addEvent(sign_guestbook, 'click', _sign_guestbook);
	}
	
	if(objClose != null)
	{
		addEvent(objClose, 'click', _closeSign);
	}
	
/*	if(header != null)
	{
		add_header();
	}
	*/
	if(albumlist != null)
	{
		//add_albumlist_functions();
	}
	
	addCustomFunctions();
}

function addCustomFunctions()
{
	/* Change height of the different columns */
	var leftcolumn = document.getElementById('leftcolumn');
	var rightcolumn = document.getElementById('rightcolumn');
	var height = leftcolumn.clientHeight;
	
	if(leftcolumn.clientHeight > rightcolumn.clientHeight)
	{
		rightcolumn.style.height = height+'px';
		leftcolumn.style.height = height-30+'px';
	}
	else
	{
		leftcolumn.style.height = rightcolumn.clientHeight-60+'px';
	}

	/* Submit sidebar form on keypress */
	var password = document.getElementById('password');
	
	if(password != null)
	{
		addEvent(password, 'keypress', submitenter);
	}
}

function _addprintFunctions(ev, obj)
{
	var location = self.location
	location = urlencode(location);
	window.open(base_url+'44/'+location,'mywin','width=800,height=600,toolbar=0,resizable=1,scrollbars=1,menubar=1');
}

function _addpreviewFunctions(ev, obj)
{
	var location = self.location
	location = urlencode(location);
	window.open(base_url+'44/'+location+'/preview','mywin','width=900,height=600,toolbar=0,resizable=1,scrollbars=1,menubar=1');
}

function urlencode(str) {
str = escape(str);
str = str.replace(/\+/gi, '%2B');
str = str.replace(/%20/gi, '+');
str = str.replace(/\*/gi, '%2A');
str = str.replace(/\//gi, '%2F');
str = str.replace(/@/gi, '%40');
return str;
}

function _addsmallFunctions(ev, obj)
{
	var input = document.getElementById('css_file');
	input.value='small';
	document.cssfile.submit();
}

function _addnormalFunctions(ev, obj)
{
	var input = document.getElementById('css_file');
	input.value='normal';
	document.cssfile.submit();
}

function _addbigFunctions(ev, obj)
{
	var input = document.getElementById('css_file');
	input.value='big';
	document.cssfile.submit();
}

function stopSomething(e)
{
	if(!e) var e = window.event;
	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
}

function openUp(page_id)
{
	/*alert(page_id);*/
	var menu = document.getElementById('menu');	
	var aItems = menu.getElementsByTagName('a');
	
	for(var i=0; i<aItems.length; i++)
	{
		var re = new RegExp('(/' + escape(page_id) + ').*$', 'i'); 
		if(re.test(aItems[i]))
		{
			var item=aItems[i]
			while(item.id != 'menuTop')
			{
				item = item.parentNode
				
				if(item.nodeName=='UL')
				{
					item.className='show';
				}
			}
		}
	}
	
	calculateSize();
	
	return;
}

function add_albumlist_functions()
{
	var contentDiv = document.getElementById("leftcolumn");
	
	contentDiv.style.height = contentDiv.clientHeight+'px';
}

function calculateSize()
{
	var menuDiv = document.getElementById("rightcolumn");
	var contentDiv = document.getElementById("leftcolumn");
	var height;
	
	if(menuDiv.clientHeight == 515)
	{
		height = 510;
	}
	else if(menuDiv.clientHeight == 383)
	{
		height = 378;
	}
	else if(menuDiv.clientHeight == 647)
	{
		height = 642;
	}
	else
	{
		height = 510;
	}
	
	if(menuDiv.clientHeight-15 > height)
	{
		contentDiv.style.height=menuDiv.clientHeight+'px';
	}
	else
	{
		contentDiv.style.height=height+'px';
	}
}

function stretch_page()
{
	/* Hier moet wat komen om te zorgen dat de pagina altijd groot genoeg is :P */
	
}

function session_check()
{
	makeRequest(base_url+'include/functions/session_time.fnc.php/get_session_time');
}

function makeRequest(url) {
    var httpRequest;

    if (window.XMLHttpRequest) 
    { 														// Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) 
        {
            httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) 
   	{ 														// IE
        try 
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
        	try 
        	{
            	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
   	}

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    httpRequest.onreadystatechange = function() 
    { 
    	alertContents(httpRequest); 
   	};
    
   	httpRequest.open('GET', url, true);
    httpRequest.send(null);

}

function makeRequest_nocallback(url) {
    var httpRequest;

    if (window.XMLHttpRequest) 
    { 														// Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) 
        {
            httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) 
   	{ 														// IE
        try 
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
        	try 
        	{
            	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
   	}

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
        
   	httpRequest.open('GET', url, true);
    httpRequest.send(null);

}

function alertContents(httpRequest) {

    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
        	var items = httpRequest.responseXML.getElementsByTagName('time_remaining');
			var itCnt = items.length;
			var time = items[0].getAttribute("label");
			
			time = parseInt(time);
			
			if(time <= 90 && time != 0)						/** When session is less then 1.5 minutes, set interval to 1 second (timer on warning) */
			{
        		clearInterval(timer);
        		timer = window.setInterval("session_check()", 1000);
        		
        		var grayout = document.getElementById('grayout');
        		var dialog = document.getElementById('dialog');
        		var body = document.getElementById('body');
        		var timetext = '';
        		timesec = time;
        		
        		if(time >= 60)
        		{
        			timetext = '1 minuut ';
        			timesec = time - 60;
        		}
        		        		
        		timetext += parseInt(timesec) + ' seconden';
        		
        		grayout.style.display = 'block';
        		if(time == 1)
        		{
        			set_dialog('Uw sessie is vervallen', 'Let op! Uw sessie is vervallen.<br>Dit houdt in dat u geen pagina\'s voor leden meer kunt bekijken', '', '',time);
        		}
        		else
        		{
        			set_dialog('Uw sessie gaat vervallen', 'Let op! Uw sessie gaat vervallen.<br>Dit houdt in dat u geen pagina\'s voor leden meer kunt bekijken<br><br>Uw sessie verloopt in '+timetext+'. <br><br>Klik op de knop "sessie behouden" om uw sessie te verlengen', '', '',time);
        		}
        		
        		dialog.style.display = 'block';				
        		set_to_full_window();
        		body.style.overflow = 'hidden';
        		
			}
			
			if(time == 0)						/** Stop the timer if session does not excist or expired */
			{
				clearInterval(timer);
			}
        	
        } else {
           // alert('There was a problem with the request.'); /* don't show the error */
        }
    }
}

function set_to_full_window()
{
	var grayoutdiv = document.getElementById('grayout');
	var dialogdiv = document.getElementById('dialog');
	var dialogwidth = dialogdiv.clientWidth;
	var dialogheight = dialogdiv.clientHeight;
	var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var windowwidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;

	var halfWidth = windowwidth / 2;
	var halfHeight = windowHeight / 2;
	var halfdialogwidth = dialogwidth / 2;
	var halfdialogheight = dialogheight / 2;
	var offsetl = halfWidth - halfdialogwidth;
	var offseth = halfHeight - halfdialogheight;
	
	grayoutdiv.style.height = windowHeight;	
	
	dialogdiv.style.left = offsetl + 'px';
	dialogdiv.style.top = offseth + 'px';
}

function set_dialog(header,content,action,footer,time)
{
	var headerspan = document.getElementById('dialog_header_text');
	var footerspan = document.getElementById('dialog_footer_text');
	var contentdiv = document.getElementById('dialog_content_text');
	var contentdivp = contentdiv.getElementsByTagName('p');
					
	if(contentdivp[0] != null)
	{
		contentdiv.removeChild(contentdivp[0]);
	}
	
	var content = content.split("<br>");
	var divcontent = document.createElement('p');
	
	for(var i=0; i<content.length; i++)
	{
		divcontent.appendChild(document.createTextNode(content[i]));
		divcontent.appendChild(document.createElement('br'));
	}
	
	if(parseInt(time) != 1)
	{
		var image = document.createElement('img');
		image.src = '/include/images/Sessie_behouden.png';
		image.id = 'session_keep_image';
		divcontent.appendChild(image);
	}
	else
	{
		var image = document.createElement('img');
		image.src = '/include/images/ok.png';
		image.id = 'ok_image';
		divcontent.appendChild(image);
	}
	
	headerspan.firstChild.nodeValue = header;
	footerspan.firstChild.nodeValue = footer;
	
	contentdiv.appendChild(divcontent);
	
	if(parseInt(time) != 1)
	{
		var button = document.getElementById('session_keep_image');
		addEvent(button, 'click', _keep_session_click);	
	}
	else
	{
		var button = document.getElementById('ok_image');
		addEvent(button, 'click', _ok_click);
	}
}

function _keep_session_click()
{
	var grayoutdiv = document.getElementById('grayout');
	var dialogdiv = document.getElementById('dialog');
	var body = document.getElementById('body');
	
	clearInterval(timer);
    timer = window.setInterval("session_check()", 30000);
	makeRequest_nocallback('include/functions/session_time.fnc.php/add_session_time');
	grayoutdiv.style.display = 'none';
	dialogdiv.style.display = 'none';
	body.style.overflow = 'auto';
}

function _ok_click()
{
	var grayoutdiv = document.getElementById('grayout');
	var dialogdiv = document.getElementById('dialog');
	var body = document.getElementById('body');
	
	clearInterval(timer);
    timer = window.setInterval("session_check()", 30000);
    grayoutdiv.style.display = 'none';
	dialogdiv.style.display = 'none';
	body.style.overflow = 'auto';
}



function submitenter(ev, obj)
{
	var keycode;
	if (window.event) 
	{
		keycode = window.event.keyCode;
	}
	else if (ev) 
	{
		keycode = ev.which;
	}
	else 
	{
		return true;
	}
	
	if (keycode == 13)
   	{
   		document[myfield].submit();
	   	return false;
   	}
	else
	{
		return true;
	}
}

function inverse_checkboxes()
{
	var inputs = document.getElementsByTagName('input');

	for(var i=0; i<inputs.length; i++)
	{
		if(inputs[i].type == 'checkbox')
		{
			if(inputs[i].checked == true)
			{
				inputs[i].checked = false;
			}
			else
			{
				inputs[i].checked = true;
			}
		}
	}
}

function _sign_guestbook()
{
	var objOverlay = document.getElementById('guestbook_sign_grayout');
	var objWindow = document.getElementById('guestbook_sign_window');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	objOverlay.style.zIndex = '90';	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	/* Position the window */
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 300) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - 400) / 2);
	
	objWindow.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objWindow.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	objWindow.style.zIndex = '100';	
	objWindow.style.display = 'block';	
}

function _closeSign()
{	
	var objOverlay = document.getElementById('guestbook_sign_grayout');
	var objWindow = document.getElementById('guestbook_sign_window');
		
	objOverlay.style.display = 'none';
	objWindow.style.display = 'none';
}

function addSmiley(emoticon,veld)
{
	var veld;
	var emoticon;
	
	document.getElementById(veld).value+=emoticon+' ';
	document.getElementById(veld).focus();
}



function debug(debug_what)
{
	if(debug_var)
	{
		if(BrowserDetect.browser == 'Firefox')
		{
			if(debug_console)
			{
				console.log(debug_what);
			}
			else
			{
				alert('Debug: '+debug_what);
			}
		}
		else
		{
			alert('Debug: '+debug_what);
		}
	}
}


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: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		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.platform,
		subString: "Linux",
		identity: "Linux"
	}
]

};

BrowserDetect.init();
