$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

// MeWithMy jQuery class
var Gregory = function($) {
	
	return {
		init : function() {
			if($.browser.mozilla){
				$('body').addClass('firefox');


			  	var version = $.browser.version.substring(0,3); 
			  	if (version == '2.0') {
			  	   majorVersion = 4; 
			  	} else if(version == '1.8') {
					majorVersion = 2;
				} else if (version == '1.9') {
					majorVersion = 3;
				} else {
				    majorVersion = version.substring(0,1);
				}

				$('body').addClass('firefox'+ majorVersion);
			}
			
			if($.browser.safari){
				$('body').addClass('safari');
			}
			
			
			// region drop down
			$('#region_chooser').hover(function() {
				$('#region_chooser ul').show();
			}, function() {
				$('#region_chooser ul').hide();
			});
			
			//sorter
			$('#product_filter').change(function() {
				var productId = $(this).val();
				
				window.location = '/good-life/index/product_id/'+ productId;
			});
			
			
			//flyouts drop down
			// $('#header ul.navigation li').hover(function() {
			// 	$(this).find('ul').show();
			// 	
			// }, function() {
			// 	$(this).find('ul').hide();
			// });
			
			// no Cufon in China
      // if (!$('body').hasClass('zh_CN') && !$('body').hasClass('ja')) {
      //  Cufon.replace('#header .navigation li a:not(#products-subnav a), .submit #right dt, .tabs a, .square_dot p, .apex, #description h3, .group #content #right h2, #left .tab_content ul li a:not(#features li a), #left ul.navigation li a, .ambassador h2, .technology #right h2, #buy_now a, .description h2, #technology h4, .good-life #upload_photo h3', {
      //    fontFamily: 'Apex New',
      //    hover: true,
      //    hoverables: { a: true }
      //  });
      // 
      //  Cufon.replace('#page_top h2, #left h1, #right h1,.square_dot h2, .description p, .description ul li', {
      //    fontFamily: 'Apex New Book'
      //  });
      // 
      //  Cufon.replace('.showcases .description ul li strong', {
      //    fontFamily: 'Apex New'
      //  });
      //  
      // };

            if ($('.step ol li').length) {
                
                $('.step ol li').each(function() {
                    $(this).wrapInner('<span>');
                });
                
            };
			
			initLike();

			var subnavTimeout = null;
			
			var subNavHtml = $('<div id="products-subnav"></div>').html($('#products-subnav').html());
			$('#products-subnav').remove();
			
			$('#menu-product-nav').after(subNavHtml);
			
			//products nav item hover
			$('#menu-product-nav').mouseenter(function() {
				$('#products-subnav').show();
				$(this).addClass('hover');
			});
			
			$('#menu-product-nav').mouseleave(function() {
				subnavTimeout = setTimeout("$('#products-subnav').hide(); $('#menu-product-nav').removeClass('hover');", 100);
			});
			
			//if you hover over another nav item, close fat nav immediately
			$('#header .navigation > li').hover(function() {
				if($(this).find('a').attr('id') != 'menu-product-nav') {
					$('#products-subnav').hide();
					$('#menu-product-nav').removeClass('hover');
				}
			});
			
			//cancel timer if you're over the fat nav
			$('#products-subnav').hover(function() {
				clearTimeout(subnavTimeout);
			});
			
			$('#menu-product-nav').hover(function() {
				clearTimeout(subnavTimeout);
			});
			
			if ($('.zh_CN #menu-online-dealers').length > 0) {
				$('#menu-online-dealers').parent().remove();
			};
			
			//start timer to hide fat nav if you mouseout of it
			$('#products-subnav').mouseleave(function() {
				subnavTimeout = setTimeout("$('#products-subnav').hide(); $('#menu-product-nav').removeClass('hover');", 100);
			});
			

		}
	};
	
	function initLike() {
		$('a.like').live('click', function() {
			var button = $(this);
			var img = "";
			if(!$(this).hasClass('disabled')) {
				var href = $(this).attr('href');

				var box = $(this);

				$.ajax({
					url: href, 
					dataType: 'json',
					beforeSend: function(data) {
						img = box.css('background-image');
						box.css('background-image', 'url(/img/good_life/ajax-loader.gif)');
					},
					success: function(json) {
						box.css('background-image', img);
						if(json.message.length > 1) {
							box.addClass('message');
						}
					
						box.html(json.message);	


						
						button.addClass('disabled').addClass('liked').removeClass('like');

						
						if(json.pulse == 1 && box.pulse) {
							box.pulse({
								backgroundColors: ['#cc5b35','#ba2f00'],
								speed: 500
							});				
						}
					},
					error: function() {
						box.html('error');
					}
				});
			}

			
			return false;
		});
		
		$('a#region_dd').hover(function() {
			$('#region_chooser').show();
			$('#region_chooser').mouseleave(function() {
				$(this).hide();
			});
		});
		
		// do nothing when you "relike" something
		$('a.liked').live('click', function() {
			return false;
		});
	}
	
}(jQuery);

$(document).ready(Gregory.init);

