﻿// JavaScript Document
var $topDropMenu = jQuery.noConflict();
var $im_menuRegularAnimationSpeed = 300;
var $im_menuProductAnimationSpeed = 300;
var $im_menuImageLoad = false;

$topDropMenu(document).ready(topDropMenu_READY);

function topDropMenu_READY(){	
	topDropMenu_READY_MAN_DOM();
	topDropMenu_READY_BIND_EVENTS();
	
	$topDropMenu('.help-me-choose-popup > a').fancybox({ 'onComplete' : ReRoundCorners, 'autoDimensions' : false, 'width': 950, 'height' : 406 });
	$topDropMenu('.help-me-choose-popup').fancybox({ 'onComplete' : ReRoundCorners, 'autoDimensions' : false, 'width': 950, 'height' : 406 });
	
	function ReRoundCorners(){
		gem_round_solid_plus_right_button_corners();
		gem_round_arrow_right_button_corners();
	}

}

//void topDropMenu_READY_MAN_DOM()
//This function will go through each menu item and submenu item
//and set each's width.  It will also add HTML to the submenu to render
//the cutout.
function topDropMenu_READY_MAN_DOM(){	
	$topDropMenu( '#top-drop-menu > ul > li' ).each( function(){
		
		$menu_width = $topDropMenu( this ).width();

		$topDropMenu( ' > div', this ).addClass( 'ie7-load-fix' ); // IE 7 Fix
		$submenu_width = $topDropMenu( ' > div', this ).width();		
		$topDropMenu( ' > div', this ).removeClass( 'ie7-load-fix' ); // IE 7 Fix
		
		if( !topDropMenu_browser_is_old_internet_explorer() ) $topDropMenu( ' > div', this ).corner( 'bottom round 8px' );

		if( $menu_width > $submenu_width ){
			$submenu_width = $menu_width + 1;
			$same_width = true;
		}else{
			$same_width = false;
		}
		
		if( $topDropMenu( this ).is( ':last-child' ) ){
			$topDropMenu( this ).addClass( 'last' );
			if( !$same_width ){
				$topDropMenu( ' > div', this ).prepend( "<div class='top'><div class='left round'></div><div class='middle'></div><div class='right no-round'></div></div>" );
				if( !topDropMenu_browser_is_old_internet_explorer() ) $topDropMenu( ' > div', this ).corner( 'tl round 8px' );
			}
		}else if( !$same_width && !$topDropMenu( this ).hasClass( 'product-menu' )){
			$topDropMenu( ' > div', this ).prepend( "<div class='top'><div class='left no-round'></div><div class='middle'></div><div class='right round'></div></div>" );
			if( !topDropMenu_browser_is_old_internet_explorer() ) $topDropMenu( ' > div', this ).corner( 'bottom round 8px' );
		}else if( !$same_width && $topDropMenu( this ).hasClass( 'product-menu' )){
			$topDropMenu( ' > div', this ).prepend( "<div class='top'><div class='left round'></div><div class='middle'></div><div class='right round'></div></div>" );
			if( !topDropMenu_browser_is_old_internet_explorer() ) $topDropMenu( ' > div', this ).corner( 'bottom round 8px' );
		}else{
			$topDropMenu( ' > div', this ).prepend( "<div class='top'><div class='left no-round'></div><div class='middle'></div><div class='right no-round'></div></div>" );
		}
		
		
		$topDropMenu( ' > div', this ).css( 'width', $submenu_width + 'px' ); 
		$topDropMenu( this ).css( 'width', $menu_width + 'px' ); 
		
		$topDropMenu( ' > div', this ).append( "<div class='bottom'><div class='round left'></div><div class='middle'></div><div class='round right'></div></div>" );
		
		if( $topDropMenu( ' > div > ul > li', this ).size() == 0 && !$topDropMenu( this ).hasClass( 'product-menu' ) )
			$topDropMenu( this ).prepend( "<span class='bottom'><span class='left'></span><span class='right'></span></span>" );
			
		$topDropMenu( this ).prepend( "<span class='top'><span class='left'></span><span class='right'></span></span>" );

		$topDropMenu( ' > .bottom', this ).width( $menu_width );
		$topDropMenu( ' > .top', this ).width( $menu_width );
		$topDropMenu( ' > div > .bottom > .middle', this ).css( 'width', ( $submenu_width - 16 ) + 'px' ); 
		$topDropMenu( ' > div > .top > .middle', this ).css( 'width', ( $menu_width - 8 ) + 'px' ); 
		
		if( $topDropMenu( this ).hasClass( 'product-menu' ) ){
			$product_offset = $topDropMenu( this ).offset();
			$parent_offset = $topDropMenu( this ).parent().parent().offset();
			$topDropMenu( ' > div', this ).css( 'left', '-' + ( $product_offset.left - $parent_offset.left ) + 'px' );
		}
	});
	
	var $passenger_block = $topDropMenu( '#top-drop-menu > ul > .product-menu > div .passenger-vehicles' );
	var $utility_block = $topDropMenu( '#top-drop-menu > ul > .product-menu > div .utility-vehicles' );
	$topDropMenu( '#top-drop-menu > ul > .product-menu' ).addClass( 'hover' );
	$topDropMenu( '#top-drop-menu > ul > .product-menu > div' ).show();
	
	if( $utility_block.height() > $passenger_block.height() ){
		$passenger_block.height( $utility_block.height() ); 
		$utility_block.height( $utility_block.height() ); 
	}else{
		$passenger_block.height( $passenger_block.height() ); 
		$utility_block.height( $passenger_block.height() );	
	}
	$topDropMenu( '#top-drop-menu > ul > .product-menu > div' ).hide();
	$topDropMenu( '#top-drop-menu > ul > .product-menu' ).removeClass( 'hover' );
	
}

//VOID topDropMenu_READY_BIND_EVENTS()
//Connects the hover events to the indian menu
function topDropMenu_READY_BIND_EVENTS(){
	var config = {    
		 over: topDropMenu_regular_menu_hover_in, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: topDropMenu_regular_menu_hover_out, // function = onMouseOut callback (REQUIRED)    
		 sensitivity: 10,
		 interval: 100 // time before hover  // old - 150
	};
	
	$topDropMenu( '#top-drop-menu > ul > li' ).hoverIntent( config );
	$topDropMenu( '#top-drop-menu > ul > li' ).eq(0).trigger( 'click' );
	$topDropMenu( '#top-drop-menu > ul > li > div > ul > li > .help-me-choose-popup' ).click( function(){ 
		$topDropMenu( '#top-drop-menu > ul > li.hover > div ' ).hide().parent().removeClass( 'hover' ).trigger( 'mouseleave' );
	});
}

//VOID topDropMenu_regular_menu_hover_in()
//This function will slide the menu down.
function topDropMenu_regular_menu_hover_in(){
	$this = $topDropMenu(this);
	
	if( $this.hasClass( 'product-menu' ) ){
		$animationSpeed = $im_menuProductAnimationSpeed;
		if( !$im_menuImageLoad ){
			$topDropMenu( ' > div > div > ul > li > a > span.image > img', $this ).each( function(){ 
				$topDropMenu(this).attr( 'src', $topDropMenu(this).attr( 'title' ) );
				$topDropMenu(this).attr( 'title', $topDropMenu(this).attr( 'alt' ) );
			});
			$im_menuImageLoad = true;
		}
	}
	else
		$animationSpeed = $im_menuRegularAnimationSpeed;
	
	if( !$this.hasClass( 'hover' ) ){
		
		$this.addClass( 'hover' );	
		
		if( $topDropMenu( ' > div > ul > li', $this ).size() > 0 || $topDropMenu( $this ).hasClass( 'product-menu' ) )
			$topDropMenu( ' > div', $this ).slideDown( $animationSpeed );
		else
			$topDropMenu( ' > a', $this ).height( 30 );	
	}		
	else if( $this.hasClass( 'hover' ) && $topDropMenu( this ).hasClass( 'transition' ) ){
			
		$this.removeClass( 'transition' );	
		$topDropMenu( '> div', $this ).clearQueue().stop(true).css( {'height' : 'auto'} ).slideDown( $animationSpeed );
		
	}
}

//VOID topDropMenu_regular_menu_hover_in()
//This function will slide the menu up.
function topDropMenu_regular_menu_hover_out(){		
	$this = $topDropMenu(this);
	
	if( $this.hasClass( 'products-menu' ) )
		$animationSpeed = $im_menuProductAnimationSpeed;
	else
		$animationSpeed = $im_menuRegularAnimationSpeed;
			
	var $item_size = $topDropMenu( ' > div > ul > li', $this ).size();
	if( $this.hasClass( 'hover' ) && !$this.hasClass( 'transition' ) && ( $item_size > 0 || $topDropMenu( $this ).hasClass( 'product-menu' ) ) ){
		
		$this.addClass( 'transition' );			
		$topDropMenu( '> div', $this ).slideUp( $animationSpeed, function(){			
			$topDropMenu( $topDropMenu(this).parent() ).removeClass( 'transition' ).removeClass( 'hover' );	
		});	
	}	
	else if( $item_size == 0 ){	
		$this.removeClass( 'hover' );	
	}
}

function topDropMenu_browser_is_old_internet_explorer(){
	var $isOldInternetExplorerBrowser = false;
	var $newInternetExplorerVersion = 9;
	
	if( $topDropMenu.browser.msie ){
  		$internetExplorerVersion = $topDropMenu.browser.version;
		if( $internetExplorerVersion < $newInternetExplorerVersion )
			$isOldInternetExplorerBrowser = true;
	}
	
	return $isOldInternetExplorerBrowser;
}
