var inWork = false;

$(document).ready(function() {	
	$('#Menu').children('ul').children('li').click(function() {
		var type = $(this).attr('data-id');
		
		$('#Menu').children('ul').children('li').removeClass('selected');
		$(this).addClass('selected');
		
		maskWork(type, true);
		positionFooter();
	});
		
	$('.switcher').children('img').click(function() {
		positionFooter();
		
		var mode = '';
		if ($(this).hasClass('list')) {
			$('.list').attr('src', '/medias/images/liste_o.png');
			$('.thumb').attr('src', '/medias/images/vignettes_n.png');
			$('#workThumb').hide();
			$('#workList').show();
			
			mode = 'list';
		} else {
			$('.list').attr('src', '/medias/images/liste_n.png');
			$('.thumb').attr('src', '/medias/images/vignettes_o.png');
			$('#workThumb').show();
			$('#workList').hide();		

			mode = 'thumb';
		}
		showHideDate();

		$.ajax({
  			type: 'POST',
  			url: '/ajax.php',
  			data: 'switcherState='+mode,
  			success: function() {
				positionFooter();
				if(inWork) window.location = '/liste/';
  			}
		});
	});
	
	var t = $('#Menu').children('ul').children('li.selected').attr('data-id');
	if (t != undefined) maskWork(t, false);

	var list = ($('.switcher').children('img').first().attr('src') == '/medias/images/liste_o.png') ? true : false;
	
	if (list) {
		$('#workThumb').hide();
		$('#workList').show();
	} else {
		$('#workThumb').show();
		$('#workList').hide();
	}
	positionFooter();
	$(window).resize(function() {
		positionFooter();
	});
	showHideDate();
	
	var adress  = 'mail';
	var domain  = 'valeriebovay';
	var tdl		= 'ch';

	$('.mail').html(adress+'@'+domain+'.'+tdl).click(function() {
		window.location = 'mailto:'+adress+'@'+domain+'.'+tdl;
	});
});

function positionFooter() {
	if ($('#Footer').offset().top < $(window).height() - $('#Footer').height()) {
		$('#Footer').css({
			'position': 'absolute',
			'bottom': 0
		});
	} else {
		$('#Footer').css({
			'position': 'static'
		});
	}
	
	if ($('#Content').get(0) != undefined) {
		if ($('#Footer').offset().top < $('#Content').offset().top+$('#Content').height()) {
			$('#Footer').css({
				'position': 'static'
			});
		}
	}	
	
	if ($('#workList').get(0) != undefined) {
		if ($('#Footer').offset().top < $('#workList').offset().top+$('#workList').height()+25) {
			$('#Footer').css({
				'position': 'static'
			});
		}
	}	
}

function maskWork(type, post) {
	$('.work').hide();
	$('.workText').hide();
	$('.cat_'+type).show();
		
	showHideDate();

	if (!post) return;

	$.ajax({
  		type: 'POST',
  		url: '/ajax.php',
  		data: 'type='+type,
  		success: function() {
			if(inWork) window.location = '/liste/';  				
	  	}
	});
}

function showHideDate() {
	$('#workList').children('h3').show();

	$('#workList').children('h3').each(function() {
		var allHidden = true;
		$(this).nextUntil('h3').each(function() {
			if ($(this).is(':visible')) allHidden = false;
		});
		
		if (allHidden) {
			$(this).hide();
		}
	});
}
