﻿// Menu Slide out function
var menuitem = 0;

function menu_open() { 
	curitem = $(this).children('ul');
	if(menuitem && curitem.html() != menuitem.html()){
		menu_close();
	}
	if(menuitem == 0 || curitem.html() != menuitem.html()){
		menuitem = curitem;
		menuitem.slideDown('medium');
	}
}
function menu_close(){  
	if(menuitem) {
		menuitem.slideUp('medium');
		menuitem=0;
	}
}

// Slideshow
function theSlideshow() {
	if($('ul.slshow li')){
		startSlideshow();
		$('ul.slshow').fadeIn(1000);
    	$('ul.slshow li').fadeIn(1000); // tweek for IE
	}
}
function startSlideshow() {
	//Set the opacity of all images to 0
	$('ul.slshow li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('ul.slshow li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('doRotate()',6000);
}
function doRotate() {	
	//Get the first image
	var current = ($('ul.slshow li.show')?  $('ul.slshow li.show') : $('ul.slshow li:first'));
    if ( current.length == 0 ) current = $('ul.slshow li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('ul.slshow li:first') :current.next()) : $('ul.slshow li:first'));
	
	var sibs = current.siblings();
    var rndNum = Math.floor(Math.random() * sibs.length );
    //var next = $( sibs[ rndNum ] );

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
};

// Init
$(document).ready(function(){ 
	$('ul.top_menu > li').bind('mouseover', menu_open);
	theSlideshow();
});

document.onclick = menu_close;

