	document.observe("dom:loaded", function() {
		highlightForms();
		floatbox();
		storyHover();
		setLoginOptions();
	});
	
	fbPageOptions = {
    	outsideClickCloses: false,
		hideFlash: true
    };

	var tabs;
 	
 	function initPage() {
 		initTabs();
 		loadCarousels();
 	}
 	
 	function loadCarousels() {
 		if ($('carousel')) {
 			hCarousel = new UI.Carousel("carousel", {
				autoGlide:"true",
				intervalAuto:6
			});
 		}
    }
    
    function initTabs() {
    	if ($('browse-tabs')) {
			tabs = new Fabtabs('browse-tabs');
    	}
    }
    
    Event.observe(window, "load", initPage);

	// Make external links open new window/tab
	this.blankwin = function(){
		var hostname = window.location.hostname;
		hostname = hostname.replace("www.","").toLowerCase();
		var a = document.getElementsByTagName("a");
		this.check = function(obj){
			var href = obj.href.toLowerCase();
			if ( href.indexOf("http://") != -1 &&
				 href.indexOf("egrmagazine.com") == -1 &&
				 href.indexOf(hostname)==-1 )
				 return true ;
			else
				return false;
		};
		this.set = function(obj){
			obj.target = "_blank";
			obj.className = "external";
		};
		for (var i=0;i<a.length;i++){
			if(check(a[i])) set(a[i]);
		};
	};

	
	// Highlight form fields on focus
	function highlightForms() {
		var highlightForm = $$('div.form input[type=text], div.form input[type=password], div.form textarea, div#email input[type=text], div.comments-post input[type=text], div.comments-post textarea');
		highlightForm.each(
			function(highlightElement) {
				highlightElement.observe('focus',function(event) {
					event.element().addClassName("highlight");
				});
				highlightElement.observe('blur',function(event) {
					event.element().removeClassName("highlight");
				});
			}
		)
	}
	
	// Redirect links to other floatboxes
	function floatbox() {
		
		$$('body#registration a[href*="/registration/login.php"]').each(
			function(loginLink) {
				loginLink.observe('click',function(event) {
					parent.fb.loadAnchor('/registration/login.php', 'sameBox:true width:500 height:335');
				});
			}
		)
		
		$$('body#registration a[href*="/registration/index.php"]').each(
			function(registerLink) {
				registerLink.observe('click',function(event) {
					parent.fb.loadAnchor('/registration/', 'sameBox:true width:954 height:545');
				});
			}
		)
		
		$$('body#registration a[href*="/registration/forgottenpassword.php"]').each(
			function(forgotpwLink) {
				forgotpwLink.observe('click',function(event) {
					parent.fb.loadAnchor('/registration/forgottenpassword.php', 'sameBox:true width:500 height:335');
				});
			}
		)
		
		// Edit floatbox and reload page
		var exitFloatbox = $("continue");
		
		if (exitFloatbox) {
			exitFloatbox.observe('click',function(event) {
				parent.fb.loadPageOnClose = 'self';
				parent.fb.end();
			});
		}
		
		// Exit floatbox and don't reload page
		var exitFloatboxNoReload = $("continue-noreload");
		
		if (exitFloatboxNoReload) {
			exitFloatboxNoReload.observe('click',function(event) {
				parent.fb.end();
			});
		}
		
		

	
		// General floatbox options
		fbPageOptions = {
			outsideClickCloses: false,
			hideFlash: true
		};

		
	}
	
	// Make stories content div clickable
	function storyHover() {
		$$("div#main-content","div#additional-content").each (
			function (storyClick) {
				storyClick.observe('click', 
					function(event) {
						
						var element = event.findElement();
										
						if (element.tagName == 'A') {
							// do nothing
						} else if (element.hasClassName('article')) {
							// do nothing
						} else if (element.up('div.article')) {
							// do nothing
						} else if (element.up('div.story,div.featured-story,div.star-job,div.event-listing')) {
							var link = element.up('div.story,div.featured-story,div.star-job,div.event-listing').down('a');
							document.location = link.href;
						} else if (element.getElementsByClassName('div.story,div.featured-story,div.star-job,div.event-listing')) {
							var link = element.down('a');
							
							if(typeof link != 'undefined') {
								document.location = link.href;
							}
						}					
					}
				);
			}
		)
	}
	
	
	//hide registration divs as necessary depending on whether the user is logged in (i.e. whether cookie egr_login_id is set)
	function setLoginOptions() {	
		
		var notLoggedInDivs = $$('.egr-notloggedin') ;
		var loggedInDivs = $$('.egr-loggedin') ;
	
		if(notLoggedInDivs || loggedInDivs) {
			var login_id = readCookie('egr_login_id');
			if(login_id == null) {	
				//user is not logged in
				notLoggedInDivs.each(function(divObj) {
					divObj.show();				
				}) ;
				loggedInDivs.each(function(divObj) {
					divObj.hide();				
				}) ;				
			} else {
				//user is logged in
				notLoggedInDivs.each(function(divObj) {
					divObj.hide();				
				}) ;
				loggedInDivs.each(function(divObj) {
					divObj.show();				
				}) ;					
			}
		}			
	
	}
	
	function readCookie(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) {
				// alert('readCookie '+name+ ' ' + ' cookie = ' + c + ' value = ' + c.substring(nameEQ.length,c.length)) ;
				return unescape(c.substring(nameEQ.length,c.length));
			}
		}
		return null;
	}	