var ProductGridWidget = {
	
	initialize: function () {
		var _self = this;
		this.parent = $('#productsGrid');
		
		this.parent.find('em').currency();
		
		$('#productsGrid .grid-product a').hover(function () {
			var hover = $(this).find('.product-over');
			if($('html').hasClass('localstorage')) {
				hover.css({'display': 'none'});
				hover.fadeIn();
			}
			return false;
		},function () {
			return false;
		});
		$('#productsGrid-tab-nav .products-tab-nav a.product-tab-btn').click(function () {
			$('#productsGrid-tab-nav .products-tab-nav a.product-tab-btn').removeClass('active');
			$(this).addClass('active');
			var id = $(this).attr('href');
			_self.gotoTab(id);
			Cufon.refresh('.helvetica');
			return false;
		});
		
		$('#productsGrid-tab-nav-regions .products-tab-nav a.product-tab-btn').click(function () {
			$('#productsGrid-tab-nav-regions .products-tab-nav a.product-tab-btn').removeClass('active');
			$(this).addClass('active');
			var id = $(this).attr('href');
			_self.gotoTab(id);
			Cufon.refresh('.helvetica');
			return false;
		});
		
		$('#productsGrid .grid-product a .pd-title').each (function () {
			var obj = $(this);
			if (obj.outerHeight() > 40) {
				var str = '';
				while (obj.outerHeight() > 40) {
					str = obj.text();
					var lastspace = str.lastIndexOf(' ');
					str = str.substr(0, lastspace);
					obj.text(str + "...");
				}
				
			}
		});
		
	},
	
	gotoTab: function (id) {
		this.parent.find('.product-tab').hide();
		var tab = this.parent.find(id);
		var products = tab.find('.grid-product');
		products.css('opacity', 0);
		tab.show();
		var i = products.length;
		var body = $('body');
		var isIE = (body.hasClass('IE6') || body.hasClass('IE7'));
		var newCSS = {'opacity':1};
		if (isIE) {
			products.css(newCSS);
		} else {
			while (i--) {
				products.eq(i).delay(i * 75).animate(newCSS, 250);
			}
		}
		
	}
	
};

$(function () {
	ProductGridWidget.initialize();
});
