// JavaScript Document
var $vh = jQuery.noConflict();
var $searchInputName = '#vh_search > input[type=text]';
var $searchDefaultContent = 'Search';
var $searchAnimationSpeed = 300;

//do not change these values
var animationSpeed = 200;

//do not put a '/' at the end of the image path!
var imagePath = "img";

//page is ready
$vh(document).ready(vh_READY);

function vh_READY(){
	vh_manip_dom();
	vh_set_menu_width();
	vh_search();
}

function vh_set_menu_width(){
	$vh('#vh_top_nav_1 > li, #vh_model_nav_1 > li' ).children('ul').each( function(){
		$vh(this).css('visibility', 'hidden').css('display', 'block');
		if(  $vh(this).parent().children('a').outerWidth() <= $vh(this).outerWidth() ){
			$vh(this).parent().children('a').css( 'width', $vh(this).parent().children('a').width() );
			$vh( ' > li > a', this).css('width', $vh(this).outerWidth() -  parseInt( $vh( ' > li > a', this).css('padding-left' ) ) -  parseInt( $vh( ' > li > a', this).css('padding-right' ) ) );
			$paddingTotal = $vh(this).outerWidth() - $vh(this).parent().children('a').width();
			
			if( $vh(this).parent().index() == $vh(this).parent().parent().children('li').size() - 1 ){
				$vh(this).parent().children('a').css('padding-right', $paddingTotal - parseInt( $vh(this).parent().children('a').css('padding-left') ) );	
			}else{
				$vh(this).parent().children('a').css('padding-left', Math.floor( $paddingTotal / 2 ) );
				$vh(this).parent().children('a').css('padding-right', Math.ceil( $paddingTotal / 2 ) );	
			}		
		}else{
			$vh(this).parent().children('a').css( 'width', $vh(this).parent().children('a').width() );
			$vh( ' > li > a', this).css( 'width', $vh(this).parent().children('a').outerWidth() - parseInt( $vh( ' > li > a', this).css('padding-left') ) - parseInt( $vh( ' > li > a', this).css('padding-right') ) - parseInt( $vh( ' > li > a', this).css('margin-left') ) - parseInt( $vh( ' > li > a', this).css('margin-right') )  );
		}
		$vh(this).hide().css('visibility', 'visible');
	});
}

function vh_search(){
	$searchDefaultContent = $vh( $searchInputName  ).attr('title');	
	$vh( $searchInputName ).focus( vh_SEARCH_FOCUS ).focusout( vh_SEARCH_FOCUS_OUT );	
	
	function vh_SEARCH_FOCUS(e)
	{
		e.stopImmediatePropagation();
		e.preventDefault();
		
		var $alternateSearchText = $vh('#alternateSearchText').val();
		
		var obj = $vh(this);
		if( obj.val() == $searchDefaultContent || obj.val() == $alternateSearchText  )
		{
			obj.fadeOut($searchAnimationSpeed , function(){ 
				obj.addClass('focused');
				obj.val('') 
			});
			obj.fadeIn($searchAnimationSpeed , function(){ 
				if( obj.val().length == 0 )
					obj.select(); 
			});
		}
	}
	
	function vh_SEARCH_FOCUS_OUT(e) 
	{
		e.stopImmediatePropagation();
		e.preventDefault();	
		
		var obj = $vh(this);
		if( $vh.trim( obj.val() ).length == 0 ){
			obj.fadeOut(0, function(){ 
				obj.val( $searchDefaultContent );
				obj.removeClass('focused');
			});
		}
		obj.fadeIn($searchAnimationSpeed  * 1.5);
	}
	
	$vh( $searchInputName ).trigger('focusout');
}

function vh_manip_dom(){
	$vh('#vh_model_nav_1 > li').hover(vh_model_nav_1_li_MOUSE_OVER, vh_model_nav_1_li_MOUSE_OUT);
	$vh('#vh_top_nav_1 > li').hover(vh_top_nav_1_li_MOUSE_OVER, vh_top_nav_1_li_MOUSE_OUT);
	
	$vh('#vh_model_nav_1 > li > .vh_models > ul > li > a').hover(vh_models_ul_li_a_MOUSE_OVER, vh_models_ul_li_a_MOUSE_OUT);
	
	//code to force top menu children to be at least the width of their correct parent, minus included padding
	$vh('#vh > #vh_top_nav_1 > li > .vh_top_nav_2 > li > a').each(function(index){
		var myParentWidth = $vh(this).parent().parent().parent().width() - 48;
    	$vh(this).css({minWidth: myParentWidth});
  	});
	
	//code to force main menu children to be at least the width of their correct parent, minus included padding
	$vh('#vh > #vh_model_nav_1 > li > .vh_model_nav_2 > li > a').each(function(index){
		var myParentWidth = $vh(this).parent().parent().parent().width() - 42;
    	$vh(this).css({minWidth: myParentWidth});
  	});

/* Added 2011-06-28 */  	
/*
	$vh('#vh_model_nav_1 > li').each(function(index) {
   		if( $vh(' > .vh_models > ul > li', this).size() == 5 ){
			$vh(' > .vh_models > ul > li:last-child', this).addClass( 'fiveItems' );
			$vh(' > .vh_models > ul > li:last-child > a.vh_models_ul_li_a_alt', this).removeClass( 'vh_models_ul_li_a_alt' );
		}
  	});
  */	
  	
/* Added 2011-08-12 */  	
	$vh('#vh_model_nav_1 > li').each(function(index) {
		$size = $vh(' > .vh_models > ul > li', this).size()
		if( ( $size % 4 ) != 0 ){
			$vh(' > .vh_models > ul > li:last-child', this).addClass( 'pos-right' );
			$vh(' > .vh_models > ul > li:last-child > a.vh_models_ul_li_a_alt', this).removeClass( 'vh_models_ul_li_a_alt' );
		}
	});
	

}

function vh_menu_check_open(e){
	$temp = $vh( e.target );
	//console.log( e.target.tagName + " : " + $temp.attr('class') );
	if( $temp.parents('ul#vh_model_nav_1').length != 1 ){
		e.preventDefault();
		e.stopPropagation();
		/* first draft */ 
		/*
		$vh('#vh_model_nav_1 > li').each(function(){
			if( !$vh(' > div', this).hasClass('fadeOut') ){
				$vh(' > a', this).removeClass('vh_model_nav_1_li_MOUSE_OVER');
				$vh(' > div', this).stop(true, true).fadeOut(animationSpeed);
			}
		});
		*/
			
		/* second draft */
		$vh('#vh_model_nav_1 > li').each(function(){
			if( !$vh(' > div', this).hasClass('fadeOut') ){
				$vh(this).trigger('mouseleave');
			}
		});

		/* third draft */
		/*
		$vh('#vh_model_nav_1 > li > a').removeClass('vh_model_nav_1_li_MOUSE_OVER');
		$vh('#vh_model_nav_1 > li > div:not(fadeOut):visible').stop(true, true).fadeOut(animationSpeed);
		*/
	}
}

function vh_models_ul_li_a_MOUSE_OVER(e){
	//e.preventDefault();
	//e.stopPropagation();
	$vh(this).addClass('vh_models_ul_li_a_MOUSE_OVER');
	$vh('> .vh_model_name', this).addClass('vh_models_ul_li_a_div_MOUSE_OVER');
}

function vh_models_ul_li_a_MOUSE_OUT(e){
	//e.preventDefault();
	//e.stopPropagation();
	$vh(this).removeClass('vh_models_ul_li_a_MOUSE_OVER');
	$vh('> .vh_model_name', this).removeClass('vh_models_ul_li_a_div_MOUSE_OVER');
}

function vh_model_nav_1_li_MOUSE_OVER(e){
	e.preventDefault();
	e.stopPropagation();
	
	$vh('*').live('mousemove', vh_menu_check_open );
	
	var oldThis = this;
	var mySelector = '> .vh_models > ul > li > a > img';
	$vh(mySelector, oldThis).each(function(index){
		var myTitle = $vh(this).attr('title');
    	$vh(this).attr('src', myTitle);
  	});
  	
	$vh('> a', this).addClass('vh_model_nav_1_li_MOUSE_OVER');
	$vh('> div', this).css( 'z-index', '500' );
	$vh('> div', this).stop(true, true).fadeIn(animationSpeed, function(){
		$vh(this).css( 'z-index', '400' );
	});
	$vh('> ul', this).show();
}

function vh_model_nav_1_li_MOUSE_OUT(e){
	e.preventDefault();
	e.stopPropagation();
	
	$vh('*').die( 'mousemove' );
	
	$vh('> div', this).addClass('fadeOut').css( 'z-index', '400' );
	$vh('> a', this).removeClass('vh_model_nav_1_li_MOUSE_OVER');
	$vh('> div', this).stop(true, true).fadeOut(animationSpeed, function(){
		$vh(this).removeClass('fadeOut').css( 'z-index', '500' );
	});
	$vh('> ul', this).hide();
}

function vh_top_nav_1_li_MOUSE_OVER(e){
	e.preventDefault();
	e.stopPropagation();
	$vh('> a', this).addClass('vh_top_nav_li_MOUSE_OVER');
	$vh('> ul', this).show();
}

function vh_top_nav_1_li_MOUSE_OUT(e){
	e.preventDefault();
	e.stopPropagation();
	$vh('> a', this).removeClass('vh_top_nav_li_MOUSE_OVER');
	$vh('> ul', this).hide();
}
