/*
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 search box onrollover
		$$('a#searchBtn').each(function(el){
			el.onclick = function(){
				new Effect.toggle('slide_wrapper','slide');
				return false;
			}
		});
		// search box functions
		$$('input#genSearchTerm').each(function(el){
			el.onfocus = function(){
				el.value = '';
				el.setStyle({ color: '#000000' })
			};
			el.onblur = function(){
				if(el.value == ''){
					el.value = '...type keyword';
					el.setStyle({ color: '#333333' })
				}
			}
			new Ajax.Autocompleter("genSearchTerm", "autocomplete_choices", siteURL  + 'search/ajax_search', {paramName: "search_term", minChars: 2});
		});
		// Suckerfish dropdowns
		$$('#sfa_nav li').each(function(el){
			el.onmouseover = function(){ el.className = 'sfhover'; }
			el.onmouseout = function(){ el.className = ''; }
		});
		$$('div#right_col h1').each(function(el){
			if(el.innerHTML == 'Eco Statement'){
				el.setStyle({ 'color' : '#508900' })
			}
		})
		// add arrows to top nav items with children
		$$('#sfa_nav li li a').each(function(el){
			if(el.parentNode.getElementsByTagName('ul').length > 0){ el.className = 'li_parent'; }
		});
		// Cross Fade Image Gallery
		$$('#img_gallery').each(function(el){ new Gallery('img_gallery'); });
		// highlight library table rows
		$$('table#library_list tr').each(function(el){
			el.onmouseover = function(){ el.setStyle({ background: '#f0f0f0' }); }
			el.onmouseout = function(){ el.setStyle({ background: '#FFFFFF' }); }
		});
		// Image Gallery popup details
		$$('a.high_res_img').each(function(el, item){
			var desc = $$('div.hr_img_desc')[item];
			new Effect.BoxTrail(el, desc);
		});
		// product type form behaviours
		$$('select#product_type_search').each(function(el){
			el.onchange = function(){
				if($F(el) != ''){
					window.location = siteURL + 'products/display/' + $F(el);
				}
			}
		});
		$$('input#product_search_term').each(function(el){
			el.onfocus = function(){ el.value = ''; }
			el.onblur = function(){ 
				if(el.value == ''){
					el.value = 'or enter keyword';
				}
			}
		});
		$$('form#product_search').each(function(el){
			el.onsubmit = function(){
				new Ajax.Updater('products_search_results', siteURL + 'search/ajax_search', { method: 'post', postBody: 'search_term=' + $F('product_search_term') });
				return false;
			}
		});	
	}
		
 }
 
Event.observe(window, 'load', Site.start ); 

var Gallery = Class.create();
Gallery.prototype = {
	
	defaultOptions : {
		thumbnails 	: 'thumbnails',
		duration 	: 0.7
	},
	initialize: function(container) {
		this.container = $(container);
		options = arguments[1] || {};
		this.options = Object.extend(Object.extend({},this.defaultOptions), options || {});
		this.options.thumbnails = $(this.options.thumbnails);
		this.start();
	},
	start: function() {
		this.options.thumbnails.getElementsBySelector('a').each(function(el,item){
			el.onclick = function(){ this.reveal(item); return false; }.bind(this);
		}.bind(this));	
	},
	getCurrItem : function() {
		var items = this.container.getElementsByClassName('main_img');
		var iter = 0;
		items.each(function(el,i){
			if(el.style.display != 'none'){ iter = i; }
		});
		return iter;
	},
	reveal : function(i){
		this.images = this.container.getElementsByClassName('main_img');
		this.currItem = this.getCurrItem();
		
		var after_finish = function(){
			this.images.each(function(b, iter){ if(iter != i){ b.hide(); } });
		}.bind(this);
		
		if(this.currItem == i){ return; }
		
		this.images[this.currItem].style.zIndex = 1;
		new Effect.Fade(this.images[this.currItem],{ duration: this.options.duration });
		
		this.images[i].style.zIndex = 2;
		new Effect.Appear(this.images[i],{ duration: this.options.duration, afterFinish: after_finish });
	}
		
}

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':16, 'y':16},
			appearDur : 0.4
		}, 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({ 'z-index': 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();
		new Effect.Appear(this.wrapper, { duration: this.options.appearDur });
	},
	hideTip: function(){
		this.element.stopObserving('mousemove');
		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};
	}
});

