/*
JavaScript Document for Street Furniture Australia, www.streetfurniture.com
Author: James Nicol, www.enilsson.com, July 2007
 */
  
var Site = {
	 
	// initialisation function
	start : function(){
		Site.behaviour();
	},
	// apply behaviours to elements via CSS selectors	
	behaviour : function() {
		//  flash the error/success messages	
		$$('p.fail_message','p.success_message').each(function(el) { Effect.Pulsate(el, { duration: 2.0, from:0.2 }); });
		// highlight the active form input
		$$('input.form_input','textarea').each(function(el){
			el.onfocus = function() { new Effect.Highlight(el, {startcolor:'#FFFFFF', endcolor:'#FFFF99', restorecolor:'#FFFF99'}); }
			el.onblur = function() {  new Effect.Highlight(el, {startcolor:'#FFFF99', endcolor:'#FFFFFF', restorecolor:'#FFFFFF'}); }
		});
		// show the book descriptions on the homepage
		$$('div.new_book').each(function(b){
			var nbImg = b.getElementsBySelector('img').first();
			var nbDesc = b.select('.book_desc').first();
			new Effect.BoxTrail(nbImg, nbDesc, { offset: { 'x': 10, 'y': -15}});
		});
		// scroll boxes by mouseover so you dont have to see the scroll bars
		$$('div.tinypress').each(function(el){ new BoxScroll(el); });
		// Add menu slide code to top navigate 
		$$('div#masterhead > ul').each(function(el){
			new MenuSlide(el);
		});
		// set the top nav menu link to on if the location matches one of its submenu hrefs
		$$('div#masterhead > ul > li').each(function(el){
			el.getElementsBySelector('a').each(function(b){
				var pageURL = new String(window.location);
				if(b.href == pageURL || pageURL.startsWith(b.href)){ 
					el.className = 'this_category'; 
					return; 
				}
			});
		});
	}
		
};
 
document.observe('contentloaded', Site.start);


Effect.BoxTrail = Class.create();
Object.extend(Object.extend(Effect.BoxTrail.prototype, Effect.Base.prototype), {
	initialize: function(element, content) {
		this.element = $(element);
		if(!this.element) throw(Effect._elementDoesNotExistError);
		this.wrapper = $(content);
		if(!this.wrapper) throw(Effect._elementDoesNotExistError);
		var options = Object.extend({
			offset: {'x':10, 'y':10},
			appearDur : 0.2
		}, arguments[2] || {});
		this.start(options);
	},
	setup: function() {
		$$('body').first().appendChild(this.wrapper); // move the wrapper to the end of the document so it can follow the mouse better
		this.wrapper.setStyle({ zIndex: 10000 });
		this.element.observe('mouseover', this.showTip.bind(this));
		this.element.observe('mousemove', this.positionTip.bind(this));
		this.element.observe('mouseout', this.hideTip.bind(this));
	},
	showTip: function(event){
		this.hideTip();
		this.wrapper.show();
		new Effect.Appear(this.wrapper, { duration: this.options.appearDur, queue: { position: 'end', scope: 'boxtrail' } });
	},
	hideTip: function(){
		var classNm = this.wrapper.className;
		$$('.' + classNm).each(function(el){ el.hide(); });
	},
	positionTip: function(event){
		var offsets = {'x': this.options.offset['x'],'y': this.options.offset['y']};
		var mouse = {'x': Event.pointerX(event), 'y': Event.pointerY(event)};
		var page = {'x':this.viewportSize()['x'], 'y':this.viewportSize()['y']};
		var tip = {	'x' : mouse['x'] + this.options.offset['x'] + this.wrapper.getWidth(),
					'y' : mouse['y'] + this.options.offset['y'] + this.wrapper.getHeight()};
		if(tip['x']>page['x']) { offsets['x'] = 0-(this.wrapper.getWidth() + this.options.offset['x']); }
		if(tip['y']>page['y']) { offsets['y'] = 0-(this.wrapper.getHeight() + this.options.offset['y']); }
		this.wrapper.setStyle({
		  left: mouse['x'] + offsets['x'] + 'px',
		  top: mouse['y'] + offsets['y'] + 'px'
		});
	},
	viewportSize : function(){
		var x = self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
		var y = self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
		return {'x': x, 'y': y};
	}
});

var BoxScroll = Class.create({
	initialize: function(element) {
		this.element = $(element);
        this.options = Object.extend({
        	duration: 0.01
        }, arguments[1] || {});
        this.setup();
	},
	setup: function(){
		this.element.setStyle({
			'overflow':'hidden',
			'position':'relative'
		});
		this.element.pagePos = this.element.cumulativeOffset()[1];
		this.pe = false;
		this.direction = false;
		this.innerHeight = this.element.down().getHeight();
	
		this.element.observe('mousemove', this.scrollBox.bindAsEventListener(this));
		this.element.observe('mouseout', this.stopScroll.bindAsEventListener(this));
	},
	scrollBox: function(e){
		var posY = Event.pointerY(e) - this.element.pagePos;
		if(posY < 40){
			this.element.setStyle({ 'cursor' : 'n-resize' })
			if(this.direction == 'down'){ this.stopScroll(); }
			if(!this.pe){
				this.direction = 'up';
				this.pe = new PeriodicalExecuter(this.scrollUp.bind(this), this.options.duration);
			}
		} else if (posY > (this.element.getHeight() - 40)){
			this.element.setStyle({ 'cursor' : 's-resize' })
			if(this.direction == 'up'){ this.stopScroll(); }
			if(!this.pe){
				this.direction = 'down';
				this.pe = new PeriodicalExecuter(this.scrollDown.bind(this), this.options.duration);
			}
		} else {
			this.element.setStyle({ 'cursor' : 'default' })
			if(this.pe){ this.stopScroll(); }
		}
	
	},
	scrollUp: function(e){
		newPos = this.element.scrollTop - 1;
		if(newPos <= 0){ this.stopScroll(); return; }
		this.element.scrollTop = newPos;	
	},
	scrollDown: function(e){
		newPos = this.element.scrollTop + 1;
		if(newPos >= this.innerHeight - this.element.getHeight()){ this.stopScroll(); return; }
		this.element.scrollTop = newPos;	
	},
	stopScroll: function() {
		if(this.pe){ 
			this.pe.stop(); 
			this.pe = false;
		}
	}
});

var MenuSlide = Class.create({
	initialize: function(element){
		this.element = $(element);
        this.options = Object.extend({
        	duration: 0.7
        }, arguments[1] || {});
        this.submenuWidths = {};
        this.setup();
	},
	setup: function() {
		this.element.getElementsBySelector('li > table').each(function(b){
			b.show();
			b.origWidth = b.getWidth();
			b.hide();
			b.setStyle({ 'width' : b.origWidth + 'px' });
			b.wrap('div', { style: "overflow:hidden;width:0px;" });
		}.bind(this));
		this.element.getElementsBySelector('li > a').each(function(b){
			b.observe('click', this.slideOut.bindAsEventListener(this, b), false);
		}.bind(this));
	},
	slideOut: function(event, button) {
		var wrapper = button.parentNode.getElementsBySelector('div').first();
		if(!wrapper){ return; }
		var wrapperWidth = wrapper.getWidth();
		var submenu = wrapper.down();
		this.closeAll();
		if(submenu.origWidth == wrapperWidth){ Event.stop(event); return; }
		new Effect.Morph(wrapper, { 	
			style: { 'width': submenu.origWidth + 'px'}, 
			duration: this.options.duration,
			beforeStart: function(){ submenu.show() } 
		});
		Event.stop(event);
	},
	slideIn: function(event, button) {
		var wrapper = button.getElementsBySelector('div').first();
		if(!wrapper){ return; }
		if(Event.element(event).parentNode.tagName == 'td'){ return; }
		var submenu = wrapper.down();
		new Effect.Morph(wrapper, { 
			style: { 'width': '0px'}, 
			duration: this.options.duration 
		});
		Event.stop(event);
	},
	closeAll: function() {
		this.element.getElementsBySelector('table').each(function(b){
			if(b.parentNode.getWidth() != 0){
				var width = Prototype.Browser.WebKit ? '1px' : '0px';
				new Effect.Morph(b.parentNode, { 	
					style: { 'width': width }, 
					duration: this.options.duration,
					afterFinish: function(){  }
				});
			}
		}.bind(this));
	}


});
