/* Flex Level Popup Menu v1.0
* Created: Dec 27th, 2009 by DynamicDrive.com. This notice must stay intact for usage 
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/
jQuery.noConflict()
var jquerypopupmenu={
	arrowpath: 'arrow.gif',
	popupmenuoffsets: [2, 0], 
	animspeed: 100, 
	showhidedelay: [100, 100], 
	startzindex:1000,
	builtpopupmenuids: [], 
	positionul:function($, $ul, e){
		var offset = $(e.currentTarget).offset();
		var istoplevel=$ul.hasClass('jqpopupmenu') 
		var docrightedge=$(document).scrollLeft()+$(window).width()-40 
		var docbottomedge=$(document).scrollTop()+$(window).height()-40
		if (istoplevel){ 
			var x=$(e.currentTarget).width()+offset.left+this.popupmenuoffsets[0];
			var y=offset.top;
if(window.location.pathname.substr(0,9) == "/products") {
	var asdfasdf = $(".mnu1").offset();
	x = asdfasdf.left - 41 ;
}
			x=(x+$ul.data('dimensions').w > docrightedge)? docrightedge-$ul.data('dimensions').w : x
			y=(y+$ul.data('dimensions').h > docbottomedge)? docbottomedge-$ul.data('dimensions').h : y
		}
		else{ 
			var $parentli=$ul.data('$parentliref')
			var parentlioffset=$parentli.offset()
			var x=$ul.data('dimensions').parentliw 
			var y=0
			x=(parentlioffset.left+x+$ul.data('dimensions').w > docrightedge)? x-$ul.data('dimensions').parentliw-$ul.data('dimensions').w : x 
			y=(parentlioffset.top+$ul.data('dimensions').h > docbottomedge)? y-$ul.data('dimensions').h+$ul.data('dimensions').parentlih : y
		}
		$ul.css({left:x, top:y})
	},
	
	showbox:function($, $popupmenu, e){
		clearTimeout($popupmenu.data('timers').hidetimer)
		$popupmenu.data('timers').showtimer=setTimeout(function(){$popupmenu.show(jquerypopupmenu.animspeed)}, this.showhidedelay[0])
	},
	hidebox:function($, $popupmenu, $target){
		clearTimeout($popupmenu.data('timers').showtimer)
		$popupmenu.data('timers').hidetimer=setTimeout(function(){$(this).unroll($target);$popupmenu.hide(100)}, this.showhidedelay[1]) //hide popup menu plus all of its sub ULs
	},
	buildpopupmenu:function($, $menu, $target){
		$menu.css({display:'block', visibility:'hidden', zIndex:this.startzindex}).addClass('jqpopupmenu').appendTo(document.body)
		$menu.bind('mouseenter', function(){
			clearTimeout($menu.data('timers').hidetimer)
		})		
		$menu.bind('mouseleave', function(){ //hide menu when mouse moves out of it
			jquerypopupmenu.hidebox($, $menu);
			$(this).unroll($target);
		})
		$menu.data('dimensions', {w:$menu.outerWidth(), h:$menu.outerHeight()}) //remember main menu's dimensions
		$menu.data('timers', {})
		var $lis=$menu.find("ul").parent() //find all LIs within menu with a sub UL
		$lis.each(function(i){
			var $li=$(this).css({zIndex: 1000+i})
			var $subul=$li.find('ul:eq(0)').css({display:'block'}) //set sub UL to "block" so we can get dimensions
			$subul.data('dimensions', {w:$subul.outerWidth(), h:$subul.outerHeight(), parentliw:this.offsetWidth, parentlih:this.offsetHeight})
			$subul.data('$parentliref', $li) //cache parent LI of each sub UL
			$subul.data('timers', {})
			$li.data('$subulref', $subul) //cache sub UL of each parent LI
			$li.children("a:eq(0)").append( //add arrow images
				'<img src="'+jquerypopupmenu.arrowpath+'" class="rightarrowclass" style="border:0;" />'
			)
			$li.bind('mouseenter', function(e){ //show sub UL when mouse moves over parent LI
				var $targetul=$(this).css('zIndex', ++jquerypopupmenu.startzindex).addClass("selected").data('$subulref')
				if ($targetul.queue().length<=1){ //if 1 or less queued animations
					clearTimeout($targetul.data('timers').hidetimer)
					$targetul.data('timers').showtimer=setTimeout(function(){
						jquerypopupmenu.positionul($, $targetul, e)
						$targetul.show(jquerypopupmenu.animspeed)
					}, jquerypopupmenu.showhidedelay[0])
				}
			})
			$li.bind('mouseleave', function(e){ //hide sub UL when mouse moves out of parent LI
				var $targetul=$(this).data('$subulref')
				clearTimeout($targetul.data('timers').showtimer)
				$targetul.data('timers').hidetimer=setTimeout(function(){$targetul.hide(100).data('$parentliref').removeClass('selected')}, jquerypopupmenu.showhidedelay[1])
			})
		})
		$menu.find('ul').andSelf().css({display:'none', visibility:'visible'}) //collapse all ULs again
		$menu.data('$targetref', $target)
		this.builtpopupmenuids.push($menu.get(0).id) //remember id of popup menu that was just built
	},	
	init:function($, $target, $popupmenu){
		if (this.builtpopupmenuids.length==0){ //only bind click event to document once
			$(document).bind("click", function(e){
				if (e.button==0){ //hide all popup menus (and their sub ULs) when left mouse button is clicked
					$('.jqpopupmenu').find('ul').andSelf().hide()
				}
			})
		}
		if (jQuery.inArray($popupmenu.get(0).id, this.builtpopupmenuids)==-1) //if this popup menu hasn't been built yet
			this.buildpopupmenu($, $popupmenu, $target)
		if ($target.parents().filter('ul.jqpopupmenu').length>0) //if $target matches an element within the popup menu markup, don't bind onpopupmenu to that element
			return
		$target.bind("mouseenter", function(e){
			$popupmenu.css('zIndex', ++jquerypopupmenu.startzindex)
			jquerypopupmenu.positionul($, $popupmenu, e)
			jquerypopupmenu.showbox($, $popupmenu, e)
		})
		$target.bind("mouseleave", function(e){
			jquerypopupmenu.hidebox($, $popupmenu, $target)
		})
	}
}
jQuery.fn.addpopupmenu=function(popupmenuid){
	var $=jQuery
	return this.each(function(){ //return jQuery obj	
		var $target=$(this)
		if($('#'+popupmenuid+' > li').length == 0) {
			$(this).bind("mouseleave", function(e){
				$(this).unroll($target)
			})
		}else{
			jquerypopupmenu.init($, $target, $('#'+popupmenuid))
		}
	})
};
jQuery.fn.unroll=function(obj) {
	var $=jQuery
	var old_pos = $(obj).css('background-position');
	if(old_pos == 'undefined' || old_pos == null) {
		var new_pos = "0px " + $(obj).css("background-position-y");
	}else{
		var new_pos = old_pos.replace("-210px ", "0px ");
	}
	$(obj).css('background-position', new_pos);;
};
jQuery(document).ready(function($){
	$("#PurchaseOnline > a").click(function(e) {
		if($(this).hasClass("hault"))
			e.preventDefault();
		$("#PO-Sub, #PO-SubRfq").fadeOut();
		var jPop = $("#" + $(this).attr('rel'));
		var off = $(this).offset();

		var coords = [Math.round($(this).outerWidth() + off.left), Math.round(off.top - 87 + Math.round($(this).outerHeight()/2))];
		jPop.css({left:coords[0]+'px', top:coords[1]+'px'}).toggle();
	});
	$("#PO-Sub, #PO-SubRfq").mouseleave(function() {
		$(this).fadeOut();
	});
	$("div.subpop").each(function() {
		$(this).parent().hide();
	});
	$("a.moreinfo").click(function(e) {
		e.preventDefault();
		$(this).parent().next().toggle();
	});

	$("#ProdTabs img").filter(":not(:last)").click(function() {			
		if($(this).className != "active") {
			//reset previously active
			$("#ProdTabs img.active").attr('src', $("#ProdTabs img.active").attr('src').replace("_over.jpg", ".jpg")).removeClass("active");
			$(this).attr('src', $(this).attr('src').replace(".jpg", "_over.jpg")).addClass("active");
			$(".ContProd").css('height', $(".ContProd").height() + 'px');
			$(".ContProd > div").hide();
			$("#"+$(this).attr('rel')).fadeIn();
			$(".ContProd").css('height', '');
		}
	});


/*  data-popupmenu attribute gives error on w3 verification... switching to title="popmenu*"
	var $anchors=$('*[data-popupmenu]')
	$anchors.each(function(){
		$(this).addpopupmenu(this.getAttribute('data-popupmenu'))
	})
*/
	var $anchors=$('[title^=popmenu]')
	$anchors.each(function(){
		$(this).addpopupmenu(this.getAttribute('title'))
	})
	$('#horzmnu li img').each(function() { var preload = new Image(); preload.src = $(this).attr('src').replace('\.png', '_on.png' );  });
		$("#mainmnu ul li").hover(
			function() {
				var old_pos = $(this).css('background-position');
				if(old_pos == 'undefined' || old_pos == null) {
					var new_pos = "-210px " + $(this).css("background-position-y");
				}else{
					var new_pos = old_pos.replace("0px ", "-210px ");
				}
				$(this).css('background-position', new_pos);
			}, function() {
				if($(this).attr('class').substring(0,4) == 'mnux')
					jQuery.fn.unroll($(this));			
			});
        $('#horzmnu ul li img').hover(
            function() {	this.src = this.src.replace('\.png', '_on.png' );	},
            function() {	this.src = this.src.replace('_on\.png', '.png' );   }
        );
		$("#logohover").bind("click", function(){ window.location = "index.html" });
	try{
		$.ajax({
				url: "getBackground.php",	
				data: "url="+window.location.pathname,
				success: function(msg){
					if(msg != "") {
						$('#body').css('background-image', "url('"+msg+"')");  
					}else{
						$.ajax({
							url: "getBackground.php",	
							data: "url="+window.location.pathname,
							success: function(msg){ $('#body').css('background-image', "url('"+msg+"')") }
						 });
					}
				}
			 });
	  }
	catch(e){
						$.ajax({
							url: "getBackground.php",	
							data: "url="+window.location.pathname,
							success: function(msg){ $('#body').css('background-image', "url('"+msg+"')") }
						 }); 
	}
	
})
function ddlistmenu(id, className){
	var menu=document.createElement('ul')
	if (id)
		menu.id=id
	if (className)
		menu.className=className
	this.menu=menu
}
ddlistmenu.prototype={
	addItem:function(url, text, target){
		var li=document.createElement('li')
		li.innerHTML='<a href="'+url+'" target="'+target+'">'+text+'</a>'
		this.menu.appendChild(li)
		this.li=li
		return this
	},
	addSubMenu:function(){
		var s=new ddlistmenu(null, null)
		this.li.appendChild(s.menu)
		return s
	}
}
