/* -------------------------------------------------------------------- */
/* J-CONS.COM Website - common.js 04-08-2007 */
/* Version: 1.0 */
/* Created by: net4visions.com */
/* Last edited: 05-04-2009 */
/* Requirements: mootools 1.2.1.js
/* -------------------------------------------------------------------- */

var Site = new Class({
	Implements: [Options, Events],
	
	options: {
		smoothTabs: 		true,
		externalLinks: 		true,
		convertTips:		true,
		columnHeights:		true,
		cufonText:			true,
		cufonColor:			'#bf0103',
		cufonSize:			'1.3',
		smoothScroll:		true,
		debug:				false
	},
	
	// INIT
	initialize: function(options) {		
		this.setOptions(options);	
		if (this.options.debug) dbug.enable();
		
		this.setFaqs();
		this.setTabs();
		this.setSlideshow();
		if (this.options.externalLinks) this.setLinks();
		if (this.options.convertTips) this.setTips();
		if (this.options.columnHeights) this.setColumnHeights();
		if (this.options.cufonText) this.setCufon();
		if (this.options.smoothScroll) this.setScroll();		
	},
	
	// SET TABS
	setTabs: function() {
		var bprTabs = new TabSwapper({
		  	selectedClass: 		'on',
		  	deselectedClass:	'off',
		  	tabs: 				$$('#tabBox li'),
		  	clickers: 			$$('#tabBox li a'),
		  	sections: 			$$('.panels div.panel'),
		  	
			/*remember what the last tab the user clicked was*/
		  	cookieName: 		'tabSet',
		  	
			/*use transitions to fade across*/
		  	smooth: 			true,
			smoothSize: 		true
		});
	},
	
	// FAQ SLIDES
	setFaqs: function() { // faq - requently asked questions
		
		var els = $$('dl#faq dd');
		if (els.length == 0) return false;
		
		var fx = els.map(function(el) {
			return new Fx.Slide(el, { 
				duration: 	'normal',
				mode: 		'vertical',
				transition:	Fx.Transitions.Quad.easeOut,
				wait: 		false
			}).hide();
		});	
		
		$$('dl#faq dt.toggler').each( function(el, idx) {			
			el.addEvent('click', function(evt) {
				evt.stop();
				fx[idx].toggle();				
			}).setStyle('cursor', 'pointer');	
		});	
	},

	// HIGHLIGHT EXTERNAL LINKS
	setLinks: function() {
		if(!this.options.externalLinks) return false;		
		$$('a[rel=external]').each(function(el) {
			if ($type(el.getFirst()) != 'element') el.addClass('external');
			el.set('target','_blank');
		});
	},
	
	// SET SLIDESHOW
	setSlideshow: function() {
		var sw = new SimpleViewer('slide');		
	},
	
	// SHOW METRIC VALUES
	setTips: function() {
		var els = $$('.inches', '.feet', '.yard', '.sqfeet', '.acres');
		if (els.length == 0) return false;
		$each(els, function(element){
			element.setStyle('cursor', 'pointer');
			var value = this._convert(parseFloat(element.get('text').replace(',','')), element.get('class'));
			element.store('tip:text', value);
		},this)
		var convertTips = new Tips(els, {className:'tip-convert', offsets: {'x': 12, 'y': 12}});
	},
	
	
	// EQUAL COLUMN HEIGHTS
	setColumnHeights: function() {
		var vals = [];
		var cols = $$('.unit div.eq');
		if (cols.length == 0) return false;
		$$('.unit div.eq').each(function(element){
			vals.push(element.getSize().y);
		});
		
		vals.sort(this._compare).reverse();
		var hsize = vals[0];
		cols.setStyle('height', hsize);
	},
	
	// CUFON
	setCufon: function() {
		if (!$defined(Cufon)) return false;
		
		// change color of boxed headings
		$$('.box h1','.box h2','.box h3','.box h4','.box h5','.box h6').filter(function(item){
			return !item.hasClass('title');
		}).setStyles({ 'color': this.options.cufonColor, 'padding-bottom': 0 });
		
		var arr = $$('h1','h2','h3','h4','h5','h6','dt.h3').filter(function(item){
			return !item.hasClass('skip-cufon');
		});
		
		arr.each(function(item){ 
			if (Browser.Engine.trident != true) {
				item.setStyles({
					'font-weight'	:'bold',
					'font-size'		: item.getStyle('font-size').toInt()*this.options.cufonSize
				});
			}	
			//alert(item.getStyle('font-size').toInt());
			Cufon.replace(item, { 'fontFamily': 'Bradley Hand ITC TT' });
 		}.bind(this));
	},
	
	// SMOOTH SCROLL
	setScroll: function() {
		new Fx.SmoothScroll({
			links: $$('a[href*=#]')
		});
	},
	
	_compare: function(a,b) {
		if ( a < b ) {return -1};
		if ( a > b ) {return 1}
		   
		return 0; // a == b	
	},
	
	// CONVERT IMPERIAL TO METRIC
	_convert: function(value, cname) {
		value = parseFloat(value);
		switch (cname){
			case 'inches': 
				value = (value*2.54).round(2) + 'cm';
				break;
			case 'feet': 
				value = (value*0.3048).round(2) + 'm';
				break;
			break;
			case 'yard': 
				value = (value*0.9144).round(2) + 'm';
				break;
			case 'sqfeet': 
				value = (value*0.0929).round(2) + 'm2';
				break;
			case 'acres': 
				value = (value*0.4047).round(2) + 'ha'+' = ' + (value*4047).round(2) +'m2';
				break;
			break;
			default : value = 'n/a';
		}		
		return value;
	}
});


// domready - init site
window.addEvent('domready', function() {							
	var site = new Site();	
});