/* -------------------------------------------------------------------- */
/* NET4VISIONS.COM Website - main.js 04-08-2009 */
/* Version: 1.0 */
/* Created by: net4visions.com */
/* Last edited: 11-06-2009 */
/* Requirements: mootools 1.2.4.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,
		convertCode:		true,
		debug:				false
	},	
	
	// INIT
	initialize: function(options) {		
		this.setOptions(options);	
		if (this.options.debug) dbug.enable();
		
		this.setFaqs();
		this.setTabs();
		this.setSlideshow();
		this.setGallery();
		this.setStream();
		if (this.options.externalLinks) this.setLinks();
		if (this.options.convertTips) this.setTips();
		if (this.options.convertCode) this.setCode();
		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');		
	},
	
	// SET GALLERY
	setGallery: function() {
		var ig01 = new IGallery('gallery-01');
		var ig02 = new IGallery('gallery-02', { start: 0 });
	},
	
	// SET COVER
	setStream: function() {
	
		if (!$('streamWidget')) return false;
		// set sound controls
	    this.isCollapsed = true;
	    
		var fxTween = new Fx.Tween('streamWidgetWrap', {
			duration: 1500,
			onComplete: function(e) {
				this.isCollapsed = this.isCollapsed ? false : true;
				var src = this.isCollapsed ? './assets/site/images/info-open.png' : './assets/site/images/info-close.png';
				this.infoEl.set('src', src);
			}.bind(this)
		}).set('margin-top', 250);
	    
	    this.infoEl  = $('btn-info');
	    this.playEl  = $('btn-play');
	    this.stopEl  = $('btn-stop');
	    this.muteEl  = $('btn-mute');
	    this.pauseEl = $('btn-pause');
	    
	    this.setSound();
	   
	   	// add event to info button
		this.infoEl.addEvent('click', function(e){
			var fromTo = this.isCollapsed ? [250, 0] : [0, 250];
			fxTween.start('margin-top', fromTo);
		}.bind(this))
		
		// current stream info
		var stream1 = new Stream('streamWidget', {
			history: 	false,
			size: 		'SmallImage',
			altCover: 	'false',
			spinner: 	false,
			onCallback: function(){
				this.setLinks();
			}.bind(this)
		});
		
		// stream history
		var stream1 = new Stream('streamWidgetHistory', {
			current: 	false,
			history: 	true,
			size: 		'MediumImage',
			altCover:	'false'
		});
	},
	
	// 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}});
	},
	
	// SETUP CODE FIELDS
	setCode: function() {
		$$('code.php', 'code.js').each(function(el){
			new Lighter(el, {
				altLines: 'hover',
				mode: 'ol'//,
				//indent: 5,
				//clipboard: true
			});
		})
	},
	
	// 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').filter(function(item){
			return !item.hasClass('skip-cufon');
		});
		
		arr.each(function(item){ 
			if (Browser.Engine.trident != true) {
				item.setStyles({
					'font-weight'	:'bold',
					'font-size'		: parseInt(item.getStyle('font-size'))*this.options.cufonSize
				});
			}
			Cufon.replace(item, { 'fontFamily': 'Bradley Hand ITC TT' });
 		}.bind(this));
	},
	
	// SMOOTH SCROLL
	setScroll: function() {
		new Fx.SmoothScroll({
			links: $$('a[href*=#]')
		});
	},
	
	// set JW Player (http://www.longtailvideo.com/)
	setSound: function() {
		if(!$defined($('streamWidget'))) return false;
		if (!this._checkFlash()) { this._onErrorPlayer(); return false; }
		
		var script = Asset.javascript('./assets/site/js/jwplayer/swfobject.js', {
			onload: function() {
				var player = './assets/site/js/jwplayer/player.swf';
				var file   = 'http://rs3.radiostreamer.com:9330/;*.nsv';
				var target = new Element('div').inject($('soundControls'));	
				var swfObj = new SWFObject(player,'jwPlayer','1','1','9.0.124');
				swfObj.addParam('allowfullscreen',		'false');
			  	swfObj.addParam('allowscriptaccess',	'always');
			  	swfObj.addParam('wmode',				'opaque');
			  	
			  	swfObj.addVariable('id',				'jwPlayer');
			  	swfObj.addVariable('controlbar',		'none');
			  	swfObj.addVariable('duration',			'5400');
			  	swfObj.addVariable('file', 				file);
			  	swfObj.addVariable('type', 				'sound');
			  	swfObj.write(target);
			  	// IE 8 fix - if hidden before inserting jwPlayer doesn't work
			  	target.setStyles({height: 0, visibility: 'hidden', overflow: 'hidden'});
			}.bind(this)
		});
	},
	
	setPlayer: function(playerObj) {
		if (!$chk(playerObj)) { this._onErrorPlayer(); return false; }
		
		this.player = $(playerObj.id);
		
		this.play 	= false;
		this.mute 	= false;
		var path 	= './assets/site/images/';
		//console.log('setPlayer');
		// set default volume
		//this.player.sendEvent('MUTE');
		//this.player.sendEvent('PLAY', 'file');
		//this.player.sendEvent('SEEK', 10);
		this.player.sendEvent('VOLUME', 50);		
		
		// toggle play
		this.playEl.addEvent('click', function(e) {
			var src = this.play ? 'btn-play.png' : 'btn-pause.png';
			this.playEl.set('src', path + src);
			this.player.sendEvent('PLAY'); // toggle PLAY
			this.play = this.play ? false : true;
		}.bind(this));
		
		// stop play
		this.stopEl.addEvent('click', function(e) {
			this.playEl.set('src', path + 'btn-play.png');
			this.player.sendEvent('STOP');
			this.pause = false;
		}.bind(this));
		
		// toggle mute
		this.muteEl.addEvent('click', function(e) {
			var src = this.mute ? 'btn-mute-on.png' : 'btn-mute-off.png';
			this.muteEl.set('src', path + src);
			this.player.sendEvent('MUTE');
			this.mute = this.mute ? false : true;
		}.bind(this));
	},
	
	// jw player not available
	_onErrorPlayer: function() {
		$('soundControls').getElements('img.ctlSound').hide();
	},
		
	// check whether Flash 9 or higher is available
	_checkFlash: function() {
		if(Browser.Plugins.Flash.version < 9) {
			if(confirm("You do not meet the Adobe Flash requirements. You need atleast version 9. You currently have version: "+Browser.Plugins.Flash.version+"\nWould you like to download the latest version of Flash now? (highly recommended)")) {
				window.location	=	"http://get.adobe.com/flashplayer/";	
			}
			
			return false;
		} else {
			return true;
		}
	},		

	
	_compare: function(a,b) {
		if ( a < b ) {return -1};
		if ( a > b ) {return 1}
		   
		return 0; // a == b
	}
});

function playerReady(playerObj) {
	Site.setPlayer(playerObj);
};

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

