$(document).ready(function()
{
	initGallery(".gallery-cnt", false);
	fixSideNav();
	fixTopNav();

});

function fixSideNav() {
	//adds the class "last" the the last li in the second level ul
	$('ul.ul2').each( function() {
		$(this).children("li:last").addClass("last");		
	});
	//removes the item2 class from a tags that don't have uls under them.
	$('.side-nav .li1').children('a').each(function() {
		if($(this).siblings('.drop').length == 0){
			$(this).children('span').children('span').css('background-image', 'none');
		}
	});
}
function fixTopNav() {
	//fixes the font size in the dropdown menus so that there is no line breaks in the words.
	$('.menu .li2 a span').each( function(){ 
		var l = $(this).text().length;
		if(l > 20 && l < 30) {
		/*
		$(this).css('font-size','9px');
		} else if(l >= 30) {
			$(this).css('font-size','7px');	
		*/
		var q = $(this).text().substr(0,17);
		$(this).text(q+'...');
		}
	});
	$('.menu .li2').each( function() {
		if($(this).children('.drop').length > 0) {
		 $(this).children('a').children('span').wrapInner('<span class="item2"></span>');		
		}
	});
	//adds the class "last" the the last li in the third level ul
	$('ul.ul2 ul').each( function() {
		$(this).children("li:last").addClass("last");		
	});
}


function initGallery(holder, _autoplay)
{
	var duration = 5000;
	var gallery;
	var timer;
	var autoplay = _autoplay;
	
	gallery = $(holder).find("div.gallery-img img");
	gallery.number = $(gallery).length;
	if(gallery.number > 1)
	{
		gallery.current = 0;
		gallery.thumbnails = $(holder).find(".gallery-list li a");
		
		$(gallery).css({"opacity": 0, "display": "none"});
		$(gallery).eq(0).css({"opacity": 1, "display": "block"});

		$(holder).find(".play").click(function()
		{
			autoplay = true;
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
			return false;
		});
		
		$(holder).find(".pause").click(function()
		{
			if(autoplay)
			{
				autoplay = false;
				clearTimeout(timer);
			}
			else
			{
				autoplay = true;
				timer = setTimeout(function()
				{
					rotate();
				}
				, duration);
			}
			return false;
		});
		
		$(gallery.thumbnails).each(function(i, el)
		{
			$(el).click(function()
			{
    				if(gallery.current != i)
				{
					var temp = gallery.current;
					$(gallery.thumbnails).removeClass("active");
					$(el).addClass("active");
					
					$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });
					$(gallery).eq(i).css({"display": "block"}).animate({"opacity": 1}, 700);
					
					clearTimeout(timer);
					
					gallery.current = i;
					
					timer = setTimeout(function()
					{
						rotate();
					}
					, duration);
				}
				return false;
			});
		});
		
		if(autoplay)
		{
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
	function play()
	{
		var temp = gallery.current;
		$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });
		
		if(++gallery.current >= gallery.number)
			gallery.current = 0;

		$(gallery.thumbnails).removeClass("active");
		$(gallery.thumbnails).eq(gallery.current).addClass("active");
		
		
		$(gallery).eq(gallery.current).css({"display": "block"}).animate({"opacity": 1}, 700);
		
		clearTimeout(timer); 
	}
	function rotate()
	{
		if(autoplay)
		{
			play();
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
}