window.addEvent('domready', initAll);

InsideWindow = true;

function initAll(){
	$('closebutton').addEvent('click',closeWindow);
	$('mainwindow').fade('hide');
	$('loading').fade('hide');
	$('button1darken').fade('hide');
	$('button2darken').fade('hide');
	$('button3darken').fade('hide');
	$('button1link').addEvent('mouseover', function(event){$('button1darken').fade('in');});
	$('button1link').addEvent('mouseout', function(event){$('button1darken').fade('out');});
	$('button2link').addEvent('mouseover', function(event){$('button2darken').fade('in');});
	$('button2link').addEvent('mouseout', function(event){$('button2darken').fade('out');});
	$('button3link').addEvent('mouseover', function(event){$('button3darken').fade('in');});
	$('button3link').addEvent('mouseout', function(event){$('button3darken').fade('out');});
	initLinks();
	initForms();
	hashChange.periodical(500);
}

//Internal link handling
function initLinks(){
	allLinks = $$('a');
	$each(allLinks, function(thisLink){
		if(thisLink.target == '' && thisLink.initDone != true && thisLink.onclick == null && thisLink != null){
			//Modify link so it opens in popup window
			thisLink.initDone = true;
			thisLink.addEvent('click',loadPage.pass(thisLink.href));
			
			if(thisLink.href.split(".com/")[1] == window.location.href.split("#")[1]){
				thisLink.addClass("aThisPage");
			}
		}
	});
}

//Handle resize
window.addEvent('resize', handleResize);
window.addEvent('load', handleResize);
function handleResize(){
	center($('home'));
	center($('mainwindow'));
}
function center(thisObject){
	if(thisObject == null){return false;}
	if(window.getHeight() < thisObject.getHeight()){
		thisObject.setStyle('top','0px');
		thisObject.setStyle('margin-top','0px');
	} else {
		thisObject.setStyle('top','50%');
		thisObject.setStyle('margin-top',-(thisObject.getHeight/2));
	}
	if(window.getWidth() < thisObject.getWidth()){
		thisObject.setStyle('left','0px');
		thisObject.setStyle('margin-left','0px');
	} else {
		thisObject.setStyle('left','50%');
		thisObject.setStyle('margin-left',-(thisObject.getWidth/2));
	}
}

//Window open/close
var windowOpen = false;
function closeWindow(){
	$('mainwindow').fade('out');
	currentHash = "";
	window.location.href = "#";
	windowOpen = false;
	dumpContent.delay(800);
	hideThrobber();
}
function dumpContent(){
	$('window-content').innerHTML = "";
}

function openWindow(){
	hideThrobber();
	initLinks();
	initScrollContent();
	initSlideshow();
	initForms();
	RecallScrollPosition();
	$('mainwindow').fade('in');
	windowOpen = true;
}

//Page load
currentHash = "";
function loadPage(pageURL){
	delayShowThrobber();
	GoTo = pageURL.replace("http://www.keypointchurch.com/","");
	
	SaveScrollPosition(lastHash, GoTo);
	
	lastHash = GoTo;
	var currentHREF = window.location.href.split("#")[0];
	window.location.href = currentHREF + "#" + GoTo;
	var pageRequest = new Request.HTML({update: $('window-content')}).get(pageURL);
	pageRequest.onSuccess = openWindow;
	return false;
}

//Scroll position
var rememberScroll = null;
function SaveScrollPosition(varFrom, varTo){
	varFrom = varFrom.split("?");
	varFrom = varFrom[0];
	varTo = varTo.split("?");
	varTo = varTo[0];
	if(varFrom == varTo){
		if($('savePosition') != null){rememberScroll = $('savePosition').scrollTop;}
	} else {
		rememberScroll = null;
	}
}
function RecallScrollPosition(){
	if(rememberScroll != null && $('savePosition') != null){
		$('savePosition').scrollTop = rememberScroll;
		if($('savePosition').scroller != null){
			$('savePosition').scroller.update();
			if($('savePosition').hasClass('selectionMenu')){$('savePosition').setStyle('margin-right','0');}
		}
	}
}

//Throbber
var loadingThrobberDelay;
function delayShowThrobber(){
	$clear(loadingThrobberDelay);
	loadingThrobberDelay = showThrobber.delay(1000);
}
function showThrobber(){
	$clear(loadingThrobberDelay);
	$('loading').fade('in');
	loadingThrobberDelay = hideThrobber.delay(5000);
}
function hideThrobber(){
	$clear(loadingThrobberDelay);
	$('loading').fade('out');
}

//Content Scrolling
function initScrollContent(){
	allContent = $$('.pContent', '.selectionMenu');
	$each(allContent, function(thisContent, thisIndex){
		thisParent = thisContent.getParent();
		thisContent.setStyle('height',(thisParent.getHeight()-30)+'px');
		//alert(thisParent.getScrollHeight() +'.'+ thisParent.getHeight())
		if(thisContent.getScrollHeight() > thisContent.getHeight()){
			thisContent.scroller = new UvumiScrollbar(thisContent);
			if(thisContent.hasClass('selectionMenu')){thisContent.setStyle('margin-right','0');}
		} else {
			thisContent.setStyle('overflow','hidden');
		}
		thisContent.setStyle('margin-top','10px');
	});
}

//Browser navigation detect
lastHash = "";
function hashChange(){
	var currentHash = window.location.href.split("#")[1];
	if(lastHash != currentHash){
		lastHash = currentHash;
		if(currentHash=="" || currentHash==undefined){
			closeWindow();
		}else{
			loadPage(currentHash);
		}
	}
}


//Form submission
function initForms(){
	allForms = $$('form');
	$each(allForms, function(thisForm){
		if(thisForm.initDone != true && thisForm.target != "uploader"){
			if(thisForm.action == ""){
				var me = window.location.hash;
				thisForm.action = me.slice(1);
			}
			thisForm.initDone == true;
			thisForm.addEvent('submit',submitForm);
		}
	});
}
function submitForm(e){
	e.stop();
	if(this.id == "editorform"){
		//Form contains editor, handle
		$('editor1').EscapeUnicode = true;
		$('xhtml1').value = $('editor1').value;
	}
	$('loading').fade('in');
	var pageRequest = new Request.HTML({url:this.action, update: $('window-content')}).post(this);
	pageRequest.onSuccess =  function(response){ initLinks();	initForms(); $('loading').fade('out');};
	//Send the form.
	//this.send();
}
function formSubmitted(){
	alert('done');
}

//Slideshow
function initSlideshow(){
	$each($$('.slideshow'),function(thisSlideshow){
		thisSlideshow.itemCount = 0;
		thisSlideshow.currentItem = 0;
		$each(thisSlideshow.getChildren(),function(thisElement){
			if(thisElement.hasClass('slideshow-photo')){
				//photos
				thisElement.fade('hide')
				++thisSlideshow.itemCount;
			}
			if(thisElement.hasClass('slideshow-buttons')){
				//next and prev buttons
				$each(thisElement.getChildren(),function(thisA){
					if(thisA.hasClass('slideshow-next')){
						thisA.addEvent('click',slideshowAction.bind(thisSlideshow,"n"));
					}
					if(thisA.hasClass('slideshow-prev')){
						thisA.addEvent('click',slideshowAction.bind(thisSlideshow,"p"));
					}
				});
			}
		});
		slideshowAction.run("n",thisSlideshow);
		
	});
}

function slideshowAction(thisAction){
	currentItem = this.currentItem;
	
	//fade out current photo
	myItem = 0;
	$each(this.getChildren(),function(thisElement){
		if(thisElement.hasClass('slideshow-photo')){
			++myItem;
			if(myItem == currentItem){
				thisElement.fade('out');
			}
		}
	});
	
	//pick next photo
	if(thisAction == 'n'){
		++this.currentItem;
		if(this.currentItem > this.itemCount){this.currentItem = 1;}
	} else {
		--this.currentItem;
		if(this.currentItem == 0){this.currentItem = this.itemCount;}
	}
	
	currentItem = this.currentItem;
	
	//fade in new photo
	myItem = 0;
	$each(this.getChildren(),function(thisElement){
		if(thisElement.hasClass('slideshow-photo')){
			++myItem;
			if(myItem == currentItem){
				thisElement.fade('in');
			}
		}
	});
	
}