/*
mercury.fx.pack.js, effects for mercurymarine.com
Thursday, Jun 28, 2007
v 1.3.0
*/
var featuresWidget = new Class({
	fwLineFX	: Array,
	fwBBoxFX	: Object,
	fwLineBoxFX	: Object,
	fwIntroBox	: Object,

	initialize: function(jsonURL,bulletContainerId,detailContainerId,options) {
		this.bulletContainerId = bulletContainerId;
		this.detailContainerId = detailContainerId;
		this.options = Object.extend({
			onComplete: Class.empty
		}, options || {});
		this.xhr = new Ajax(jsonURL, { method: 'get', onComplete: this.renderFeatureBullets.bind(this) } ).request();
	}, // constructor..initialize

	renderFeatureBullets: function(responseText) {
		//-- we're back from the break...
		try {
			this.engineData = Json.evaluate(responseText);
		} catch(e) {
			alert("featuresWidget :: There was difficulty in retriving data for this feature.");
			return false;
		} // try..catch

		//-- set up bullets
		var html = '';
		this.engineData.engine.features.each(
			function(feature,featureIdx) {
				html += '<div style="top: '+(feature.bulletCoords.top-15)+'px; left: '+(feature.bulletCoords.left-15)+'px;" class="bullet"><a href="javascript:void(0);" id="'+this.bulletContainerId+'bullet_'+featureIdx+'"></a></div>';
			}.bind(this) // function..feature
		); // each..features
		new Element('div').injectInside(this.bulletContainerId).setHTML(html);

		//-- link up event handlers
		this.engineData.engine.features.each(
			function(feature,featureIdx) {
				$(this.bulletContainerId+'bullet_'+featureIdx).onclick = function() {
					this.renderFeaturePane(featureIdx);
				}.bind(this);
			}.bind(this) // function..feature
		) // each..features

		$(this.detailContainerId).setStyle('opacity',0); //-- init state
		if($('detailCloseAnchor')) {
			$('detailCloseAnchor').onclick = function() { this.closeBulletBox(); }.bind(this);
		} // if

		this.options.onComplete();
	}, // method..renderFeatureBullets

	closeBulletBox: function() {
		this.fwBBoxFX = new Fx.Styles(this.detailContainerId, {duration: this.options.duration});
		for(i=0;i<3;i++) {
			this.fwLineFX[i] = new Fx.Styles('lines_'+i, {duration: this.options.duration});
		} // for
		this.fwIntroBox = new Fx.Styles('introcopy', {duration:this.options.duration});

		this.fwBBoxFX.custom({
			'opacity':[1,0]
		});
		for(i=0;i<3;i++) {
			this.fwLineFX[i].custom({
				'opacity':[1,0]
			});
		} // for
		this.fwIntroBox.custom({
			'opacity':[0,1]
		});

		this.clearBulletStates();
	}, // method..closeBulletBox

	clearLines: function() {
		if($('lineContainer')) $('lineContainer').remove();
	}, // method..clearLines

	clearBulletStates: function() {
		for(i=0;i<this.engineData.engine.features.length;i++) $(this.bulletContainerId+'bullet_'+i).removeClass('on');
	}, // method..clearBulletStates

	renderFeaturePane: function(currentFeatureIdx) {
		feature = this.engineData.engine.features[currentFeatureIdx];

		if($('introcopy').getStyle('opacity')!=0) {
			this.fwIntroBox = new Fx.Styles('introcopy', {duration:this.options.duration});
			this.fwIntroBox.custom({
				'opacity':[1,0]
			});
		} // if

		//-- set bullet state to on
		this.clearBulletStates();
		$(this.bulletContainerId+'bullet_'+currentFeatureIdx).addClass('on');

		//-- update bulletbox
		if(feature.image!='') {
			$$('#'+this.detailContainerId+' .pic').setStyle('width','187px');
			$$('#'+this.detailContainerId+' .pic').setStyle('backgroundImage',"url("+this.engineData.engine.imagePath+feature.image+")");
		} else {
			$$('#'+this.detailContainerId+' .pic').setStyle('width','0');
			$$('#'+this.detailContainerId+' .pic').setStyle('backgroundImage','');
		} // if..else

		$$('#'+this.detailContainerId+' .detailTitle').setHTML(feature.detailTitle);
		$$('#'+this.detailContainerId+' .detailDescription').setHTML(feature.detailDescription);

		if($(this.detailContainerId).getStyle('opacity')==0) {
			this.fwBBoxFX = new Fx.Styles(this.detailContainerId, {duration: this.options.duration});
			this.fwBBoxFX.custom({
				'opacity':[0,1]
			});
		} // if

		//-- create lines
		this.clearLines();
		html  = '';
		html += '<div style="top: '+feature.bulletCoords.top+'px; left: '+feature.bulletCoords.left+'px; width: '+(this.options.lineEntryCoords[0]+1-feature.bulletCoords.left)+'px; height: 1px; border-width: 1px 0 0 0;" class="lines" id="lines_0"></div>';
		if(feature.bulletCoords.top < this.options.lineEntryCoords[1]) {
			html += '<div style="top: '+feature.bulletCoords.top+'px; left: '+this.options.lineEntryCoords[0]+'px; width: 1px; height: '+(this.options.lineEntryCoords[1]-feature.bulletCoords.top)+'px; border-width: 0 1px 0 0;" class="lines" id="lines_1"></div>';
		} else {
			html += '<div style="top: '+this.options.lineEntryCoords[1]+'px; left: '+this.options.lineEntryCoords[0]+'px; width: 1px; height: '+(feature.bulletCoords.top-this.options.lineEntryCoords[1]+1)+'px; border-width: 0 1px 0 0;" class="lines" id="lines_1"></div>';
		} // if..else
		html += '<div class="daline" id="lines_2"></div>';

		new Element('div').injectInside(this.bulletContainerId).setHTML(html).setProperty('id','lineContainer');

		for(i=0;i<3;i++) {
			this.fwLineFX[i] = new Fx.Style('lines_'+i, 'border-color', {duration: this.options.duration});
			this.fwLineFX[i].custom('FFFFFF',this.options.lineColor);
		} // for
	} // method..renderFeaturePane

}); // featuresWidget..class

//---------------------------------------------------------------

var simpleSlideShow = new Class({
	initialize: function(foreImg, backImg, imgLink, imgList) {
		this.foreImg = $(foreImg);
		this.backImg = $(backImg);
		this.imgLink = $(imgLink);
		this.currentIdx = 0;
		this.imgList = imgList;
	}, // constructor..initialize

	step: function() {
		this.backImg.src = null; //-- so Safari fires an onload if .src's are identical by chance
		this.backImg.onload = function() {
			this.fwOpacity = new Fx.Style(this.foreImg, 'opacity', { duration: this.imgList[this.currentIdx].duration });
			this.fwOpacity.set(0);
			setTimeout(function() {
				this.foreImg.onload = function() {
					this.fwOpacity.custom(0,1);
				}.bind(this);
				this.foreImg.src = this.imgList[this.currentIdx].image;
				if((this.imgLink!=undefined) && (this.imgList[this.currentIdx].link!='')) {
					this.imgLink.href = this.imgList[this.currentIdx].link;
				} // if
				this.currentIdx = (this.currentIdx+1) % this.imgList.length;
				this.timer = setTimeout(this.step.bind(this),this.imgList[this.currentIdx].linger+this.imgList[this.currentIdx].duration+20);
			}.bind(this)
			,20);
		}.bind(this);
		this.backImg.src = this.foreImg.src;
	}, // method..step

	shuffle: function() {
	  var i = this.imgList.length;
	  if ( i == 0 ) return false;
	  while ( --i ) {
	     var j = Math.floor( Math.random() * ( i + 1 ) );
	     var tempi = this.imgList[i];
	     var tempj = this.imgList[j];
	     this.imgList[i] = tempj;
	     this.imgList[j] = tempi;
	   }
	}, // method..shuffle

	pause: function() {
		clearTimeout(this.timer);
	}, // method..pause

	resume: function() {
		this.step.bind(this);
	}, // method..resume

	start: function() {
		this.currentIdx = 0;
		this.timer = setTimeout(this.step.bind(this), this.imgList[this.currentIdx].linger);
	} // method..start
}); // class..simpleSlideShow

//---------------------------------------------------------------
