$(function(){
	$("ul#menu li").bind('mouseenter mouseleave', function(event){
		pLiLast = $(this).parent().parent().parent().is(':last');
		$(this).toggleClass('hover').find('>ul.level3').css({
			left: pLiLast ? $(this).outerWidth() : 'auto',
			right: pLiLast ? 'auto' : $(this).outerWidth()
		});
	});
	/*** NEWS ***/
	var img = "/img/";
	
	if ($('#slider').length) {
		$('#slider').s3Slider({
			timeOut: 15000 //15 seconds
		});
	}
	
	/*** SEARCH ***/
	$('#header .search .submit').click(function(){
		window.location = '/search/query/'+$('#header .search .query').val();
	}).bind('mouseenter mouseleave', function(){
		$(this).toggleClass('hover');
	});
	
	$('#news').carousel({
		loop: true,
		autoSlide: true,
		dispItems: 1,
		stepItems: 1,
		autoSlideInterval: 10000
	});
	
	$("h4.faq").click(function(){
		next = $(this).nextUntil("h4");
		if (!next.is(":visible")) {
	 		$(this).parent().find("h4.faq").nextUntil('h4').slideUp();
		}
		next.slideToggle();
	});
	
	displayGalleryContents = function(cur) {
		c = gallery.contents[cur];
		dis = $('div#gallery div#image div.contents:first');
		tit = $('div#gallery h2:first');
		desc  = $('div#gallery p:first');
		if (c.type=='image') {
			dis.html('<img src="'+c.image+'" alt="" />');
		}
		else if (c.type='video') {
			// width="608" height="500"
			dis.html('<iframe width="405" height="333" src="http://youtube.com/embed/'+c.image+'?wmode=transparent" frameborder="0" allowfullscreen></iframe>');
		}
		tit.html(c.title);
		desc.html(c.description);
	}
	$('div#gallery input.carousel-control').click(function(){
		if ($(this).hasClass('previous')) {
			gallery.current--;
		}
		else if ($(this).hasClass('next')) {
			gallery.current++;
		}
		if (gallery.current < 0) {
			gallery.current = gallery.contents.length - 1;
		}
		if (gallery.current > gallery.contents.length - 1) {
			gallery.current = 0;
		}
		displayGalleryContents(gallery.current);
	});
});
