var HomeCore = new Class({
	initialize : function() {
		var i = 0;
		$( 'home-bubble' ).setStyle( 'opacity', 0 );
		$( 'home-bubble' ).fx = new Fx.Morph( $( 'home-bubble' ), { duration : 300 });
		$$( '#home-core ul li a' ).each( function( item ) {
			item.partnerImage = this.imageForIndex( i );
			if( i != 1 )
				item.partnerImage.setStyle( 'opacity', 0 );
			else
				this.currentImage = item.partnerImage;
			item.gfx = i + 1;
			item.fx = item.partnerImage.fx = new Fx.Morph( item.partnerImage, { duration : ( Browser.Version == 6 ? 0 : 800 ), transition : Fx.Transitions.linear } );
			item.addEvents({
				mouseover : this.mouseEvent.bind( this ),
				mouseout : this.mouseEvent.bind( this )
			})
			i++;
		}.bind( this ));
	},
	imageForIndex : function( index ) {
		return $( 'home-core-images' ).getChildren()[ index ];
	},
	mouseEvent : function( e ) {
		if( e.type == 'mouseover' ) {
			$( 'home-bubble' ).style.background = 'url( images/quote' + e.target.gfx +  ( Browser.Version == 6 ? '.gif' : '.png' ) + ' ) no-repeat';
			$( 'home-bubble' ).fx.cancel();
			$( 'home-bubble' ).fx.start({
				'opacity' : 1
			});
			$( 'home-bubble' ).setStyle( 'top', e.target.getPosition().y - $( 'home-bubble' ).getSize().y );
		} else {
			$( 'home-bubble' ).fx.cancel();
			$( 'home-bubble' ).fx.start({
				'opacity' : 0
			});
		}
		if( this.currentImage == e.target.partnerImage || this.isMorphing )
			return;
		this.isMorphing = true;
		this.currentImage.fx.start({ 'opacity' : 0 });
		e.target.partnerImage.fx.start({ 'opacity' : 1 }).chain( function() { 
			this.isMorphing = false;
		}.bind( this ));
		this.currentImage = e.target.partnerImage;
	}
})
var xSlide = new Class({
	
	target : null,
	duration : 500,
	container : null,
	amount : null,
	time : 3000,
	timer : null,
	direction : 'left',
	currentSlide : 0,
	
	initialize : function( options ) {
		for( var key in options )
			this[key] = options[key];
		this.target.setStyles({
			position : 'relative',
			overflow : 'hidden',
			height : 300,
			width : 180
		});
		this.container = new Element( 'div', {
			styles : {
				position : 'absolute',
				top : 0,
				left : 0,
				width : this.target.getChildren().length * this.amount
			}
		});
		maxHeight = 0;
		$each( this.target.getChildren(), function( item ) {
			item.setStyle( 'float', 'left' );
			item.setStyle( 'width', 180 );
			if ( item.getSize().x > maxHeight) {
				maxHeight = item.getSize().x;
			}
			item.injectInside( this.container );
		}.bind( this ));
		this.target.setStyle('height',(maxHeight+5));
		this.container.injectInside( this.target );
		if (this.amount == null) {
			this.amount = this.target.getSize().x;
		}
		this.start();
	},
	
	start : function() {
		this.timer = this.slide.periodical( this.time, this );
	},
	
	stop : function() {
		$clear( this.timer );
	},
	
	slide : function() {			
		new Fx.Morph( this.container, { duration : this.duration, transition: Fx.Transitions.Expo.easeOut } ).start({
			left : this.container.getStyle( 'left' ).toInt() - this.amount
		}).chain( function() {
			this.container.getChildren()[0].injectInside( this.container );
			this.container.setStyle( 'left', 0 );
		}.bind( this ));
	}
})
window.addEvent( 'domready', function() {
	new HomeCore();
	for( var i = 1; i <= 4; i++ ) {
		var a = new Image();
		a.src = 'images/quote' + i + '.png';
	}
	new xSlide({
		target : $('boxswipe'),
		amount : 180,
		time : 7000
		})

})


function officeSelector () {
	var officeShown = document.forms['officeselect'].elements['officevalue'].value;
	if (officeShown=='all') {
		self.location = 'contact-us/index.php';
		return false;
	}
	if (officeShown) {
		officeShown = officeShown;
	}
	officeShown = officeShown.replace(/\|/g,"<br />");
	officeShown = officeShown.replace(/\[\[/,"<a href=\"mailto:");
	officeShown = officeShown.replace(/{{/,"<a href=\"");
	officeShown = officeShown.replace(/]]/g,'" class="bullet_green">');
	officeShown = officeShown.replace(/{}/g,'</a>');
	$('home-map-info').set('html',officeShown);
}