/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};

// main 3
/*$(function() {
	$("#tabs_links").find("a").click(function() {
		$(this).blur();
		
		if ($(this).hasClass("selected")) return false;
	
		$(this).siblings().removeClass("selected");
		$(this).addClass("selected");
		
		var index = $(this).index();
		$("#tabs .cat_index").each(function() {
			if ($(this).index() === index) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});
		
		if (index === 0) {
			$("#deal1").attr("checked", 1);
			$("#deal2").attr("checked", null);
		}
		if (index === 1) {
			$("#deal2").attr("checked", 1);
			$("#deal1").attr("checked", null);
		}
		
		$(".deal a").eq(index).addClass("selected").siblings("a").removeClass("selected");
		
		return false;
	});
	
	$(".deal a").click(function() {
		$('#tabs_links a').eq($(this).index()).click();
		return false;
	});
});*/

$(function() {
	$(".tabs_links").find("a").click(function() {
		$("input[name=deal]").eq($(this).index()).click().trigger("change");

		return false;
	});
});

$(function() {
    $('input[placeholder], textarea[placeholder]').placeholder();
});

// Смена регионов при выборе страны
$(function() {
	$("select[name=country]").change(function() {
		var $select = $(this);
		var country = $(this).val();
		var region  = $(this).attr("region");

		var $region = $select.closest("form").find("select[name=region]");
		if (!$region.length) return;
		
		var r = $region[0];
		
		r.options.length = 0;
		
		var option = document.createElement("option")
		option.value = ""
		option.appendChild(document.createTextNode("Выберите регион"));
		r.appendChild(option)
		for (var i in country_regions[country]) {
			var option = document.createElement("option")
			option.value = country_regions[country][i]
			option.selected = (region == country_regions[country][i])
			option.appendChild(document.createTextNode(country_regions[country][i]));
			r.appendChild(option)
		}
	}).trigger("change");
});

// Переключение покупка/аренда в поиске
$(function() {
	$("input[name=deal]").click(function() {
		var deal = $(this).val();
		
		// Перенаправление
		if ($(document.body).data("page") == 1) {
			if (deal == 1) {
				var url = $(".tabs_links").first().find("a").eq(0).attr("href");
			} else {
				var url = $(".tabs_links").first().find("a").eq(1).attr("href");
			}
			window.location.href = url;
			return false;
		}

		if (deal == 1) {
			$(".deal1").show();
			$(".deal2").hide();
			
			$("#form-deal1 input[name=deal]:first")[0].checked = true;
		} else {
			$(".deal1").hide();
			$(".deal2").show();
			
			$("#form-deal2 input[name=deal]:last")[0].checked = true;
		}
		
		return false;
	});
});

// Диапазон цен
$(function() {
	var $form;

	$("select[name=cost]").change(function() {
		var $select = $(this);
		var $option = $select.find("option:selected");

		$form = $select.closest("form");
		
		if ($option.attr("value") === "other") {
			$("#cost").overlay({
				load: true,
				top: 140,
				left: 'center',
				closeOnClick: false,
				fixed: $.browser.msie ? false : true,
				mask: {
					color: '#f8f6ed',
					loadSpeed: 200,
					opacity: 0.7
				},
				onBeforeLoad: function() {
				},
				onLoad: function() {
					$("#cost .close").empty().append("<img src='/cliff/images/close.png'>")
					$("#cost input").first().focus();
				},
				onClose: function() {
					$("#cost input:text").val("");
					$("#cost").removeData("overlay");

					var cost1 = parseInt($form.find("input[name=cost1]").val(), 10);
					var cost2 = parseInt($form.find("input[name=cost2]").val(), 10);

					if (!cost1 && !cost2) {
						$select.find("option").first().attr("selected", "selected");
						return;
					}
					
					var selected = false;
					$select.find("option").each(function() {
						var ocost1 = $(this).attr("cost1");
						var ocost2 = $(this).attr("cost2");
						if (!ocost1 && !ocost2) return;
						
						if (ocost1 == cost1 && ocost2 == cost2) {
							$(this).attr("selected", "selected");
							selected = true;
						}
					});
					if (selected) return;
					
					var text = "от " + cost1 + " у.е. до " + cost2 + " у.е.";
					if (!cost1) {
						text = "до " + cost2 + " у.е.";
					}
					if (!cost2) {
						var text = "свыше " + cost1 + " у.е.";
					}
					
					var $o = $("<option selected='selected'></option>").attr("cost1", cost1).attr("cost2", cost2).text(text);
					$select.append($o);
				}
			});
			return;
		} 
		
		var cost1 = $option.attr("cost1");
		var cost2 = $option.attr("cost2");

		if ($option.attr("value") === "") {
			cost1 = "";
			cost2 = "";
		}

		$select.siblings("input[name=cost1]").val(cost1);
		$select.siblings("input[name=cost2]").val(cost2);
	});
	
	$("#cost form").submit(function() {
		var cost1 = $(this).find("input[name=cost1]").val();
		var cost2 = $(this).find("input[name=cost2]").val();
		
		cost1 = parseInt(cost1.replace(" ", ""), 10);
		cost2 = parseInt(cost2.replace(" ", ""), 10);
	
		$form.find("input[name=cost1]").val(cost1);
		$form.find("input[name=cost2]").val(cost2);

		$("#cost").find(".close").click();
		
		return false;
	});
});

// Карты
$(function() {
	$(".map").each(function() {
		var $map = $(this);

		var text = $.trim($map.text());
		text = text.replace(/\s{2,}/g, " ");

		$map.text("");

		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({
			address: text
		}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				$map.show();

				var map = new google.maps.Map($map[0], {
					zoom: 8,
					center: results[0].geometry.location,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				});

/*				var marker = new google.maps.Marker({
					map: map, 
					position: results[0].geometry.location
				});*/
			} else {
				$map.remove();
			}
		});		
	});
});
