var init = function() {
	if(arguments.callee.done) return;
	arguments.callee.done = true;
	
	set_cookie();
	var logged_in = check_cookie();
	if (logged_in == 'yes') {
		make_button('logout_button', '/logout.html');
	} else {
		var body = document.getElementsByTagName('body')[0];
		var section = body.className;
		var subwrapper = document.getElementById('subwrapper');
		if(section=="explore") {
			subwrapper.style.backgroundImage = 'url(/images/page_top_bg_explore_out_2007.png)';
		} else if (section=="logistics") {
			subwrapper.style.backgroundImage = 'url(/images/page_top_bg_logistics_out_2007.png)';
		} else if (section=="entry") {
			subwrapper.style.backgroundImage = 'url(/images/page_top_bg_entry_out_2007.png)';
		} else {
			subwrapper.style.backgroundImage = 'url(/images/page_top_bg_out_2007.png)';
		}
		
		make_button('login_button', 'http://arcticblast.polarhusky.com/login');
		make_button('join_button', 'http://arcticblast.polarhusky.com/register');
	}
	get_l3_elements();
  if (protected_pages) {
	  var links = document.getElementsByTagName('a');
	  for (var i = 0; i < links.length; i++) {
	     if (logged_in != 'yes' && window.location.hostname != 'gonorth.obiki.org') { 
	       var href = links[i].getAttribute('href')
	       if (href && !href.match(/http:\/\/2007/)) {  
		 href = href.replace(/http:\/\/[-.A-Za-z0-9]+\//,'/');
		 href = href.replace(/\.[a-z]+$/,'');
		 if (protected_pages[href]) {
		   links[i].setAttribute('href', 'http://arcticblast.polarhusky.com/login');
		 } 
	       }
	     }
	  }
  }
  if(window.legacy_init) { legacy_init(); }
  if(window.media_init) { media_init(); }
  if(window.countdown_init) { countdown_init(); }
}
 
/*  Fun way to make sure that the Javascript processes after the DOM is loaded, but before the rest of the page is done. (Thanks Dean Edwards) */

/*  Mozilla Method */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/*  IE Method */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        init(); // call the onload handler
    }
};
/*@end @*/

/* Safari Method */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            clearInterval(_timer);
            init(); // call the onload handler
        }
    }, 10);
}

/* Fallback Method */
window.onload = init;




function make_button( id, href ) {
	var a = document.createElement('a');
	a.id = id;
	a.href = href;
	document.getElementById('subwrapper').appendChild(a);
}

function set_cookie() {
	if (location.search.indexOf('topID=1') != -1) {
		document.cookie = "login=yes; path=/";
	} else if (location.search.indexOf('topID=0') != -1) {
		document.cookie = "login=no; path=/";
	}
}

function check_cookie() {
	var logged_in = 'no';
	var cookies = document.cookie.split(';');
	for (var i=0; i < cookies.length; i++) {
		if (cookies[i].indexOf('login=') != -1) {
			logged_in = cookies[i].substring(cookies[i].indexOf('=') + 1);
		}
	}
	return logged_in;
}

