/* Modified bgsleight script to fix PNG alpha transparency in IE6 - used getElementById to specify specific elements on which to run filter */
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	window.attachEvent("onload", alphaBackgrounds);
}
function alphaBackgrounds(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	var applyTo = new Array();
	applyTo[0] = "contentPanel";
	applyTo[1] = "pageFooter";
	applyTo[2] = "GFDMbrand";
	
	for (i=0; i<applyTo.length; i++){
		var bg = document.getElementById(applyTo[i]).currentStyle.backgroundImage;
		var br = document.getElementById(applyTo[i]).currentStyle.backgroundRepeat;
		if (br.match(/no-repeat/i) != null){
			var mysizemethod = "crop";
		} else {
			var mysizemethod = "scale";
		}
		if (itsAllGood && bg){
			if (bg.match(/\.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				document.getElementById(applyTo[i]).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='"+mysizemethod+"')";
				document.getElementById(applyTo[i]).style.backgroundImage = "url('/interface/js/x.gif')";
			}
		}
	}
}
/* jQuery scripts */
$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};
/* Product nav menu functions */
$.fn.showsubmenu = function() {
	$(this).find('ul:first').css({visibility: "visible",display: "none"}).show();
	$(this).find('a:first').queue(function () {
		$(this).addClass("selected");
		$(this).dequeue();
	});
};
$.fn.hidesubmenu = function() {
	$(this).find('ul:first').hide();
	$(this).find('a:first').queue(function () {
		$(this).removeClass("selected");
		$(this).dequeue();
	});
};
$.fn.getsubnav = function(navitem,menuid) {
	var itemid = $(navitem).attr('id');
	$(navitem).append('<span class="sublevel"><ul class="sublevel1"><li class="navItem">Loading menu...</li></ul></span>');
	$.ajax({
		type: "GET",
		url: '/cgi-bin/beads/ajax_processes/product_subnav.html',
		data: $(navitem).attr('data'),
		success: function(data) {
			$(navitem).find('ul.sublevel1').replaceWith(data);
		},
		complete: function() {
			$(" #"+menuid+" li.showsub").hidesubmenu();
			$(navitem).showsubmenu();
			// Display second sublevel on hover
			$(navitem).find('li.sublevelItem').hover(function(){
				$(this).showsubmenu();
			},function(){
				$(this).hidesubmenu();
			});
		}
	});
};
// mainmenu function for use with ajax-driven navigation
function mainmenu(menuid,showtime,hidetime){
	var stimer = null; // timer for showing menu
	var htimer = null; // timer for hiding menu
	
	$("#"+menuid+" ul").css({display: "none"}); // Opera Fix
	$(" #"+menuid+" li.navItem").hover(function(){
		$(" #"+menuid+" li.showsub").hidesubmenu();
	});
	$("#"+menuid+" li.showsub").hover(function(){
			var menuel = this;
			stimer = setTimeout(function() {
				//$('#devNotice').html('menu triggered for id '+menuid+' &gt; '+$(menuel).attr('id'));
				if($(menuel).find('span.sublevel').length == 0) {
					//$('#devNotice').append(' | submenu does not exist, building... ');
					$(menuel).getsubnav(menuel,menuid);
				} else {
					//$('#devNotice').append(' | submenu exists, displaying...');
					$(menuel).showsubmenu();
				}
			}, showtime);
		},
		function() {
			var thisid = $(this).attr('id');
			clearTimeout(stimer);
			htimer = setTimeout(function() {
				//$('#devNotice').html('hiding menu for '+thisid);
				$(" #"+menuid+" li.showsub").hidesubmenu(500,1);
			}, hidetime);
	});
}//mainmenu

/* function to display large images in product list */
$.fn.largeViewer = function(trigger) {
	$(this).css("cursor","url(/interface/cursors/magnify.cur), pointer");
	$(this).click(
		function() {
			var mycontainer = $(this).parent().attr('id');
			var myid = 'lg'+mycontainer;
			var lgimg = $(this).attr('src').replace('/photos/','/large_photos/');
			$('#'+myid).html('<img src="'+lgimg+'" width="320" height="240" border="0"><div class="lvClose">Click image to close Large View.</div>').fadeIn("normal");
			$('#'+myid+' img').css('cursor','pointer').click(function() {
				$('#'+myid).fadeOut("normal");
			});
		}
	);
}
/* function to create collapsable FAQ */
$.fn.faqList = function() {
	if($(this).attr('id') == 'sbContextFAQContent') {
		$(this).prepend('<p>Click a question to view its answer.</p>');
		$(this).find(".question").css('cursor','pointer').hover(
			function() {
				$(this).css('color','#FFCC00')
			},
			function() {
				$(this).css('color','#FFFF99')
			}
		).click(
			function() {
				var qid = $(this).attr('id');
				var aid = qid.replace('Q','A');
				var question = '<h3>'+$('#'+qid).html()+'</h3>';
				var answer = '<div>'+$('#'+aid).html()+'</div>';
				$("#dialog").html(question+answer).dialog({
					width: 500,
					height: 300,
					modal: true,
					draggable: false,
					resizable: false,
					title: 'Frequently Asked Questions',
					buttons: {
						Close: function() {
							$(this).dialog('close');
						}
					}
				}).dialog("open");
			}
		);
	} else {
		$(this).find(".answer").hide();
		$(this).prepend('<p>Click any question below to show/hide its answer.</p><div class="faqToggles"><a id="showAll">Show All Answers</a> | <a id="hideAll">Hide All Answers</a>');
		$("#showAll").click(function() {
			$(".answer").show();
		});
		$("#hideAll").click(function() {
			$(".answer").hide();
		});
		$(this).find(".question").css('cursor','pointer').hover(
			function() {
				$(this).css('color','#FFCC00')
			},
			function() {
				$(this).css('color','#FFFF99')
			}
		).toggle(
			function() {
				var qid = $(this).attr('id');
				var aid = qid.replace('Q','A');
				$("#"+aid).slideDown("normal");
			},
			function() {
				var qid = $(this).attr('id');
				var aid = qid.replace('Q','A');
				$("#"+aid).slideUp("normal");
			}
		);
	}
}
// Run Scripts
$(document).ready(function(){
	mainmenu('catalogNav',500,300);
	if($("#salesDisplay").length > 0) {
		$(".saleCarousel").jCarouselLite({btnNext: ".saleNext", btnPrev: ".salePrev", vertical: true, scroll: 2, auto: 8000, speed: 1000});
	}
	$(".faqList").faqList();
	$("img.largeView").largeViewer('hover');
	// Create a clean, easy-to-navigate presentation for the Library Contents and Project Contents
	$("#libraryContents .infobar").html('Click topic categories to expand. Click article headlines to read articles.'); // Add some instructional text to the Library Contents panel
	$("#projectContents .infobar").html('Click categories to expand. Click project titles to view projects.'); // Add some instructional text to the Projects Contents panel
	$("#libraryContents .accordion,#projectContents .accordion").accordion({header:'h4', autoHeight:false, clearStyle:true, collapsible: true, animated:false, active:false}); // End libraryContents.accordion
	/*** Scripts for Projects Section ***/
	$(".tabbedNav").tabs();
});