(function($, config){
	$(document).ready(function(){
	
		
		function colorboxAlert(msg, title) {
			$.colorbox({
				'html' : "<div style=\"margin:0px;width:350px;\">"+msg+"</div>", //+"<div style=\"text-align:right;margin:10px;\"><input type=\"image\" src=\""+config.base_path+"/img/ok.gif\" onclick=\"$.colorbox.close(); \" value=\"Ok\">
				'close' : 'Ok'//,
				//'title' : title
			});
			//$('#fancybox-content').css({'border-width': '0px', 'width': '350px'});
			//$('#fancybox-wrap').css({'width': '350px'});
		}
		window.alert = function(text, title) {
			out = '';
			if(title){
				title = '<h3>'+title+'</h3>';
			} else {
				title = false;
			}
			out = out + '<p style="margin:10px;">'+text+'</p>';
			colorboxAlert(out, title);
		}
	
		$(window).resize(function(){
			div = (($('.owr').width()-$('.owr .wrapper').width())/2);
			$('.ear_right').css({
				'width': div+'px',
				'right': '-'+div+'px'
			});
		});
		$(window).triggerHandler('resize');
		
		$('.cart_block').each(function(){
			var conteiner = $(this),
			removeButton = conteiner.find('.item .meta .remove a');
			removeButton.click(function(e){
				var button = $(this);
				e.preventDefault();
				$.ajax({
					type: "POST",
					cache: false,
					url: config.cart_path,
					data: {'id': removeButton.index(button.get(0)), 'action' : 'delete' },
					success: function(msg){
						response = eval(msg);
						if(response.error){
							alert(response.error);
						} else {
							if(response.total>0){
								$('.header .cart .cart_info p').text(response.content);
								$('.header .cart .cart_info').show();
								conteiner.find('.button strong').text(response.total);
							} else {
								$('.header .cart .cart_info p').text('');
								$('.header .cart .cart_info').hide();
								conteiner.find('.button').remove();
								conteiner.append('Ваша корзина пуста');
							}
							
							button.parents('.item').fadeOut(function(){
								button.parents('.item').remove();
							});
						}
					}
				});
			});
			conteiner.find('.button .update').bind('click', function(e){
				$('#form_action').val('update');
				conteiner.find('form').trigger('submit');
				e.preventDefault();
			});
		});
		
		$('.autochange').each(function(){
			var value = $(this).val();
			$(this).bind({
				'focus' : function(){
					if($(this).val()==value) $(this).val('');
				},
				'blur' : function(){
					if($(this).val()=='') $(this).val(value);
				}
			});
		});
		
		$('.promoblock').each(function(){
			var items = $(this).find('.img img');
			var navigation = $(this).find('.links .item');
			navigation.bind({
				'mouseover': function(){
					indexThis = navigation.index($(this));
					indexCurrent = navigation.index(navigation.filter('.current'));
					if(indexThis!=indexCurrent){
						items.eq(indexCurrent).fadeOut(function(){
							items.eq(indexThis).fadeIn();
							navigation.removeClass('current');
							navigation.eq(indexThis).addClass('current');
						});
					}
				},
				'mouseout': function(){
				
				}
			});
		});
		
		
		
		function addtocart(inp){
			targetform = $(inp).parents('form');
			productId = targetform.find('input[name=product]').val();
			$.ajax({
				type: "POST",
				cache: false,
				url: config.cart_path,
				data: {'id': productId, 'action' : 'add' },
				success: function(msg){
					response = eval(msg);
					if(response.error){
						alert(response.error);
					} else {
						//$('.header .cart .cart_info').hide();
						$('.header .cart .cart_info p').text(response.content);
						$('.header .cart .cart_info').show();
						targetform.hide();
						targetform.after('<div class="incart">&nbsp;</div>');
					}
					//alert(msg);
				}
			});
			
		}
		
		$('.addtocart').bind('click', function(e){
			addtocart($(this));
			e.preventDefault();
		});
		$.featureList( $('#home_slider-nav > div'), $('#home_slides > a '), {
			start_item : 0,
			transition_interval : -1,
			transition_speed : -1,
			interaction : 'click'
		});
	});

})(jQuery, settings || {});
