// UTF8‼

// IQD Distributors
$(document).ready(function(){

	$("#map_flt_form select")
		.hide()
		.each(function(i) {
			// Create Drop Down Containers and Title Container for each HTML Select
			$(this).parent().append('<div class="title">All</div><div class="dropdown" id="ul_' + $(this).attr("id") + '"><div class="pos"><div class="options"><ul></ul></div></div></div>');
			// Create Link for each Option
			$(this).children("option").each(function(i) {
				$("#ul_" + $(this).parent().attr("id")).children("div.pos").children("div.options").children("ul")
					.append('<li><a onclick="document.getElementById(\'' + $(this).parent().attr("id") + '\').value=\'' + $(this).attr("value") + '\';"' + (($(this).attr("title") == "") ? '' : ' title="' + $(this).attr("title") +'"') + '>' + $(this).html() + '</a></li>');
				// Update Title if selected
				($(this).attr("selected") == true) ? $("#th_" + $(this).parent().attr("id") + " div.title").html($(this).html()) : '';
			});
		});

	$("#map_flt_form div.dropdown div.pos").hide();

	// Process All Drop Downs
	var a_elm = $("#map_flt_form div.dropdown");
	for (v_loop = 0; v_loop < a_elm.length; v_loop++) {
		v_grp_div_id = a_elm[v_loop].id;
		// (Get width of main container)
		var v_width = $("#" + v_grp_div_id + " div.pos").css("width");
		// (need to set fixed width of ul because of IE loosing focus when menu is over table text)
		$("#" + v_grp_div_id + " div.pos ul").css({
			width:"" + (v_width.replace("px","")-4) + "px"
		});
		// Each Option in Group
		var a_grp_div_elm = $("#" + v_grp_div_id + " ul li");
		// If there are more than 17 options
		// Setup limiting/scrolling in drop boxes (inc letter press jumps to)
		if (a_grp_div_elm.length > 17) {
			// Add Scroll controls
			$("#" + v_grp_div_id + " div.pos")
				.prepend('<ul class="up"><li><a>Up</a></li></ul>')
				.append('<ul class="down"><li><a>Down</a></li></ul>');
			// Limit height of option container (item height x number of items) and set-up for positioning
			$("#" + v_grp_div_id + " div.pos div.options").css({
				position:"relative",
				height:"358px"
			});
			$("#" + v_grp_div_id + " div.pos div.options ul").css({
				position:"absolute"
			});
			// Add Down triggers
			$("#" + v_grp_div_id + " div.pos ul.down li a")
				.hover(function(){
					// Get number of items and calculate height of option container
					var a_grp_div_elm = $(this).parent().parent().parent().children("div.options").children("ul").children("li");
					$(this).parent().parent().parent().children("div.options").children("ul").animate({
						top:"-" + (((a_grp_div_elm.length)-17)*21) + "px"
					},2000);
				},function () {
					$(this).parent().parent().parent().children("div.options").children("ul").stop();
				});
			// Add Up triggers
			$("#" + v_grp_div_id + " div.pos ul.up li a")
				.hover(function(){
					$(this).parent().parent().parent().children("div.options").children("ul").animate({
						top:"0px"
					},2000);
				},function () {
					$(this).parent().parent().parent().children("div.options").children("ul").stop();
				});
		}

	}

	// Drop Down Mechanism
	$("#map_flt_form div.dropdown")
		.hover(function(){
			// Hide All other drop downs here (this needed because of input z-index messes up the normal fadeout)
			$("#map_flt_form div.dropdown").not("#" + $(this).attr("id")).children("div.pos").fadeOut("fast");
			// Show this drop down
			$(this).children("div.pos").fadeIn("fast");
			// Set-up keyboard shortcuts
			v_id_cur = $(this).attr("id");
			$(document).keypress(function(e) {
				if ((e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 88) || (e.which >= 97 && e.which <= 120)) {
					var v_key = String.fromCharCode(e.which);
					// Each Option
					var a_elm = $("#" + v_id_cur + " div.pos div.options ul li a");
					// If there are more than 17 options then check if key shortcut
					if (a_elm.length > 17) {
						for (v_loop = 0; v_loop < a_elm.length; v_loop++) {
							// Check for first match
							if (v_key.toLowerCase() == a_elm[v_loop].innerHTML.toLowerCase().substring(0,1)) {
								v_top = v_loop*21; // New Position
								v_top_max = (((a_elm.length)-17)*21); // 
								if (v_top > v_top_max) {
									v_top = v_top_max;
								}
								$("#" + v_id_cur + " div.pos div.options ul").animate({
									top:"-" + v_top + "px"
								},"fast");
								break;
							}
						}
					}
				}
			});

		},function () {
			$(this).children("div.pos").fadeOut("fast");
			// Remove the keyboard shortcuts
			$(document).unbind();
		});

	//--------------------------------------------------------------		
		
	// Hide Rows missing ANY required class(s)
		
	var area = 
	{
		"a_alcom"		: { "a_class"  : ['.map_flt_dcmp-6-','.map_flt_dcmp-34-','.map_flt_dcmp-38-'] },
		"a_arrow"		: { "a_class"  : ['.map_flt_dcmp-1-','.map_flt_dcmp-3-','.map_flt_dcmp-185-','.map_flt_dcmp-188-','.map_flt_dcmp-14-','.map_flt_dcmp-19-','.map_flt_dcmp-141-','.map_flt_dcmp-142-','.map_flt_dcmp-143-','.map_flt_dcmp-145-','.map_flt_dcmp-147-','.map_flt_dcmp-146-','.map_flt_dcmp-154-','.map_flt_dcmp-148-','.map_flt_dcmp-149-','.map_flt_dcmp-150-','.map_flt_dcmp-151-','.map_flt_dcmp-153-','.map_flt_dcmp-152-','.map_flt_dcmp-155-','.map_flt_dcmp-144-','.map_flt_dcmp-81-','.map_flt_dcmp-27-','.map_flt_dcmp-29-','.map_flt_dcmp-187-','.map_flt_dcmp-52-','.map_flt_dcmp-54-','.map_flt_dcmp-228-','.map_flt_dcmp-86-','.map_flt_dcmp-44-','.map_flt_dcmp-87-','.map_flt_dcmp-177-','.map_flt_dcmp-178-','.map_flt_dcmp-179-','.map_flt_dcmp-180-','.map_flt_dcmp-186-'] },
		"a_easby"		: { "a_class"  : ['.map_flt_dcmp-181-'] },
		"a_edv"			: { "a_class"  : ['.map_flt_dcmp-264-'] },
		"a_elfa"		: { "a_class"  : ['.map_flt_dg-12-'] },
		"a_dove"		: { "a_class"  : ['.map_flt_dg-11-'] },
		"a_gateway"		: { "a_class"  : ['.map_flt_dcmp-62-'] },
		"a_farnell"		: { "a_class"  : ['.map_flt_dcmp-61-'] },
		"a_future"		: { "a_class"  : ['.map_flt_dg-3-'] },
		"a_hitek"		: { "a_class"  : ['.map_flt_dcmp-66-'] },
		"a_newwoo"		: { "a_class"  : ['.map_flt_dcmp-262-'] },
		"a_richardson"	: { "a_class"  : ['.map_flt_dcmp-2-','.map_flt_dcmp-9-','.map_flt_dcmp-11-','.map_flt_dcmp-13-','.map_flt_dcmp-15-','.map_flt_dcmp-18-','.map_flt_dcmp-20-','.map_flt_dcmp-23-','.map_flt_dcmp-28-','.map_flt_dcmp-32-','.map_flt_dcmp-35-','.map_flt_dcmp-37-','.map_flt_dcmp-39-','.map_flt_dcmp-49-','.map_flt_dcmp-53-','.map_flt_dcmp-58-','.map_flt_dcmp-59-','.map_flt_dcmp-77-','.map_flt_dcmp-78-','.map_flt_dcmp-80-','.map_flt_dcmp-102-','.map_flt_dcmp-112-','.map_flt_dcmp-113-','.map_flt_dcmp-160-','.map_flt_dcmp-161-','.map_flt_dcmp-191-'] },
		"a_rs"			: { "a_class"  : ['.map_flt_dcmp-5-','.map_flt_dcmp-8-','.map_flt_dcmp-16-','.map_flt_dcmp-21-','.map_flt_dcmp-24-','.map_flt_dcmp-31-','.map_flt_dcmp-40-','.map_flt_dcmp-45-','.map_flt_dcmp-55-','.map_flt_dcmp-57-','.map_flt_dcmp-63-','.map_flt_dcmp-192-','.map_flt_dcmp-193-','.map_flt_dcmp-194-','.map_flt_dcmp-195-','.map_flt_dcmp-196-','.map_flt_dcmp-197-','.map_flt_dcmp-198-','.map_flt_dcmp-200-','.map_flt_dcmp-201-','.map_flt_dcmp-202-','.map_flt_dcmp-204-','.map_flt_dcmp-205-','.map_flt_dcmp-206-','.map_flt_dcmp-207-','.map_flt_dcmp-208-','.map_flt_dcmp-209-','.map_flt_dcmp-210-','.map_flt_dcmp-211-','.map_flt_dcmp-212-','.map_flt_dcmp-213-','.map_flt_dcmp-214-','.map_flt_dcmp-215-','.map_flt_dcmp-216-','.map_flt_dcmp-217-','.map_flt_dcmp-218-','.map_flt_dcmp-219-','.map_flt_dcmp-220-','.map_flt_dcmp-221-','.map_flt_dcmp-222-','.map_flt_dcmp-223-','.map_flt_dcmp-224-','.map_flt_dcmp-225-','.map_flt_dcmp-226-','.map_flt_dcmp-227-'] },
		"a_spoerle"		: { "a_class"  : ['.map_flt_dcmp-3-','.map_flt_dcmp-141-','.map_flt_dcmp-142-','.map_flt_dcmp-143-','.map_flt_dcmp-144-','.map_flt_dcmp-145-','.map_flt_dcmp-146-','.map_flt_dcmp-154-','.map_flt_dcmp-148-','.map_flt_dcmp-149-','.map_flt_dcmp-150-','.map_flt_dcmp-151-','.map_flt_dcmp-148-','.map_flt_dcmp-153-','.map_flt_dcmp-152-','.map_flt_dcmp-155-'] },
		"a_wiselink"	: { "a_class"  : ['.map_flt_dcmp-50-','.map_flt_dcmp-190-','.map_flt_dcmp-36-','.map_flt_dcmp-60-'] },
		"a_wdi"			: { "a_class"  : ['.map_flt_dcmp-267-'] },
		"a_road-well"	: { "a_class"  : ['.map_flt_dg-34-'] }
	};
	
	//alert(area['a_alcom'].a_class.length);
	
	$("#logos li")
	.css("cursor","pointer")	
	.click(function(){
		$("#distributors tbody tr").hide().removeClass("selected");
	
		var v_name = "a_" + $(this).attr("id");
	
		for (i=0;i<area[v_name].a_class.length;i++) {
		  $(area[v_name].a_class[i]).show().addClass("selected");
		}
		
		$("#distributors tbody tr.selected").each(function(i){
			// Get Current Row Country Class Name
			v_class = $(this).attr("class");
			v_class = v_class.substring(v_class.indexOf("map_flt_dcntry"),v_class.indexOf(" ",v_class.indexOf("map_flt_dcntry"))).replace(" ","");
			if (v_class != "dcntry_title") {
				// Show all country title rows with country title class
				$("#distributors tbody tr[class*='dcntry_title'][class*='" + v_class + "']").show();
			}
		});
	});

	//--------------------------------------------------------------

	// Drop Down Option Triggers
	$("#map_flt_form div.dropdown div.options a").click(function(){
		$("#distributors tbody tr").removeClass("selected");
		// Update Selected Filter Title
		$(this).parents(".item").children(".title").html($(this).html());
		// Hide Drop Down
		$(this).parents("div.pos").fadeOut("fast",function(){
			// Build Array of Selected Filters from Form Values
			var a_dst_flt = new Array();
			$("#map_flt_form select").each(function(i){
				if ($(this).val() != "" && $(this).val() != 'undefined') {
					a_dst_flt[i] = new Array();
					a_dst_flt[i][0] = $(this).attr("id");
					a_dst_flt[i][1] = $(this).val();
				}
			});
			// Hide Rows missing ANY required class(s)
			$("#distributors tbody tr")
				.show()
				.each(function(i){
					var row = $(this);
					jQuery.each(a_dst_flt,function(i,val){
						var v_class = String(val).replace(",","-");
						if (v_class != 'undefined') {
							if (!row.hasClass(v_class + '-')) {
								row.hide();

							} else {
								row.addClass("selected");
							}
						}
					});
				});
			// Check Visible Rows have their Country Title
			$("#distributors tbody tr.selected").each(function(i){
				// Get Current Row Country Class Name
				v_class = $(this).attr("class");
				v_class = v_class.substring(v_class.indexOf("map_flt_dcntry"),v_class.indexOf(" ",v_class.indexOf("map_flt_dcntry"))).replace(" ","");
				if (v_class != "dcntry_title") {
					// Show all country title rows with country title class
					$("#distributors tbody tr[class*='dcntry_title'][class*='" + v_class + "']").show();
				}
			});
		});
	});

});