/*
 * Non-DOM Options for functions
 */

/*
//returns true if the element is in the DOM
var exists = function() {
	return (this.getIndex() >= 0);
}
*/

//Get the Domain Name
var site_url = location.href.match('[a-z]+:\/\/([a-z0-9\-]{1,70}(?=\.[a-z]{2,4}))');

//Only want the 1st element
site_url = site_url[1];

//Scroll the user back to the content area when needed
//var scroller = new Fx.Scroll(window, {offset: {'x': 0, 'y': 500}, duration: 2000});
var scroller;

//URL path to the theme dir
var theme_path = '/MicroMVC/themes/forest_depths/';

//The Element to place the Ajax requested HTML in
var ajax_output = 'content';

//Page Wrapper element
var max_size = 'container';

//The HTML Element to show the spinner in
var ajax_spinner = 'ajax_spinner';

//Time in ms that the fade effect should last
var fade_duration = 800;

//The object that will control our fades
var fadefx;

//The object that will hold the AJAX request
var my_request;

/*
 * Load the Day/Night Theme
*/
function load_theme() {
	//Caculate time
	var date = new Date();
	var css_file = '';
	
	if ( (date.getHours() < 8) || (date.getHours() > 17) ) {
		css_file = 'night';
	} else {
		css_file = 'day';
	}

	//Load CSS file
	css_file = new Asset.css(theme_path + css_file + '.css', {id: css_file + 'css', title: css_file + 'css'});
	
}

/* 
Perform FX and send an AJAX requst for link/form 
*/
function send_request(url, form_data) {
	
	//Fade-out the ajax result area (so that we can fade-in the new results)
	//fadefx.start({'opacity': [1, 0]});
	ajax_output.fade('out');
	
	//Show the ajax spinner
	ajax_spinner.addClass('ajax_loader');
	
	//Send the user back to the top of the page for the new content
	//scroller.toTop.delay(1000, scroller);
	
	
	//Dumb function wrapper to keep the ajax from 
	//comming in before the fade out is complete.
	(function(){
		my_request.send({url: url, data: form_data, method: 'post'});
		scroller.toElement('header');
	//Wait (fade_duration) seconds before you send the Ajax request
	}).delay(fade_duration); 
	

}


//The following code will run after EVERYTHING is loaded.
window.addEvent('domready', function() {
	
	/* First we need to setup some variables */
	
	//The Element to place the Ajax requested HTML in
	ajax_output = $(ajax_output);

	//The HTML Element to show the spinner in
	ajax_spinner = $(ajax_spinner);

	//Max Screen Size
	max_size = $(max_size).getCoordinates();
	
	//set the scroller
	scroller = new Fx.Scroll(window, {
		duration: 2000,
		offset: {
	        'x': 0,
	        'y': -200
	    }
    });
    
	/*
	 * FUNCTIONS
	 */
	
	//Create the HTML AJAX request object
	my_request = new Request.HTML({
		method: 'get',
		//The location that the result should be placed in
		update: ajax_output,
		//When the request completes
		onComplete: function() {
			//ajax_output.highlight('#ddf');
			makeajaxlinks();
			
			// kill the ajax-spinner
			ajax_spinner.removeClass('ajax_loader');
			
			//Fade the content area back in!
			ajax_output.fade('in');
			
			//Should we setup nawte?
			//if($('comment_form').exists()) {
			if ($('comment_body') === undefined) {
				//alert('comment form not found!');
			} else {
				setup_nawte();
			}
			
		}
	});
	
	/*
	Process each link on the page and if they are not
	a link to a forieng site - then make an ajax request
	for the HTML. Re-Called after each request!
	*/
	function makeajaxlinks() {
		
		//Run a function for each link
		$$('a').each(function(item){
			
			//Get the link's href
			var theurl = (''+item.getProperty("href")+'');
			
			//Links to "#" shouldn't be messed with
			if(!theurl || theurl.charAt(0) == '#') {
				//alert(theurl);
				return; 
			}
			
			//If the link does NOT contain a HTTP - OR - if it DOES contain a HTTP with this site_url
			if( (!theurl.contains('http://')) || theurl.contains(site_url) ) {
				
				//If the event was already made - delete it!
				item.removeEvents('click');

				//for each of the links add an onClick event
				item.addEvent('click', function(e) {
					
					//Pass URL we are going to get
					send_request(this.getProperty("href"));
					
					//Keep the browser from going to the link location
					e.stop();
					//return false;
					
				});
			} else {
				//it is a foreign link so make it open in a new tab
				item.target = '_blank';
			}
			
		});
	}
	
	//Ok, now we need to make all the links ajax links
	makeajaxlinks();

	
	
	/*************************
	* Part 2: Make backgroun move!
	**************************/
	/*
	load_theme();
	
	var cur=0;
	sky=function(){
		document.body.style.backgroundPosition=(cur=cur+1)+'px 0px'
	}
	repeat=setInterval(sky,140);
	
	
	var cur2=0;
	var container = document.getElementById('container');
	star=function(){
		container.style.backgroundPosition=(cur2=cur2-1)+'px 0px'
	}
	repeat2=setInterval(star,140);
	
	
	/* THIS PUTS A STRAIN ON THE PC!!!!! *
	var planetx=200;
	var planet = document.getElementById('planet');
	//starting point
	var planety = Math.floor(50 + (150-50)* Math.random());
	//alert(planety);
	
	planetfun=function(){
		//if over the limit
		if(planetx > max_size.right) { planetx = -200; }
		if(planety < 0) { planety = 100; }
		
		//Should it move _y ?
		//if(Math.floor(Math.random()*20) == 3) { planety = planety-1}
		
		planet.style.backgroundPosition=(planetx=planetx+1)+'px '+ planety + 'px';
		//-250px 100px
	}
	repeat2=setInterval(planetfun,400);
	*/
	
	
	
	
	//var mySecondElement = new Element('div', {id: 'address'});
	
	//$('main').inject(mySecondElement);
	
});