var _cache = {};
var _current = "index.html";
var autoplayMusic = false;
var videos = [
	{title:"Advent Conspiracy",url:"http://www.youtube.com/v/eVqqj1v-ZBU&hl=en&fs=1",thot:"I really liked this when it was sent to me...<br/><br/>It has always seemed to me that most of us have missed the entire opportunity in christmas; regardless what you believe."},
	{title:"Savior Based Economy",url:"http://www.youtube.com/v/aZwQQ0lgiWs&hl=en&fs=1",thot:"Exactly..."}
];
var songs = [
	{title:"Plumb - Sleep",url:"http://www.youtube.com/v/D73wnGRGUGs&hl=en&fs=1&autoplay="+(autoplayMusic?1:0),length:320,thot:"Well do you have some of those days when you cant be who you want to be? When you just need to close your eyes; pull the covers up so high and drift away. And sleep so soundly. And dream profoundly. Cast all your cares on the only thing that you really need and sleep so soundly."},
	{title:"Jars of Clay - Oh My God",url:"http://www.youtube.com/v/tMbxP-TL0L8&hl=en&fs=1&autoplay="+(autoplayMusic?1:0),length:365,thot:"Sometimes when I lose my grip, I wonder what to make of heaven. All the times I thought to reach up, all the times I had to give. Babies underneath their beds, hospitals that cannot treat, all the wounds that money causes, all the comforts of cathedrals, all the cries of thirsty children - this is our inheritance. All the rage of watching mothers - this is our greatest offense. Oh my God! Oh my God! Oh my God!"}
];
var quotes = [
		"Of one hundred men one will read the Bible; the ninety-nine will read the Christian.~Dwight L. Moody",
		"I dream of a quiet man who explains nothing and defends nothing, but only knows where the rarest wildflowers are blooming, and who goes, and finds that he is smiling not by his own will.~Wendell Berry",
		"Never forget in the dark what you saw in the light.~Dr Robert Munger",
		"Aim at heaven and you will get earth thrown in. Aim at earth and you get neither.~C. S. Lewis",
		"I believe in Christianity as I believe that the sun has risen: not only because I see it, but because by it I see everything else.~C. S. Lewis ",
        "To be persuasive, we must be believable; to be believable, we must be credible; to be credible, we must be truthful.~Edward R. Murrow "
	];
var movieIndex = videos.length-1; //Math.round(Math.random() * (videos.length-1));
var musicIndex = Math.round(Math.random() * (songs.length-1));

var skin = {};
skin['BORDER_COLOR'] = '#cccccc';
skin['ENDCAP_BG_COLOR'] = '#e0ecff';
skin['ENDCAP_TEXT_COLOR'] = '#333333';
skin['ENDCAP_LINK_COLOR'] = '#0000cc';
skin['ALTERNATE_BG_COLOR'] = '#ffffff';
skin['CONTENT_BG_COLOR'] = '#ffffff';
skin['CONTENT_LINK_COLOR'] = '#0000cc';
skin['CONTENT_TEXT_COLOR'] = '#333333';
skin['CONTENT_SECONDARY_LINK_COLOR'] = '#7777cc';
skin['CONTENT_SECONDARY_TEXT_COLOR'] = '#666666';
skin['CONTENT_HEADLINE_COLOR'] = '#333333';
skin['POSTS_PER_PAGE'] = '10';
skin['POSITION'] = 'bottom';
skin['DEFAULT_COMMENT_TEXT'] = '- add your comment here -';

//bug in prototype
window.JSON = {parse:function(st){return st.evalJSON();},stringify:function(obj){return Object.toJSON(obj);}};
//init friendconnects
google.friendconnect.container.setParentUrl('/');

function onLoad(start) {
	//cache index.html 
	_cache['index.html'] = $("main").innerHTML;
	//gadgets
	skin['HEADER_TEXT'] = 'Comments';
	google.friendconnect.container.renderWallGadget({id:'div-1228245624659',site:'04024949953527079368','view-params':{"disableMinMax":"true","scope":"SITE","features":"video,comment","startMaximized":"true"}},skin);
	skin['HEADER_TEXT'] = 'Members';
	google.friendconnect.container.renderMembersGadget({id:'div-1228245427085',site:'04024949953527079368'},skin);
	//hide loading
	if (start)
		switchTab(start);
	else
		Effect.Fade("loading",{duration:.15});
}

function switchTab(file,form) {
	var main = $("main");
	var loading = $("loading");
	var _timer = null;
	var postFunk = function (transport) {
		_current = file;
		//set content
		if (transport) 
			_cache[file] = transport.responseText;
		//fade in content
		Effect.Fade(main,{
			afterFinish:function(){
				main.innerHTML = _cache[file];
				Effect.Appear(main,{duration:.5});
				//init content
				switch (file) {
					case "index.html":
						onLoad();
						Effect.BlindDown($("photos"));
						break;
					case "email.php":
						_timer = setTimeout(function(){switchTab('index.html');},3000);
						break;
					default:
						Effect.BlindUp($("photos"));
						break;
				}
				//hide loading
				if (transport)
					Effect.Fade(loading,{duration:.15});
				//remove cache for php files
				if (file.indexOf(".php") > 0)
					_cache[file] = null;
			},
			to: .1,
			duration: .5
		});
	}
	//show loading
	if (file != _current) {
		//unset any timer
		clearTimeout(_timer);
		//unset tab
		if ($("tab_"+_current.split(".")[0]))
			$("tab_"+_current.split(".")[0]).style.background = "url(images/roll.png) no-repeat left top";
		//set tab
		if ($("tab_"+file.split(".")[0]))
			$("tab_"+file.split(".")[0]).style.background = "url(images/roll.png) no-repeat 0 -27px";
		//set content
		if (_cache[file]) 
			postFunk();
		else if (!form) {
			Effect.Appear(loading,{duration:.15});
			new Ajax.Request(file,{onComplete:postFunk,method:'get'});
		}
		else {
			Effect.Appear(loading,{duration:.15});
			$(form).request({onComplete:postFunk});
		}
		//analytics
		pageTracker._trackEvent("switchTab",file);
	}
}

function clearValue(field,origValue) {
	if (field.value == origValue)
		field.select();
}

function fillValue(field,origValue) {
	if (field.value == '')
		field.value = origValue;
}

function showQuote() {
	var div = $("quote");
	Effect.Fade(div,{
		afterFinish: function(){
			var newHTML = div.innerHTML;
			while (div.innerHTML == newHTML)
				newHTML = quotes[Math.round(Math.random()*(quotes.length-1))].replace("~", "<br/><br/>~");
			div.innerHTML = newHTML;
			Effect.Appear(div,{to:.5});
			//set timeout for another!
			setTimeout(showQuote,10000);
		}
	});
}

function showMovie() {
	var div = $("movie");
	var html = '<object width="300" height="248" id="the_movie">'+
			'<param name="movie" value="{{ url }}"></param>'+
			'<param name="allowFullScreen" value="true"></param>'+
			'<param name="allowscriptaccess" value="always"></param>'+
			'<embed src="{{ url }}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="248"></embed>'+
		   '</object>';
	//kill flash
	div.innerHTML = "";
	//start fade
	Effect.Fade(div.parentNode, {
		afterFinish: function(){
			var video = videos[movieIndex];
			//populate html
			$("movie_title").innerHTML = video.title;
			$("movie_thots").innerHTML = video.thot;
			//show rater
			skin['HEADER_TEXT'] = 'Rate This';
			google.friendconnect.container.renderReviewGadget({id: 'div-1228861886692',site: '04024949953527079368','view-params':{"disableMinMax":"false","scope":"ID","docId":"movie_"+movieIndex,"startMaximized":"false"}},skin);
			//fade in
			Effect.Appear(div.parentNode, {
				afterFinish: function () {
					//add flash
					div.innerHTML = html.replace(/{{ url }}/gi,video.url);
					//set timeout for another based on length of this one!
					//movieIndex = Math.round(Math.random() * (videos.length - 1));
					//setTimeout(showMovie,(video.length*1000)+5000);
				}
			});
		},
		to:.1
	});
}
function showMusic() {
	var div = $("music");
	var html = '<object width="300" height="110" id="the_music">'+
          		  '<param name="movie" value="{{ url }}"></param>'+
				  '<param name="wmode" value="transparent"></param>'+
				  '<embed src="{{ url }}" type="application/x-shockwave-flash" width="300" height="110" wmode="transparent"></embed>'+
			   '</object>';
	//kill flash
	div.innerHTML = "";
	//start fade
	Effect.Fade(div.parentNode, {
		afterFinish: function(){
			var song = songs[musicIndex]
			//popuplate html
			$("music_thots").innerHTML = song.thot;
			//show rater
			skin['HEADER_TEXT'] = 'Rate This';
			google.friendconnect.container.renderReviewGadget({id: 'div-1228861886693',site: '04024949953527079368','view-params':{"disableMinMax":"false","scope":"ID","docId":"music_"+musicIndex,"startMaximized":"false"}},skin);
			//fade in content
			Effect.Appear(div.parentNode, {
				afterFinish: function () {
					//add flash
					div.innerHTML = html.replace(/{{ url }}/gi,song.url);
					//set timeout for another based on length of this one!
					//musicIndex = Math.round(Math.random() * (songs.length - 1));
					//setTimeout(showMusic,(song.length*1000)+5000);
				}
			});
		},
		to:.1
	});
}