/* CREDITS
 * ----------------------------------------------------------
 * Title:  Main JavaScript file
 * ----------------------------------------------------------
 * Inventis - Web Architects - We design the Web!
 * www.inventis.be
*/
$(document).ready(function(){

	// Fancybox for images
	$('a[rel=lightbox]').fancybox({
		titlePosition		: 'over',
		hideOnContentClick	: true,
		centerOnScroll		: true
	});

	// Fancybox for youtube movies
	$('a.lightbox[rel=youtube]').click(function(){
		$.fancybox({
			'type'			:	'iframe',
			'width'			:	510,
			'height'		:	418,
			'href'			:	this.href.replace(new RegExp("watch\\?v=", "i"), 'embed/'),
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'overlayOpacity':	0.6,
			'overlayColor'	:	'#000'
		});
		return false;
	});
	
	// Homepage action slideshow
	if($("#actionslideshow")){
		var $tabs = $("#actionslideshow").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 10000, true);
		
		$("#actionslideshow").hover(
			function() {
				$("#actionslideshow").tabs("rotate",0,true);
			},
			function() {
				$("#actionslideshow").tabs("rotate",10000,true);
			}
		);

		var totalSize = $(".ui-tabs-panel").size() - 1;

		$('#ui-tabs-nav-arrows-prev').click(function() {
			var selected = $tabs.tabs('option', 'selected');
			var index = selected - 1;
			if(index < 0){
				index = totalSize;
				for(var i=0; i<totalSize; i++){
					$('#actionslideshow .buttons.next').click();
				}
			}else{
				$('#actionslideshow .buttons.prev').click();
			}
			$tabs.tabs('select', index);
			return false;
		});

		$('#ui-tabs-nav-arrows-next').click(function() {
			var selected = $tabs.tabs('option', 'selected');
			var index = selected + 1;
			if(index > totalSize){
				index = 0;
				for(var i=0; i<totalSize; i++){
					$('#actionslideshow .buttons.prev').click();
				}
			}else{
				$('#actionslideshow .buttons.next').click();
			}
			$tabs.tabs('select', index);
			return false;
		});


		$('#actionslideshow .navigation').tinycarousel({ pager: true, display: 1, duration: 500, rewind: true });
	}

	// Products slideshow
	if($('#productslideshow')){
		$('#productslideshow').tinycarousel({ pager: true, display: 1, duration: 500, rewind: true, interval: true });
	}
	
	// Custom selects
	$('select').customSelect();

	// Superfish
	$('#navigation ul.clearfix').superfish();
	
	// Collapsible lists
	$('dl.collapsible dt:first').addClass('active');
	$('dl.collapsible dd:first').show();
	$('dl.collapsible dt').click(function(){
		if(!$(this).hasClass('active')){
			$(this).parent().children('dt').removeClass('active');
			$(this).parent().children('dd').slideUp();
			$(this).next('dd').slideDown();
			$(this).addClass('active');
		}
	});
	
});
