// UTF8‼
$(document).ready(function(){

	var v_meta_lang = 'en';
	switch (location.host.toLowerCase()) {
		case "www.iqdfrequencyproducts.de":
			v_meta_lang = 'de';
		break;
		case "www.iqdfrequencyproducts.fr":
			v_meta_lang = 'fr';
		break;
		case "www.iqdfrequencyproducts.ru":
			v_meta_lang = 'ru';
		break;
		default:
			if (location.pathname.toLowerCase().indexOf('_de/') > -1) {
				v_meta_lang = 'de';
			}
			if (location.pathname.toLowerCase().indexOf('_fr/') > -1) {
				v_meta_lang = 'fr';
			}
			if (location.pathname.toLowerCase().indexOf('_ru/') > -1) {
				v_meta_lang = 'ru';
			}
		break;
	}
	
	

	var a_translations = new Array(
		new Array(
			'All',
			new Array(
				new Array('fr','Tout'),
				new Array('de','Alle')
			)
		),
		new Array(
			'Enter',
			new Array(
				new Array('fr','Saisir'),
				new Array('de','Geben Sie')
			)
		)
	);
	function f_translate(v_needle) {
		var translation = v_needle;
		if (v_meta_lang != 'en') {
			$.each(a_translations,function(i){
				// Find Word Translation
				if (this[0] == v_needle) {
					// Find Language
					$.each(a_translations[i][1],function(i){
						if (this[0] == v_meta_lang) {
							translation = this[1];
						}
					});
				}
			});
		}
		return translation;
	}

	$("#main_content").append('<div id="dropdowns"></div>');

	$("#search-prod select")
		.hide()
		.each(function(i) {
			// Create Drop Down Containers and Title Container for each HTML Select
			$("#dropdowns").append('<div class="dropdown" id="ul_' + $(this).attr("id") + '"><div class="pos"><div class="options"><ul></ul></div></div></div>');
			$("#th_" + $(this).attr("id")).append('<div class="title">' + f_translate('All') + '</div>');
			// Create Link for each Option
			$(this).children("option").each(function(i) {
				
				if ($(this).attr("class") != 'hidden') {
					$("#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()) : '';
			});
		});

	// Define text input presets
	var a_dropdowns_values = new Array(
		new Array(
			'pfg16rr',new Array(
				new Array('',f_translate('All')),
				new Array('0,50','0 to 50°C'),
				new Array('0,70','0 to 70°C'),
				new Array('-10,60','-10 to 60°C'),
				new Array('-10,70','-10 to 70°C'),
				new Array('-20,70','-20 to 70°C'),
				new Array('-20,75','-20 to 75°C'),
				new Array('-30,75','-30 to 75°C'),
				new Array('-30,80','-30 to 80°C'),
				new Array('-30,85','-30 to 85°C'),
				new Array('-40,85','-40 to 85°C'),
				new Array('-55,125','-55 to 125°C')
			)
		),
		new Array(
			'pfg6r',new Array(
				new Array('',f_translate('All')),
				new Array('5','±5ppm'),
				new Array('10','±10ppm'),
				new Array('15','±15ppm'),
				new Array('20','±20ppm'),
				new Array('25','±25ppm'),
				new Array('30','±30ppm'),
				new Array('50','±50ppm'),
				new Array('100','±100ppm'),
				new Array('200','±200ppm'),
				new Array('1000','±1,000ppm'),
				new Array('2000','±2,000ppm'),
				new Array('10000','±10,000ppm')
			)
		),
		new Array(
			'pfg7r',new Array(
				new Array('',f_translate('All')),
				new Array('5','±5ppm'),
				new Array('10','±10ppm'),
				new Array('15','±15ppm'),
				new Array('20','±20ppm'),
				new Array('25','±25ppm'),
				new Array('30','±30ppm'),
				new Array('50','±50ppm'),
				new Array('100','±100ppm'),
				new Array('200','±200ppm'),
				new Array('1000','±1,000ppm'),
				new Array('2000','±2,000ppm'),
				new Array('10000','±10,000ppm')
			)
		),
		new Array(
			'pfg8rr',new Array(
				new Array('',f_translate('All')),
				new Array('0,50','0 to 50°C'),
				new Array('0,70','0 to 70°C'),
				new Array('-10,60','-10 to 60°C'),
				new Array('-10,70','-10 to 70°C'),
				new Array('-20,70','-20 to 70°C'),
				new Array('-20,75','-20 to 75°C'),
				new Array('-30,75','-30 to 75°C'),
				new Array('-30,80','-30 to 80°C'),
				new Array('-30,85','-30 to 85°C'),
				new Array('-40,85','-40 to 85°C'),
				new Array('-55,125','-55 to 125°C')
			)
		),
		new Array(
			'pfg9r',new Array(
				new Array('',f_translate('All')),
				new Array('6','6pF'),
				new Array('9','9pF'),
				new Array('12.5','12.5pF'),
				new Array('16','16pF'),
				new Array('18','18pF'),
				new Array('20','20pF'),
				new Array('30','30pF'),
				new Array('SR','SR')
			)
		)
	);

	// Pre-Defined text input Drop Downs
	var v_dropdowns_html = ('<div id="dropdowns_text">');
	var a_inputs = $("#search-prod input");
	for (var i = 0; i < a_inputs.length; i++) {
		// Loop All Search Selects
		if (a_inputs[i].type == 'text') {
			// Loop Drop Down Preset Array
			for (var v_dropdowns_loop = 0; v_dropdowns_loop < a_dropdowns_values.length; v_dropdowns_loop++) {
				// Check for match in preset array for current input id
				if (a_dropdowns_values[v_dropdowns_loop][0] == a_inputs[i].id) {
					v_dropdowns_html += ('<div class="dropdown" id="ul_' + a_inputs[i].id + '">');
					v_dropdowns_html += ('<div class="pos">');
					v_dropdowns_html += ('<div class="options">');
					v_dropdowns_html += ('<ul>');
					for (var v_dropdowns_loop2 = 0; v_dropdowns_loop2 < a_dropdowns_values[v_dropdowns_loop][1].length; v_dropdowns_loop2++) {
						v_dropdowns_html += ('<li>');
						v_dropdowns_html += ('	<a onclick="document.getElementById(\'' + a_inputs[i].id + '\').value=\'' + a_dropdowns_values[v_dropdowns_loop][1][v_dropdowns_loop2][0] + '\';">' + a_dropdowns_values[v_dropdowns_loop][1][v_dropdowns_loop2][1] + '</a>');
						v_dropdowns_html += ('</li>');
					}
					v_dropdowns_html += ('</ul>');
					v_dropdowns_html += ('</div>');
					v_dropdowns_html += ('</div>');
					v_dropdowns_html += ('</div>');
					break;
				}
			}
		}
	}
	v_dropdowns_html += ('</div>');

	// Output the Drop Downs (and hide them)
	$("#dropdowns").append(v_dropdowns_html);
	$("#dropdowns div.dropdown div.pos").hide();

	// Process All Drop Downs
	var a_elm = $("#dropdowns 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();
				});
		}
	}

	// Disable Temp Range Text Boxes (Presets only as request by Paul Fear)
	$("#pfg8rr, #pfg16rr, #pfg15r")
		.attr("readonly","readonly")
		.css({
			backgroundColor:"#96B2DD",
			cursor:"default"
		});

	// Drop Down Mechanism
	$("#dropdowns div.dropdown")
		.click(function(){
			// Hide All other drop downs here (this needed because of input z-index messes up the normal fadeout)
			$("#dropdowns 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;
							}
						}
					}
				}
			});

		})
		.hover(function(){},function(){
			$(this).children("div.pos").fadeOut("fast");
			// Remove the keyboard shortcuts
			$(document).unbind();
		});

	// Drop Down Mechanism (for Pre-Defined text inputs)
	$("#search-prod input")
		.click(function(){
			$("#dropdowns div div.pos").hide();
			$('#ul_' + ($(this).attr("id")) + ' div.pos').fadeIn("fast");
		})
		.hover(function(){},function(){
			($(this).attr("value") == "") ? $(this).attr("value",f_translate('All') + "...")  :'';
		});

	// Drop Down Option Triggers
	$("#dropdowns div.dropdown div.options").click(function(){
			var v_id = $(this).parent().parent().attr("id");
			if (v_id == "ul_pfg0o") {
				// Selecting Product Type Clears All Other Search Fields (exc itself)
				$("select").not("#pfg0o").val("a");
				$("input").val("");
			}
			if (v_id == "ul_pfg_f") {
				$(this).parent().fadeOut("fast");
				// Only update Title (dont submit the form)
				v_value = $("#" + v_id.replace("ul_","") ).attr("value");
				$("#th_pfg_f div.title").html(v_value);
			} else {
				// Submit the Form
				$("#main_content tbody, #dropdowns").empty();
				$("#search-prod").submit();
			}
		});


	// Product Images (clearing - IE doesn't always capture the hover out event to hide the pop over image)
	$("#search-prod tbody td").not(".thumb").hover(function(){
			$("#prod_img").hide();
			$("#search-prod tr.hover").removeClass("hover");
		},function () {
		});

	// Product Images
	$("#search-prod tbody td.thumb img").parent().hover(function(e){
			$(this).parent().addClass("hover");
			($("#prod_img").length == 0) ? $("body").append('<div id="prod_img"><img alt="Product Image" /></div>') : '';
			$("#prod_img")
				.show()
				.css({
					position:"absolute",
					top:(e.pageY-40)+"px",
					left:(e.pageX-130)+"px"
				})
				.hover(function(){
				},function () {
					$(this).hide();
					$("#search-prod tr.hover").removeClass("hover");
				})
				.children("img")
					.attr("src",$(this).children("img").attr("src").replace("XS","M"))
					.attr("alt",$(this).children("img").attr("alt"));
			
			if ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent)) {
				// PNG Fix
				DD_belatedPNG.fix('#prod_img img');
			}
			//t_prod_img_hide = setTimeout("f_prod_img_hide()",2000);
		},function () {
		});

	// Capture Certain Links (to pass the form values for pre-filling the contact form)
	$("a[href*='/contact/']").click(function(){
		$(this).attr("href",($(this).attr("href") + "?" + $("form").serialize()));
	});
	
	// Capture Certain Links (to load PDF specification as new page)
	$("a[href*='/products/details/']").attr('target','_blank');

});

//function f_prod_img_hide() {
//	clearTimeout(t_prod_img_hide);
//	$("#prod_img").hide();
//	$("#search-prod tr.hover").removeClass("hover");
//}
