var rollingOver = false;

$( function() {
	var zIndexNumber = $('div').length + 1;
	$( "div:not(#mainDropMenuContainer div,.medImgRollver,#purpleLabelIntro,#lightbox-overlay,#lightbox)" ).each(function() {
		$( this ).css('zIndex', zIndexNumber);
		zIndexNumber--;
	});
	
	$( 'table.multiProducts td div.ThumbProduct' ).mouseover( function() {
		$( this ).css( 'position', 'relative' );
	} );
	$( 'div#mainDropMenuContainer ul>li' ).moseover( function() {
		$( 'table.multiProducts td div.ThumbProduct' ).css( 'position', 'static' );
	} );
	
	$( '.thumbImgRollover' ).hover( function() {
		var screenWidth = $(window).width();
		var midScreen = parseInt( screenWidth / 2 );
		var screenHeight = $(window).height();
		
		//enters
		if ( rollingOver ) {
			return false;//fail if its in the middle of rolling over
		}
		
		closeAllMouseOvers();
		if ( $( this ).closest( 'div' ).find( 'div.medImgRollver' ).length ) {
			rollingOver = true;
			$( this ).closest( 'div' ).find( 'div.medImgRollver' ).show( 1, function() {
				
				//detect position and move if needed
				var offset = $( this ).offset();
				
				$( this ).css( {
					'z-index': '300000',
					'left': '124px',
					'right': 'auto',
					'top': '-10px'//reset after moving
				} );
				
				var width = $( this ).outerWidth();
				var totalWidth = offset.left + width;
				var boxHeight = $( this ).outerHeight();
				
				var animate = parseInt( ( boxHeight - 120 ) / 2 ) * -1;
				$( this ).animate( { 'top': animate + 'px' }, 800 );
				
				rollingOver = false;
			} );
		}
	}, closeAllMouseOvers );
	
	$( 'div.medImgRollver,body' ).mouseover( closeAllMouseOvers );

} );

function closeAllMouseOvers() {
	$( 'div.medImgRollver' ).hide();
}
