
var AODBT = {};
AODBT.confettiArr = [];

AODBT.confettiArr[0] = 'confetti8.jpg';
AODBT.confettiArr[1] = 'confetti2.jpg';
AODBT.confettiArr[2] = 'confetti7.jpg';
AODBT.confettiArr[3] = 'confetti1.jpg';
AODBT.confettiArr[4] = 'confetti4.jpg';
AODBT.confettiArr[5] = 'confetti3.jpg';
AODBT.confettiArr[6] = 'confetti5.jpg';
AODBT.confettiArr[7] = 'confetti9.jpg';
AODBT.confettiArr[8] = 'confetti6.jpg';


AODBT.lastConf = 'confetti';

AODBT.Tt = new Class({
	Implements: [Options, Events],
	options: {
		elem: ''
	},
	initialize: function(options){
		this.setOptions(options);
		this.addTips(this.options.elem);
	},
	addTips: function(el){
		$$('.tTip').dispose();
		$$(el).each(function(item, index){
			item.coo = item.getCoordinates();
			
			var tt = item.get('id').split('::');
			item.title = tt[0];
			//item.store('tip-title', tt[0]);
			if(tt[1] != null){
				item.text = tt[1];
				//item.store('tip-text', tt[1]);
			}else{
				item.text = '';
				//item.store('tip-text', '');
			}
			
			item.tipbg = new Element('div', {
				'class': 'tip-bg',
				'id': 'tipBG',
				'styles': {
					'max-width': (item.coo.width - 25) + 'px',
					'min-width': (item.coo.width - 25) + 'px'
				},
				'html': '<span class="tip-title">' + item.title + '</span><br><span class="tip-text">' + item.text + '</span>'
			});
						
			item.tipwedge = new Element('img',{
				src: 'img/styles/red/tip/tipwedge.png',
				alt: '',
				width: '10px',
				height: '10px',
				border: '0'
			});
			
			item.tip = new Element('div', {
				'class': 'tTip',
				'styles': {
					'position': 'absolute',
					'top': (item.coo.top - 10),//(item.coo.top - item.coo.height - 10),
					'left': (item.coo.left - 10),
					'opacity': 0
				}
			});
			
			item.tip.inject(item, 'after');
			item.tipbg.inject(item.tip);
			item.tipwedge.inject(item.tip);
						
			
			item.fadeOut = new Fx.Morph(item.tip, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
			item.fadeIn = new Fx.Morph(item.tip, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
			
			item.addEvents({
				'mouseenter': function(){
					Cufon.replace('.tip-title');
					Cufon.replace('.tip-text');
					this.fadeOut.pause();
					this.fadeIn.start({
						'opacity': 1
					});
				},
				'mouseleave': function(){
					this.fadeIn.pause();
					
					this.fadeOut.start({
						'opacity': 0
					});
				}
			});
		});
	}
});

AODBT.thumbnails = new Class({
	Implements: [Options, Events],
	options: {
		elem: ''
	},
	initialize: function(options){
		this.setOptions(options);
		this.goThumb(this.options.elem);
	},
	
	goThumb: function(el){
		$(el).setStyles({
			'opacity': 0.8,
			'margin-top': '-' + ($(el).getHeight()) + 'px'
		});
		
		$(el).fadeOut = new Fx.Morph(el, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
		$(el).fadeIn = new Fx.Morph(el, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
		
		goF = function(){
			$(el). fadeOut.start({
				'opacity': 0.01
			});
		}
		
		goF.delay(1000);
		
		$(el).addEvents({
			'mouseenter': function(){
				$(el).fadeOut.pause();
				$(el).fadeIn.start({
					'opacity': 0.8
				});
			},
			'mouseleave': function(){
				$(el).fadeIn.pause();
				$(el).fadeOut.start({
					'opacity': 0.01
				});
			}
		});
		
		$$('#' + el + ' a').each(function(item){
			item.addEvents({
				'click': function(e){
					e.stop();
					$$('.img-holder img').set('src', this.get('ref'));
				}
			});
		});
	}

});

window.addEvent('domready', function(){
	Cufon.replace('.lin', {hover: true});
	Cufon.replace('.toggler');
	Cufon.replace('.contact-footer');
	Cufon.replace('.foot-grey');
	Cufon.replace('.foot-heavy');
	Cufon.replace('h1');
	Cufon.replace('.error');
	
	AODBT.bodyElement = $(document.body);
	
	/*** NAVIGATION CONTROLLER ***/
	AODBT.currentSection = AODBT.bodyElement.get('id') - 1;
	
	AODBT.addToggles = function(name){
		$$('.toggler').addEvents({
			'mouseenter': function(){
				//this.fireEvent('click');
			},
			'click': function(){
			}
		});
	}
	
	AODBT.fadeToggles = function(el, fadeTo){
		if(AODBT.currentSection >= 0){
			if(el.fader){
				el.fader.pause();
			}
			
			/*el.fader = new Fx.Morph(el, {duration: 500, transition: Fx.Transitions.Sine.easeOut}).start({
				'opacity': fadeTo
			});*/
			el.morph({
				'opacity': fadeTo,
				'duration': 500
			});			
		}else{
			$$('.toggler').each(function(item){
				if(item.fader){
					item.fader.pause();
				}
				if(item != el && el != 'none'){
					/*item.fader = new Fx.Morph(item, {duration: 500, transition: Fx.Transitions.Sine.easeOut}).start({
						'opacity': 0.4
					});*/
					item.morph({
						'opacity': 0.4,
						'duration': 500
					});
				}else if(item == el){
					/*item.fader = new Fx.Morph(item, {duration: 500, transition: Fx.Transitions.Sine.easeOut}).start({
						'opacity': 1
					});*/
					item.morph({
						'opacity': 1,
						'duration': 500
					});
				}else if(el == 'none'){
					/*item.fader = new Fx.Morph(item, {duration: 500, transition: Fx.Transitions.Sine.easeOut}).start({
						'opacity': 1
					});*/
					item.morph({
						'opacity': 1,
						'duration': 500
					});
				}
			});
		}
	}
	
	AODBT.navigation = new Accordion($('navigation'),'span.toggler', 'div.menu',{
		display: AODBT.currentSection,
		opacity: 0.5,
		onActive: function(toggler){
			$$('.toggler').removeEvents('mouseenter');
			AODBT.fadeToggles(toggler, 1);
		},
		onBackground: function(toggler){
			if(AODBT.currentSection >= 0){
				AODBT.fadeToggles(toggler, 0.4);
			}
		},
		onComplete: function(){
			AODBT.addToggles.delay(2000);
		}
	});
	
	AODBT.hider = '';
	
	AODBT.hideNav = function(){
		AODBT.navigation.display(AODBT.currentSection);
		if(AODBT.currentSection < 0){
			AODBT.fadeToggles('none');
		}
	}
	
	$$('.toggler').addEvent('mouseenter', function(){
			//this.fireEvent('click');
	});
	
	/*** MOUSE OUT OF AREA ***/
	if($('navigation')){
		$('navigation').addEvents({
			'mouseenter': function(){
				if(AODBT.hider != ''){
					AODBT.hider = $clear(AODBT.hider);
				}
			},
			'mouseleave': function(){
				AODBT.hider = AODBT.hideNav.delay(2000);
			}
		});
	}
	/***END NAVIGATION CONTROLLER ***/
	
	/*** CONFETTI ***/
	AODBT.confetti = function(){
		var rand = function(n){
		  return ( Math.floor ( Math.random ( ) * n + 1 ) );
		}

		var picture = new Asset.image('img/styles/red/' + AODBT.confettiArr[rand(AODBT.confettiArr.length) - 1], {
			alt: '',
			id: 'oldimage',
			onload: function(){
				if(AODBT.lastConf == 'confetti2'){
					$('confetti').empty();
					this.inject($('confetti'));
					AODBT.lastConf = 'confetti';
					var confFade1 = new Fx.Morph('confetti', {duration: 2000, transition: Fx.Transitions.Sine.easeOut}).start({'opacity': 1.0});
					var confFade2 = new Fx.Morph('confetti2', {
						duration: 2000,
						transition: Fx.Transitions.Sine.easeOut,
						onComplete: function(){
							AODBT.confetti.delay(10000);
						}
					}).start({'opacity': 0.0});
				}else{
					$('confetti2').empty();
					this.inject($('confetti2'));
					AODBT.lastConf = 'confetti2';
					var confFade1 = new Fx.Morph('confetti', {duration: 2000, transition: Fx.Transitions.Sine.easeOut}).start({'opacity': 0.0});
					var confFade2 = new Fx.Morph('confetti2', {
						duration: 2000,
						transition: Fx.Transitions.Sine.easeOut,
						onComplete: function(){
							AODBT.confetti.delay(10000);
						}
					}).start({'opacity': 1.0});
				}	
			}
		});
	}
	
	AODBT.confetti.delay();
	/*** END CONFETTI ***/
	
	/*** TOOL TIPS ***/	
	AODBT.tooltips = new AODBT.Tt({elem: '.tooltips'});
	/*** END TOOL TIPS ***/
	
	if($('thumb-holder')){
		AODBT.thumbs = new AODBT.thumbnails({elem: 'thumb-holder'});
	}
	
	/*** FORMS ***/
	$$('.button').addEvents({
		'mouseenter': function(){
			var srcFinal = '';
			var src = this.get('src');
			var srcArr = src.split('.');
			for(i=0; i < (srcArr.length - 1); i++){
				srcFinal += srcArr[i] + '.';
			}
			srcFinal += 'over.' + srcArr[srcArr.length - 1];
			this.set('src', srcFinal);
		},
		'mouseleave': function(){
			var srcFinal = '';
			var src = this.get('src');
			var srcArr = src.split('.');
			srcArr.splice((srcArr.length - 2), 1);
			for(i=0; i < (srcArr.length - 1); i++){
				srcFinal += srcArr[i] + '.';
			}
			srcFinal += srcArr[srcArr.length - 1];
			this.set('src', srcFinal);
		}
	});
	
	if($('browse')){
		/*$('browse').addEvent('click', function(e){
			e.stop();
			alert('attach resume');
		});*/
		
		AODBT.up = new FancyUpload3.Attach('attach', '#browse', {
			path: 'js/Swiff.Uploader.swf',
			url: '_aodbt_sys/upload.script.php5',
			method: 'post',
			fileSizeMax: 2 * 1024 * 1024,
	 		queued: false,
			verbose: true,
			multiple: false,
			allowDuplicates: true,
			
			onBeforeStart: function(file){
				AODBT.up.setOptions({data: {name: $$('input[name=name]').getProperty('value')}});
			},
	 
			onSelectFail: function(files) {
				this.list.empty();
				files.each(function(file) {
					new Element('span', {
						'class': 'error',
						events: {
							click: function() {
								this.destroy();
							}
						}
					}).adopt(
						new Element('span', {html: file.validationErrorMessage || file.validationError})
					).inject(this.list, 'bottom');
				}, this);
				Cufon.replace('.error');	
			},
	 
			onFileSuccess: function(file) {
				var srcFinal = '';
				var src = $('browse').get('src');
				var srcArr = src.split('.');
				srcArr.splice((srcArr.length - 2), 1);
				for(i=0; i < (srcArr.length - 1); i++){
					srcFinal += srcArr[i] + '.';
				}
				srcFinal += srcArr[srcArr.length - 1];
				$('browse').set('src', srcFinal);
			},
	 
			onFileError: function(file) {
				file.ui.cancel.set('html', 'Retry').removeEvents().addEvent('click', function() {
					file.requeue();
					return false;
				});
	 
				new Element('span', {
					html: file.errorMessage,
					'class': 'error'
				}).inject(file.ui.element, 'after');
				Cufon.replace('.error');
			},
	 
			onFileRequeue: function(file) {
				file.ui.element.getElement('.error').destroy();
	 
				file.ui.cancel.set('html', 'Cancel').removeEvents().addEvent('click', function() {
					file.remove();
					return false;
				});
	 
				//this.start();
			}
	 
		});

	}
	/*** END FORMS ***/
	
	/*** NO THANKS ***/
	/*$$('.no_thanks').addEvent('click', function(e){
		e.stop();
		$$('.browser-msg').setStyle('display', 'none');
	});*/
	/*** END NO THANKS ***/
	
	/*** RCC SLIDE ***/
	if($('rcc')){
		AODBT.mySlide = new Fx.Slide('rcc', {duration: 500});
		AODBT.mySlide.hide();
		
		$('rcc_toggle').addEvent('click', function(e){
			e.stop();
			AODBT.mySlide.toggle();
		});
	}
	/*** END RCC SLIDE ***/
	
	/*** X POSITION ***/
	AODBT.positionX = function(){
	$('close_img').setStyle('display', 'none');
		$('work_img').addEvent('load', function(){
			this.xPos = $('work_img').getSize().y;
			
			$('close_img').setStyle('margin-top', '-' + this.xPos + 'px');
			$('close_img').setStyle('display', '');
			$('work_name').setStyle('margin-top', this.xPos + 20 + 'px');
		});
	}
	if($('work_img')){
		//AODBT.positionX();
	}
	/*** END X POSITION ***/
	
});/*** END window.addEvent ***/

window.addEvent('resize', function(){
	AODBT.tooltips = new AODBT.Tt({elem: '.tooltips'});
});
