/**
 * Picometrix Jquery Object
 *
 * @author  Ryan Sexton (ryan@floatingbytes.com)
 *
 * @date    April, 2008
 * @version 1.0
 *
 *       
 */

(function($)
{
	/**
	 * SwapIt
	 *
	 * Swapit is a quick and easy plugin designed to make 
	 * simple mouseover image swapping easy.
	 *
	 * @example     $('.classname').swapit('_over');
	 */
	$.fn.swapit = function(a) {
		return this.each(function() {
			if($(this).get(0).tagName.toUpperCase() == 'IMG') {
				var original = $(this).attr("src");
				if(original.indexOf('_over') == -1) {
					// get the new src string
					var over = original.split('.');
					if(over.length >= 2) {
						// add before the extension if we have one
						over[over.length-2] += a;
						over = over.join('.');
					} else {
						over = original + a;
					}
					$(this).mouseover(function() { $(this).attr('src', over); }).mouseout(function() { $(this).attr('src', original); });
				}
			}
		});
	}

	$.preloadimages = function()
	{	for(var i = 0; i<arguments.length; i++)
		{	img = new Image();
			img.src = arguments[i];
		}
	}

    $.reload = function () {
        window.location.reload(true);
    }
    
    $.ucwords = function (arg) 
    {
        if (arg == undefined || arg == null) {
            return arg;
        }
        arg = arg.replace('_', ' ');
        var newstring = '' 
        $.each(arg.split(' '), function(i, n) 
            {   newstring += n.substr(0, 1).toUpperCase()+n.substr(1)+' '; });
        
        return $.trim(newstring);
    }
    
	
    
})(jQuery);



$(document).ready(function(){

    $.preloadimages('images/lo/piconav/about_over.gif',
                    'images/lo/piconav/core_competencies_over.gif', 
                    'images/lo/piconav/proucts_over.gif', 
                    'images/lo/piconav/news_over.gif', 
                    'images/lo/piconav/support_over.gif',
					'images/lo/piconav/search_over.gif',
					'images/lo/piconav/contact_over.gif'
                    );

    $('.imgswap').swapit('_over');
	
	if ($('#dNavMultiple').length) {
	
		$('#dNavMultiple').hide();
		var _top = null;
		var _selected = null;
		var _found = false;
		var _issub = false;
		
		var _top_link = null;
		
		var _local = document.location.pathname;
		
		if (_local.indexOf('?') >= 0) {
			_local = _local.substring(0, _local.indexOf('?'));
		}
		
		_local = _local.split('/');
		_local = _local.pop();
		
		if (_local.length == 0) {
			_local = 'index.asp';	
		}
		
		/**
		 * 	Walk through all li's and figure out if any match
		 *	the current page we're looking at. This way we
		 *	can mark it with the Selected class. We also use this
		 *	loop to figure out if the Selected class was used
		 *	which top level was it under. Also we are hiding
		 *	all children, to be shown if an item is Selected
		 */
		
		var _f_int = 1;
		var _last_sub = 0;
		var _last_top = 0;
		$('#dNavMultiple ul li').each(function() { 
			
			$(this).attr('zd', _f_int);
			$(this).attr('zp', 0);
			
			if ($(this).hasClass('arGreen') || $(this).hasClass('arPurple') || $(this).hasClass('arBlue')) {
				$(this).addClass('Toplevel');
				_last_top = _f_int;
			}
			
			if ($(this).hasClass('Subnav') || $(this).hasClass('Subhead')) {
				_last_sub = _f_int;
				$(this).attr('zp', _last_top);
			}
			
			if ($(this).hasClass('Subsub')) {
				$(this).attr('zp', _last_sub);
			}
			
			_f_int++;
		});
		
		var _int_position = 1;
		var _int_parent = 0;
		var _int_parent_top = 0;
		var _found_parent = 0;
		
		$('#dNavMultiple ul li').each(function() { 
			
			var _href = '';
			
			if ($(this).hasClass('Subsub') || $(this).hasClass('Subnav') || $(this).hasClass('Subhead')) {
				
				$(this).hide();
				
				if ($(this).hasClass('Subhead') == false) {
					
					_href = $(this).children('a').eq(0).attr('href');
					if (_href.indexOf('?') >= 0) {
						_href = _href.substring(0, _href.indexOf('?'));
					}
					
					_href = _href.split('/');
					_href = _href.pop();
					
					if (_href == _local) {
						_found_parent = _int_parent;
						_found = true;	
						$(this).addClass('Selected');
						if ($(this).hasClass('Subsub')) {
							_issub = true;
						}
					}
				}
			
			} else {
				
				_href = $(this).children('a').eq(0).attr('href');
				if (_href.indexOf('?') >= 0) {
					_href = _href.substring(0, _href.indexOf('?'));
				}
				
				_href = _href.split('/');
				_href = _href.pop();
				
				if (_href == _local) {
					$(this).addClass('TSelected');
					_found_parent = _int_parent;
					_found = true;
				}
			}
			
			_int_position++;
			
		});
		
		/**
		 *	Using the above found information we locate the top level
		 *	and show the children for that top level.
		 */
		
		if (_found == true) {
			var _children = false;
			var _gotit = false;
			var _toplevelID = 0;
			var _toplevelfound = false;
			var selectedID = 0;
			
			if ($('#dNavMultiple ul li.Selected').length) {
				selectedID = $('#dNavMultiple ul li.Selected').eq(0).attr('zd');
			} else {
				if ($('#dNavMultiple ul li.TSelected').length) {
					selectedID = $('#dNavMultiple ul li.TSelected').eq(0).attr('zd');
				}
			}
			
			if (_issub && selectedID > 0) {
				$('#dNavMultiple ul li').each(function() {
					if (_toplevelfound == false) {
						if ($(this).hasClass('Toplevel')) {
							_toplevelID = $(this).attr('zd');
						}
						
						if ($(this).attr('zd') == selectedID) {
							_toplevelfound = true;
						}
					}
				});
			}
			
			if ($("#dNavMultiple ul li[zd='"+selectedID+"']").length) {
				var _t = $("#dNavMultiple ul li[zd='"+selectedID+"']");
				if ($(_t).hasClass('Subsub')) {
					var _pid = $(_t).attr('zp');
					$("#dNavMultiple ul li[zp='"+_pid+"']").each(function() {
						$(this).show();
					});
					$("#dNavMultiple ul li[zp='"+_toplevelID+"']").each(function() {
						$(this).show();
					});
				} else {
					
					if ($(_t).hasClass('Subnav')) {
						var _pid = $(_t).attr('zp');
						$("#dNavMultiple ul li[zp='"+_pid+"']").each(function() {
							$(this).show();
						});
					}
				}
			}
			
			$('#dNavMultiple ul li').each(function() {
				
				_gotit = false;
				if ($(this).attr('zp') == _found_parent || $(this).attr('zd') == _found_parent) {
					_gotit = true;
				}
				if  (selectedID == _found_parent || $(this).attr('zp') == selectedID) {
					$(this).show();
				}
				if (_gotit) {
					$(this).show();
				}
			});
		}
		$('#dNavMultiple').show();
	}
    
});