/* MegaMenu Start */
var megaMenu = (function(){
		return {
			speedIn: 200,
			speedOut: 150,
			open: function() {
				var menuContainer	= $(this);
				// Menu
				menuContainer.children('.dropdown').fadeIn(megaMenu.speedIn);
				
				// Tab
				menuContainer.addClass('activeMenu')
				menuContainer.find('a.menulink').clone().prependTo(menuContainer).addClass('clone').hide().fadeIn(megaMenu.speedIn);
				
			},
			close: function() {
				var menuContainer	= $(this);
				
				// Menu
				menuContainer.children('.dropdown').fadeOut(megaMenu.speedOut);
				
				// Tab
				menuContainer.removeClass('activeMenu',megaMenu.speedOut);
				menuContainer.find('.clone').fadeOut(megaMenu.speedOut, function(){
					$(this).remove();
				});
				
			},
			init: function() {
			
				// default behaviour
				$('.menuitem').hoverIntent({
					interval: 120,
					out: megaMenu.close,
					over: megaMenu.open,
					sensitivity: 7,
					timeout: 200
				});
			}	
		};	
}());
/* End MegaMenu */


/* Begin Slideshow */
var slideshow = {
	
	timerInt: 5000,
	idx: 0,
	currentSlide: 0,
	controls: $('.slideshow_controls'),
	numslides: 0,
	getNumSlides: function(){
	  var slides = $('.slide');
	  this.numslides = slides.length - 1;
	},
	nextSlide: function(){
        // determine the next slide number
            var nextSlide;
            if (this.currentSlide == (this.numslides - 1)) {
                nextSlide = 0;
            } else {
                nextSlide = this.currentSlide + 1;
            }
/*             console.log('next: '+nextSlide); */
	
	    this.idx = nextSlide;
        this.changeactiveslide(nextSlide);
	},
	prevSlide: function(){
	  // determine the next slide number
	      var nextSlide;
	      if(this.currentSlide == 0) {
	          nextSlide = this.numslides - 1;
	      } else {
	          nextSlide = this.currentSlide - 1;
	      }
	      
	      this.idx = nextSlide;
	      this.changeactiveslide(nextSlide);
	},
	init: function(){
		this.getNumSlides();
		this.currentSlide = 0;
	},
	
	changeactiveslide: function(id){
		
		var newSlideID		= "#slide_"+id;
/* 		console.log(newSlideID); */
		this.currentSlide   = id;
		
		// change the slide
				
		$(".current_slide").addClass('old_slide').removeClass('current_slide');
		$(newSlideID).clone().addClass('coming_up').prependTo("#slides").addClass('current_slide').removeAttr('id').hide().fadeIn('slow',function(){
			
			// remove the coming_up class
            $('.coming_up').removeClass('coming_up');
			
            // get rid of the old slide
            $('.old_slide').remove();
			
		});
		
		// set the current id to the selected slide
		this.idx	= id;
	
	},
	changeslide: function(id) {
		
	  if(id == (this.numslides-1)) {
        this.changeactiveslide(0);
	  } else {
	    this.changeactiveslide(id+1);
	  }
	},
	showcontrols: function() {
        $('.slideshow_controls').stop().fadeTo(400,1);
	},
	hidecontrols: function() {
        $('.slideshow_controls').stop().fadeTo(400,0);
	}

	
} // end var waterslide



$('#control_next').click(function(){
   slideshow.nextSlide();
});
$('#control_prev').click(function(){
   slideshow.prevSlide();
});


var slidetimer;

function starttimer() {
	slidetimer		= setInterval(function(){slideshow.changeslide(slideshow.idx)},slideshow.timerInt);
}

function stoptimer() {
	clearInterval(slidetimer);
}

$("#slides").hover(
	function(){
		stoptimer();
		//console.log('hovered');
		slideshow.showcontrols();
	},
	function(){
		starttimer();
		slideshow.hidecontrols();
	}
)

$('.mmitem').hover(
    function(){
		stoptimer();
		//console.log('hovered');
		//slideshow.showcontrols();
	},
	function(){
		starttimer();
		// slideshow.hidecontrols();
	}

)



starttimer();

/* end Slideshow */

	
$(function(){
	megaMenu.init();
	slideshow.init();
});

