var activemenuhref = null;
var timeout = null;
var active = null;
var q = null;
var quotes= new Array();
var updating;
var console = new Array;
var slideInterval;
var quoteInterval;

$(document).ready(function(){
	/************* SET UPS ****************/
	Shadowbox.init({language:'en'});
    $('.text-carousel').jcarousel({
        wrap: 'circular'
	});
    $('.archive-event-carousel').jcarousel({
        wrap: 'circular'
    });	
    $('#gallery-carousel').jcarousel({
        wrap: 'circular'
	});
	$('img','#gallery-big-frame').load(function(){
		$(this).css('left',(800-$(this).width())/2+'px');
		$(this).css('top',(600-$(this).height())/2);
		$(this).animate({opacity:1});
	});
    $('#gallery-carousel').find('img').click(function(){
		$('#gallery-big-frame').append('<img src=""/>').find('img:last').addClass('sec');
		var sec=$('.sec','#gallery-big-frame');
		sec.attr('src',$(this).attr('big'));
		sec.load(function(){
			$(this).css('left',(800-$(this).width())/2);
			$(this).css('top',(600-$(this).height())/2);
			$('.first','#gallery-big-frame').animate({opacity:0},function(){
				$(this).remove();
				$('.sec','#gallery-big-frame').removeClass('sec').addClass('first');
			});
		});
	});
	$('ul#quotes').hide();
	$('ul#quotes').find('li').each(function(){
		quotes.push($(this).html());
		/*console.log($(this).html());*/
	});
	q = Math.floor(Math.random(quotes.length));
	quoteSwitch();
	quoteInterval=setInterval("quoteSwitch()",8000);

	if(updating==true){
		$('body').append('<div id="smogg">').find('div#smogg').height($(document).height()).append('<img src="http://www.m55-bike.com/images/ud.png">');
	}
	
	
	/************* CUFON ****************/
	Cufon.replace('#top-menu a',{
		hover : false,
		fontSize : '12px'
	});
	Cufon.replace('#stage #download',{
		hover : true,
		fontSize : '14pt',
		color : '#c9c9c9'
	});
	Cufon.replace('#main_content span#title',{
		hover : true,
		fontSize : '14pt',
		color : '#696969'
	});
	Cufon.replace('#orderForm .button',{
		hover : true,
		fontSize : '14pt',
		color : '#ffffff'
	});	
	Cufon.replace('.third .cufon',{
		hover : true,
		fontSize : '13px',
		color : '#4a4a4a'
	});
	Cufon.replace('.third #email_btn',{
		hover : true,
		fontSize : '10px'
	});
	Cufon.replace('.third #signup_btn',{
		hover : true,
		fontSize : '14px',
		color : '#c9c9c9'
	});
	Cufon.replace('#nav-below a',{
		hover : true,
		fontSize : '16px',
		color : '#000000'
	});
	Cufon.replace('.pdf h3',{
		hover : true,
		fontSize : '14px',
		color : '#000000'
	});
	Cufon.replace('h2.download-category',{
		hover : true,
		fontSize : '20pt',
		color : '#575757'
	});

	/************* EVENTS ****************/
	$("#top-menu a.button").mouseover(function(){
		$(this).addClass('over');
		Cufon.replace('#top-menu a',{
			hover : false,
			fontSize : '12px'
		});		
		var href = $(this).attr('href');
		setMenuTimeOut('stop', null);
		if ( activemenuhref == href ) return false;
		activemenurel = href;
		$('div#top-menu div.submenu:visible').hide();
		if ( !($("div.submenu", $(this).parent()).length > 0) ) return false;
		$("div.submenu", $(this).parent()).show();
		return false;
	});
	$("#top-menu div.submenu").mouseover(function(){
		setMenuTimeOut('stop', null);
	});
	$("#top-menu div.submenu a").mouseover(function(){
		$(this).addClass('over');
		Cufon.replace('#top-menu a',{
			hover : false,
			fontSize : '12px'
		});		
	});
	$("#top-menu div.submenu a").mouseout(function(){
		$(this).removeClass('over');
		Cufon.replace('#top-menu a',{
			hover : false,
			fontSize : '12px'
		});		
	});	
	$("#top-menu div.menuItem").mouseout(function(){
		$('a.button',$(this)).removeClass('over');
		Cufon.replace('#top-menu a',{
			hover : false,
			fontSize : '12px'
		});		
		var ez = $(this);
		setMenuTimeOut('start', function(){
			$('div.submenu', ez).hide();
			activemenuhref = null;
		});
	});
	$(".BW").mouseover(function(){
		$(this).css('background-color','#ffffff');
	});
	$(".BW").mouseout(function(){
		$(this).css('background-color','#000000');
	});

	$(document).click(function(){
		setMenuTimeOut('stop');
		$('#top-menu div.submenu:visible').hide();
		activemenuhref = null;
	});
/*
	$('div.submenu').each(function(){
		var offset = $('a.mainmenu', $(this).parent()).position();
		$(this).css({
			'top': (offset.top + 30) + 'px',
			'left': (offset.left + (($.browser.msie && parseInt($.browser.version)==6)?6:14) ) + 'px'
		});
	});
*/
	$("#socialLinks a").mouseover(function(){
		$(this).css('background-position','0 bottom');
	});
	$("#socialLinks a").mouseout(function(){
		$(this).css('background-position','0 0');
	});
	if(!updating){
		slideInterval=setInterval( "slideSwitch()", 8000 );
	}
	
	$('h2.download-category').click(function(){
		if(!$(this).hasClass('active-category')){
		/*console.log('active-category');*/
			$('div#'+$('h2.active-category').attr('cat')).hide();
			$('div#'+$(this).attr('cat')).show();
			$('h2.active-category').removeClass('active-category');
			$(this).addClass('active-category');
		}
	});
		
	$('img', '#slideshow').click(function(){
		document.location=$(this).attr('link');
	});
	$('div','div#galleryButtons').click(function(){
		if($('#slideshow img[gal="'+$(this).attr('class')+'"]').length>0){
			clearInterval(slideInterval);
			clearInterval(quoteInterval);
			$('#slideshow img').removeClass('active');
			$('div','div#galleryButtons').removeClass('hover');
			$('#slideshow img[gal="'+$(this).attr('class')+'"]:first').addClass('active');
			$(this).addClass('hover');
			slideInterval=setInterval( "slideSwitch()", 8000 );
			quoteInterval=setInterval("quoteSwitch()",8000);
		}
});
	
});
function quoteSwitch(){
	$('p','#text2').animate({opacity:0},function(){
		$(this).html(quotes[q]);
		$(this).animate({opacity:1});
	});
	q=q<quotes.length-1?q+1:0;
}
function slideSwitch() {
    var $active = $('#slideshow IMG.active');
	
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
	var actGallery = $active.attr('gal');
	
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
	var nextGallery = $next.attr('gal');
	/*console.log(actGallery);*/
	$('.'+actGallery,'div#galleryButtons').removeClass('hover');
	$('.'+nextGallery,'div#galleryButtons').addClass('hover');
    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

	
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


function setMenuTimeOut(mode, func){
	if ( timeout != null ){
		clearTimeout(timeout);
		timeout = null;
	}
	if ( mode == 'start' ){
		timeout = setTimeout(func, 100);
	}
	return true;
}


/*var slider = new Class({
	container: null,
	thumbsPager: null,
	thumbsFrame: null,
	page: 1,
	maxPage: 0,
	prevButton: null,
	nextButton: null,
	animateWidth: 0,
	initialize: function(container){
		this.container = container;
		this.thumbsPager = $('div.thumbsPager', this.container);
		this.thumbsFrame = $('div.thumbsFrame', this.thumbsPager);
		this.prevButton = $('a.arrowLeft', this.container);
		this.nextButton = $('a.arrowRight', this.container);
		this.maxPage = $('div.thumbsItemsContainer', this.thumbsFrame).length;
		this.animateWidth = this.thumbsPager.width();
		this.thumbsFrame.width(this.maxPage * this.animateWidth);
		this.prevButton.click(function(){
			return this.animatePager('prev');
		}.bind(this));
		this.nextButton.click(function(){
			return this.animatePager('next');
		}.bind(this));
		this.checkPager();
	},
	checkPager: function(){
		if ( this.page < this.maxPage ) this.nextButton.removeClass('nextDisabled');
		else this.nextButton.addClass('nextDisabled');
		if ( this.page < 2 ) this.prevButton.addClass('prevDisabled');
		else this.prevButton.removeClass('prevDisabled');
	},
	animatePager: function(method){
		this.page = method == 'prev' ? this.page - 1 : this.page + 1;
		if ( this.page < 1 ){
			this.page = 1;
			return false;
		}
		if ( this.page > this.maxPage ){
			this.page = this.maxPage;
			return false;
		}
		var move = (this.page - 1) * this.animateWidth;
		this.thumbsFrame.animate({
			left: '-'+move+'px'
		}, 2500, 'swing');
		this.checkPager();
		return false;
	}
});*/
