$j = jQuery.noConflict();

var BB = {};

BB.menu = {
	
	init: function() {
		BB.menu.item();
		BB.menu.submenu();
	},
	
	item: function() {
		$j(".bb_menu_item").hover(
			function() {
				$j(this).parent().addClass("bb_hovered");
				var submenu = $j("#submenu_"+$j(this).attr("id"));
				submenu.css("top", $j(this).position().top + 30);
				submenu.css("left", $j(this).position().left - 8);
				$j(".bb_submenu").hide();
				submenu.fadeIn(200);
			}, function() {
				$j(this).parent().removeClass("bb_hovered");
				var submenu = $j("#submenu_"+$j(this).attr("id"));
				submenu.hide();
			}
		);
	},
	
	submenu: function() {
		$j(".bb_submenu").hover(
			function() {
				var item = $j("#"+$j(this).attr("id").replace("submenu_", ""));
				item.parent().addClass("bb_hovered");
				$j(this).show();
			}
			, function() {
				$j(this).hide();
				$j(".bb_item").removeClass("bb_hovered");
			}
		);
	}
};

BB.searchTop =  {
	 init: function() {
	 	$j("#searchTopForm").submit(function (event) {
			if('' == $j('#search').val() || 'Tapez votre recherche' == $j('#search').val()) {
				alert('Merci de taper un mot clé');
				event.preventDefault();
			}
			else {
				var search = BB.searchTop.stripAccents($j("#search").val()).toLowerCase().replace(/ /g, "-");
				$j(this).attr("action", "/recherche/"+ search +".html");
			}
		});
	 },
	 
	 stripAccents: function(str) {
		var norm = new Array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ý','þ','ÿ','\'','"');
		var spec = new Array('A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I', 'D','N','O','O','O','0','O','O','U','U','U','U','Y','b','s','a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','u','u','u','u','y','y','b','y','','');
		
		for (var i = 0; i < spec.length; i++) {
			while (str.indexOf(norm[i]) != -1) {
				str = str.replace(norm[i], spec[i]);
			}
		}
		
		return str;
	 }
};

BB.shoppingTable = {
	init: function() {
		if ($j(".bb_shopping-table tr").size() > 0) {
			$j(".bb_shopping-table tr").click(function() {
				var price_id = $j(this).attr("id");
				window.open("go.php?id="+price_id)
			});
		}
	}
};

BB.lastNewsTable = {
	init: function() {
		if ($j(".bb_last_news-table tr").size() > 0) {
			$j(".bb_last_news-table tr").click(function() {
				window.location = $j(this).attr('id');
			});
		}
	}
};

BB.topTen = {
	init: function() {
		if ($j(".bb_top-link").size() > 0) {
			$j(".bb_top-link").mouseover(function() {
				var top = $j("#top-"+$j(this).attr("id"));
				$j(".bb_top-products").hide();
				top.show();
				$j(".bb_tab-bar td").removeClass("bb_selected");
				$j(this).parent().addClass("bb_selected");
			});
		}
	}
};

BB.productSlider = {
	
	slideTimer: 0,
	slideMax: 0,
	
	moveSlide: function(id, dir, element, maxToSlide) {
		if(BB.productSlider.slideMax == maxToSlide && maxToSlide > 0){
			BB.productSlider.slideMax = 0;
			clearInterval(BB.productSlider.slideTimer);
			BB.productSlider.slideTimer = 0;
			return;
		}
		if (document.getElementById(element).style.left == '') {
			document.getElementById(element).style.left = '0px';
		}
		
		var left = parseInt(document.getElementById(element).style.left) + dir;
		var max = document.getElementById(element+'a').offsetWidth - document.getElementById(element).offsetWidth;
		if (left < max) { left = max; }
		if (left > 0) { left = 0; }
	
		document.getElementById(element).style.left = left + 'px';
	
		if (BB.productSlider.slideTimer == 0) {
			BB.productSlider.slideTimer = setInterval("BB.productSlider.moveSlide(24, " + dir + ", '" + element + "', "+maxToSlide+");", id);
		}
		
		if(maxToSlide > 0) BB.productSlider.slideMax++;
	}
};



BB.homeSlider = {
	
	init: function() {
		if ($j("#preview_1").size() > 0) {
			var image = 1;
			var nb_images = parseInt($j("#bb_previews").attr("name"));
	
			var timer = window.setInterval(function() {
				BB.homeSlider.updatePreview(image);
				if (image < nb_images) {
					image ++;
				} else {
					image = 1;
				}
			}, 3000);
			
			$j("#preview_1").show();
			$j("#preview_1 .pagination_1").addClass("bb_selected");
			
			$j(".bb_preview").click(function() {
//				var url = $j(this).attr("title");
				var url = $j(this).prev('.bb_preview_url').val();
				window.location = url;
			});
			
			$j(".bb_pagination_item").mouseenter(function(){
				image = $j(this).attr("id");
				BB.homeSlider.updatePreview(image);
			});
		}
	},
	
	updatePreview: function(image) {
		$j(".bb_preview").hide();
		$j(".bb_pagination td.bb_selected").removeClass("bb_selected");
		$j("#preview_"+image).show();
		$j(".pagination_"+image).addClass("bb_selected");
	}
};


BB.news = {
	init: function() {
		if ($j('.bb_link_modal').size() > 0) {
			BB.news.modal();
		}
		
		if ($j('.bb_link_modal_mini').size() > 0) {
			BB.news.modalMini();
		}
	},
	
	// Show a block with product's informations on a link's hover
	modal: function() {
		$j('.bb_link_modal').each(function(index) {
			var tmpNumSplit = $j(this).attr('id').split('bb_modal-target-');
			if ($j('#bb_modal-content-' + tmpNumSplit[1]).length <= 0)
				$j(this).removeClass('bb_link_modal');
		});
		
		$j('.bb_link_modal').ezpz_tooltip({
			contentPosition: 'belowStatic',
			stayOnContent: true,
			offset: 0
		});
	},
	
	// Show a block with to explain a word
	modalMini: function() {
		$j('.bb_link_modal_mini').ezpz_tooltip({
			contentPosition: 'belowStatic',
			stayOnContent: true,
			offset: 0
		});
	}
};


// Comments for news and tests
BB.comment = {
	
	init: function() {
		if ($j('#bb_comment_name').size() > 0) {
			BB.comment.fillFields();
			BB.comment.help();
			BB.comment.smiley();
		}
	},
	
	// if the visitor has voted, we put his data in the comment's fields
	fillFields: function() {
		if ($j.cookie('comment_name') != null && $j('#bb_comment_name').val() == '') $j('#bb_comment_name').val($j.cookie('comment_name').replace('+', ' '));
		if ($j.cookie('comment_link') != null && $j('#bb_comment_link').val() == '') $j('#bb_comment_link').val($j.cookie('comment_link'));
		if ($j.cookie('comment_mail') != null && $j('#bb_comment_email').val() == '') $j('#bb_comment_email').val($j.cookie('comment_mail'));
	},
	
	// show help block on the ? hover
	help: function() {
		$j('#bb_new_help_icon').hover(function() {
			$j('#bb_new_help_block').slideDown();
		}, function() {
			$j('#bb_new_help_block').slideUp();
		});
	},
	
	// put smileys in textarea when a person click on a smiley
	smiley: function() {
		$j('.bb_new_smiley').click(function() {
			var comment = $j('#bb_comment_content').val();
			if ($j('#bb_comment_content').val().length > 0 && comment[$j('#bb_comment_content').val().length - 1] != ' ') comment += ' ';
			comment += $j(this).attr('alt') + ' ';
			$j('#bb_comment_content').val(comment);
		});
	}
};

BB.test = {
	
	init: function() {
		if ($j('#bb_test_btn_want').size() > 0) {
			BB.test.buttons();
		}
		
		if ($j(".bb_tests-table tr.bb_list-item").size() > 0) {
			BB.test.table();
		}
		
		if ($j(".bb_download_button").size() > 0) {
			BB.test.manual_download_button();
		}
	},
	
	// Buttons if the visitor have or want the product
	buttons: function() {
		$j('#bb_test_btn_want').click(function() {
			$j('#bb_test_btn_nb_want').load('/server.php?type=want&product=' + $j(this).attr('name'));
		});
	
		
		$j('#bb_test_btn_have').click(function() {
			$j('#bb_test_btn_nb_have').load('/server.php?type=got&product=' + $j(this).attr('name'));
		});
	},
	
	table: function() {
		$j(".bb_tests-table tr.bb_list-item").click(function() {
			var url = $j(this).children("td.bb_description").children("h1").children("a").attr("href");
			window.location = url;
		});
	},
	
	manual_download_button: function() {
		$j(".bb_download_button").click(function() {
			var url = "http://www.monsieurmanuel.com/notice-" + $j(this).attr("id") + ".html";
			// window.open(url);
		});
	}
};

BB.opinions = {
	
	init: function() {
		if ($j('.bb_opinions_filter').size() > 0) {
			BB.opinions.change_type();
		}
		
		if ($j('#bb_opinions_order').size() > 0) {
			BB.opinions.change_order();
		}
	},
	
	change_type: function() {
		$j('.bb_opinions_filter').change(function() {
			var opinions_positive = $j("#bb_opinions_positive_box").attr("checked");
			var opinions_negative = $j("#bb_opinions_negative_box").attr("checked");
			
			if (opinions_positive && !opinions_negative) {
				window.location = "avis-"+$j(this).attr("name")+"_positifs.html"+$j("#bb_order").val();
			} else if (!opinions_positive && opinions_negative) {
				window.location = "avis-"+$j(this).attr("name")+"_negatifs.html"+$j("#bb_order").val();
			} else {
				window.location = "avis-"+$j(this).attr("name")+".html"+$j("#bb_order").val();
			}
		});
	},
	
	change_order: function() {
		$j('#bb_opinions_order').change(function() {
			window.location = "?order="+$j(this).val();
		});
	}
};

BB.prices = {
	
	init: function() {
		if ($j('.bb_best_prices_row').size() > 0) {
			BB.prices.price_rows();
		}
		
		if ($j('.bb_resume_best_prices_row').size() > 0) {
			BB.prices.resume_price_rows();
		}
		
		if ($j('.bb_resume_row').size() > 0) {
			BB.prices.resume_rows();
		}
		
		if ($j('.bb_price_filter').size() > 0) {
			BB.prices.change_type();
		}
		
		if ($j('.bb_price_subscribe_email').size() > 0) {
			BB.prices.subscribe();
		}
		
		if ($j('.bb_price_evolution .bb_best_price_row').size() > 0) {
			BB.prices.best_price_link();
		}
	},
	
	price_rows: function() {
		$j('.bb_best_prices_row').click(function() {
			window.open("/go.php?id="+$j(this).attr("id"));
		});
	},
	
	resume_price_rows: function() {
		$j('.bb_resume_best_prices_row td.bb_clickable').click(function() {
			window.open("/go.php?id="+$j(this).parent().attr("id"));
		});
	},
	
	resume_rows: function() {
		$j('.bb_resume_row td.bb_clickable').click(function() {
			window.location = "/resume-"+$j(this).parent().attr("id")+".html";
		});
	},
	
	change_type: function() {
		$j('.bb_price_filter').change(function() {
			var price_new = $j("#bb_price_new_box").attr("checked");
			var price_old = $j("#bb_price_old_box").attr("checked");
			
			if (price_new && !price_old) {
				window.location = "prix-"+$j(this).attr("name")+"_new.html";
			} else if (!price_new && price_old) {
				window.location = "prix-"+$j(this).attr("name")+"_old.html";
			} else {
				window.location = "prix-"+$j(this).attr("name")+".html";
			}
		});
	},
	
	subscribe: function() {
		$j(document).ready(function() {
			if ($j('.bb_price_subscribe_email').val() == "votre adresse email ici") {
				$j('.bb_price_subscribe_email').addClass("bb_empty");
			}
		});
		
		$j('#bb_price_subscribe').click(function() {
			var model = $j("#product_id").attr("name");
			var email = $j(".bb_price_subscribe_email").val();
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (filter.test(email)) {
				$j(".bb_price_subscribe_email").load("/server.php?product_id="+$j("#product_id").val()+"&subscribe_email="+$j(".bb_price_subscribe_email").val(), function(response, status, xhr) {
					$j(".bb_confirm").show();
					if (response == 'success') {
						$j(".bb_confirm").addClass("bb_success");
						$j(".bb_confirm").html('Vous êtes désormais inscrit aux alertes concernant le prix du '+$j("#product_id").attr("name")+'.');
					} else {
						$j(".bb_confirm").addClass("bb_error");
						$j(".bb_confirm").html('Une erreur technique est survenue lors de la requête, veuillez réessayer un peu plus tard.');
					}
				});
			} else {
				$j(".bb_price_subscribe_email").css("border", "1px solid red");
			}
		});
		
		$j('.bb_price_subscribe_email').focus(function() {
			if ($j(this).val() == "votre adresse email ici") {
				$j(this).removeClass("bb_empty");
				$j(this).val("");
			}
		});
		
		$j('.bb_price_subscribe_email').blur(function() {
			if ($j(this).val() == "") {
				$j(this).addClass("bb_empty");
				$j(this).val("votre adresse email ici");
			}
		});
	},
	
	best_price_link: function() {
		$j(".bb_price_evolution .bb_best_price_row").click(function() {
			window.open("/go.php?id="+$j(this).attr("id"));
		});
	}
};

// Management of votes for test
BB.voteTest = {
	first_vote: true,		// to know if the visitor has already voted
	status_star_timer: 'go',
	user_note: 0,
	
	init: function() {
		if ($j('.bb_test_summary_note').size() > 0 || $j('.bb_summary_note').size() > 0) {
			var user_note_text;
			$j("#dump").html($j.cookie('note_product['+$j('#product_id').val()+']'));
			// Check if the visitor have already voted
			if ($j.cookie('note_product['+$j('#product_id').val()+']') != null) {
				BB.voteTest.user_note = $j.cookie('note_product['+$j('#product_id').val()+']');
	
				if (BB.voteTest.user_note != 0) {
					BB.voteTest.first_vote = false;
				}
			}
			
			// Put stars empty with the effect
			if (BB.voteTest.first_vote) {
				user_note_text = '<span id="bb_test_summary_note_label">Notez ce produit :</span>';
				user_note_text += '<img src="/images/bb_test_summary_note_star_empty.png" alt="" class="bb_test_summary_star bb_test_summary_star_empty" id="bb_test_summary_star1" />';
				user_note_text += '<img src="/images/bb_test_summary_note_star_empty.png" alt="" class="bb_test_summary_star bb_test_summary_star_empty" id="bb_test_summary_star2" />';
				user_note_text += '<img src="/images/bb_test_summary_note_star_empty.png" alt="" class="bb_test_summary_star bb_test_summary_star_empty" id="bb_test_summary_star3" />';
				user_note_text += '<img src="/images/bb_test_summary_note_star_empty.png" alt="" class="bb_test_summary_star bb_test_summary_star_empty" id="bb_test_summary_star4" />';
				user_note_text += '<img src="/images/bb_test_summary_note_star_empty.png" alt="" class="bb_test_summary_star bb_test_summary_star_empty" id="bb_test_summary_star5" />';
				
				$j('.bb_test_summary_note').html(user_note_text);
				
				BB.voteTest.starblink();		// start the effect
			} else {
				// the visitor has already voted, we calculate the number of star full and empty
				var nb_star_note_complete, nb_star_note_empty;
				
				if (BB.voteTest.user_note < 5) {
					nb_star_note_complete = Math.floor(BB.voteTest.user_note);
					nb_star_note_empty = 5 - nb_star_note_complete;
				} else {
					nb_star_note_complete = 5;
					nb_star_note_empty = 0;
				}
				
				user_note_text = 'Votre note :';
				
				for (var j = 0; j < nb_star_note_complete; j++) {
					user_note_text += '<img src="/images/bb_test_summary_note_star.png" alt="" />';
				}
				
				for (var j = 0; j < nb_star_note_empty; j++) {
					user_note_text += '<img src="/images/bb_test_summary_note_star_empty.png" alt="" />';
				}
				
				for (var j = 1; j <= nb_star_note_complete; j++) {
					$j("#bb_summary_star"+j).attr("src", "/images/bb_test_blue_star_full.png");
				}
				
				$j('.bb_summary_star').removeClass('bb_summary_star');
				$j('#bb_test_summary_note_label').html('Votre note :');
				
				$j('.bb_test_summary_note').html(user_note_text);
			}
		}
		
		// when a visitor hovers over a star, we stop the effect
		$j('.bb_test_summary_star').hover(function() {
			if (BB.voteTest.first_vote) {
				clearInterval(star_timer);
				
				$j(this).attr('src', '/images/bb_test_summary_note_star.png');
				$j(this).prevAll('.bb_test_summary_star').attr('src', '/images/bb_test_summary_note_star.png');
				$j(this).nextAll('.bb_test_summary_star').attr('src', '/images/bb_test_summary_note_star_empty.png');
				
				BB.voteTest.status_star_timer = 'go';
			}
		});
		
		// when a visitor hovers over a star, we stop the effect
		$j('.bb_summary_star').hover(function() {
			if (BB.voteTest.first_vote) {
				$j(this).attr('src', '/images/bb_test_blue_star_full.png');
				$j(this).prevAll('.bb_summary_star').attr('src', '/images/bb_test_blue_star_full.png');
				$j(this).nextAll('.bb_summary_star').attr('src', '/images/bb_test_blue_star_empty.png');
			}
		});
		
		// when a person leave the block with stars, we restart the effect
		$j(".bb_test_summary_note").mouseleave(function() {
			if (BB.voteTest.status_star_timer == 'go' && BB.voteTest.first_vote) {
				BB.voteTest.starblink();
			}
		});
		
		// save the user note when he click on a star
		$j('.bb_test_summary_star').click(function() {
			if (BB.voteTest.first_vote) {
				$j.get('/server.php', {'type': 'note', 'product': $j('#product_id').val(), 'note': $j(this).attr('id').replace('bb_test_summary_star', '')}, function() {
					$j('#bb_test_summary_note_label').html('Votre note :');
					$j('.bb_test_summary_star').removeClass('bb_test_summary_star');
				});
			}
			BB.voteTest.first_vote = false;		// we desactivate the boolean to desactivate the effect
		});
		
		// save the user note when he click on a star
		$j('.bb_summary_star').click(function() {
			if (BB.voteTest.first_vote) {
				$j.get('/server.php', {'type': 'note', 'product': $j('#product_id').val(), 'note': $j(this).attr('id').replace('bb_summary_star', '')}, function() {
					$j('.bb_users_stars').load('/server.php?load_notes='+$j('#product_id').val(), function() {
						$j('.bb_vote_stars').hide();
						$j('.bb_users_stars').show();
					});
				});
			}
			BB.voteTest.first_vote = false;
		});
		
		$j('.bb_users_stars').mouseenter(function() {
			if (BB.voteTest.first_vote) {
				$j(this).hide();
				$j('.bb_vote_stars').show();
			}
		});
		
		$j('.bb_vote_stars').mouseleave(function() {
			$j(this).hide();
			$j('.bb_users_stars').show();
		});
	},
	
	// the effect on the stars
	starblink: function() {
		var i = 1;
		var type = 'asc';
		BB.voteTest.status_star_timer = 'stop';
		star_timer = setInterval(function() {
			if (type == 'asc') $j('#bb_test_summary_star' + i).attr('src', '/images/bb_test_summary_note_star.png');
			else $j('#bb_test_summary_star' + i).attr('src', '/images/bb_test_summary_note_star_empty.png');
			
			if (type == 'asc' && i < 7) {
				i++;
				if (i == 6) type = 'desc';
			} else if (type == 'desc' && i > 1) {
				i--;
				if (i == 1) type = 'asc';
			}
		}, 150);
	}
};

// Pop-up on click on images for a news or a test
BB.popup = {
	init: function() {
		$j('.open_popup_test').click(function() {
			showPopup();
			showBackground();
			$('popup-img').innerHTML = '<br /><img src="'+$j(this).attr("href")+'" alt="" />';
			$('popupad').src = "http://www.mobiles-actus.com/iframe/ad.htm";
			$j("#popup-next, #popup-previous").hide();
			return false;
		});
		
		$j('.open_popup, .bb_gallery a img').click(function() {
			$j("#popup-next, #popup-previous").show();
		});
		
		if ($j('.open_popup').size() > 0) {
			BB.popup.show('.open_popup');
		}
		
		var group_photos_official = new Array();
		var group_photos_topmobile = new Array();
		var group_photos_other = new Array();
		var group_photos_menu = new Array();
		
		// Official photos
		$j(".bb_official").each(function(i) {
			group_photos_official[i] = $j(this).attr("src").replace("http://www.pooki.fr", "");
		});
		$j("a.bb_link_official").click(function(){
			var img_src = $j(this).children().attr("src").replace("http://www.pooki.fr", "");
			showPicture(img_src, group_photos_official);
			return false;
		});
		
		// Topmobile photos
		$j(".photo_topmobile").each(function(i) {
			group_photos_topmobile[i] = $j(this).attr("src").replace(/\/s([0-9]+)/, '\/$1');
		});
		$j("a.bb_link_topmobile").click(function(){
			var img_src = $j(this).children().attr("src").replace(/\/s([0-9]+)/, '\/$1');
			showPicture(img_src, group_photos_topmobile);
			return false;
		});
		
		// Other photos
		$j(".photo_menu").each(function(i) {
			group_photos_other[i] = $j(this).attr("src").replace('_s.jpg', '.jpg');
		});
		$j("a.bb_link_menu").click(function(){
			var img_src = $j(this).children().attr("src").replace('_s.jpg', '.jpg');
			showPicture(img_src, group_photos_menu);
			return false;
		});
		
		// Menu photos
		$j(".photo_other").each(function(i) {
			group_photos_other[i] = $j(this).attr("src").replace('_s.jpg', '.jpg');
		});
		$j("a.bb_link_other").click(function(){
			var img_src = $j(this).children().attr("src").replace('_s.jpg', '.jpg');
			showPicture(img_src, group_photos_other);
			return false;
		});
	},
	
	show: function(bloc_class) {
		photos_news = [];
		i = 0;
		
		$$(bloc_class).each(function (test) {
			photos_news[i] = test.href.replace(/thumbs\//, '');
			test.onclick = function() { showPicture(test.href.replace(/thumbs\//, ''), photos_news); return false; };
			test.onmouseover = function() { test.addClassName('open_popup_hover'); };
			test.onmouseout = function() { test.removeClassName('open_popup_hover'); };
			i++;
		});
	}
};


// Coverflow for product's pictures for tests
BB.coverflow = {
	init: function() {
		if ($j('#myRoundabout').size() > 0) {
			$j("#myRoundabout li").focus(function() {
				$j(this).children('.button').show();
			});
			
			$j("#myRoundabout li").blur(function() {
				$j(this).children('.button').hide();
			});
			
			$j("#myRoundabout").roundabout({
				minOpacity: 0,
				minScale: 0.4,
				btnNext: '.gallery_next',
				btnPrev: '.gallery_previous',
				clickToFocus: false
			});
		}
	}
};

BB.products = {
	init: function() {
		if ($j('.bb_product_menu').size() > 0) {
			$j(".bb_product_menu a.bb_menu_open").click(function() {
				var id = $j(this).attr("id").replace("-open", "");
				if ("none" == $j("#bb_"+id+"_menu").css("display")) {
					$j(".bb_product_menu table").hide();
					$j("#bb_"+id+"_menu").show();
				} else {
					$j(".bb_product_menu table").hide();
				}
			});
			
			$j(".bb_product_menu").parent().mouseleave(function() {
				$j(".bb_product_menu table").hide();
			});
			
			$j("a.bb_add").click(function() {
				var id = $j(this).attr("id");
				var c = BB.compare.count_items();
				var item = $j("#input-"+id);
				var model = $j(this).attr("name");
				if ("" == item.val()) {
					if (c.count < 5) {
						item.val("checked");
						$j("td.bb_checkbox>span#"+id).html("&#10003;");
						$j(".bb_product_menu a#"+id+"-open").html("<span style=\"color:green;\">&#10003;</span>");
						$j(".bb_add").html("Ajouter au comparateur");
						$j.cookie("compare["+id+"][status]", "on");
						$j.cookie("compare["+id+"][name]", model);
					} else {
						$j(".bb_add").html("<span style=\"color:red;\">5 produits max.</span>");
					}
				} else if ("checked" == item.val()) {
					item.val("");
					$j("td.bb_checkbox>span#"+id).html("");
					$j(".bb_product_menu a#"+id+"-open").html("&#9662;");
					$j(".bb_add").html("Ajouter au comparateur");
					$j.cookie("compare["+id+"][status]", null);
					$j.cookie("compare["+id+"][name]", null);
				}
				
				c = BB.compare.count_items();
				
				if (c.count > 1) {
					$j(".bb_compare_link").show();
				} else {
					$j(".bb_compare_link").hide();
				}
				
				BB.compare.update_list($j(this));
			});
		}
	}
};

BB.questions = {
init: function() {
	if ($j('#bb_questions_sort').size() > 0) {
		BB.questions.show();
		BB.questions.order();
		BB.questions.category();
	}
	if ($j('.bb_constructors').size() > 0) {
		BB.questions.products();
	}
	if ($j('#bb_question_name').size() > 0) {
		BB.questions.fillFields();
		BB.comment.help();
		BB.questions.smiley();
	}
},

	show: function() {
		$j('#show').change(function() {
			$j('#bb_questions_sort').submit();
//			BB.questions.constructUrl();
		});
	},
	
	order: function() {
		$j('#order').change(function() {
			$j('#bb_questions_sort').submit();
//			BB.questions.constructUrl();
		});
	},
	
	category: function() {
		$j('#category').change(function() {
			$j('#bb_questions_sort').submit();
//			BB.questions.constructUrl();
		});
	},
	
	products: function() {
		$j('.bb_constructors').change(function() {
			if (0 != $j(this).val()) {
				$j.get('/server.php', {'get_products_list': 1, 'constructor': $j(this).val()}, function(data) {
					$j('#bb_question_products').html(data);
				});
			} else {
				$j('#bb_question_products').html('<option value="0">Choisissez un produit...</option>');
			}
		});
	},
	
	constructUrl: function() {
		var url = '/aide';
		
		if ($j('#product_type').val() != undefined) {
			url += '-' + $j('#product_type').val();
		}
		
		if ($j('#category').val() != 'all') {
			url += '-' + $j('#category').val();
		}
		
		if ($j('#show').val() != 'all') {
			url += '-' + $j('#show').val();
		}
		
		if ($j('#order').val() != 'date') {
			url += '-' + $j('#order').val();
		}
		
		window.location = url + '.html';
	},
	
	// if the visitor has voted, we put his data in the comment's fields
	fillFields: function() {
		if ($j.cookie('question_name') != null && $j('#bb_question_name').val() == '') $j('#bb_question_name').val($j.cookie('question_name').replace(/\+/g, ' '));
		if ($j.cookie('question_email') != null && $j('#bb_question_email').val() == '') $j('#bb_question_email').val($j.cookie('question_email').replace(/\+/g, ' '));
		if ($j.cookie('question_title') != null && $j('#bb_question_title').size() > 0 && $j('#bb_question_title').val() == '') $j('#bb_question_title').val($j.cookie('question_title').replace(/\+/g, ' '));
		if ($j.cookie('question_reply') != null && $j('#bb_question_reply').size() > 0 && $j('#bb_question_reply').val() == '') $j('#bb_question_reply').val($j.cookie('question_reply').replace(/\+/g, ' '));
		if ($j.cookie('question_content') != null && $j('#bb_question_content').size() > 0 && $j('#bb_question_content').val() == '') $j('#bb_question_content').val($j.cookie('question_content').replace(/\+/g, ' '));
	},
	
	smiley: function() {
		$j('.bb_new_smiley').click(function() {
			var comment = $j('#bb_question_content').val();
			if ($j('#bb_question_content').val().length > 0 && comment[$j('#bb_question_content').val().length - 1] != ' ') comment += ' ';
			comment += $j(this).attr('alt') + ' ';
			$j('#bb_question_content').val(comment);
		});
	}
};

BB.replies = {
	
	init: function() {
		if (0 < $j('.bb_question_reply_positive').size() && 0 < $j('.bb_question_reply_positive').size()) {
			BB.replies.positive();
			BB.replies.negative();
		}
	},
	
	positive: function() {
		$j('.bb_question_reply_positive').click(function() {
			if ($j(this).prev().prev('.bb_comment_reply_id').val() != undefined) {
				$j(this).prev('.bb_question_reply_note').load('/server.php?save_comment_opinion=' + $j(this).prev().prev('.bb_comment_reply_id').val() + '&opinion=1');
			} else {
				$j(this).prev('.bb_question_reply_note').load('/server.php?save_reply_opinion=' + $j(this).prev().prev('.bb_question_reply_id').val() + '&opinion=1');
			}
		});
	},
	
	negative: function() {
		$j('.bb_question_reply_negative').click(function() {
			if ($j(this).prev().prev().prev('.bb_comment_reply_id').val() != undefined) {
				$j(this).prev().prev('.bb_question_reply_note').load('/server.php?save_comment_opinion=' + $j(this).prev().prev().prev('.bb_comment_reply_id').val() + '&opinion=-1');
			} else {
				$j(this).prev().prev('.bb_question_reply_note').load('/server.php?save_reply_opinion=' + $j(this).prev().prev().prev('.bb_question_reply_id').val() + '&opinion=-1');
			}
		});
	}
};

BB.manual = {
	init: function() {
		if ($j('#pdf-preview').size() > 0) {
			var position = $j("div.manuelpreview table td.selected").position();
			var leftPos = position.left;
			var parentPosition = $j("div.manuelpreview table td.selected").parent().position();
			var parentLeftPos = parentPosition.left;
			$j("div.manuelpreview").scrollLeft(leftPos-parentLeftPos);
		}
	}
};

BB.accessoire = {
	init: function() {
		if ($j('.bb_best_accessories_row').size() > 0) {
			$j('.bb_accessories_radio').click(function() {
				BB.accessoire.filter();
			});
		}
			
		if ($j('#bb_accessories_filter').size() > 0) {
			$j('#bb_accessories_filter').change(function() {
				BB.accessoire.filderp();
			});
		}
	},
	filderp: function() {
		if ($j('#bb_accessories_filter').val() == "all") {
			$j('.bb_test_prices_content .bb_prices_list table').show();
			$j('.bb_test_prices_content .bb_prices_list table#bb_none').hide();
		} else {
			$j('.bb_test_prices_content .bb_prices_list table').hide();
			$j('table.bb_full.bb_accessories_'+$j('#bb_accessories_filter').val()).show();
			if ($j('table.bb_full.bb_accessories_'+$j('#bb_accessories_filter').val()).size() == 0) {
				$j('.bb_test_prices_content .bb_prices_list table#bb_none').show();
			}
		}
	},
	filter: function() {
		if (!BB.accessoire.isFiltered()) {
			$j('.bb_test_prices_content .bb_prices_list table').show();
			$j('.bb_test_prices_content .bb_prices_list table#bb_none').hide();
		} else {
			var hasResults = false;
			$j('.bb_test_prices_content .bb_prices_list table').hide();
			$j('.bb_accessories_radio').each(function(index) {
				if ($j(this).attr('checked')) {
					$j('.bb_test_prices_content .bb_prices_list table[class*="' + $j(this).attr('id') + '"]').show();
					if ($j('.bb_test_prices_content .bb_prices_list table[class*="' + $j(this).attr('id') + '"]').size() > 0)
						hasResults = true;
				}
			});
			
			if (hasResults)
				$j('.bb_test_prices_content .bb_prices_list table#bb_none').hide();
			else
				$j('.bb_test_prices_content .bb_prices_list table#bb_none').show();
		}
	},
	isFiltered: function() {
		var isChecked = false;
		$j('.bb_accessories_radio').each(function(index) {
			if ($j(this).attr('checked')) {
				isChecked = true;
				return;
			}
		});
		return isChecked;
	}
};

BB.compare = {
	init: function() {
		if ($j('#bb_compare_form').size() > 0) {
			$j(".bb_top-products input:checked").each(function() {
				if ("on" == $j.cookie("compare["+$j(this).attr("id").replace("input-", "")+"][status]")) {
					$j(this).parent().parent().addClass("bb_selected");
				} else {
					$j(this).removeAttr("checked");
				}
				BB.compare.update_list($j(this));
			});
			
			if ($j('.bb_compare_recap .bb_submit a.bb_generic_button').size() > 0) {
				BB.compare.blink();
			}
			
			BB.compare.display_list();
		
			$j(".bb_products-table td").hover(function() {
				var id = $j(this).attr("id");
			}, function() {
				var id = $j(this).attr("id");
				var checked = $j("#bb_compare-"+id+" input").attr("checked");
			});
			
			$j(".bb_compare_box").hover(function() {
				$j(this).children("label").show();
			}, function() {
				$j(this).children("label").hide();
			});
			
			$j(".bb_compare_box input").click(function() {
				var c = BB.compare.count_items();
				var item = $j(this);
				var id = item.attr("id").replace("input-", "");
				var model = $j("td#"+id).children("a").children("p").children("label").html();
				if (item.attr("checked")) {
					if (c.count < 5) {
						$j.cookie("compare["+id+"][status]", "on");
						$j.cookie("compare["+id+"][name]", model);
						$j("#"+id+"_name").addClass("bb_selected");
					} else {
						BB.compare.deny_item(item);
					}
				} else {
					$j("#"+id+"_name").toggleClass("bb_selected");
					$j.cookie("compare["+id+"][status]", null);
					$j.cookie("compare["+id+"][name]", null);
				}
				
				BB.compare.update_list($j(this));
			});
			
			$j("#bb_compare_more").toggle(function() {
				$j(".bb_bullet").html("&#9662;");
				$j(".bb_compare_list").show();
			}, function() {
				$j(".bb_bullet").html("&#9656;");
				$j(".bb_compare_list").hide();
			});
			
			$j(".bb_compare_recap a.bb_generic_button").click(function() {
				var c = BB.compare.count_items();
				if (c.count > 1) {
					$j('#bb_compare_form').submit();
				} else {
					alert('Veuillez sélectionner deux produits minimum.');
				}
			});
		}
		
		if ($j('div.bb_compare').size() > 0) {
			$j(".bb_specs").fixedtableheader();
				
			$j(".bb_model").click(function() {
				if ('' != $j(this).attr("id")) {
					window.location = "/resume-"+$j(this).attr("id")+".html";
				}
			});
			
			$j("div.bb_share_link_content input").click(function() {
				$j(this).select();
			});
			$j("div.bb_share_link_content input").focus(function() {
				$j(this).select();
			});
		}
	},
	
	update_list: function(item) {
		var c = BB.compare.count_items();
		
		if (c.count <= 5) {
			BB.compare.display_list();
		} else {
			BB.compare.deny_item(item);
		}
	},
	
	remove_item: function(id) {
		$j("#input-"+id).removeAttr("checked");
		$j("#"+id+"_name").toggleClass("bb_selected");
		$j.cookie("compare["+id+"][status]", null);
		$j.cookie("compare["+id+"][name]", null);
		BB.compare.display_list();
	},
	
	deny_item: function(item) {
		item.removeAttr("checked");
		item.parent().parent().removeClass("bb_selected");
		$j(".bb_compare_recap span.bb_warning").css("color", "red");
		$j(".bb_compare_recap span.bb_warning").fadeOut(100);
		$j(".bb_compare_recap span.bb_warning").fadeIn(100);
		$j(".bb_compare_recap span.bb_warning").fadeOut(100);
		$j(".bb_compare_recap span.bb_warning").fadeIn(100, function() {
			$j(".bb_compare_recap span.bb_warning").css("color", "#444");
		});
	},
	
	count_items: function() {
		var cookies = new Object();
		var split = document.cookie.split(';');
		var c = 0;
		var url, model;
		var pattern = /compare/g;
		var pattern2 = /\[name\]/g;
		$j.each(split, function() {
			var name = this.split("=");
			if (pattern2.test(name[0])) {
				var url = name[0].replace("compare[", "");
				var url = url.replace("][status]", "");
				var url = url.replace("][name]", "");
				var url = url.replace(" ", "");
				cookies[decodeURIComponent(url)] = decodeURIComponent(name[1]);
				c++;
			}
		});
		return {count : c, cookies : cookies};
	},
	
	display_list: function() {
		var c = BB.compare.count_items();
		
		$j("#bb_compare_count").html(c.count);
		$j(".bb_compare_list").html("");
		
		$j.each(c.cookies, function(id, model) {
			$j(".bb_compare_list").append("<p id=\""+id+"\">"+model+" <a onclick=\"BB.compare.remove_item('"+id+"')\" class=\"bb_remove_item\" title=\"Retirer le produit\">(-)</a></p>");
		});
		
		/*if (c.count > 1) {
			$j(".bb_compare_recap .bb_submit").html("<a class=\"bb_generic_button\" onclick=\"$j(\'#bb_compare_form\').submit();\"><img src=\"/images/magnifying.png\" />comparez !</a>");
		} else {
			$j(".bb_compare_recap .bb_submit").html("");
		}*/
	},
	
	blink: function() {
		setInterval(function() {
			$j(".bb_compare_recap .bb_submit a.bb_generic_button").toggleClass("bb_highlight");
		}, 500);
	}
};

BB.comparator = {
	init: function() {
		if ($j('#bb_filter_form').size() > 0) {
			$j(".bb_slider-range").slider({
				range: true,
				min: 0,
				values: [0, 10],
				slide: function(event, ui) {
					var id = $j(this).attr("id");
					var min = ui.values[0];
					var max = ui.values[1];
					if (max == $j("#"+id+"_max").val()) {
						max = $j("#"+id+"_max").val()+"+";
					}
					
					$j("#"+id+"_min_amount").val(min);
					$j("#"+id+"_max_amount").val(max);
				},
				create: function(event, ui) {
					var id = $j(this).attr("id");
					var max = $j("#"+id+"_max").val();
					
					if ($j("#"+id+"_max").size() > 0) {
						var max = parseInt($j("#"+id+"_max").val());
					} else {
						var max = 10;
					}
					
					if ($j("#"+id+"_min").size() > 0) {
						var min = parseInt($j("#"+id+"_min").val());
					} else {
						var min = 0;
					}
					
					if ($j("#"+id+"_step").size() > 0) {
						var step = parseInt($j("#"+id+"_step").val());
					} else {
						var step = 1;
					}
					
					$j("#"+id).slider("option", "max", max);
					$j("#"+id).slider("option", "min", min);
					$j("#"+id).slider("option", "step", step);
					$j("#"+id).slider("option", "values", [min, max]);
					$j("#"+id+"_min_amount").val(min);
					$j("#"+id+"_max_amount").val(max+"+");
				}
			});
			
			$j(".bb_min_amount,.bb_max_amount").change(function() {
				var id = $j(this).attr("id").replace(/_(min|max)_amount/i, "");
				var min = $j("#"+id+"_min_amount").val();
				var max = $j("#"+id+"_max_amount").val();
				$j("#"+id).slider("option", "values", [parseInt(min), parseInt(max)]);
			});
			
			$j("#bb_filter_parent").click(function() {
				if (!$j(this).attr('checked')) {
					$j("#bb_filter_child").removeAttr('checked');
				}
			});
		
			$j("#bb_filter_child").click(function() {
				if ($j(this).attr('checked')) {
					$j("#bb_filter_parent").attr('checked', 'checked');
				}
			});
			
			if ("quelecran" == $j("#bb_website").val()) {
				$j("#bb_select_tv_category").change(function() {
					$j("#bb_filter_form").submit();
				});
			}
			
			$j(".bb_pagination a").click(function(event) {
				$j("#bb_filter_form").attr("action", $j(this).attr("href"));
				$j("#bb_filter_form").submit();
				event.preventDefault();
			});
			
			$j("a.bb_submit").click(function() {
				$j("#bb_filter_form").submit();
			});
			
			$j("#bb_select_constructor,#bb_select_category,#bb_select_sort").change(function() {
				var action = "comparateur";
				
				if ("" != $j("#bb_select_category").val()) {
					action += "-"+$j("#bb_select_category").val();
				}
				
				if ("" != $j("#bb_select_constructor").val()) {
					action += "-"+$j("#bb_select_constructor").val();
				}
				
				$j("#bb_filter_form").attr("action", action+".html");
				$j("#bb_filter_form").submit();
			});
		}
	}
};

BB.compareFromSummary = {
	init: function() {
		if ($j('.bb_compare_from_summary').size() > 0) {
			$j(".bb_compare_from_summary td.bb_checkbox input").click(function() {
				var id = $j(this).attr("id").replace("input-", "");
				var c = BB.compare.count_items();
				var item = $j(this);
				var model = $j("#bb_name-"+id).attr("name");
				if (item.attr("checked")) {
					if (c.count < 5) {
						$j.cookie("compare["+id+"][status]", "on");
						$j.cookie("compare["+id+"][name]", model);
					}
				} else {
					$j.cookie("compare["+id+"][status]", null);
					$j.cookie("compare["+id+"][name]", null);
				}
				
				c = BB.compare.count_items();

				if (c.count > 1) {
					$j(".bb_compare_link").show();
				} else {
					$j(".bb_compare_link").hide();
				}
				
				BB.compare.update_list($j(this));
			});
		}
	}
};

// call inits
$j(document).ready(function() {
	BB.menu.init();
	BB.searchTop.init();
	BB.shoppingTable.init();
	BB.lastNewsTable.init();
	BB.topTen.init();
	BB.homeSlider.init();
	BB.comment.init();
	BB.news.init();
	BB.test.init();
	BB.opinions.init();
	BB.prices.init();
	BB.voteTest.init();
	BB.popup.init();
	BB.coverflow.init();
	BB.products.init();
	BB.questions.init();
	BB.replies.init();
	BB.manual.init();
	BB.accessoire.init();
	BB.compare.init();
	BB.comparator.init();
	BB.compareFromSummary.init();
});
