/*  Author: Virid, Inc.
	
*/
BUCKETS = {//those guys on the home page
	currBucket: 1,
	autoTime: setTimeout( function(){BUCKETS.slideUp($('section#ftrHomeMagic a.homeBucket:eq(' + (BUCKETS.currBucket) + ')')) } , 5000 ),
	cleared: false,
	cancelTimer: function(domEl){
		clearTimeout(this.autoTime);
		BUCKETS.cleared = true;
		BUCKETS.slideUp(domEl);
	},
	slideUp: function(domEl){
		BUCKETS.currBucket++;
		$('section#ftrHomeMagic a.homeBucket').stop();
		$('section#ftrHomeMagic a.homeBucket').animate({top: '130',height:'210px'},100,'linear', function(){
			$(this).removeClass('active').find('span.txtBlue').toggleClass('txtBlue txtGreen');
		})
		domEl.animate({
			top: '0'
		}, 300, 'linear', function(){
			domEl.addClass('active');
			domEl.find('span.txtGreen').toggleClass('txtBlue txtGreen');
			if (!BUCKETS.cleared){
				BUCKETS.autoTime = setTimeout( function(){ BUCKETS.slideUp($('section#ftrHomeMagic a.homeBucket:eq(' + BUCKETS.currBucket + ')')) } , 5000 );
				if (BUCKETS.currBucket > 3){BUCKETS.currBucket = 0;}
			}
		});
	},
	slideDown: function(domEl){
		clearTimeout(this.autoTime);
		$('section#ftrHomeMagic article.homeBucket').stop();
		domEl.animate({
			top: '130'
		}, 500, 'linear', function(){
			domEl.removeClass('active').css('height','80px');
			domEl.find('span.txtBlue').toggleClass('txtBlue txtGreen');
		});
	}
}
VIRID = {
	contactText: null,
	contactHover: function(){
		$('input#txtContEmail').attr('value','email address');
		$('div#intContactWidget').delay(100).css('bottom','92px').show().animate({height: '112'},350,'linear');
	},
	contactOut: function(){
		$('div#intContactWidget').delay(200).animate({height: 0},150,'linear', function(){
			$('div#intContactWidget').css('bottom','0').hide();
			$('section#ftrContact').removeClass('active');
			$('input#txtContEmail').attr('value','');
			$('div#contWidgetBlurb').removeClass('txtPink txtBold').text(VIRID.contactText).addClass('txt999');
			$('input#txtContEmail').attr('value','email address');
		})
	},
	common: {
		init: function(){
			$('section#ftrContact').hover(
				function(){
					VIRID.contactHover();
				},
				function(){
					VIRID.contactOut();
				}
			);
			var defContText = 'email address';
			$('input#txtContEmail').focus(function(){
				$(this).attr('value','');
			});
			$('input#txtContEmail').blur(function(){
				if ($(this).val() == ''){
					$('input#txtContEmail').attr('value',defContText);
				}
			});
			$('a#btnContWidget').click(function(e){
				e.preventDefault();
				var emailAddr = $('input#txtContEmail').val();
				var blurb = $('div#contWidgetBlurb');
				blurb.removeClass('txtPink txtBold').addClass('txt999');
				var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(!filter.test(emailAddr)){
					blurb.removeClass('txt999').addClass('txtPink txtBold').text('Please enter a valid email address.');
				}else{
					$.ajax({
						data: 'emailAddr='+emailAddr,
						url: '_contact.aspx',//need the aspx file here,
						success: function(data){
							blurb.removeClass('txt999').addClass('txtPink txtBold').html(data);
							setTimeout(VIRID.contactOut, 2000);
						}
					});
				}
			});
		},
		finalize: function(){
			//window.log('VIRID.common.finalize();');
		}
	},
	bodyHome: {
		init: function(){
			//window.log('VIRID.bodyHome.init();');
			var bgNum = Math.floor(Math.random()*3);
			$('body#bodyHome').addClass('bg'+bgNum);
			$('div#newsIntro').tweet({
	    		username: 'viridtweets',
	    		join_text: 'auto',
	    		avatar_size: 32,
	    		count: 1,
	    		auto_join_text_default: '', 
	    		auto_join_text_ed: '',
	    		auto_join_text_ing: '',
	    		auto_join_text_reply: '',
	    		auto_join_text_url: '',
	    		loading_text: 'loading tweets&hellip;',
				template: '{time}{text}'
			});
		},
		bodyHome: function(){
			$('section#ftrHomeMagic a.homeBucket').hoverIntent(function(){BUCKETS.cancelTimer($(this));},function(){BUCKETS.slideDown($(this));});
		},
		finalize: function(){
			//window.log('VIRID.bodyHome.finalize();');
		}
	},
	bodyInterior: {
		defText: null,
		init: function(){
			$('div#contForm input.txtDef').each(function(){
				var inputEl = $(this);
				VIRID.bodyInterior.defText = inputEl.prev('label').text();
				inputEl.attr('value', VIRID.bodyInterior.defText)
				inputEl.bind({
					focus: function(){
						inputEl.attr('value','');
					},
					blur: function(){
						if (inputEl.val() == ''){
							inputEl.attr('value',inputEl.prev('label').text())
						}
					}
				})
			});
			$('input#chkDemo').uniform();
			$('select#selInterest').dropkick();
			VIRID.contactText = $('div#contWidgetBlurb').text();
		},
		bodyCampaign: function(){
			$('a#btnContWidget').click(function(e){
				e.preventDefault();
				/* TODO
					validate email, AJAX to something, success? failure? error?
				*/
				$('div#intContactWidget').delay(1000).hide();
				$('section#ftrContact').removeClass('active');
			});
		}
	},
	bodyMainNav: {
		init: function(){
			$('#jquery_jplayer_1').jPlayer({
				ready: function(){
					$(this).jPlayer('setMedia', {
						ogv: 'assets/vird_reel.ogv',
						mp4: '/assets/virid_reel.apple.mp4'
					});
				},
				size: {
					width: '217px',
					height: '150px'
				},
				swfPath: '/assets/js',
				supplied: "ogv,mp4",
				errorAlerts: true
			});
			$('div#someTweets').tweet({
	    		username: 'viridtweets',
	    		join_text: 'auto',
	    		avatar_size: 32,
	    		count: 3,
	    		auto_join_text_default: '', 
	    		auto_join_text_ed: '',
	    		auto_join_text_ing: '',
	    		auto_join_text_reply: '',
	    		auto_join_text_url: '',
	    		loading_text: 'loading tweets&hellip;',
				template: '{text}{time}'
			});
			$('div#featuresAcc').accordion();
		},
		bodyMainNav: function(){
			
		}
	},
	bodyWork: {
		$link: null,
		brand: null,
		$slider: null,
		$info: null,
		init: function(){
			var vars = [], hash;
    		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    		for(var i = 0; i < hashes.length; i++){
        		hash = hashes[i].split('=');
        		vars.push(hash[0]);
        		vars[hash[0]] = hash[1];
    		}
			if(vars.brand){
				this.brand = vars.brand;
				VIRID.bodyWork.$link = $('a#lnk'+this.brand);
				VIRID.bodyWork.$slider = $('div#slideHolder' + VIRID.bodyWork.brand);
				VIRID.bodyWork.$info = $('div#txt' + VIRID.bodyWork.brand);
				$('ul#ulWorkLogos li a').removeClass('active');
				VIRID.bodyWork.$link.addClass('active');
				$('div.slideHolder:visible').hide();
				$('div.workText:visible').hide();
				VIRID.bodyWork.$slider.show().easySlider({
					continuous: true,
					prevId: 'prevSlide'+VIRID.bodyWork.brand,
					nextId: 'nextSlide'+VIRID.bodyWork.brand,
					prevText: '',
					nextText: ''
				});
				VIRID.bodyWork.$info.show();
				
			}else{
				this.brand = $('div#workContent div.slideHolder:visible').attr('id').replace('slideHolder','');
			}
			$('div#workContent div.slideHolder:visible').easySlider({
				continuous: true,
				prevId: 'prevSlide'+VIRID.bodyWork.brand,
				nextId: 'nextSlide'+VIRID.bodyWork.brand,
				prevText: '',
				nextText: ''
			});
		},
		bodyWork: function(){
			$('ul#ulWorkLogos li a').click(function(e){
				e.preventDefault();
				VIRID.bodyWork.$link = $(this)
				VIRID.bodyWork.brand = VIRID.bodyWork.$link.attr('data-brand');
				VIRID.bodyWork.$slider = $('div#slideHolder' + VIRID.bodyWork.brand);
				VIRID.bodyWork.$info = $('div#txt' + VIRID.bodyWork.brand);
				window.log(VIRID.bodyWork.$link, ' ',VIRID.bodyWork.brand, ' ',VIRID.bodyWork.$slider, ' ',VIRID.bodyWork.$info);
				$('ul#ulWorkLogos li a').removeClass('active');
				VIRID.bodyWork.$link.addClass('active');
				$('div.slideHolder:visible').hide();
				$('div.workText:visible').hide();
				VIRID.bodyWork.$slider.show().easySlider({
					continuous: true,
					prevId: 'prevSlide'+VIRID.bodyWork.brand,
					nextId: 'nextSlide'+VIRID.bodyWork.brand,
					prevText: '',
					nextText: ''
				});
				VIRID.bodyWork.$info.show();
			});
		}
	}
}
UTIL = {
	fire : function(func,funcname,args){
		var namespace = VIRID;
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		}
	},
	loadEvents : function(){
		var bodyId = document.body.id;
		UTIL.fire('common');
		
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
			UTIL.fire(classnm,bodyId);
		});
		UTIL.fire('common','finalize');
	}
}
$(function(){
	UTIL.loadEvents();
});
