$(document).ready(function(){ 
// Load XML
$.ajax({
		type: "GET", url: "dataworks.xml", dataType: "xml", success: function(xml) {
			$(xml).find('work').each(function(){
				
				var id = $(this).attr('id');
				var titre = $(this).find('titre').text();
				var type = $(this).find('type').text();
				var url = $(this).find('url').text();
				var thumb = $(this).find('thumb').text();
				var desc = $(this).find('descThumb').text();
				
				if ($(this).attr('new') == 1) {
					var divNew = $('<div />').addClass('new');			
				}

				$('<li />').appendTo('ul#listToFilter')
						   .addClass(type)
						   .addClass("enabled")
						   .append(
						   		$('<div />').addClass('overthumb')
											.css({ 'opacity' : '0.9' })
											.append(
												$('<p />').addClass('titrethumb')
														  .css({ 'margin-top' : 0 })
														  .html('<a class="linkthumb" href="'+url+'">'+titre+'</a>')
											)
											.append(
												$('<p />').addClass('descthumb')
														  .css({ 'margin-top' : '100px' })
														  .html(desc)
											)
											.append(
												$('<div />').addClass('clickme')
														  .html('<a href="'+url+'">&nbsp; Plus d\'informations &nbsp;</a>')
											)
											
						   )
						   
						   .append(
						   		$('<img />').attr('src', './img/thumbs/'+thumb+'_thumb.png')
											.attr('alt', titre)
						   )
						   .append(divNew)
						   .hover(function() {
								if($(this).hasClass('enabled')) {
									$(this).find('div.new').stop(true, true).fadeOut('fast');
									$(this).find('div.overthumb').stop(true, true).fadeIn('fast');
									$(this).stop(true, true).animate({top: '-=4px'}, 200);			
									$(this).find('p.titrethumb').stop(true, true).fadeIn('fast');
									$(this).find('p.titrethumb').stop(true, true).animate({'margin-top' : '+=20px'}, 200);			
									$(this).find('p.descthumb').stop(true, true).fadeIn('fast');
									$(this).find('p.descthumb').stop(true, true).animate({'margin-top' : '60px'}, 200);
								}
						   }, function() {
								if($(this).hasClass('enabled')) {
									$(this).find('div.new').stop(true, true).fadeIn('fast');
									$(this).find('div.overthumb').stop(true, true).fadeOut('slow');
									$(this).stop(true, true).animate({top: '+=4px'}, 200);
									$(this).find('p.titrethumb').stop(true, true).animate({'margin-top' : '-=20px'}, 200);
									$(this).find('p.descthumb').stop(true, true).animate({'margin-top' : '100px'}, 200);
								}						 
						   });
			});
			
			// Scroll to top
			$(".scrollToTop").click(function(event){
				event.preventDefault();
				var full_url = this.href;
				var parts = full_url.split("#");
				var trgt = parts[1];
				var target_offset = $("#global").offset();
				var target_top = target_offset.top;
				$('html, body').animate({scrollTop:target_top}, 500);
			});
				
			// Filtre
			$.fn.listFilter = function(form, options) {
				var selecteur='';
				var opts = $.extend({}, $.fn.listFilter.defaults, options);
				list = $(this);
				form = $(form);
			    
			    if (list.length) {
					var rows = list.children(opts.listElement);
					form.submit(function() { return false; });
					$(':input', form).keyup(filter).change(filter);
					filter();
				}
			    
				return this;
				function filter() {
			    	var scores = [], terms = [];
			    	$("[id$='_filter']", form).each(function() {
			    		terms[$(this).attr('id').toLowerCase().replace('_filter','')] = $(this).val().toLowerCase();
			    		selecteur=terms[$(this).attr('id').toLowerCase().replace('_filter','')] = $(this).val().toLowerCase();
			    	});
					rows.each(function(i) {
						var score = true;
						if (selecteur=='all') { for (term in terms) { score = true; } }
						else { for (term in terms) { score = score && ( $(this).attr('class').toLowerCase().indexOf(terms[term]) >= 0 ); } }
						scores.push([score, i]);
					});
					$.each(scores, function() {
						if (this[0]) { $(rows[ this[1] ]).fadeTo('slow', 1).addClass('enabled'); }
						else { $(rows[ this[1] ]).fadeTo('slow', 0.3).removeClass('enabled'); }
					});
				}
			};
			$('#listToFilter').listFilter('#criteria', {listElement: 'li', transition: 'perso', speed: 500});

			// Popup
			$('a.poplight[href^=#]').click(function() {
				var popID = $(this).attr('rel'); var popURL = $(this).attr('href');
				var query= popURL.split('?'); var dim= query[1].split('&'); var popWidth = dim[0].split('=')[1];
				$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });
				var popMargTop = ($('#' + popID).height() + 80) / 2;
				var popMargLeft = ($('#' + popID).width() + 80) / 2;
				$('#' + popID).css({ 'margin-top' : -popMargTop, 'margin-left' : -popMargLeft });	
				$('body').append('<div id="fade"></div>');
				$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();				
				return false;
			});
			$('a.close, #fade').live('click', function() { $('#fade , .popup_block').fadeOut(); return false; });
			
	}
	
});


});


