$(document).ready( function() {

	var left = $.cookie("search_left");
	var right = $.cookie("search_right");
	
	if ( left == null ) {
		left = 3000;
	}
	
	if ( right == null ) {
		right = 6000;
	}
	
	$("#priceSlide").trackbar({
		width : 280,
		onMove : function() {
			setInputValues( this.leftValue, this.rightValue);
		},
		leftLimit : 0,
		rightLimit : 50000,
		leftValue : left,
		rightValue : right,
		roundUp:1000,
		clearLimits: true
	});
	
	$("#bodyLeftCat li").hover( function() {
		$(this).find(".popupCat").css("display", "block");
	}, function() {
		$(this).find(".popupCat").css("display", "none");
	});
	
	$("#rightNext a").click( function() {
		
		var cat = $("#cat").val();
		var page = $("#page").val();
		
		if ( cat > 0 && page > 0 ) {
			getNextPage( cat, page );
		}
		return false;
	});
	
	$("#selectCityNow").click( function() {
		if ( $("#popupCity").css("display") == "none" ) {
			$("#popupCity").css("display", "block");
		} else {
			$("#popupCity").css("display", "none");
		}
	});
	scrollBasket();
	
	
	$("#popupShortImg a").click( function() {
		var href = $(this).attr("href");
		
		if ( href != '' ) {
			$("#popupFullImg img").attr( "src", href );
			$("#popupShortImg a").removeClass( "action" );
			$(this).addClass( "action" );
		}
		
		return false;
	});
});

function setInputValues( left, right ) {
	$("#min_price").val( left );
	$("#max_price").val( right );
	
	$.cookie("search_left", left);
	$.cookie("search_right", right);
	

}
$(function(){
	$('.scrollProducts').jScrollPane({showArrows: true});
	
});

function scrollBasket() {
	var offset = $(".basketSlide").offset();
	
	var topPadding = 15;
	$(window).scroll(function() {
		
		var basket = $("#basketCenterR div").text();
		
		if ($(window).scrollTop() > offset.top && basket != 'Корзина пуста' ) {
			$(".basketSlide").stop().animate({marginTop: $(window).scrollTop() - offset.top + topPadding});
		} else {
			$(".basketSlide").stop().animate({marginTop: 0});
		};
	});
}

function getNextPage( cat, page ) {
	if ( cat > 0 && page > 0 ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: "run=catalog&action=getnextpage&cat=" + cat +"&page=" + page,
			success: function( data ) {
				if ( data != 'error' ) {
					$("#catalogProductsBody").append( data );
					page = $("#page").val( page * 1 + 1);
				}
			}
		});
	}
}

function addProduct( articul ) {

	if ( articul != '' ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: "run=catalog&action=addbasket&articul=" + articul,
			success: function( data ) {
				if ( data != 'error' ) {
					if ( data == 1 ) {
						basketRefresh();
				
					} else if ( data == 2 ) {
						alert( 'Товар уже в корзине' );
					}	
				}
			}
		});	
	} else {
		alert( 'ошибка добавления товара в корзину' );
	}
}

function basketRefresh() {
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: "run=catalog&action=basketRefresh",
		success: function( data ) {
			if ( data != 'error' ) {
				$("#basket").addClass( "basketSlide" );
				$("#basketCenterR").html(data);
				/* Сфиг */
				var offset = $(".basketSlide").offset();
				var topPadding = 15;
				var basket = $("#basketCenterR div").text();
		
				if ($(window).scrollTop() > offset.top && basket != 'Корзина пуста' ) {
					$(".basketSlide").stop().animate({marginTop: $(window).scrollTop() - offset.top + topPadding});
				} else {
					$(".basketSlide").stop().animate({marginTop: 0});
				};
				/* Сфиг */	
				
				
				
				$("#basketPlus").animate({ 'opacity' : '1'}, 300, function() {
					$("#basketPlus").animate({ 'opacity' : '1'}, 1000, function() {
						$("#basketPlus").animate({ 'opacity' : '0'}, 300);
					});	
				});
			}
		}
	});	
}

function deleteOrderItem( id, obj ) {
	if ( id > 0 ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: "run=order&action=deleteOrderItem&id=" + id,
			success: function( data ) {
				if ( data != 'error' ) {
					$(obj).parent().parent().remove();
					location.reload()
				} else {
					alert( 'Ошибка' );
				}
			}
		});	
	}
}

function setAdminStatus( type, articul ) {
	if ( type != '' && articul != '' ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: "run=catalog&action=setstatus&type=" + type + '&articul=' + articul,
			success: function( data ) {
				if ( data != 'error' ) {
					$("#adminStatus").html( data );
				} else {
					alert( 'Ошибка' );
				}
			}
		});	
	}
}

function delimg( id, obj ) {
	if ( id != '' ) {
		$.ajax({
			type: "POST",
			url: "/ajax.php",
			data: "run=catalog&action=delimg&id=" + id,
			success: function( data ) {
				if ( data == '1' ) {
					$(obj).parent().remove();
				} else {
					alert( 'Ошибка' );
				}
			}
		});	
	}
}

function getPopup( id, plus ) {
	var scrollTop = $(document).scrollTop();

	var top = 0;
	top = $(id).css("top").replace("px", "");

	var topmargin = scrollTop*1 + plus*1;

	$("body").prepend('<div id="busy_layer" onclick="closePopup(); return false;" style="display: block; z-index:1000; position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; background-color: black; opacity: 0.8; -ms-filter: \'progid:DXImageTransform.Microsoft.Alpha(Opacity=80)\'; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=80); "></div>');
	
	$("#busy_layer").css( "height", $(document).height() );
	
	$( id ).css({
		top: topmargin,
	//	left: ($(window).width() - $( id ).outerWidth())/2,
		left: 100,
		display: "block"
	});
}
function closePopup() {
	$("#busy_layer").remove();
	$(".popups").css( "display", "none" );
}
function openGalery() {
	getPopup("#popupImages", 0);
}
