/*-- websta*07 --*/

/* 
* w|p 2.1 'scrolling panes'
*/

//set up our scrolling window, cover, breadcrumbs
initScrollWin = function(){
	//hide the cover
	var cover = $('cover');
	var fx = new Fx.Styles(cover, {duration:200, wait:false});
	fx.set({'opacity': 0});
	//initiate the scrollwin
	scrollWin = new Fx.Scroll('lookingGlass', {
		wait: false,
		duration: 800,
		offset: {'x': 1, 'y': 1},
		transition: Fx.Transitions.Cubic.easeInOut
	});
	//set cover effects
	coverFx = cover.effects({duration: 200, transition: Fx.Transitions.Quad.easeOut});
	coverFxExt = cover.effects({duration: 500, transition: Fx.Transitions.Quad.easeOut});
	var crumbs = $('crumbs');
	crumSlide = new Fx.Slide(crumbs, {mode: 'horizontal'});
	$ES('a', crumbs).each(function(crumbLink){ crumbLink.addClass('paneLink') });
}

//attach ajax events to links
initAjaxLinks = function(links){
		links.each(function(paneLink){
			paneLink.removeEvents('click');
			paneLink.addEvent('click', function(event) {
				event = new Event(event).stop();
				toScrollWin(paneLink);
			});
		});
}

//called from anchors to change content in our scrolling window
toScrollWin = function(paneLink){
				url = paneLink.href;
				urlExp = url.split('/');
				slug = urlExp[urlExp.length-1];
				if(slug=='')  window.location = 'http://07.webprogression.co.nz/';
				else{
					subSlug = urlExp[urlExp.length-2];
					//make safe for dev in case our urls aren't DOM ID compatibale
					newslug = slug.split('.');
					slug = newslug[0];
					if(slug == '') slug = 'home';
					var wrap = $('paneWrapper');
					var content = $(slug);
					$$('div.pane').each(function(pane){
						pane.removeClass('scrollPane');
					});
					crumSlide.slideOut();
					if(content == null){
						//the requested content hasnt been previously loaded. create a new pane for it and request via ajax
						content = new Element('div', {'class': 'pane ' + subSlug, 'id': slug});
						wrap.adopt(content);
						var aRequest = new Ajax(url + '/ajax?requestTime=' + now.valueOf(), { method: 'get', update: content, evalScripts: true, loading: coverFx.start({'opacity': 0.9}), onComplete: loadAndContentIn }).request();
					}else{
						//content alraedy here so lets just scroll to it
						coverFx.start({'opacity': 0.9}).chain(function(){
							contentIn(content);
						});
					}
				}
}


//work around function to enbale toScrollWin to load new content without errors
//- Some Browsers dont like "var aRequest = new Ajax( ~ onComplete: contentIn(content) }).request()" - although it works fine.
//- So instead              "var aRequest = new Ajax( ~ onComplete: loadAndContentIn }).request()" 
//called from our scroll pane handler function on Ajax::onComplete()
//get extended dom el of the requested ajax calls' update param and run contentIn on it.
loadAndContentIn = function() {
		var content = $(this.options.update);
		contentIn(content);
}
	
//scroll to requested pane add scrolling as needed, remove cover, set up our breadcrumbs inc ajax events, roll them out.
contentIn = function(content){
		scrollWin.toElement(content).chain(function(){
					content.addClass('scrollPane');
					coverFxExt.start({'opacity': 0});
					var crumb = $E('.crumbs', content);
					var crumbs = $('crumbs');
					crumbs.setHTML(crumb.innerHTML);
					initAjaxLinks($ES('a', crumbs));
					crumSlide.slideIn();
			});
}

initQuickJump = function(){
	qj = $('QuickJump');
	qj.effects({}).set({'opacity': 1});
	qj.addEvent('change', function(event) {
				event = new Event(event).stop();
				if(qj.value!=''){
					paneLink = new Element('a', {'href': qj.value});
					toScrollWin(paneLink);
				}
	});
}

//new date object for our ajax requests
var now = new Date();

//wind her up jimmy
window.addEvent('domready', function() {
	initScrollWin();
	initQuickJump();
	initAjaxLinks($$('a.paneLink'));
});
