function fancyJobs(){
	$fancyLinks = $('.jobs .readmore, .jobs h2 a');
	if ($fancyLinks.length === 0 ) { return; }
	
	$fancyLinks.each(function(i, link){
		$link = $(link);
		$header = $link.parent().prev().find('h2');
		$content = $link.parent().find('.full_job_text').clone();
		$content.prepend( $header.clone() );
		$content.children('.hidden').removeClass('hidden');
		
	
		$link.fancybox({
			'content': $content,
			'autoDimensions': false,
			'width': 728,
			'height': 563,
			'padding': 20,
			'transitionIn': 'elastic',
			'transitionOut': 'fade',
			'orig': $header,
			'speedOut': 50

		});
		
	});
	
}

function fancyCatalogue(){
	$productsList = $('.fancyCatalogue');
	
	if ( $productsList.length === 0 ) { return; }
	
	$productsList.children('.hproduct').each(function(i, product){
		
		classes = $(product).attr('class');
		
		$content = $('<div class="'+classes.replace('hproduct-closed', 'hproduct-opened clearfix')+'"></div>');

		$content.append( $(product).clone().html() );
		
		$('<img src="' +  $content.find('a.photo').attr('href') + '" />').prependTo( $content );
		$content.find('a.photo').remove('');
		
		$( product ).data({
			content: $content
		});
		$( product ).fancybox({
			content: $content,
			autoDimensions: true,
			padding: 20,
			transitionIn: 'elastic',
			transitionOut: 'fade',
			speedOut: 50
		});
	});
	
}

function bigSlider(){
	$slideContainer = $('#big-slider').css({
	});
	
	if ( $slideContainer.length === 0 ) { return; }
	
	$slides = $slideContainer.find('li');

	var screenWidth, screenHeight, currentSlide;
	var colors = [];
	
	currentSlide = 0;
	
	$('<a id="bs-next" class="bs-arrow" href="#">&gt;</a>').appendTo( $slideContainer ).click( function(event){
		event.preventDefault();
		switchSlide(true);
	} );
	
	$('<a id="bs-previous" class="bs-arrow" href="#">&lt;</a>').appendTo( $slideContainer ).click( function(event){
		event.preventDefault();
		switchSlide(false);
	} );	
	
	$slides.each(function(i, slide){
		$slide = $(slide);
		slideImgUrl = $(slide).children('img').hide().attr('src');
		colors[i] = $(slide).children('img').css('color');
		$slide.css({
			width: '100%',
			position: 'absolute',
			backgroundImage: 'url('+slideImgUrl+')',
			overflow: 'hidden'
		});
	});


	$('.sidenav .active a').css({
		color: colors[currentSlide]
	});

	showHideArrows = function(){
		/*
if ( currentSlide === $slides.length-1) {
			$('#bs-next').fadeOut();
			$('#bs-previous').fadeIn();
		} else if ( currentSlide === 0 ){
			$('#bs-next').fadeIn();
			$('#bs-previous').fadeOut();
		} else if ( currentSlide > 0 ) {
			$('#bs-next:hidden').fadeIn();
			$('#bs-previous:hidden').fadeIn();
		}
*/
	};
	
	switchSlide = function(next){
		oldSlide = currentSlide;
		if (next) { 
			currentSlide = currentSlide+1; 
		} else {
			currentSlide = currentSlide-1+$slides.length ;
		}
		//console.log(oldSlide + ' > ' + currentSlide);

		currentSlide = currentSlide % $slides.length;
		
		//console.log(oldSlide + ' > ' + currentSlide);

		$oldSlide = $slideContainer.find('ul>li:nth-child('+(oldSlide+1)+')');
		$currentSlide = $slideContainer.find('ul>li:nth-child('+(currentSlide+1)+')');
				
		showHideArrows();
		
		$('.sidenav .active a').animate({
			color: colors[currentSlide]
		}, 1000);
		direction = (next) ? -1 : 1;

		$currentSlide.css({
			left: (-1) * direction * screenWidth
		}).stop(true, true).animate({
			left: 0
		}, 1000, 'easeInOutQuad');
		
		$oldSlide.stop(true, true).animate({
			left: direction*screenWidth
		}, 1000, 'easeInOutQuad');
		


		/*
$slideContainer.children('ul').animate({
			left: (-1)*currentSlide*screenWidth
		}, 1000, 'easeInOutQuad', function(){
			showHideArrows();
		});
*/
	
	};

	setDimensions = function(){
		
		screenWidth = ( $(window).width() > 1200 ) ? $(window).width() : 1200;
		
		screenHeight = ( $(window).height() > 650 ) ? $(window).height() : 650;
		
		$('.main-page-wrap').css({
			width: screenWidth,
			height: screenHeight,
			overflow: 'hidden',
			position: 'relative'
		});

		$slideContainer.children('ul').css({
			width: screenWidth,
			height: screenHeight,
			overflow: 'hidden',
			left: 0
		});

		$slides.each(function(i, slide){
		
			$slide = $(slide);
			
			$slide.css({
				left: 0,
				height: screenHeight,
				width: screenWidth
			});
		});
	};
	
	$(window).resize(function(){
		setDimensions();
	});

	$('.sidenav a').css('color', '#fff' ).hover(function(){
		$(this).stop().animate({
			color: colors[currentSlide]
		});
	}, function(){
		$(this).stop().animate({
			color: '#fff'
		});
	});
	$('.sidenav .active a').unbind('mouseenter mouseleave').css('color', colors[currentSlide] );

	setDimensions();
	
	$slides.each(function(i, slide){
		$(slide).hide().css({
			visibility: 'visible',
			left: i*screenWidth
		}).delay(i*200).fadeIn('250');
	});

}


function sideMenuPositioner(){
	$sidebar = $('.sidebar');
	$sidemenu = $('.sidenav');
	$footer = $('.footer');


	

	if ( ($(window).height() - 176 - $sidemenu.outerHeight() - $footer.height()) <= 0 ){
		$sidebar.removeClass('relative_to_bottom').addClass('relative_to_top');

	} else {
		$sidebar.addClass('relative_to_bottom').removeClass('relative_to_top');
	}
	
	if( $('.page-wrap').height() < $(window).height() ) {

		$footer.css({
			top: $(document).height() - $footer.outerHeight() - 15
		})
	
	} else {

		$footer.css({
			top: $(document).height() - $footer.outerHeight() - 15
		})

	}


	
}

$(window).resize(function(){
	sideMenuPositioner();
});

$(document).ready(function(){
	//fancyJobs();
	fancyCatalogue();
	bigSlider();
	sideMenuPositioner();
	$("a.readmore").fancybox();
});

