(function($) {
	var Book = function() {
		var obj = {
			filters: {
				keywords: [],
				categories: [],
				types: [],
				colors: [],
				clients: []
			},

			events: function() {
				var o = this;

				$(document).bind('ready', function() {
					o.hideBlocks();
					o.eventsSidebarBlock();
					o.putClientsScrollbar();
					o.eventsBoxInfos();
					o.eventsSortLinks();

                    if($("#p").attr('id')) {
                        $('#background').remove();
                        $("body").css("background", "url(/images/background_4.jpg) center center");
                        $('#page_container').css('width', 'inherit');
                        $('#global_container').css('margin', 0);
                        $('#global_container').css('position', 'static');
                        $('#global_container').css('width', 'inherit');
                    }
				});
				
				$(window).bind('resize', function() {
					o.putClientsScrollbar();
				});
			},	

			eventsSortLinks: function() {
				var o = this;

				$('#sidebar_search a').bind('click', function(e) {
					var s = ($(this).attr('rel')).split('-');
					var name = s[0];
					var value = s[1];

                    $.each($('#sidebar_search a'), function(index_, value) {
                        if($(value).hasClass('actif')) {
                            //if($(this).html() != $(value).html()) {
                                $(value).removeClass('actif');
                            //}
                        }
                    });

					if($(this).hasClass('actif')) {
						$(this).removeClass('actif');
						o.removeFilter(name, value);
					}
					else {
						$(this).addClass('actif');
						o.addFilter(name, value);
                    }

					o.applyFilters();
				});
			},

			removeFilter: function(name, value) {
				var o = this;
				var a = null;

				switch(name) {
					case "category":a = o.filters.categories;break;
					case "keyword":a = o.filters.keywords;break;
					case "color":a = o.filters.colors;break;
					case "type":a = o.filters.types;break;
					case "client":a = o.filters.clients;break;
				}			

				if(a !== null) {
					$.each(a, function(i, v) {
						if(v == value) {
							a[i] = null;
						}
					});
				}
			},

			addFilter: function(name, value) {
				var o = this;
				value = parseInt(value);

                o.filters.categories = [];
                o.filters.keywords = [];
                o.filters.colors = [];
                o.filters.types = [];
                o.filters.clients = [];

				switch(name) {
					case "category":o.filters.categories.push(value);break;
					case "keyword":o.filters.keywords.push(value);break;
					case "color":o.filters.colors.push(value);break;
					case "type":o.filters.types.push(value);break;
					case "client":o.filters.clients.push(value);break;
				}
			},

			applyFilters: function() {
				var o = this;

				$.each(projects, function(i, v) {
					var id = v.id;
					var keep = false;
					var empty_count = 0;
					var only_null = true;
					
					$.each(o.filters.categories, function(i_, v_) {
						if(v_ != null) {
							only_null = false;
						}

						if($.inArray(v_, v.categories) != -1) {
							keep = true;
						}
					});

					$.each(o.filters.keywords, function(i_, v_) {
						if(v_ != null) {
							only_null = false;
						}

						if($.inArray(v_, v.keywords) != -1) {
							keep = true;
						}
					});

					$.each(o.filters.colors, function(i_, v_) {
						if(v_ != null) {
							only_null = false;
						}				

						if($.inArray(v_, v.colors) != -1) {
							keep = true;
						}
					});

					$.each(o.filters.types, function(i_, v_) {
						if(v_ != null) {
							only_null = false;
						}

						if($.inArray(v_, v.types) != -1) {
							keep = true;
						}
					});

					$.each(o.filters.clients, function(i_, v_) {
						if(v_ != null) {
							only_null = false;
						}

						if(v_ == v.client) {
							keep = true;
						}
					});

					if(keep || only_null) {
						$('#b'+id).show();
					}
					else {
						$('#b'+id).hide();
					}
				});
			},

			eventsBoxInfos: function() {
				$('.box_projet')
				.bind('mouseenter', function() {
					var i = $(this).attr('id');
					$('#'+i+' .infobulle').fadeIn();
				})
				.bind('mouseleave', function() {
					var i = $(this).attr('id');
					$('#'+i+' .infobulle').fadeOut();
				});
			},

			putClientsScrollbar: function() {
				var book_footer_position = $('#book_footer').position();
				
				if($('#clients > div').hasClass('jScrollPaneContainer')) {
					$('#clients > div').html(
						$('#clients #box_clients').html()
					)
					.removeClass()
					.attr('id', 'box_clients');
				}

				$('#box_clients')
				.css(
					'height', (book_footer_position.top)-550
				)
				.jScrollPane({
					scrollbarWidth: 9, 
					scrollbarMargin: 10, 
					dragMinHeight: 9, 
					dragMaxHeight: 10
				});
			},

			hideBlocks: function() {
				$('#categorie .block_content, #media .block_content, #tags .block_content, #couleurs .block_content').css('display', 'none');
			},

			eventsSidebarBlock: function() {
				var ids = ['clients', 'categorie', 'media', 'tags', 'couleurs'];
				$.each(ids, function(i, v) {
					$('#'+v).bind('click', function() {
						var target = $(this);

						$.each(ids, function(i_, v_) {
							var selector = '#'+v_+' > div';
                            var selector2 = '#'+v_+' > img';
							if(target.attr('id') != v_) {
								$(selector).hide(100);
                                $(selector2).css('background-position', '0 0');
							}
							else {
								$(selector).show(100);
                                $(selector2).css('background-position', '0 -23px');
							}
						});
					});
				});
			},

			init: function() {
				this.events();
			}
		}

		obj.init();

		return obj;
	}

    new Book();
})(jQuery);

