(function($){
	var dia = null;
	var VerifyReAdd = function(form)
	{
		xx = form;
		var productName = $(form).find('input.productName').val();
		dia = $('<div>The Item <strong>'+ productName+'</strong> is already in your basket.<br/><br/>Are you sure you would like to continue?</div>').dialog({
				title:'This item is already in your basket',
				draggable:false,
				modal: true,
				resizable:false,
				buttons:
				{
					'Yes' : function(){ form.submit(); },
					'No' : function(){ dia.dialog('close');  }
				},
				close:function(){
					dia=null;
				}
			}
		);
	};
	
	/*var AlertAdd = function(form)
	{
		dia = $('<div>Item Already In Basket</div>').dialog({
			title:'Item Already In Basket',
			draggable:false,
			modal: true,
			buttons:
			{
				'Yes' : function(){ form.submit(); },
				'No' : function(){ dia.dialog('close');  }
			},
			close:function(){
				dia=null;
			}
		});
	};*/
	
	var NotifyAddSuccess = function(){
		var val = 3;
		dia = $('<div>Your item was added successfuly.<br /><br /><br /><br /><span class="greyText">This message will auto-close in <span class="someTime">'+val+'</span> seconds</span></div>');
		var timeSpan = dia.find('span.someTime');
		var greyText = dia.find('span.greyText');
		greyText.css({color:'#aaaaaa'});

		var counterInterval = setInterval(function(){ if(timeSpan) timeSpan.html(--val); },1000);
		var closeTimeout = setTimeout(function(){dia.dialog('close')},(val+1)*1000);
		
		dia.dialog({
			title:'Item Added',
			draggable:false,
			modal: true,
			resizable:false,
			position:'center',
			close:function(){
				dia=null;
				clearTimeout(counterInterval);
				clearTimeout(closeTimeout);
			},
			buttons:
			{
				'Close Now' : function(){ 
					dia.dialog('close');  
				}
			}
		});

	}

	$(document).ready(function(){
		$('form[action^="/basket/add"]').submit(function(ev){
			
			if(dia==null && typeof basket[$(this).find('input[name=product_id],select[name=product_id],').val()] != "undefined"){

				ev.stopPropagation();
				VerifyReAdd(this);
				return false;
			}
			return true;
		});
		$(document).ready(function(){
			if(typeof itemAddedSuccessfuly != 'undefined')
			{
				setTimeout(function(){NotifyAddSuccess();},1000);
			}
		});
	})
})(jQuery);
