/* Begin Slideshow */
var slideshow = {
	
	timerInt: 5000,
	idx: 0,
	currentSlide: 0,
	controls: $('.slideshow_controls'),
	numslides: 0,
	getNumSlides: function(){
	  var slides = $('.slide');
	  this.numslides = slides.length;
/* 	  console.log(this.numslides); */
	},
	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){
		
		// used to seek to a new slide by id number
		
		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').removeClass('inactive_slide');
			
            // get rid of the old slide
            $('.old_slide').remove();
			
			  $(".modal_video").colorbox({iframe:true, innerWidth:600, innerHeight:365});
			
		});
		
		this.updateactivemarker(id);
		
		// set the current id to the selected slide
		this.idx	= id;
	
	},
	changeslide: function(id) {
		
      id = Number(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);
	},
	updateactivemarker: function(id) {
	   // used to set the marker corresponding to the currently displayed slide
	   var markerid = "#marker_"+id;
/* 	   console.log(markerid); */
	   $('a.active').removeClass('active').addClass('inactive');
	   $(markerid).addClass('active').removeClass('inactive');
	
	}
} // end var waterslide

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();
	}
)

// Slideshow Controls
// Click on Marker to move to new slide
$('.slide-marker').click(function(){
    // get the id of slide we're trying to seek.
    var marker = $(this);
    if(marker.hasClass('inactive')) {    
        var markerid = marker.attr('id');
        var pieces = markerid.split('_');
        slideshow.changeactiveslide(pieces[1]);
    }
});

starttimer();

/* end Slideshow */

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

/* Hover Intent Menu Files */
$(function(){

/* MegaMenu Start */
var megaMenu = (function(){
		return {
			speedIn: 200,
			speedOut: 150,
			open: function() {
				var menuContainer	= $(this);
				// Menu
				menuContainer.children('.sub-menu-items').fadeIn(megaMenu.speedIn);
				
				// Tab
				menuContainer.addClass('activeMenu')
				menuContainer.find('a.menulink').clone().prependTo(menuContainer).addClass('clone').addClass('activeMenuItem').hide().fadeIn(megaMenu.speedIn);
				
			},
			close: function() {
				var menuContainer	= $(this);
				
				// Menu
				menuContainer.children('.sub-menu-items').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 */

megaMenu.init();

});

$(document).ready(function(){
  $(".modal_video").colorbox({iframe:true, innerWidth:600, innerHeight:365});
});
