var a = {
	init:function(){
		jc.ui.slideShow.interval = 6500;
		jc.init();
        a.fancybox.init();
		
		if (typeof $.fancybox !== 'undefined') {
			$(".imgGallery a").fancybox({
				'overlayShow'	: true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition'	: 'inside',
				'overlayColor'	: '#000',
				'overlayOpacity': 0.7
			});
		}

		//a.mMenu.init();
		a.brandTicker.init();
		a.ui.init();
	},
	ui:{
		init:function(){
			// fix no border li's
			$('.mMenu ul.left li').last().addClass('noBorder');
			$('.mMenu ul.right li').last().addClass('noBorder');
		}
	},
	mMenu:{
		init:function(){
			$('.mMenuExpandTrigger').mouseover(function(){ a.mMenu.show($(this)); });
			$('.mMenuExpandTrigger, .mMenuExpand').mouseout(function(e){
				if ($(e.relatedTarget).closest('.mMenuExpandTrigger, .mMenuExpand, .mMenu ul.left').length == 0)
					a.mMenu.hide($(this).find('.mMenuExpand')); 
			});
			$(document).click(function(){
				$('.mMenuExpand .box').slideUp('fast');
			});
		},
		show:function(e){
			var c = e.attr('class');
			c = '.'+c.replace('mMenuExpandTrigger','box').replace(' ','.'); // build selector
			$('.box').not(c).slideUp('fast'); // hide all others
			$(c).slideDown('fast');
		},
		hide:function(e){
			var c = e.attr('class');
			c = '.'+c.replace('mMenuExpandTrigger','box').replace(' ','.'); // build selector
			$(c).slideUp('fast');
		}
	},
	brandTicker:{
		timer:null,
		strength:0,
		scrollTolerance:350, // pixel distance from left and right that initiates scrolling
		tickerTape:null,
		width:null,
		init:function(){
			a.brandTicker.tickerTape = $('.brandTicker .brands');
			$('.brandTicker').mousemove(function(e){ a.brandTicker.trackMouse($(this), e); });
			$('.brandTicker').mouseout(function(e){
				if ($(e.relatedTarget).closest('.brandTicker').length == 0)
					a.brandTicker.stopTrackMouse(e);
			});
		},
		trackMouse:function(t, e){
			var mX = e.pageX-t.offset().left;
			var min = 0;
			var max = 980;
			var grace = 60; // how far past the min/max will we allow them to scroll
			if (mX < (min+a.brandTicker.scrollTolerance)) {
				a.brandTicker.strength = (min+a.brandTicker.scrollTolerance)-mX;
				a.brandTicker.startTimer();
			} else if (mX > (max-a.brandTicker.scrollTolerance)) {
				a.brandTicker.strength = -(mX-(max-a.brandTicker.scrollTolerance));
				a.brandTicker.startTimer();
			} else {
				a.brandTicker.strength = 0;
				a.brandTicker.stopTimer();
			}
		},
		stopTrackMouse:function(){
			a.brandTicker.stopTimer();
		},
		startTimer:function(){
			if (a.brandTicker.width == null)
				a.brandTicker.calculateWidth();
			if (a.brandTicker.timer == null)
				a.brandTicker.timer = setInterval(function(){ 
					l2 = a.brandTicker.tickerTape.position().left+(a.brandTicker.strength/30);
					if (l2 > 60) { a.brandTicker.stopTimer(); return; }
					if (l2 < -(a.brandTicker.width-250)) { a.brandTicker.stopTimer(); return; }
					a.brandTicker.tickerTape.css({left:l2+'px'});
				},10);
		},
		stopTimer:function(){
			if (a.brandTicker.timer != null) {
				clearInterval(a.brandTicker.timer);
				a.brandTicker.timer = null;
			}
		},
		calculateWidth:function(){
			var w = 0;
			$.each(a.brandTicker.tickerTape.find('img'),function(i,e){
				w += $(e).width();														
			});
			a.brandTicker.width = w;
		}
	},
	appointment:{
		validate:function(){
			if ($('#firstName').val() == '') {
				alert('Please provide us with your first name.');
				return false;
			}
			
			if ($('#lastName').val() == '') {
				alert('Please provide us with your last name.');
				return false;
			}
			
			if ($('#email').val() == '' || !jc.form.validate.email($('#email').val())) {
				alert('Please provide us with a valid email.');
				return false;
			}
			
			if ($('#phone1').val() == '') {
				alert('Please provide us with a phone number where we can reach you.');
				return false;
			}
			
			if ($('#appDate_mon').attr('selectedIndex') == 0 || $('#appDate_day').attr('selectedIndex') == 0 || $('#appDate_year').attr('selectedIndex') == 0) {
				alert('Please provide us with a valid appointment date.');
				return false;
			}
			
			return true;	
		}	
	},
	vip:{
		validate:function(){
			if ($('#firstName').val() == '') {
				alert('Please provide us with your first name.');
				return false;
			}
			
			if ($('#lastName').val() == '') {
				alert('Please provide us with your last name.');
				return false;
			}
			
			if ($('#email').val() == '' || !jc.form.validate.email($('#email').val())) {
				alert('Please provide us with a valid email.');
				return false;
			}
		}	
	},
    fancybox:{
        init:function(){
            if (typeof $.fancybox != 'undefined') {
                $(".jc-youtube").click(function() {
                    $.fancybox({
                            'padding'       : 0,
                            'autoScale'     : false,
                            'overlayColor'  : '#000',
                            'overlayOpacity': 0.8,
                            'transitionIn'  : 'none',
                            'transitionOut' : 'none',
                            'title'         : this.title,
                            'width'         : 680,
                            'height'        : 495,
                            'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                            'type'          : 'swf',
                            'swf'           : {
                            'wmode'         : 'transparent',
                            'allowfullscreen'   : 'true'
                            }
                        });             
                    return false;
                }); 
                $(".jc-vimeo").click(function() {
                    $.fancybox({
                        'padding'       : 0,
                        'autoScale'     : false,
                        'overlayColor'  : '#000',
                        'overlayOpacity': 0.8,
                        'transitionIn'  : 'none',
                        'transitionOut' : 'none',
                        'title'         : this.title,
                        'width'         : 680,
                        'height'        : 495,
                        'href'          : this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
                        'type'          : 'swf'
                    });
                    return false;
                });
            }
        }   
    }	
};

$(document).ready(function(){ a.init(); });
