// JavaScript Document
var $indianMenu = jQuery.noConflict();
var $im_menuRegularAnimationSpeed = 300;
var $im_menuProductAnimationSpeed = 300;
var $im_menuImageLoad = false;

$indianMenu(document).ready(indianMenu_READY);

function indianMenu_READY(){	
	indianMenu_READY_MAN_DOM();
	indianMenu_READY_BIND_EVENTS();
	indianMenu_extend_footer();
}

//void indianMenu_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 indianMenu_READY_MAN_DOM(){	
	$indianMenu( '#indian-menu-bottom-menu > .regular' ).each( function(){
		$menu_width = $indianMenu( this ).width();

		$indianMenu( ' > div', this ).addClass( 'ie7-load-fix' ); // IE 7 Fix
		$submenu_width = $indianMenu( ' > div', this ).width();
		$indianMenu( ' > div', this ).removeClass( 'ie7-load-fix' ); // IE 7 Fix
		
		if( $menu_width > $submenu_width ){
			$submenu_width = $menu_width;
			$same_width = true;
		}else
			$same_width = false;

		if( $indianMenu( this ).is( ':last-child' ) )
			$indianMenu( this ).addClass( 'last' );
				
		$indianMenu( ' > div', this ).css( 'width', $submenu_width + 'px' ); 
		$indianMenu( this ).css( 'width', $menu_width + 'px' ); 
		
		$indianMenu( ' > div', this ).prepend( indianMenu_CREATE_CUTOUT( $same_width, $indianMenu( this ).hasClass( 'last' ), false ) );
		$indianMenu( ' > div > .cutout > .middle', this ).width( $menu_width - ( $same_width ? 15 : 7 ) );
	});
	
	$menu_width = $indianMenu( '#indian-menu-bottom-menu > .products > a' ).outerWidth();
	$left_width = $indianMenu( '#indian-menu-bottom-menu' ).parent().width() - $indianMenu( '#indian-menu-bottom-menu' ).width();	
	$indianMenu( '#indian-menu-bottom-menu > .products > div' ).prepend( indianMenu_CREATE_CUTOUT( false, false, true ) );
	$indianMenu( '#indian-menu-bottom-menu > .products > div > .cutout' ).css( 'left', ( $left_width - 4 ) );
	$indianMenu( '#indian-menu-bottom-menu > .products > div > .cutout > .middle' ).width( ( $menu_width - 4 )  );
}

//HTML indianMenu_CREATE_CUTOUT( $menu_submenu_same_width, $last_item, $products )
//This function will create the cutout html.  The cutout is used at the top of the 
//submenu directly beneath the menu item. 
function indianMenu_CREATE_CUTOUT( $menu_submenu_same_width, $last_item, $products ){
	
	if( ( !$menu_submenu_same_width && $last_item ) || $products )
		$left_orientation = "top-left";
	else
		$left_orientation = "bottom-left";
	
	if( ( !$menu_submenu_same_width && !$last_item ) || $products )
		$right_orientation = "top-right";
	else
		$right_orientation = "bottom-right";
	
	$cutout = "<div class='cutout'>";
	$cutout = $cutout + "<div class='left " + $left_orientation + "'></div>";
    $cutout = $cutout + "<div class='middle'></div>";
    $cutout = $cutout + "<div class='right " + $right_orientation + "'></div>";
    $cutout = $cutout + "</div>";
	
	return $cutout;
}

//VOID indianMenu_READY_BIND_EVENTS()
//Connects the hover events to the indian menu
function indianMenu_READY_BIND_EVENTS(){
	var config = {    
		 over: indianMenu_regular_menu_hover_in, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: indianMenu_regular_menu_hover_out, // function = onMouseOut callback (REQUIRED)    
		 sensitivity: 10,
		 interval: 100 // time before hover  // old - 150
	};
	
	$indianMenu( '#indian-menu-bottom-menu > li' ).hoverIntent( config );
}

//VOID indianMenu_regular_menu_hover_in()
//This function will slide the menu down.
function indianMenu_regular_menu_hover_in(){
	$this = $indianMenu(this);
	
	if( $this.hasClass( 'products' ) ){
		$animationSpeed = $im_menuProductAnimationSpeed;
		if( !$im_menuImageLoad ){
			$indianMenu( ' > div > ul > li > a > span > img', $this ).each( function(){ 
				$indianMenu(this).attr( 'src', $indianMenu(this).attr( 'title' ) );
				$indianMenu(this).attr( 'title', $indianMenu(this).attr( 'alt' ) );
			});
			$im_menuImageLoad = true;
		}
	}
	else
		$animationSpeed = $im_menuRegularAnimationSpeed;
	
	if( !$this.hasClass( 'hover' ) ){
		
		$this.addClass( 'hover' );	
		
		if( $indianMenu( ' > div > ul > li', $this ).size() > 0 )
			$indianMenu( ' > div', $this ).slideDown( $animationSpeed );
		else
			$indianMenu( ' > a', $this ).height( 30 );	
	}		
	else if( $this.hasClass( 'hover' ) && $indianMenu( this ).hasClass( 'transition' ) ){
			
		$this.removeClass( 'transition' );	
		$indianMenu( '> div', $this ).clearQueue().stop(true).css( {'height' : 'auto'} ).slideDown( $animationSpeed );
		
	}
}

//VOID indianMenu_regular_menu_hover_in()
//This function will slide the menu up.
function indianMenu_regular_menu_hover_out(){		
	$this = $indianMenu(this);
	
	if( $this.hasClass( 'products' ) )
		$animationSpeed = $im_menuProductAnimationSpeed;
	else
		$animationSpeed = $im_menuRegularAnimationSpeed;
			
	var $item_size = $indianMenu( ' > div > ul > li', $this ).size();
	
	if( $this.hasClass( 'hover' ) && !$this.hasClass( 'transition' ) && $item_size > 0 ){
		
		$this.addClass( 'transition' );			
		$indianMenu( '> div', $this ).slideUp( $animationSpeed, function(){			
			$indianMenu( $indianMenu(this).parent() ).removeClass( 'transition' ).removeClass( 'hover' );	
		});	
	}	
	else if( $item_size == 0 ){	
		$this.removeClass( 'hover' );	
	}
}

function indianMenu_extend_footer(){
	var $footer_location = '#bottomRow';

	//alert( 'Calc ' + document.body.offsetHeight );
	//alert( 'body height: ' + $indianMenu( 'body' ).height() );

	var winW = 630, winH = 460;
	if (document.body && document.body.offsetWidth) {
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}

	if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
		winW = document.documentElement.offsetWidth;
		winH = document.documentElement.offsetHeight;
	}

	if (window.innerWidth && window.innerHeight) {
		winW = window.innerWidth;
		winH = window.innerHeight;
	}

	//alert('Window height = '+winH);

	var $window_page_difference = winH - $indianMenu( 'body' ).height();
	
	if( $window_page_difference > 0 ){
		var $footer_new_height_height = $indianMenu( $footer_location ).height() + $window_page_difference;
		$indianMenu( $footer_location ).height( $footer_new_height_height );
	}

}

