$(document).ready(function(){
	$('.entry.tight p').hide();
	
	// set a.ext elements to open in new tab/window
	$('a.ext').attr('target','_blank');
	
	widgets.start();
});

var widgets = {
	start:function(){
		var instance = this; // widgets
		this.cart.start({});
		this.attachHandlers({});
	},
	
	attachHandlers:function(options){
		var instance = this;
		this.handlers.cart({});
	},
	
	handlers:{
		instance:this, // widgets
		cart:function(options){
			// basically this handles the send, and then we let the widget handle its update
			var instance = this; // widgets.handlers
			$('.product-buy-now-btn a').click(function(event){
				event.preventDefault();
				var el = this;
				var cart = widgets.cart; // this brings us to widgets.cart actually
				cart.msg = messaging.notify.cart; // this adds the messaging.notify object tothe cart object
				var parentContainer = $(el).closest('span');
				// kill default action
				// set parent LI position to relative
				parentContainer.css({'position':'relative','display':'inline-block'});
				// show a loading icon
				var loadImgEl = assets.images.loadingIcon.el.clone();
				loadImgEl.css({
					'position':'absolute',
					'top':'-5px',
					'left':'21px',
					'display':'none'
				});
				if($(el).hasClass('has-options')){
					window.location = $(el).attr('href');
					/*
					var msg = $('<div><p>This product has options to choose before you can add it to the cart.</p><p><a href="' + $(el).attr('href') +'">Click here to go to this product.</a></p></div>');
					msg.css({
						'display':'block',
						'position':'absolute',
						'bottom':'40px',
						'left':'10px',
						'width':'140px',
						'background-color':'#ffc',
						'border':'1px solid #f90',
						'font-weight':'bold',
						'z-index':'800',
						'padding':'10px',
						'display':'none'
					});
					
					var parent_selector = false;
					if(options.parent_selector){
						parent_selector = options.parent_selector;
					} else {
						parent_selector = 'li';
					}
						
					$(el).closest(parent_selector).css({'position':'relative'}).append(msg);
					msg.fadeIn();
					
					setTimeout(function(){msg.fadeOut();}, 10000);
					*/
				} else {
					parentContainer.append(loadImgEl);
					$(el).animate({opacity:0.25} , 1200, function(){});
					loadImgEl.show();
					//cart.msg.addProduct('',{event:event});
					$(cart.container).load($(el).attr('href') + ' ' + cart.containerSelector, function(response, status, xhr){
						// callback instructions
						$(el).animate({opacity:1.0} , 300, function(){});
						loadImgEl.fadeOut();
						loadImgEl.remove();
					});
				}
			});
			/*
			$('#product_info .product-meta form').bind('submit', function(event){
				event.preventDefault();
				var el = this;
				var href = $(el).attr('action');
				var cart = widgets.cart; // this brings us to widgets.cart actually
				
				$.ajax({
					url: href,
					type:'POST',
					data:$(el),
					dataType:'html',
					success:function(data){
						alert('added');
					},
					error:function(data){
					}
				});		
				
				//alert('hi');
			});
			*/
		}
	},
	
	cart:{
		instance:this, // widget
		container:false,
		containerSelector:false,
		start:function(options){this._init(options);},
		_init:function(options){
			var instance = this; // widget.cart
			instance.containerSelector = '#shopping-cart-widget';
			instance.container = $(instance.containerSelector);
			$(instance.container).css({'border':'1px solid transparent'});
			
		},
	}
}
var assets = {
	images:{
		loadingIcon:{
			el:$('<img src="/includes/slimbox/css/loading.gif" alt="Loading..." class="loading-icon"/>'),
			src:'/includes/slimbox/css/loading.gif',
		}
	}
};

var messaging = {
	error:{
		eAlert:function(msg, options){
			alert(msg);
		}
	},
	notify:{
		cart:{
			addProduct:function(msg, options){
				if(!options){
					var options = {};
				}
				if(!options.stop){
					// start it
					
				} else {
				}
			},
			productAdded:function(msg, options){
			}
		}
	}
}
