function reset_search_categories(){
	checks = $("input[name='search_categories']").attr('checked', false);
}	

function reset_search_regions(){
	checks = $("input[name='search_regions']").attr('checked', false);
}

function show_other_regions(){
	$("#other_regions").load('/listing/other_regions_ajax/');
	$("#other_regions_link").hide();
}

function comment_plus(comment_pk){
	$("#feedback"+comment_pk+"").load('/listing/comment_plus/'+comment_pk+'/');
}

function comment_minus(comment_pk){
	$("#feedback"+comment_pk+"").load('/listing/comment_minus/'+comment_pk+'/');
}

function refreshDocs(){
    $("#docs_list").load('/listing/get_uploaded_docs/');                
}

function del_doc(pk, session_key){
	$("#del_docs").show();
	$("#del_docs").load('/listing/del_docs/'+pk+'/'+session_key+'/'); 
	refreshDocs();
	return false;
}
var verticalSliderAnimationSpeed = 1000;

$(document).ready(function() {
	//$("#content .mpTwoCols .colRight .slider").loadSlider();
	$("#content  .slider").loadSlider();
	
	$("#content .rezpoiskCols .advSearchParams .expandableSelect ul").expandable();
	
	
	
	$('a.hint').cluetip({
 		/* splitTitle: '|', */
 		arrows: true, 
 		local: true,
 		cursor: 'help',
 		leftOffset: '15'	
 	});
	
	/* Event sample */
	$(".ratingCtrl").each(function(){
		this.onvalchange = function(val){
			var ad_pk = $(".ratingCtrl").attr("id");
			$("#feedback_price").load('/listing/price_evaluation/'+ad_pk+'/'+val+'/'); 
			
		};
	});
	
	
	$(".ratingCtrl").ratingCtrl();
	
	/* Event sample */
	$(".docsCtrl").each(function(){
		this.onvalchange = function(val){
			var ad_pk = $(".ratingCtrl").attr("id");
			$("#feedback_docs").load('/listing/info_evaluation/'+ad_pk+'/'+val+'/'); 
		};
	});
	
	$(".docsCtrl").docsCtrl();
	
	$(".docsCtrlReadOnly").docsCtrlReadOnly();
	
	$("#content .moreInfo .rotator").verticalRotator();
});

jQuery.fn.extend({
	
	loadSlider: function() {
		return this.each(function(){
			var totalWidth = 0;
			var slider = this;
			var sldrAnimateNow = false;
			
			$(slider).contents().find(".img").each(function(){
				totalWidth += $(this).width() + parseInt($(this).css("paddingLeft")) + parseInt($(this).css("paddingRight"));
			});
			$(slider).contents().find(".plane").css("width", totalWidth + "px").
				css("position", "relative").css("left", "0");
			
			$(this).contents(".arrow").click(function(){
				if (!sldrAnimateNow) {
					var plus = parseInt($(slider).contents().find(".plane").css("width")) /
						$(slider).contents().find(".img").size();
						
					if ($(this).hasClass("aright")) {
						plus *= -1;
					}
					
					var newLeft = parseInt($(slider).contents().find(".plane").css("left")) + plus;
	
					if (newLeft >= $(slider).contents(".wrapper").width()) {
						newLeft -= plus;
					}
					else if ((newLeft < 0) && (-newLeft >= $(slider).contents().find(".plane").width()) ) {
						newLeft -= plus;
					}
	
					//$(slider).contents().find(".plane").css("left", plus + "px");
					
					sldrAnimateNow = true;
					$(slider).contents().find(".plane").animate({
						left:newLeft + "px"
					}, 500, "", function(){sldrAnimateNow = false;});
				}
				return false;
			});
			
		});
		
	},
	
	expandable: function() {
		return this.each(function(){
			var expandable = $(this);
			
			expandable.children("li").each(function(){
				var li = $(this);
				
				if (li.children("ul").size() > 0) {
					li.addClass("expandable");
					
					li.click(function(){
						ul = li.children("ul");
						
						if (ul.css("display") == "none") {
							ul.css("display", "block").
								css("opacity", 0);
							
							ul[0].height = ul.height();
							ul.css("height", "0");
						
							ul.animate({
								opacity:1,
								height:ul[0].height + "px"
							}, 300);
						
							$(this).addClass("active");
						} else {
							ul.animate({
								height:0,
								opacity:0
							}, 300, "", function(){
								ul.css("display", "none").
									css("height", ul[0].height + "px");
								li.removeClass("active");
							});
						}
						
						return false;
					});
				}
				
			});
		});
	},
	
	ratingCtrl: function(){
		return this.each(function(){
			var ctrl = $(this);
			var clearCtrl = this;
			
			ctrl.append("<span></span>")
			
			var begun = ctrl.children("span");
			var changing = false;
			var lastPos;
			
			var val = parseInt(ctrl.attr("title"));
			
			if (val) {
				setPos(Math.round((ctrl.width() - 4) / 100 * val));
			} else {
				setPos(Math.round((ctrl.width() - 4) / 100 * 50));
			}
			
			if (!ctrl.hasClass("readOnly")) {
			
				begun.mousedown(function(){
					changing = true;
					return false;
				}).mouseup(function(){
					onvalchange(val);
					changing = false;
					return false;
				}).click(function(){
					return false;
				});
			
				ctrl.mouseleave(function(){
					if (changing) {
						onvalchange(val);
						changing = false;
					}
				}).mousedown(function(){
					changing = true;
					setPos(lastPos);
					return false;
				}).mouseup(function(){
					onvalchange(val);
					changing = false;
					return false;
				}).mousemove(function(e){
					var newPos = e.clientX - ctrl.offset().left - parseInt(ctrl.css("paddingLeft")) - 2;
					if (changing) {
						setPos(newPos);
					}
					lastPos = calcPos(newPos);
				});
			
			}
			
			function onvalchange(val) {
				if (clearCtrl.onvalchange) {
					clearCtrl.onvalchange(ctrl.attr("title"));
				}
			}
			
			function calcPos(newPos) {
				if (newPos < 0) {
					newPos = 0;
				}
				else if (newPos > ctrl.width() - 4) {
					newPos = ctrl.width() - 4;
				}
				return newPos;
			}
			
			function setPos(newPos) {
				newPos = calcPos(newPos);
				begun.css("left", newPos + "px");
				ctrl.attr("title", Math.round(newPos / (ctrl.width() - 4) * 100));
			}
			
		});
	},
	
	
	docsCtrlReadOnly: function(){
		return this.each(function(){
			var ctrl = $(this);
			var clearCtrl = this;
			var val = parseInt(ctrl.attr("title"));
			
			for (var n = 0; n < 5; n++) {
				ctrl.append('<b>  </b>');
			}
			
			var aas = ctrl.children("b");
			
			if (!val) 
				val = 1;
				
			setVal(val);
			
			function setVal(value) {
				aas.removeClass("active");
				aas.filter('b:lt(' + value + ')').addClass("active");
			}
		});
	},
			
	docsCtrl: function(){
		return this.each(function(){
			var ctrl = $(this);
			var clearCtrl = this;
			var val = parseInt(ctrl.attr("title"));
			
			for (var n = 0; n < 5; n++) {
				ctrl.append('<a href="#"></a>');
			}
			
			var aas = ctrl.children("a");
			
			if (!val) 
				val = 1;
				
			setVal(val);
			
			if (!ctrl.hasClass("readOnly")) {
				aas.each(function(index){
					$(this).mouseenter(function(){
						setVal(index + 1);
					}).mouseleave(function(){
						setVal(val);
					}).click(function(){
						val = index + 1;
						ctrl.attr("title", val);
						if (clearCtrl.onvalchange) {
							clearCtrl.onvalchange(val);
						}
						return false;
					});
				});
			}
			else {
				aas.css("cursor", "default").click(function(){return false});
			}
			
			function setVal(value) {
				aas.removeClass("active");
				aas.filter('a:lt(' + value + ')').addClass("active");
			}
			
		});
	},
	
	verticalRotator: function(){
		return this.each(function(){
			
			var rotator = $(this);
			rotator.append('<div class="navigator"><a href="#" class="slideUp">' + 
				'<br/></a><a href="#" class="slideDown"><br/></a>' + 
				'<div class="container"><div class="plane">' + 
				'</div></div></div>');
			var container = rotator.contents().find(".container");
			var plane = rotator.contents().find(".plane");
			var images = rotator.children("a").remove().appendTo(plane);
			var slideUp = rotator.contents().find(".slideUp");
			var slideDown = rotator.contents().find(".slideDown");
			var scrollingNow = false;
			
			var firstA = images.eq(0);
			var oneHeight = firstA.height() + 
				parseInt(firstA.css("marginBottom")) +
				parseInt(firstA.css("paddingTop")) +
				parseInt(firstA.css("paddingBottom"));
			
			rotator.append('<div class="display"></div>')
			
			var display = rotator.children(".display");
			
			images.each(function(index){
				this.index = index;
				
				$(this).click(function(){
					if (!(images.filter(".current")[0] == this)) {
						flipImages($(this));
					}
					
					return false;
				});
			});
			
			slideUp.click(function(){
				if (!scrollingNow) {
					if (parseInt(plane.css("top")) < 0) {
						scrollingNow = true;
						plane.animate({
							top:parseInt(plane.css("top")) + oneHeight + "px"
						}, 500, "", function(){scrollingNow = false;});
					}
				}
				return false;
			});
			slideDown.click(function(){
				if (!scrollingNow) {
					if (plane.height() + parseInt(plane.css("top")) > container.height()) {
						scrollingNow = true;
						plane.animate({
							top:parseInt(plane.css("top")) - oneHeight + "px"
						}, 500, "", function(){scrollingNow = false;});
					}
				}
				return false;
			});
			
			appendImageDiv(images.eq(0), true);
			images.eq(0).addClass("current");
			
			function appendImageDiv(a, isFirst) {
				var cls = "photo";
				
				if (!isFirst) {
					cls += " new";
				}
				display.append('<a href="#"><div style="background:url(' + a.attr("href") + ') left top no-repeat" class="' + cls + '"></div></a>')
			}
			
			function flipImages(a) {
				images.filter(".current").removeClass("current");
				
				var oldImg = display.children(".photo");
				appendImageDiv(a);
				var newImg = display.children(".photo.new");
				
				a.addClass("current");
				
				newImg.css("opacity", "0").
					animate({
						opacity:1
					}, verticalSliderAnimationSpeed, "", function(){
						oldImg.remove();
						newImg.removeClass("new");
					});
			}
			
		});
	}
	
});
