// Stanley loses his balloon
$(document).ready(function() {
	
	// Get Stanley's position
	var stanley = $("#stanley_bear_core");
	var balloon = $("#balloon");
	var is_animating = false;
	var current_bubble = false;

	stanley.mouseover(function() {

		if (!(is_animating)) {
			
			// Animate the balloon
			is_animating = true;
			
			// If the browser is Internet Explorer, use simplified animation
			if ( $.browser.msie ) {
				
			// Else if anything else use advanced animation
			} else {
				$(balloon).animate({ opacity: 0, top: '-=700', right: '400'}, 5000, function() {
				
					$(balloon).animate({top: '+=700', right: '-400'}, 0, function () {
					
						// Fade the balloon back
						balloon.fadeTo(500, 1);	
						is_animating = false;

					});
				});
			}
		}
	});
	
	$('.product_container').mouseenter(function() {
		
		// Only animate if it is not in an animation cycle
		if (is_animating == false) {

			if ((current_bubble) && (current_bubble != $(this).attr('id'))) {
				
				$(this).find('.product_bubble').fadeTo(300, 0).slideUp(0);
				current_bubble = false;
			}
			
			is_animating = true;
			$(this).find('.product_bubble').fadeTo(300, 1).slideDown(0, function() {
				
				// Finish the animation
				is_animating = false;
				current_bubble = $(this).parent().attr('id');

			});
			
		}
		
	}).mouseleave(function() {
		
		// Only animate if it is not in an animation cycle
		if (is_animating == false) {
			
			$(this).find('.product_bubble').fadeTo(300, 0).slideUp(0);
			current_bubble = false;
		}
	});
	
	// Do the prompt for callback
	var window_height = $(window).height();
	var window_width = $(window).width();
	
	// Do calculations required for message box
	var box_height = 300;
	var box_width = 450;
	
	var top_bottom = (window_height - box_height) / 2;
	var left_right = (window_width - box_width) / 2;
	
	
	$(document).find('#callback_overlay').css({'padding-left' : left_right, 
											   'padding-right': left_right, 
											   'padding-top': top_bottom, 
											   'padding-bottom': top_bottom, 
											   'height': box_height, 
											   'width': box_width});

	$(document).find('#balloon').css('z-index', 99);
	$(document).find('#callback_overlay').delay(3000).fadeTo(600, 1);
	
	$('#close_callback').click(function() {
		
		$(document).find('#callback_overlay').fadeTo(600, 0);
	});
});
