$(document).ready(function(){


	$("a[rel=products]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition'		: 'outside',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.9,
		'titleFormat'		: 
		function(title, alias, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Produkt ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '<br /><br /><iframe src="http://www.facebook.com/plugins/like.php?href=' + alias + '&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;font=tahoma&amp;colorscheme=light&amp;height=25" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px;" allowTransparency="true"></iframe></span>';					
		}
	});
	
	$("a[rel=map]").fancybox({
		'width'				: '75%',
		'height'			: '75%',
        'autoScale'     	: false,
		'cyclic'            : false,
		'showNavArrows'     : false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	$('#frmSS3').attr('onsubmit', 'return CheckForm3(this);');
	
	if( $('#nav li ul li a#selected').length ) {
		$('#nav li ul li a#selected').parentsUntil('#nav').addClass('selected');
	}
						  
	slideShow(5000);


});

function slideShow(speed) {

		
	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').after('<div id="slideshow-caption" class="caption"></div>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	$('#slideshow-caption').html($('ul.slideshow li.show').find('img').attr('alt'));
		
	//Display the caption
	//$('#slideshow-caption').css({opacity: 0.7, bottom:0});
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over
	$('ul.slideshow').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	
}

function gallery() {

	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
	
	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
	
	$('ul.slideshow').css({width: $(next).find('img').css('width')});
	$('ul.slideshow').css({height: $(next).find('img').css('height')});
	
	//Get next image caption
	var desc = next.find('img').attr('alt');	
		
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	$('#slideshow-caption').html(desc); 

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');
}

/* Custom fonts with cufon */
Cufon.replace('#nav',  { hover: true });
Cufon.replace(['h1,h2,h3,h4'], { fontFamily: 'Anivers' });
